From 02b9b21e5af96966c24daeb0c99287003ec110cb Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 10 Jul 2017 14:45:46 -0400 Subject: Use percent error instead of fixed margin --- __tests__/dice.test.js | 26 ++++++++++++++++---------- 1 file 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) }) } -- cgit v1.2.3