1

The code below is used to play a Lottery game.

let Lotto = {
    _nMap: [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
    ],
    _sMap: [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    ],

    /**
     * @param {Array} shuffleArr
     *
     * @return {Array}
     */
    _shuffleArr(shuffleArr) {
        let rndNr, tmpValue;

        for (let elmNr = shuffleArr.length - 1; elmNr > 0; elmNr--) {
            rndNr = Math.floor(
                Math.random() * (elmNr + 1)
            );

            tmpValue = shuffleArr[rndNr];

            shuffleArr[rndNr] = shuffleArr[elmNr];
            shuffleArr[elmNr] = tmpValue;
        }

        return shuffleArr;
    },

    /**
     * @return {Object}
     */
    getPick() {
        return {
            n: this._shuffleArr(this._nMap).slice(0, 5),
            s: this._shuffleArr(this._sMap).slice(0, 2)
        }
    }
};

Now I want to verify whether the implementation is correct. For example: it should return a unique set of numbers. How do I test this? Run the .getPick() method once and validate the output or ...?

1 Answers1

0

You can write a test case like, getting the script to run 10 to 15 times, and putting the values inside an array and finding the pattern. Something like this would be nice:

let Lotto = {
  _nMap: [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
  ],
  _sMap: [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  ],

  /**
   * @param {Array} shuffleArr
   *
   * @return {Array}
   */
  _shuffleArr(shuffleArr) {
    let rndNr, tmpValue;

    for (let elmNr = shuffleArr.length - 1; elmNr > 0; elmNr--) {
      rndNr = Math.floor(
        Math.random() * (elmNr + 1)
      );

      tmpValue = shuffleArr[rndNr];

      shuffleArr[rndNr] = shuffleArr[elmNr];
      shuffleArr[elmNr] = tmpValue;
    }

    return shuffleArr;
  },

  /**
   * @return {Object}
   */
  getPick() {
    return {
      n: this._shuffleArr(this._nMap).slice(0, 5),
      s: this._shuffleArr(this._sMap).slice(0, 2)
    }
  }
};
var entries = [];
for (var i = 0; i < 100; i++)
  entries.push(JSON.stringify(Lotto.getPick()));
var counts = {};
for (var i = 0; i < entries.length; i++) {
  counts[entries[i]] = 1 + (counts[entries[i]] || 0);
}
console.log(counts);
if (Object.keys(counts).length == 100)
  alert("Excellent, 0 dupes!");
else if (Object.keys(counts).length > 75)
  alert("More than 75% unique.");
else
  alert("Less than 75% unique.");

Here, I could find every output is a distinct one. That's how you need to automate or write test cases. The above snippet itself is a testing solution. Try it out and let me know if it works out for you.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Thank You. In my case I adjusted the Unit Test as the order of the generated row is unimportant. Now I know that the data is random, or at least the definition of what random is for this case. But now I still haven't verified whether each sequence doesn't contain the same numbers. So I've created another test to verify that but should I run it a dozen times or...? –  Dec 27 '16 at 21:46
  • I ask that because it is alsmost impossible to run it against every possible outcome as there are millions. –  Dec 27 '16 at 21:49
  • @43a7898a80 Yea, I would prefer a 100 times. I don't think you would need more than that to get a black sheep. `:)` – Praveen Kumar Purushothaman Dec 27 '16 at 21:51
  • Yes, Thank You! Note: there are 2 118 760 possibilities for this._nMap when the sequence used is unique and the order is unimportant. When a number is declared twice, there are only 1 924 180 possibilities of which 17 296 will have 2x the same number in it. Thus you should run the test at least 1 924 180 / 17 296 = 112 times. Off course, you'll probably not reach that rate each time, so you should multiply it a few times. When I ran the test 2 240 times, the test found the error each time for 40 minutes. Then I stopped :-) –  Dec 28 '16 at 00:29
  • Woah... Didn't expect that. – Praveen Kumar Purushothaman Dec 28 '16 at 00:43