Skip to content

Commit

Permalink
feat: expanded unit test coverage for Euler, Quaternion, Vector2 and …
Browse files Browse the repository at this point in the history
…Vector3.
  • Loading branch information
bhouston committed Jul 22, 2020
1 parent 7be5842 commit 820a635
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 13 deletions.
61 changes: 61 additions & 0 deletions src/lib/math/Euler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { Euler, EulerOrder } from "./Euler";
import { makeEulerFromQuaternion, makeEulerFromRotationMatrix4 } from "./Euler.Functions";
import { makeMatrix4RotationFromEuler } from "./Matrix4.Functions";
import { makeQuaternionFromEuler } from "./Quaternion.Functions";

const testOrders = [EulerOrder.XYZ, EulerOrder.YXZ, EulerOrder.ZXY, EulerOrder.ZYX, EulerOrder.YZX, EulerOrder.XZY];
const e0 = new Euler();
const eX = new Euler(1, 0, 0);
const eY = new Euler(0, 1, 0);
const eZ = new Euler(0, 0, 1);
const eXY = new Euler(1, 0.5, 0);
const eYZ = new Euler(0, 1, 0.5);
const eXZ = new Euler(0.5, 0, 1);
const eXYZ = new Euler(0.25, 0.5, 1);

const testValues = [e0, eX, eY, eZ, eXY, eYZ, eXZ, eXYZ];

function delta(a: Euler, b: Euler): number {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y) + Math.abs(a.z - b.z);
}

test("Instancing", () => {
const a = new Euler();
Expand All @@ -12,4 +31,46 @@ test("Instancing", () => {
expect(b.y).toBe(2);
expect(b.z).toBe(3);
expect(b.order).toBe(EulerOrder.ZXY);

const c = b.clone();
expect(c.x).toBe(1);
expect(c.y).toBe(2);
expect(c.z).toBe(3);
expect(c.order).toBe(EulerOrder.ZXY);

const d = new Euler().copy(b);
expect(d.x).toBe(1);
expect(d.y).toBe(2);
expect(d.z).toBe(3);
expect(d.order).toBe(EulerOrder.ZXY);
});

describe("Euler-Quaternion", () => {
testValues.forEach((euler, ei) => {
testOrders.forEach((order, oi) => {
test(`e ${ei} order ${oi}`, () => {
const e = euler.clone();
e.order = order;
const q = makeQuaternionFromEuler(e);
const e2 = makeEulerFromQuaternion(q, e.order);
expect(delta(e, e2)).toBeLessThan(0.000001);
expect(e.order).toBe(e2.order);
});
});
});
});

describe("Euler-Matrix4", () => {
testValues.forEach((euler, ei) => {
testOrders.forEach((order, oi) => {
test(`e ${ei} order ${oi}`, () => {
const e = euler.clone();
e.order = order;
const m = makeMatrix4RotationFromEuler(e);
const e2 = makeEulerFromRotationMatrix4(m, e.order);
expect(delta(e, e2)).toBeLessThan(0.000001);
expect(e.order).toBe(e2.order);
});
});
});
});
63 changes: 62 additions & 1 deletion src/lib/math/Quaternion.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,76 @@
import { EulerOrder } from "./Euler";
import { makeEulerFromQuaternion } from "./Euler.Functions";
import { makeMatrix4RotationFromQuaternion } from "./Matrix4.Functions";
import { Quaternion } from "./Quaternion";
import { makeQuaternionFromEuler, makeQuaternionFromRotationMatrix4 } from "./Quaternion.Functions";

test("Instancing", () => {
const qX = new Quaternion(1, 0, 0).normalize();
const qY = new Quaternion(0, 1, 0).normalize();
const qZ = new Quaternion(0, 0, 1).normalize();
const qW = new Quaternion(0, 0, 0, 1).normalize();
const qXY = new Quaternion(1, 0.5, 0).normalize();
const qYZ = new Quaternion(0, 1, 0.5).normalize();
const qXZ = new Quaternion(0.5, 0, 1).normalize();
const qXYZ = new Quaternion(0.25, 0.5, 1).normalize();
const qXYW = new Quaternion(1, 0.5, 0, 0.25).normalize();
const qYZW = new Quaternion(0, 1, 0.5, 0.25).normalize();
const qXZW = new Quaternion(0.5, 0, 1, 0.25).normalize();

const testValues = [qX, qY, qZ, qW, qXY, qYZ, qXZ, qXYZ, qXYW, qYZW, qXZW];
const testOrders = [EulerOrder.XYZ, EulerOrder.YXZ, EulerOrder.ZXY, EulerOrder.ZYX, EulerOrder.YZX, EulerOrder.XZY];

test("Constructor", () => {
const a = new Quaternion();
expect(a.x).toBe(0);
expect(a.y).toBe(0);
expect(a.z).toBe(0);
expect(a.w).toBe(1);
});

test("Constructor Values", () => {
const b = new Quaternion(1, 2, 3, 4);
expect(b.x).toBe(1);
expect(b.y).toBe(2);
expect(b.z).toBe(3);
expect(b.w).toBe(4);
});

test("Clone", () => {
const b = new Quaternion(1, 2, 3, 4);
const c = b.clone();
expect(c.x).toBe(1);
expect(c.y).toBe(2);
expect(c.z).toBe(3);
expect(c.w).toBe(4);
});

test("Copy", () => {
const b = new Quaternion(1, 2, 3, 4);
const d = new Quaternion().copy(b);
expect(d.x).toBe(1);
expect(d.y).toBe(2);
expect(d.z).toBe(3);
expect(d.w).toBe(4);
});

describe("Quaternion-Euler ", () => {
testValues.forEach((q, qi) => {
testOrders.forEach((eulerOrder, ei) => {
test(`q${qi} order ${ei}`, () => {
const e = makeEulerFromQuaternion(q, eulerOrder);
const q2 = makeQuaternionFromEuler(e);
expect(q2.clone().sub(q).length()).toBeLessThan(0.000001);
});
});
});
});

describe("Quaternion-Matrix4", () => {
testValues.forEach((q, qi) => {
test(`q ${qi}`, () => {
const m = makeMatrix4RotationFromQuaternion(q);
const q2 = makeQuaternionFromRotationMatrix4(m);
expect(q2.clone().sub(q).length()).toBeLessThan(0.000001);
});
});
});
8 changes: 8 additions & 0 deletions src/lib/math/Vector2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ test("Instancing", () => {
const b = new Vector2(1, 2);
expect(b.x).toBe(1);
expect(b.y).toBe(2);

const c = b.clone();
expect(c.x).toBe(1);
expect(c.y).toBe(2);

const d = new Vector2().copy(b);
expect(d.x).toBe(1);
expect(d.y).toBe(2);
});
22 changes: 10 additions & 12 deletions src/lib/math/Vector3.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { Vector3 } from "./Vector3";

test("Instancing", () => {
const a = new Vector3();
expect(a.x).toBe(0);
expect(a.y).toBe(0);
expect(a.z).toBe(0);

const b = new Vector3(1, 2, 3);
expect(b.x).toBe(1);
expect(b.y).toBe(2);
expect(b.z).toBe(3);
});

test("Instancing", () => {
const a = new Vector3();
expect(a.r).toBe(0);
Expand All @@ -22,4 +10,14 @@ test("Instancing", () => {
expect(b.r).toBe(1);
expect(b.g).toBe(2);
expect(b.b).toBe(3);

const c = b.clone();
expect(c.x).toBe(1);
expect(c.y).toBe(2);
expect(c.z).toBe(3);

const d = new Vector3().copy(b);
expect(d.x).toBe(1);
expect(d.y).toBe(2);
expect(d.z).toBe(3);
});

0 comments on commit 820a635

Please sign in to comment.