summaryrefslogtreecommitdiff
path: root/modules/Util.test.js
blob: 000d54e26692698ca0c98e6a60c30d233586cc9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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));
});