Skip to content

Commit 3e9e276

Browse files
committed
add unit tests
1 parent 41187ef commit 3e9e276

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

tests/tests.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<meta charset=utf-8>
2+
<div id=log></div>
3+
<script src="../glutil_min.js"></script>
4+
<script src="tests.js"></script>
5+
<script>
6+
test();
7+
if(FailedTests==0){
8+
clog("All tests passed");
9+
}
10+
</script>

tests/tests.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Adapted from public domain Mozilla unit tests
2+
3+
var EPSILON = 0.00001
4+
var FailedTests = 0;
5+
6+
function clog(x){
7+
if(document){
8+
var div=document.getElementById("log")
9+
if(div){
10+
div.innerHTML+=x+"<br>"
11+
} else {
12+
console.log(x)
13+
}
14+
} else {
15+
console.log(x)
16+
}
17+
}
18+
19+
function info(x){
20+
clog(x)
21+
}
22+
function ok(a,b){
23+
if(!a){
24+
FailedTests++;
25+
clog(b)
26+
}
27+
}
28+
function is(a,b,c){
29+
if(a==b){
30+
info("expected "+b+", got "+a)
31+
clog(b)
32+
}
33+
}
34+
35+
function isApprox(num1, num2, delta) {
36+
if (Math.abs(num1 - num2) > (delta || EPSILON)) {
37+
info("isApprox expected " + num1 + ", got " + num2 + " instead.");
38+
return false;
39+
}
40+
return true;
41+
}
42+
43+
function isApproxVec(vec1, vec2, delta) {
44+
vec1 = Array.prototype.slice.call(vec1);
45+
vec2 = Array.prototype.slice.call(vec2);
46+
47+
if (vec1.length !== vec2.length) {
48+
return false;
49+
}
50+
for (var i = 0, len = vec1.length; i < len; i++) {
51+
if (!isApprox(vec1[i], vec2[i], delta)) {
52+
info("isApproxVec expected [" + vec1 + "], got [" + vec2 + "] instead.");
53+
return false;
54+
}
55+
}
56+
return true;
57+
}
58+
59+
function isEqualVec(vec1, vec2) {
60+
vec1 = Array.prototype.slice.call(vec1);
61+
vec2 = Array.prototype.slice.call(vec2);
62+
63+
if (vec1.length !== vec2.length) {
64+
return false;
65+
}
66+
for (var i = 0, len = vec1.length; i < len; i++) {
67+
if (vec1[i] !== vec2[i]) {
68+
info("isEqualVec expected [" + vec1 + "], got [" + vec2 + "] instead.");
69+
return false;
70+
}
71+
}
72+
return true;
73+
}
74+
function test(){
75+
ok(isApproxVec(GLUtil.toGLColor("#f00"), [1, 0, 0, 1]),
76+
"The hex2rgba() function didn't calculate the 1st rgba values correctly.");
77+
78+
ok(isApproxVec(GLUtil.toGLColor("#ff0000"), [1, 0, 0, 1]),
79+
"The hex2rgba() function didn't calculate the 3rd rgba values correctly.");
80+
81+
ok(isApproxVec(GLUtil.toGLColor("rgba(255, 0, 0, 0.5)"), [1, 0, 0, 0.5]),
82+
"The hex2rgba() function didn't calculate the 5th rgba values correctly.");
83+
84+
ok(isApproxVec(GLUtil.toGLColor("rgb(255, 0, 0)"), [1, 0, 0, 1]),
85+
"The hex2rgba() function didn't calculate the 6th rgba values correctly.");
86+
87+
var m1 = ([
88+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
89+
90+
var m2 = ([
91+
0, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16]);
92+
93+
m1=GLMath.mat4multiply(m1, m2);
94+
ok(isApproxVec(m1, [
95+
275, 302, 329, 356, 304, 336, 368, 400,
96+
332, 368, 404, 440, 360, 400, 440, 480
97+
]), "The mat4.multiply() function didn't set the values correctly.");
98+
m1=GLMath.mat4translate(m1, [1, 2, 3]);
99+
ok(isApproxVec(m1, [
100+
275, 302, 329, 356, 304, 336, 368, 400,
101+
332, 368, 404, 440, 2239, 2478, 2717, 2956
102+
]), "The mat4.translate() function didn't set the values correctly.");
103+
104+
m1=GLMath.mat4scale(m1, [1, 2, 3]);
105+
ok(isApproxVec(m1, [
106+
275, 302, 329, 356, 608, 672, 736, 800,
107+
996, 1104, 1212, 1320, 2239, 2478, 2717, 2956
108+
]), "The mat4.scale() function didn't set the values correctly.");
109+
110+
m1=GLMath.mat4rotate(0.5, [1, 1, 1]);
111+
ok(isApproxVec(m1, [
112+
210.6123046875, 230.2483367919922, 249.88438415527344, 269.5204162597656,
113+
809.8145751953125, 896.520751953125, 983.2268676757812,
114+
1069.9329833984375, 858.5731201171875, 951.23095703125,
115+
1043.8887939453125, 1136.5465087890625, 2239, 2478, 2717, 2956
116+
]), "The mat4.rotate() function didn't set the values correctly.");
117+
118+
m1=GLMath.mat4rotate(0.5,1,0,0);
119+
ok(isApproxVec(m1, [
120+
210.6123046875, 230.2483367919922, 249.88438415527344, 269.5204162597656,
121+
1122.301025390625, 1242.8154296875, 1363.3297119140625,
122+
1483.843994140625, 365.2230224609375, 404.96875, 444.71453857421875,
123+
484.460205078125, 2239, 2478, 2717, 2956
124+
]), "The mat4.rotateX() function didn't set the values correctly.");
125+
126+
m1=GLMath.mat4rotate( 0.5,0,1,0);
127+
ok(isApproxVec(m1, [
128+
9.732441902160645, 7.909564018249512, 6.086670875549316,
129+
4.263822555541992, 1122.301025390625, 1242.8154296875, 1363.3297119140625,
130+
1483.843994140625, 421.48626708984375, 465.78045654296875,
131+
510.0746765136719, 554.3687744140625, 2239, 2478, 2717, 2956
132+
]), "The mat4.rotateY() function didn't set the values correctly.");
133+
134+
m1=GLMath.mat4rotate(0.5,0,0,1);
135+
ok(isApproxVec(m1, [
136+
546.6007690429688, 602.7787475585938, 658.9566650390625, 715.1345825195312,
137+
980.245849609375, 1086.881103515625, 1193.5162353515625,
138+
1300.1514892578125, 421.48626708984375, 465.78045654296875,
139+
510.0746765136719, 554.3687744140625, 2239, 2478, 2717, 2956
140+
]), "The mat4.rotateZ() function didn't set the values correctly.");
141+
142+
var m3 = GLMath.mat4frustum(0, 100, 200, 0, 0.1, 100);
143+
ok(isApproxVec(m3, [
144+
0.0020000000949949026, 0, 0, 0, 0, -0.0010000000474974513, 0, 0, 1, -1,
145+
-1.0020020008087158, -1, 0, 0, -0.20020020008087158, 0
146+
]), "The mat4.frustum() function didn't compute the values correctly.");
147+
148+
var m4 = GLMath.mat4perspective(45, 1.6, 0.1, 100);
149+
ok(isApproxVec(m4, [1.5088834762573242, 0, 0, 0, 0, 2.4142136573791504, 0,
150+
0, 0, 0, -1.0020020008087158, -1, 0, 0, -0.20020020008087158, 0
151+
]), "The mat4.frustum() function didn't compute the values correctly.");
152+
153+
var m5 = GLMath.mat4ortho(0, 100, 200, 0, -1, 1);
154+
ok(isApproxVec(m5, [
155+
0.019999999552965164, 0, 0, 0, 0, -0.009999999776482582, 0, 0,
156+
0, 0, -1, 0, -1, 1, 0, 1
157+
]), "The mat4.ortho() function didn't compute the values correctly.");
158+
159+
var m6 = GLMath.mat4lookat([1, 2, 3], [4, 5, 6], [0, 1, 0]);
160+
ok(isApproxVec(m6, [
161+
-0.7071067690849304, -0.40824830532073975, -0.5773502588272095, 0, 0,
162+
0.8164966106414795, -0.5773502588272095, 0, 0.7071067690849304,
163+
-0.40824830532073975, -0.5773502588272095, 0, -1.4142135381698608, 0,
164+
3.464101552963257, 1
165+
]), "The mat4.lookAt() function didn't compute the values correctly.");
166+
}

0 commit comments

Comments
 (0)