<commit_before>const RatioMap = require('../lib/ratio_map');

const {Map} = require('immutable');
const is    = require('is_js');
const jsc   = require('jsverify');

test('RatioMap.randomKey() return some key', () => {
  const posInteger = jsc.integer(1, 10000);
  jsc.assertForall(jsc.nearray(jsc.pair(jsc.asciistring, posInteger)), (data) => {
    const input      = data.reduce((map, [key, ratio]) => map.set(key, ratio), Map()).toJSON();
    const candidates = data.filter(([_, ratio]) => ratio > 0).map(([key, _]) => key);

    const result = new RatioMap(input).randomKey();
    return candidates.some((c) => c === result);
  });
});

test('RatioMap.randomKey() return null', () => {
  jsc.assertForall(jsc.nearray(jsc.pair(jsc.asciistring, jsc.integer(0))), (data) => {
    const input = data.reduce((map, [key, ratio]) => map.set(key, ratio), Map()).toJSON();

    const ratioMap = new RatioMap(input);
    return is.null(ratioMap.randomKey());
  });
});

test('RatioMap.validate() throw error', () => {
  const ratios = [
    {a: undefined},
    {a: null     },
    {a: '0'      },
    {a: []       },
    {a: {}       },
  ];

  for (ratio of ratios) {
    expect(() => RatioMap.validate(ratio)).toThrow(/v_i/);
  }
});

test ('RatioMap.ranges should be range expression', () => {
  const data = [
    [{a: 1      }, {a: [1, 1]}           ],
    [{a: 1, b: 0}, {a: [1, 1]}           ],
    [{a: 1, b: 1}, {a: [1, 1], b: [2, 2]}],
    [{a: 2, b: 1}, {a: [1, 2], b: [3, 3]}],
    [{a: 1, b: 2}, {a: [1, 1], b: [2, 3]}],
  ];

  for ([ratio, expected] of data) {
    const ratioMap = new RatioMap(ratio);
    expect(ratioMap.ranges.toJSON()).toEqual(expected);
  }
});

test ('RatioMap.max should be max of range expression', () => {
  const data = [
    [{a: 1      }, 1],
    [{a: 1, b: 0}, 1],
    [{a: 1, b: 1}, 2],
    [{a: 2, b: 1}, 3],
    [{a: 1, b: 2}, 3],
  ];

  for ([ratio, expected] of data) {
    const ratioMap = new RatioMap(ratio);
    expect(ratioMap.max).toEqual(expected);
  }
});
<commit_msg>Use jsc.dict()<commit_after>const RatioMap = require('../lib/ratio_map');

const {Map} = require('immutable');
const is    = require('is_js');
const jsc   = require('jsverify');

test('RatioMap.randomKey() return some key', () => {
  const posInteger = jsc.integer(1, 10000);
  jsc.assertForall(jsc.dict(posInteger), (input) => {
    if (is.empty(input)) {
      return true;
    }
    const candidates = Map(input).keySeq();

    const ratioMap = new RatioMap(input);
    const result   = ratioMap.randomKey();
    return candidates.some((c) => c === result);
  });
});

test('RatioMap.randomKey() return null', () => {
  jsc.assertForall(jsc.dict(jsc.integer(0)), (input) => {
    const ratioMap = new RatioMap(input);
    return is.null(ratioMap.randomKey());
  });
});

test('RatioMap.validate() throw error', () => {
  const ratios = [
    {a: undefined},
    {a: null     },
    {a: '0'      },
    {a: []       },
    {a: {}       },
  ];

  for (ratio of ratios) {
    expect(() => RatioMap.validate(ratio)).toThrow(/v_i/);
  }
});

test ('RatioMap.ranges should be range expression', () => {
  const data = [
    [{a: 1      }, {a: [1, 1]}           ],
    [{a: 1, b: 0}, {a: [1, 1]}           ],
    [{a: 1, b: 1}, {a: [1, 1], b: [2, 2]}],
    [{a: 2, b: 1}, {a: [1, 2], b: [3, 3]}],
    [{a: 1, b: 2}, {a: [1, 1], b: [2, 3]}],
  ];

  for ([ratio, expected] of data) {
    const ratioMap = new RatioMap(ratio);
    expect(ratioMap.ranges.toJSON()).toEqual(expected);
  }
});

test ('RatioMap.max should be max of range expression', () => {
  const data = [
    [{a: 1      }, 1],
    [{a: 1, b: 0}, 1],
    [{a: 1, b: 1}, 2],
    [{a: 2, b: 1}, 3],
    [{a: 1, b: 2}, 3],
  ];

  for ([ratio, expected] of data) {
    const ratioMap = new RatioMap(ratio);
    expect(ratioMap.max).toEqual(expected);
  }
});

{'fraction_non_alphanumeric': 0.23255208333333333, 'fraction_numerical': 0.024739583333333332, 'mean_word_length': 2.814299900695134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7424453', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1444, 'n_words': 362}