diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-10 14:04:25 -0400 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-10 14:04:57 -0400 |
commit | b170693c5d8e73286f38e499a0ba91ee9bafa1bb (patch) | |
tree | 3690b5fe4ce30f9489473dd2ac6e26c669171dd1 | |
parent | c09ab019efddfd6f072500bb8f9d862cca6b3a67 (diff) |
Add variance tests
-rw-r--r-- | __tests__/dice.test.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index 489750f..45c859b 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -37,6 +37,19 @@ expect.extend({ `expected all to${pass ? ' not ' : ' '}be equal to ${expected}` ) } + }, + toHaveVariance(received, expected, margin = 0.5) { + const average = received.reduce(plus) / received.length + const variance = received.map((value) => (Math.pow(value - average, 2))) + .reduce(plus) / (received.length - 1) + const pass = variance > expected - margin && variance < expected + margin + + return { + pass, + message: () => ( + `expected variance to${pass ? ' not ' : ' '}be around ${expected} (margin: ${margin}), got ${variance}` + ) + } } }) @@ -72,6 +85,14 @@ const testDie = (die, testSpecs, numberRolls = defaultNumberRolls) => { }) } + if ('variance' in testSpecs) { + let { variance, margin } = testSpecs.variance + + it(`has a variance of ${variance}`, () => { + expect(rolls).toHaveVariance(variance, margin) + }) + } + if ('bounds' in testSpecs) { let { low, high, expectExtrema } = testSpecs.bounds |