diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-10 14:45:46 -0400 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-10 15:18:45 -0400 |
commit | 02b9b21e5af96966c24daeb0c99287003ec110cb (patch) | |
tree | 25358396d7bc9a880565c2da4e75c2c8a795b122 | |
parent | 8cb2f0e3b007d64e91648d732741c1b0371407fb (diff) |
Use percent error instead of fixed margin
-rw-r--r-- | __tests__/dice.test.js | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index 275e0a9..ea4d415 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -1,16 +1,22 @@ const { d, add } = require('../src/dice.js') const defaultNumberRolls = 500 +const defaultError = 0.2 + +const isWithinError = (value, expected, error) => { + const margin = expected * error + return value >= expected - margin && value <= expected + margin +} expect.extend({ - toBeOnAverage(received, expected, margin = 0.5) { + toBeOnAverage(received, expected, error = defaultError) { const average = received.reduce(plus) / received.length - const pass = average > expected - margin && average < expected + margin + const pass = isWithinError(average, expected, error) return { pass, message: () => ( - `expected average to${pass ? ' not ' : ' '}be around ${expected} (margin: ${margin}), got ${average}` + `expected average to${pass ? ' not ' : ' '}be around ${expected} (error: ${error}%), got ${average}` ) } }, @@ -38,16 +44,16 @@ expect.extend({ ) } }, - toHaveVariance(received, expected, margin = 0.5) { + toHaveVariance(received, expected, error = defaultError) { 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 + const pass = isWithinError(variance, expected, error) return { pass, message: () => ( - `expected variance to${pass ? ' not ' : ' '}be around ${expected} (margin: ${margin}), got ${variance}` + `expected variance to${pass ? ' not ' : ' '}be around ${expected} (error: ${error}%), got ${variance}` ) } } @@ -78,18 +84,18 @@ const testDie = (die, testSpecs, numberRolls = defaultNumberRolls) => { } if ('average' in testSpecs) { - let { average, margin } = testSpecs.average + let { average, error } = testSpecs.average it(`has an expected value of ${average}`, () => { - expect(rolls).toBeOnAverage(average, margin) + expect(rolls).toBeOnAverage(average, error) }) } if ('variance' in testSpecs) { - let { variance, margin } = testSpecs.variance + let { variance, error } = testSpecs.variance it(`has a variance of ${variance}`, () => { - expect(rolls).toHaveVariance(variance, margin) + expect(rolls).toHaveVariance(variance, error) }) } |