gravity/vector.test.js
2026-01-29 11:28:39 -06:00

28 lines
701 B
JavaScript

import {add, weightedAvg} from "./vector.js";
const assert = (expected, actual) => {
if (expected !== actual) {
console.error(`fail: ${expected} !== ${actual}`);
throw new Error('fail');
}
console.log(`${expected} === ${actual}`);
};
// add
{
const v1 = {x: 1, y: 1};
const v = add(v1, v1, v1);
assert('{"x":3,"y":3}', JSON.stringify(v));
}
// weightedAvg
{
const v1 = {x: 1, y: 1};
const v2 = {x: -1, y: -1};
const v3 = {x: 2, y: 2};
assert('{"x":0,"y":0}', JSON.stringify(weightedAvg([[v1, 1], [v2, 1]])));
assert('{"x":1,"y":1}', JSON.stringify(weightedAvg([[v1, 1], [v1, 1]])));
assert('{"x":1.25,"y":1.25}', JSON.stringify(weightedAvg([[v1, 3], [v3, 1]])));
}