From e2799cb092359038374cde25eb691d667b309383 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 25 May 2022 22:22:41 -0500 Subject: add Util.js --- modules/Util.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 modules/Util.test.js (limited to 'modules/Util.test.js') diff --git a/modules/Util.test.js b/modules/Util.test.js new file mode 100644 index 0000000..000d54e --- /dev/null +++ b/modules/Util.test.js @@ -0,0 +1,23 @@ +import { test, assert} from './test-assert.js'; +import { useAverage } from './Util.js'; + + +test('Average correctly accumulates an average', () => { + let [avg, avg_append] = useAverage(); + let data = []; + for (let i=0; i<5000; i++) { + let d = Math.random(); + data.push(d); + avg_append(d); + } + + let manual_average = 0; + for (let d of data) manual_average += d; + manual_average /= data.length; + + const precision = (decimalPlaces, num) => { + const theta = 10**decimalPlaces; + return Math.floor(num * theta) / theta; + }; + assert.equal(precision(5, avg()), precision(5, manual_average)); +}); -- cgit v1.2.1