Skip to content

Commit

Permalink
Added some more interesting tests
Browse files Browse the repository at this point in the history
- Fixed a bug with normalize()
- Checked that we can't change the value of our constants
  • Loading branch information
saecob committed Oct 7, 2011
1 parent f1bcfa4 commit b3818d9
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 21 deletions.
13 changes: 8 additions & 5 deletions src/Math.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ var _Math = function( options ) {
var dim = v1.length;
for( var i = 0; i < dim; ++ i ) {
if ( Math.abs(v1[i] - v2[i]) > e ) {
//if( v1[i] != v2[i] ) {
return false;
}
}
Expand Down Expand Up @@ -114,9 +113,9 @@ var _Math = function( options ) {

// Return a Vector(result) with same direction as v having unit length
normalize: function( v, result ) {
// Need to find |v| not v.length (length of array)
for( var i = 0, len = vector.length(v); i < len; ++ i ) {
result[i] = v[i] / len;
var len = v.length;
for( var i = 0, abslen = vector.length(v); i < len; ++ i ) {
result[i] = v[i] / abslen;
}

return result;
Expand Down Expand Up @@ -180,8 +179,12 @@ var _Math = function( options ) {
// Computes a Vector2 with same direction as v having unit length
normalize: function( v, result ) {
result = result || that.Vector2();
var len = vector.length(v);

return vector.normalize( v, result );
result[0] = v[0]/len;
result[1] = v[1]/len;

return result;
},

// Computes v1 - v2 -> result
Expand Down
37 changes: 32 additions & 5 deletions test/Math.Vector2.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
math.vector2.clear(vec1);
ok(
math.vector2.equal( vec1, vec3 ),
vec1[0] + ' vector.clear, set to 0,0|' + vec1[1]
vec1[0] + ' vector.clear, set to 0,0 ' + vec1[1]
);

});
Expand All @@ -69,21 +69,25 @@
test( 'Constants', function() {
expect( 4 );

math.vector2.x[0] = 8.7;
deepEqual(
math.vector2.x,
new math.Vector2( [1, 0] ),
'Vector2.x'
);
math.vector2.y[0] = 22.7;
deepEqual(
math.vector2.y,
new math.Vector2( [0, 1] ),
'Vector2.y'
);
math.vector2.zero[1] = Math.sqrt(87);
deepEqual(
math.vector2.zero,
new math.Vector2( [0, 0] ),
'Vector2.zero'
);
math.vector2.one[0] = -76;
deepEqual(
math.vector2.one,
new math.Vector2( [1, 1] ),
Expand All @@ -103,11 +107,13 @@
});

test( 'Equality', function() {
expect( 2 );
expect( 3 );

var vec1 = new math.Vector2( [1, 1] );
var vec1 = new math.Vector2( [1, 1.00000000001] );
var vec2 = new math.Vector2( [1, 1] );
var vec3 = new math.Vector2( [2, 3] );

var vec4 = new math.Vector2( [2.000002, 3.000002] );

ok(
math.vector2.equal( vec1, vec2 ),
Expand All @@ -117,6 +123,11 @@
!math.vector2.equal( vec1, vec3 ),
'Two different vectors are not equal'
);
deepEqual(
math.vector2.equal( vec3, vec4 ),
false,
'e = 0.000001'
);
});

test( 'Length', function() {
Expand Down Expand Up @@ -185,7 +196,7 @@
});

test( 'Dot Product / Normalize', function() {
expect( 2 );
expect( 3 );

var vec1 = new math.Vector2( [12, -5] );
deepEqual(
Expand All @@ -201,10 +212,18 @@
Math.round ( (100/13) * Math.pow(10,6) ), // Correct to 6 digits
' [ (12/13), (-5/13) ] . [ 10, 4 ] = (100/13) '
);

// Normalized vector
var isNormalized = new math.Vector2( [ 1/Math.sqrt(2), 1/Math.sqrt(2) ] );
deepEqual (
math.vector2.normalize( isNormalized ),
isNormalized,
'Normalized vector is already normalized'
);
});

test( 'Angle()', function() {
expect( 1 );
expect( 2 );

var vec1 = new math.Vector2( [10, 8] );
var vec2 = new math.Vector2( [6, 6] );
Expand All @@ -216,6 +235,14 @@
Math.round ( res * Math.pow(10,6) ), // Correct to 6 digits
' angle( vec1, vec2 ) = acos(9/(Math.sqrt(82)))'
);

var vec3 = new math.Vector2( [1, 0] );
var vec4 = new math.Vector2( [0, 1] );
deepEqual(
math.vector2.angle(vec3, vec4),
(Math.PI/2),
' Right angle axis test = pi/2'
);
});

}());
49 changes: 42 additions & 7 deletions test/Math.Vector3.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,31 @@
test( 'Constants', function() {
expect( 5 );

math.vector3.x[0] = -0.88262;
deepEqual(
math.vector3.x,
new math.Vector3( [1, 0, 0] ),
'Vector3.x'
);
math.vector3.y[1] = 0.0000000000001;
deepEqual(
math.vector3.y,
new math.Vector3( [0, 1, 0] ),
'Vector3.y'
);
math.vector3.z[2] = -34;
deepEqual(
math.vector3.z,
new math.Vector3( [0, 0, 1] ),
'Vector3.y'
);
math.vector3.zero[1] = Math.sqrt(198);
deepEqual(
math.vector3.zero,
new math.Vector3( [0, 0, 0] ),
'Vector3.zero'
);
math.vector3.one[2] = -9982.22;
deepEqual(
math.vector3.one,
new math.Vector3( [1, 1, 1] ),
Expand All @@ -108,9 +113,9 @@
});

test( 'Equality', function() {
expect( 2 );
expect( 3 );

var vec1 = new math.Vector3( [1, 1, 1] );
var vec1 = new math.Vector3( [1, 1, 1.00000000001] );
var vec2 = new math.Vector3( [1, 1, 1] );
var vec3 = new math.Vector3( [2, 3, 4] );

Expand All @@ -122,16 +127,30 @@
!math.vector3.equal( vec1, vec3 ),
'Two different vectors are not equal'
);

var vec4 = new math.Vector3( [2.000002, 3.000002, 4.000002] );
deepEqual(
math.vector3.equal( vec3, vec4 ),
false,
'e = 0.000001'
);
});

test( 'Length', function() {
expect( 1 );
expect( 2 );

var vec1 = new math.Vector3( [1, 1, 1] );
ok(
Math.sqrt( 3 ) === math.vector3.length( vec1 ),
'|(1, 1, 1)| = sqrt(3)'
);

var vec2 = new math.Vector3( [2, 4, 1] );
deepEqual(
math.vector3.length( vec2 ),
Math.sqrt(21),
'|(2, 4, 2)| = sqrt(21)'
);
});

test( 'Addition', function() {
Expand Down Expand Up @@ -192,17 +211,17 @@
test( 'Cross Product', function() {
expect (1);

var vec1 = new math.Vector3( [3, -3, 2] ); // x,y,1
var vec1 = new math.Vector3( [3, -3, 2] );
var vec2 = new math.Vector3( [-12, 12, -4] );
deepEqual(
math.vector3.cross( vec1, vec2 ),
new math.Vector3( [-12, -12, 0] ), // 0,0,0
new math.Vector3( [-12, -12, 0] ),
'[3, -3, 1] X [-12, 12, -4] = [0, 0, 0]'
);
});

test( 'Dot Product / Normalize', function() {
expect( 2 );
expect( 3 );

var vec1 = new math.Vector3( [12, -5, 7] );
var den = Math.sqrt(218);
Expand All @@ -219,10 +238,18 @@
Math.round ( (57 * Math.sqrt(2/109)) * Math.pow(10,6) ), // Correct to 6 digits
' [(6*sqrt(2/109), -5/sqrt(218), 7/sqrt(218))] . [ 10, 4, 2 ] = (57 * Math.sqrt(2/109)) '
);

// Normalized vector
var isNormalized = new math.Vector3( [ 1/Math.sqrt(3), 1/Math.sqrt(3), 1/Math.sqrt(3)] );
deepEqual (
math.vector3.normalize( isNormalized ),
isNormalized,
'Normalized vector is already normalized'
);
});

test( 'Angle()', function() {
expect( 1 );
expect( 2 );

var vec1 = new math.Vector3( [10, 8, 2] );
var vec2 = new math.Vector3( [6, 6, 1] );
Expand All @@ -234,6 +261,14 @@
Math.round ( res * Math.pow(10,6) ), // Correct to 6 digits
' angle( vec1, vec2 ) = acos(55/(Math.sqrt(3066)))'
);

var vec3 = new math.Vector3( [1, 0, 0] );
var vec4 = new math.Vector3( [0, 0, 1] );
deepEqual(
math.vector3.angle(vec3, vec4),
(Math.PI/2),
' Right angle axis test = pi/2'
);
});

}());
37 changes: 33 additions & 4 deletions test/Math.Vector4.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,37 @@
test( 'Constants', function() {
expect( 6 );

math.vector4.x[0] = -0.88262;
deepEqual(
math.vector4.x,
new math.Vector4( [1, 0, 0, 0] ),
'Vector4.x'
);
math.vector4.y[1] = -22.998;
deepEqual(
math.vector4.y,
new math.Vector4( [0, 1, 0, 0] ),
'Vector4.y'
);
math.vector4.z[2] = 7635.22;
deepEqual(
math.vector4.z,
new math.Vector4( [0, 0, 1, 0] ),
'Vector4.y'
);
math.vector4.w[3] = -0.88262;
deepEqual(
math.vector4.w,
new math.Vector4( [0, 0, 0, 1] ),
'Vector4.y'
);
math.vector4.zero[3] = 7635.22;
deepEqual(
math.vector4.zero,
new math.Vector4( [0, 0, 0, 0] ),
'Vector4.zero'
);
math.vector4.one[0] = -0.88262;
deepEqual(
math.vector4.one,
new math.Vector4( [1, 1, 1, 1] ),
Expand All @@ -113,9 +119,9 @@
});

test( 'Equality', function() {
expect( 2 );
expect( 3 );

var vec1 = new math.Vector4( [1, 1, 1, 1] );
var vec1 = new math.Vector4( [1, 1, 1, 1.00000000001] );
var vec2 = new math.Vector4( [1, 1, 1, 1] );
var vec3 = new math.Vector4( [2, 3, 4, 5] );

Expand All @@ -127,6 +133,13 @@
!math.vector4.equal( vec1, vec3 ),
'Two different vectors are not equal'
);

var vec4 = new math.Vector4( [2.000002, 3.000002, 4.000002, 5.000002] );
deepEqual(
math.vector4.equal( vec3, vec4 ),
false,
'e = 0.000001'
);
});

test( 'Length', function() {
Expand Down Expand Up @@ -195,7 +208,7 @@
});

test( 'Dot Product / Normalize', function() {
expect( 2 );
expect( 3 );

var vec1 = new math.Vector4( [12, -5, 7, 1] );
var den = Math.sqrt(219);
Expand All @@ -212,10 +225,18 @@
Math.round ( (35*Math.sqrt(3/73)) * Math.pow(10,6) ), // Correct to 6 digits
' [ (12/13), (-5/13) ] . [ 10, 4 ] = (100/13) '
);

// Normalized vector
var isNormalized = new math.Vector4( [ 1/2, 1/2, 1/2, 1/2] );
deepEqual (
math.vector4.normalize( isNormalized ),
isNormalized,
'Normalized vector is already normalized'
);
});

test( 'Angle()', function() {
expect( 1 );
expect( 2 );

var vec1 = new math.Vector4( [10, 8, 2, 7] );
var vec2 = new math.Vector4( [6, 6, 1, -4] );
Expand All @@ -227,6 +248,14 @@
Math.round ( res * Math.pow(10,6) ), // Correct to 6 digits
' angle( vec1, vec2 ) = acos(82/(Math.sqrt(19313)))'
);

var vec3 = new math.Vector4( [1, 0, 0, 0] );
var vec4 = new math.Vector4( [0, 0, 1, 0] );
deepEqual(
math.vector4.angle(vec3, vec4),
(Math.PI/2),
' Right angle axis test = pi/2'
);
});

}());

0 comments on commit b3818d9

Please sign in to comment.