4 ** Copyright (c) 2014 The Khronos Group Inc.
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
28 <title>J3DIMath unit test</title>
29 <script src="J3DIMath.js"></script>
31 var log = function(msg) {
32 var p = document.createElement('p');
33 if (msg.indexOf('FAIL') == 0) {
34 p.style.color = '#900';
35 } else if (msg.indexOf('PASS') == 0) {
36 p.style.color = '#090';
39 document.body.appendChild(p);
43 var check = function(result, msg) {
51 var testMultiplication = function() {
52 var A = new J3DIMatrix4();
53 var B = new J3DIMatrix4();
54 A.$matrix.m21 = 3; // 2nd column, 1st row
55 B.$matrix.m12 = 2; // 1st column, 2nd row
56 A.multiply(B); // multiply from the right
57 // [ 1 3 0 0 ] [ 1 0 0 0 ] [ 7 3 0 0 ]
58 // | 0 1 0 0 | x | 2 1 0 0 | = | 2 1 0 0 |
59 // | 0 0 1 0 | | 0 0 1 0 | | 0 0 1 0 |
60 // [ 0 0 0 1 ] [ 0 0 0 1 ] [ 0 0 0 1 ]
61 check(A.$matrix.m11 == 7 && A.$matrix.m12 == 2 && A.$matrix.m21 == 3 && A.$matrix.m22 == 1,
62 'Matrix multiplication order is as specified');
65 var testFloatArrayIO = function() {
67 var array = [ 7, 2, 0, 0,
71 var A = new J3DIMatrix4(array);
72 check(A.$matrix.m11 == 7 && A.$matrix.m12 == 2 && A.$matrix.m21 == 3 && A.$matrix.m22 == 1,
73 'Matrix can be constructed from column-major array');
74 var outArray = A.getAsArray();
76 for (var i = 0; i < array.length; ++i) {
77 match = match && array[i] == outArray[i];
79 check(match, 'Matrix array out matches array in');
82 var testCrossProduct = function() {
83 var a = new J3DIVector3(1, 5, 11);
84 var b = new J3DIVector3(3, 7, 13);
86 var arr = a.getAsArray();
87 check(arr[0] == 5 * 13 - 11 * 7, 'Vector cross product x is correct');
88 check(arr[1] == 11 * 3 - 1 * 13, 'Vector cross product y is correct');
89 check(arr[2] == 1 * 7 - 5 * 3, 'Vector cross product z is correct');
92 var testLinearAlgebra = function(useCSSMatrix) {
93 J3DIHasCSSMatrix = useCSSMatrix;
94 log('Using CSSMatrix: ' + J3DIHasCSSMatrix);
101 var test = function() {
102 testLinearAlgebra(false);
103 testLinearAlgebra(true);
107 <body onload="test()">