From c3fb9bf31b3a42007917d3c7f11bbdcf040ce254 Mon Sep 17 00:00:00 2001 From: Mario Gonzalez Macedo Date: Sat, 16 Aug 2014 12:42:34 -0700 Subject: [PATCH 1/3] Improved vector-test.js' mutation score to 0.85 Mutation analysis on vector-test.js was done with Mutator (http://ortask.com/mutator/). I improved the suite's mutation score from 0.64 to 0.85. I'll get it to 1.0 soon. --- tests/vector-test.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/vector-test.js b/tests/vector-test.js index cf50ff9..108ccc8 100644 --- a/tests/vector-test.js +++ b/tests/vector-test.js @@ -2,6 +2,11 @@ var v = require('../').vector; var should = require('should'); describe('Creating vectors: ', function () { + it('truly creates a vector', function () { + var a = new v(1); + a.isVector.should.eql(true); + }); + it('Should pass with 3 arguments', function () { var a = new v(1,0,0); a.v.length.should.eql(3); @@ -29,28 +34,54 @@ describe('Creating vectors: ', function () { var a = new v(1,0,0); var b = new v(1,1,0); +var d = new v(0,1,1); describe('Between two vectors: ', function () { it('should add two vectors easily', function () { var c = a.add(b); c.v.length.should.eql(3); c.v.should.eql([ 2, 1, 0 ]); + + var c = a.add(d); + c.v.length.should.eql(3); + c.v.should.eql([ 1, 1, 1 ]); }); it('should calculate the dot product', function () { var c = a.dot(b); c.should.eql(1); + + var c = a.dot(d); + c.should.eql(0); }); it('should calculate the cross product', function () { var c = a.cross(b); c.v.length.should.eql(3); c.v.should.eql([0,0,1]); + + var c = b.cross(a); + c.v.length.should.eql(3); + c.v.should.eql([0,0,-1]); + + var c = a.cross(d); + c.v.length.should.eql(3); + c.v.should.eql([0,-1,1]); + + var c = d.cross(b); + c.v.length.should.eql(3); + c.v.should.eql([-1,1,-1]); }); it('should calculate the distance between the two', function () { var c = a.distanceFrom(b); c.should.eql(1); + + var c = a.distanceFrom(a); + c.should.eql(0); + + var c = b.distanceFrom(d); + c.should.eql(Math.sqrt(2)); }); it('should calculate the length of the vector', function () { From c013a57bd1b8efa928f5318af53071da28c76b71 Mon Sep 17 00:00:00 2001 From: Mario Gonzalez Macedo Date: Fri, 29 Aug 2014 10:25:22 -0700 Subject: [PATCH 2/3] Improved matrix-test.js' mutation score to 0.91 Mutation analysis was performed on matrix-test.js with Mutator (http://ortask.com/mutator/). I improved the suite's mutation score from 0.697 to 0.91. --- tests/matrix-test.js | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/matrix-test.js b/tests/matrix-test.js index 116554d..9a70a3b 100644 --- a/tests/matrix-test.js +++ b/tests/matrix-test.js @@ -17,9 +17,15 @@ describe('Creating matrices: ', function () { D.should.eql(7); done(); }); + it('allows identity matrix iff matrix is square', function (){ + (function () { + var A = new Matrix(7, 3, true); + }).should.throw(); + }); describe('2x2 matrices: ', function () { var A = new Matrix(2,2); var B = new Matrix(2,2); + var D = new Matrix(2,2); it('should set a specific value', function () { // var A = new Matrix(3,4); @@ -84,11 +90,29 @@ describe('Creating matrices: ', function () { C.get(1,1).should.eql(-4); }); + + it('scale detects out of bounds access', function () { + D.set(0,0,2); + D.set(0,1,4); + D.set(0,2,Infinity); + D.set(1,0,6); + D.set(1,1,8); + D.set(1,2,Infinity); + var C = D.scale(-1); + C.get(0,0).should.eql(-2); + C.get(0,1).should.eql(-4); + C.get(1,0).should.eql(-6); + C.get(1,1).should.eql(-8); + should.not.exist(C.get(0,2)); + should.not.exist(C.get(1,2)); + }); + }); describe('3x3 matrices: ', function () { var A = new Matrix(3,3); var B = new Matrix(3,3); + var D = new Matrix(3,3); it('should set a specific value', function () { A.set(0,0,1); @@ -153,6 +177,18 @@ describe('Creating matrices: ', function () { it('should calculate the determinant of the matrix', function () { var C = A.det(); C.should.eql(0); + + D.set(0,0,0); + D.set(0,1,1); + D.set(0,2,1); + D.set(1,0,1); + D.set(1,1,1); + D.set(1,2,1); + D.set(2,0,1); + D.set(2,1,1); + D.set(2,2,1); + var C = D.det(); + parseFloat(D.det().toFixed(12)).should.eql(0); }); it('should calculate the trace of the matrix', function () { @@ -183,6 +219,40 @@ describe('Creating matrices: ', function () { parseFloat(A.det().toFixed(10)).should.eql(-23); }); + + it('detects non-homogenous matrix', function () { + (function () { + var A = new Matrix(3, 4); + A.getPoint(); + }).should.throw(); + }); + + it('returns the specified point', function () { + var A = new Matrix(4, 4); + A.setRow(0, [3,4,4,-1]); + A.setRow(1, [2,1,5,3]); + A.setRow(2, [2,1,3,4]); + A.setRow(3, [0,-2,1,3]); + A.getPoint().v.should.eql([-1,3,4]); + }); + + it('calculates the rotation vector for the matrix', function () { + var D = new Matrix(4,4); + D.setRow(0, [0.2,1,1,1]); + D.setRow(1, [1,0.3,1,1]); + D.setRow(2, [1,1,0.1,1]); + D.setRow(3, [0,1,1,1]); + D.calcRotVec().should.eql(new Vector(0, 0, 0)); + + var D = new Matrix(4,4); + D.setRow(0, [-0.03,1,1,1]); + D.setRow(1, [-1,-0.2,1,1]); + D.setRow(2, [-1,-1,-0.1,1]); + D.setRow(3, [0,-1,-1,-1]); + D.calcRotVec().should.eql(new Vector(-1.338968862122, 1.338968862122, -1.338968862122)); + + }); + }); describe('5x5 matrix', function () { it('should calculate det', function () { From 100928b10d0de210d17028b18a728f9336ff8f40 Mon Sep 17 00:00:00 2001 From: Mario Gonzalez Macedo Date: Fri, 29 Aug 2014 10:29:29 -0700 Subject: [PATCH 3/3] Some fixes while improved matrix-test.js' quality Mutation analysis was performed on matrix-test.js with Mutator (http://ortask.com/mutator/). These are some fixes that were made for small issues in the source file while applying the analysis. --- lib/matrix.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/matrix.js b/lib/matrix.js index 71a0941..edc0cf6 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -17,9 +17,13 @@ var Matrix = function Matrix (rows, cols, isIdentity) { } //square matrices can be declared as identities: - if(this.rows === this.cols && isIdentity){ - for(var i = 0; i