<commit_before>/**
 * @module
 */
import _ from 'lodash';
import Random from 'random-js';
import assert from 'assert';
import {stdOpts} from './constants';
import {isArbitrary} from './arbitrary';
import {constant} from './combinators';

/**
 * @callback Formula
 * @param {random-js.engine} engine
 * @return {FormulaResult}
 */

/**
 * @typedef {Object} FormulaResult The result of evaluating formula.
 * @property {boolean} success formula is valid.
 * @property {Array<*>} counterExample samples falsy this formula.
 * @property {Exception} e
 */

/**
 * @typedef {Object} CheckResult
 * @property {number} numTests
 * @property {number} totalTests tot
 * @property {boolean} pass
 * @property {string} reason
 * @property {FormulaResult} testResult
 */

/**
 * For All Quntalifier.
 *
 */
class ForAll {
  /**
   * Create a ForAll.
   *
   * @param {Array} arbs arbitraries
   * @return {ForAll}
   * @example
   * new ForAll([ke.int, ke.int])
   */
  constructor(arbs) {
    assert(_.every(arbs, isArbitrary), 'arbs must be an array of Arbitrary.');
    this.arbs = arbs;
  }
  /**
   * Evaluating a test.
   *
   * @param {function} f any function takes generated values as parameters.
   * @param {?random-js.Engine} random engine.
   * @return {*}
   * @example
   * new ForAll([ke.int]).eval(x => x + 1);
   */
  eval(f, engine) {
    function genValue(arb) {
      if (engine) {
        arb.engine(engine);
      }
      return arb.generate();
    };
    return f.apply(null, this.arbs.map(genValue));
  }
  /**
   * Make a formula.
   *
   * @param {function} predicate
   * @return {Formula}
   */
  makeFormula(predicate) {
    return (engine) => {
      const [samples, showExamples] = this.arbs.map(arb => {
        if (engine) {
          arb.engine(engine);
        }
        const v = arb.generate();
        return [v, arb.show()(v)];
      });
      try {
        const success = !!predicate.apply(null, samples);
        return {
          success: success,
          counterExample: showExamples
        };
      }
      catch (e) {
        return {
          success: false,
          counterExample: showExamples,
          exception: e
        };
      }
    };
  }
}

/**
 * Property.
 */
class Property {
  /**
   * @param {string} name
   * @param {function} predicate
   * @return {Property}
   */
  constructor(name, predicate) {<commit_msg>Fixed Forall generates wrong sample values.<commit_after>/**
 * @module
 */
import _ from 'lodash';
import Random from 'random-js';
import assert from 'assert';
import {stdOpts} from './constants';
import {isArbitrary} from './arbitrary';
import {constant} from './combinators';

/**
 * @callback Formula
 * @param {random-js.engine} engine
 * @return {FormulaResult}
 */

/**
 * @typedef {Object} FormulaResult The result of evaluating formula.
 * @property {boolean} success formula is valid.
 * @property {Array<*>} counterExample samples falsy this formula.
 * @property {Exception} e
 */

/**
 * @typedef {Object} CheckResult
 * @property {number} numTests
 * @property {number} totalTests tot
 * @property {boolean} pass
 * @property {string} reason
 * @property {FormulaResult} testResult
 */

/**
 * For All Quntalifier.
 *
 */
class ForAll {
  /**
   * Create a ForAll.
   *
   * @param {Array} arbs arbitraries
   * @return {ForAll}
   * @example
   * new ForAll([ke.int, ke.int])
   */
  constructor(arbs) {
    assert(_.every(arbs, isArbitrary), 'arbs must be an array of Arbitrary.');
    this.arbs = arbs;
  }
  /**
   * Evaluating a test.
   *
   * @param {function} f any function takes generated values as parameters.
   * @param {?random-js.Engine} random engine.
   * @return {*}
   * @example
   * new ForAll([ke.int]).eval(x => x + 1);
   */
  eval(f, engine) {
    function genValue(arb) {
      if (engine) {
        arb.engine(engine);
      }
      return arb.generate();
    };
    return f.apply(null, this.arbs.map(genValue));
  }
  /**
   * Make a formula.
   *
   * @param {function} predicate
   * @return {Formula}
   */
  makeFormula(predicate) {
    return (engine) => {
      const samples = this.arbs.map(arb => {
        if (engine) {
          arb.engine(engine);
        }
        return arb.random;
      });
      try {
        const success = !!predicate.apply(null, samples);
        return {
          success: success,
          counterExample: samples
        };
      }
      catch (e) {
        return {
          success: false,
          counterExample: samples,
          exception: e
        };
      }
    };
  }
}

/**
 * Property.
 */
class Property {
  /**
   * @param {string} name
   * @param {function} predicate
   * @return {Property}
   */
  constructor(name, predicate) {
{'fraction_non_alphanumeric': 0.14959626009349766, 'fraction_numerical': 0.00042498937526561835, 'mean_word_length': 2.42078488372093, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4391291', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1490, 'n_words': 392}