Skip to content

Commit a0e21dd

Browse files
authored
Merge pull request #30 from z3dev/master
Initial Tests for Vector2D
2 parents f4fa7b6 + adc390e commit a0e21dd

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

helpers/nearlyEqual.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Compare two numeric values for near equality.
2+
// the given test is fails if the numeric values are outside the given epsilon
3+
function nearlyEqual(t, a, b, epsilon, failMessage) {
4+
if (a == b) { // shortcut, also handles infinities and NaNs
5+
return true;
6+
}
7+
8+
var absA = Math.abs(a);
9+
var absB = Math.abs(b);
10+
var diff = Math.abs(a - b);
11+
if (a == 0 || b == 0 || diff < Number.EPSILON) {
12+
// a or b is zero or both are extremely close to it
13+
// relative error is less meaningful here
14+
if (diff > (epsilon * Number.EPSILON)) {
15+
failMessage = failMessage == undefined ? 'near zero Numbers outside of epsilon' : failMessage;
16+
t.fail(failMessage);
17+
}
18+
}
19+
// use relative error
20+
if ((diff / Math.min((absA + absB), Number.MAX_VALUE)) > epsilon) {
21+
failMessage = failMessage == undefined ? 'Numbers outside of epsilon' : failMessage;
22+
t.fail(failMessage);
23+
}
24+
}
25+
26+
module.exports = {
27+
nearlyEqual: nearlyEqual,
28+
};

test/csg-vector2d-operations.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import test from 'ava';
2+
import {CSG} from '../csg';
3+
import {nearlyEqual} from "../helpers/nearlyEqual";
4+
5+
test("CSG.Vector2D creation", t => {
6+
// FAILS const v1 = new CSG.Vector2D()
7+
const v2 = new CSG.Vector2D(5,5)
8+
t.is(v2.x,5)
9+
t.is(v2.y,5)
10+
11+
const v3 = new CSG.Vector2D([-5,-5])
12+
t.is(v3.x,-5)
13+
t.is(v3.y,-5)
14+
15+
const v4 = new CSG.Vector2D({x: 1, y: -1})
16+
t.is(v4.x, 1)
17+
t.is(v4.y,-1)
18+
19+
const v5 = new CSG.Vector2D(v2)
20+
t.is(v5.x,5)
21+
t.is(v5.y,5)
22+
23+
const v6 = CSG.Vector2D.Create(-5,5)
24+
t.is(v6.x,-5)
25+
t.is(v6.y, 5)
26+
27+
const v7 = CSG.Vector2D.fromAngleRadians(2)
28+
nearlyEqual(t,v7.x,-0.4161468365,1e-10)
29+
nearlyEqual(t,v7.y, 0.9092974268,1e-10)
30+
31+
const v8 = CSG.Vector2D.fromAngle(-2)
32+
nearlyEqual(t,v8.x,-0.4161468365,1e-10)
33+
nearlyEqual(t,v8.y,-0.9092974268,1e-10)
34+
35+
const v9 = CSG.Vector2D.fromAngleDegrees(45)
36+
nearlyEqual(t,v9.x, 0.7071067811,1e-10)
37+
nearlyEqual(t,v9.y, 0.7071067811,1e-10)
38+
});
39+
40+
test("CSG.Vector2D operations", t => {
41+
const v1 = CSG.Vector2D.fromAngleDegrees(45)
42+
43+
var v2 = v1.clone()
44+
nearlyEqual(t,v2.x,0.7071067811,1e-10)
45+
nearlyEqual(t,v2.y,0.7071067811,1e-10)
46+
47+
var l = v2.length()
48+
t.is(l,1.0)
49+
l = v2.lengthSquared()
50+
t.is(l,1.0)
51+
52+
var a = v2.angle()
53+
nearlyEqual(t,a, 0.7853981633,1e-10)
54+
a = v2.angleRadians()
55+
nearlyEqual(t,a, 0.7853981633,1e-10)
56+
a = v2.angleDegrees()
57+
nearlyEqual(t,a,45.0,1e-10)
58+
59+
var v3 = v2.negated()
60+
nearlyEqual(t,v3.x,-0.7071067811,1e-10)
61+
nearlyEqual(t,v3.y,-0.7071067811,1e-10)
62+
63+
v3 = v2.unit()
64+
nearlyEqual(t,v3.x,0.7071067811,1e-10)
65+
nearlyEqual(t,v3.y,0.7071067811,1e-10)
66+
67+
v3 = v3.negated().abs()
68+
nearlyEqual(t,v3.x,0.7071067811,1e-10)
69+
nearlyEqual(t,v3.y,0.7071067811,1e-10)
70+
71+
v3 = v2.normal()
72+
nearlyEqual(t,v3.x, 0.7071067811,1e-10)
73+
nearlyEqual(t,v3.y,-0.7071067811,1e-10)
74+
75+
t.true(v1.equals(v2))
76+
77+
// use the 4 corners
78+
const c1 = new CSG.Vector2D( 5, 0)
79+
const c2 = new CSG.Vector2D( 0, 5)
80+
const c3 = new CSG.Vector2D(-5, 0)
81+
const c4 = new CSG.Vector2D( 0,-5)
82+
83+
v2 = c1.dividedBy(2)
84+
t.is(v2.x,2.5)
85+
t.is(v2.y,0.0)
86+
87+
v2 = c1.times(5)
88+
t.is(v2.x,25.0)
89+
t.is(v2.y, 0.0)
90+
91+
v2 = c1.plus(c4)
92+
t.is(v2.x, 5.0)
93+
t.is(v2.y,-5.0)
94+
95+
v2 = c1.minus(c4)
96+
t.is(v2.x, 5.0)
97+
t.is(v2.y, 5.0)
98+
99+
v2 = c1.lerp(c2,5)
100+
t.is(v2.x,-20.0)
101+
t.is(v2.y, 25.0)
102+
103+
v2 = c1.min(c4)
104+
t.is(v2.x, 0.0)
105+
t.is(v2.y,-5.0)
106+
107+
v2 = c1.max(c4)
108+
t.is(v2.x,5.0)
109+
t.is(v2.y,0.0)
110+
111+
var d = c1.dot(c2)
112+
t.is(d, 0)
113+
d = c1.dot(c3)
114+
t.is(d,-25)
115+
d = c1.dot(c4)
116+
t.is(d, 0)
117+
118+
d = c1.distanceTo(c2)
119+
nearlyEqual(t,d,7.0710678118,1e-10)
120+
d = c1.distanceTo(c3)
121+
nearlyEqual(t,d,10.0,1e-10)
122+
d = c1.distanceTo(c4)
123+
nearlyEqual(t,d,7.0710678118,1e-10)
124+
125+
d = c1.distanceToSquared(c2)
126+
t.is(d,50.0)
127+
d = c1.distanceToSquared(c3)
128+
t.is(d,100.0)
129+
d = c1.distanceToSquared(c4)
130+
t.is(d,50.0)
131+
132+
d = c1.cross(c2)
133+
t.is(d,25.0)
134+
d = c1.cross(c3)
135+
t.is(d,0.0)
136+
d = c1.cross(c4)
137+
t.is(d,-25.0)
138+
139+
/*
140+
v2 = c1.multiply4x4(matrix4x4)
141+
v2 = c1.transform(matrix4x4)
142+
*/
143+
});
144+
145+
test("CSG.Vector2D conversions", t => {
146+
const v1 = new CSG.Vector2D({x: 1, y: -1})
147+
148+
var s1 = v1.toString()
149+
t.is(v1.toString(), '(1.00, -1.00)')
150+
151+
var v3 = v1.toVector3D(5)
152+
t.is(v3.x, 1)
153+
t.is(v3.y,-1)
154+
t.is(v3.z, 5)
155+
});
156+

0 commit comments

Comments
 (0)