summaryrefslogtreecommitdiff
path: root/src/Util/Util.test.js
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-05-31 20:52:15 -0500
committersanine <sanine.not@pm.me>2022-05-31 20:52:15 -0500
commit5efc7885e1c3959aa165be640858ffb3f8a5860b (patch)
tree6865327d6affd6df2c4f61ed3b9d5ab446e2b23e /src/Util/Util.test.js
parent8aa6645f2311de78f74b35f804cc45c7fcf38f57 (diff)
refactor: remove modules/ folder
Diffstat (limited to 'src/Util/Util.test.js')
-rw-r--r--src/Util/Util.test.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/Util/Util.test.js b/src/Util/Util.test.js
new file mode 100644
index 0000000..9c6adbf
--- /dev/null
+++ b/src/Util/Util.test.js
@@ -0,0 +1,41 @@
+import { test, assert} from '../test-assert.js';
+import { useAverage, clamp, lerp } 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));
+});
+
+
+test('Clamp correctly constrains values', () => {
+ assert.equal(clamp(5, 0, 10), 5);
+ assert.equal(clamp(-1, 0, 10), 0);
+ assert.equal(clamp(15, 0, 10), 10);
+});
+
+
+test ('Lerp correctly interpolates values', () => {
+ const a = 15;
+ const b = 10;
+ assert.equal(lerp(a, b, 0), a);
+ assert.equal(lerp(a, b, 1), b);
+ assert.equal(lerp(a, b, 0.5), (a+b)/2);
+ assert.equal(lerp(a, b, 0.25), (3*a + b)/4);
+ assert.equal(lerp(a, b, 0.75), (a + (3*b))/4);
+});