28 lines
813 B
TypeScript
28 lines
813 B
TypeScript
import { expect, test } from "bun:test";
|
|
import { vec } from "./given.ts";
|
|
import { dot, length, normalize } from "./mathv.ts";
|
|
|
|
test("length uses Pythagoras", () => {
|
|
expect(length(vec(3, 4))).toBe(5);
|
|
expect(length(vec(0, 0))).toBe(0);
|
|
});
|
|
|
|
test("normalize gives a unit vector in the same direction", () => {
|
|
const n = normalize(vec(3, 4));
|
|
expect(n.x).toBeCloseTo(0.6);
|
|
expect(n.y).toBeCloseTo(0.8);
|
|
expect(length(n)).toBeCloseTo(1);
|
|
});
|
|
|
|
test("normalize of the zero vector is (0,0), not NaN", () => {
|
|
const n = normalize(vec(0, 0));
|
|
expect(n).toEqual({ x: 0, y: 0 });
|
|
expect(Number.isNaN(n.x)).toBe(false);
|
|
expect(Number.isNaN(n.y)).toBe(false);
|
|
});
|
|
|
|
test("dot product", () => {
|
|
expect(dot(vec(1, 0), vec(0, 1))).toBe(0); // perpendicular
|
|
expect(dot(vec(2, 3), vec(4, 5))).toBe(23); // 8 + 15
|
|
});
|