Add more tests.

pull/6/head
Atul Varma 2021-02-16 06:49:45 -05:00
rodzic 428a21d6df
commit cbf57f7fef
2 zmienionych plików z 24 dodań i 0 usunięć

21
lib/point.test.tsx 100644
Wyświetl plik

@ -0,0 +1,21 @@
import { normalizePoint } from "./point";
describe("normalizePoint()", () => {
it("Does nothing to points w/ length 1", () => {
expect(normalizePoint({ x: 1, y: 0 })).toEqual({ x: 1, y: 0 });
expect(normalizePoint({ x: 0, y: 1 })).toEqual({ x: 0, y: 1 });
});
it("Raises an exception on points w/ length 0", () => {
expect(() => normalizePoint({ x: 0, y: 0 })).toThrow(
"Unable to normalize point with length 0"
);
});
it("Normalizes points", () => {
expect(normalizePoint({ x: 1, y: 1 })).toEqual({
x: 1 / Math.sqrt(2),
y: 1 / Math.sqrt(2),
});
});
});

Wyświetl plik

@ -9,6 +9,9 @@ export function subtractPoints(p1: Point, p2: Point): Point {
export function normalizePoint(p: Point): Point {
const len = Math.sqrt(Math.pow(p.x, 2) + Math.pow(p.y, 2));
if (len === 0) {
throw new Error(`Unable to normalize point with length 0`);
}
return {
x: p.x / len,
y: p.y / len,