Skip to content

Commit

Permalink
test: add some tests and test on latest node
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Sep 9, 2015
1 parent dd041c8 commit 36e8a55
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- "iojs"
- "stable"
sudo: false
9 changes: 6 additions & 3 deletions src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class Matrix extends Array {
constructor(nRows, nColumns) {
if (Matrix.isMatrix(nRows)) {
return nRows.clone();
} else if (typeof nRows === 'number' && nRows > 0) { // Create an empty matrix
} else if (Number.isInteger(nRows) && nRows > 0) { // Create an empty matrix
super(nRows);
if (typeof nColumns === 'number' && nColumns > 0) {
if (Number.isInteger(nColumns) && nColumns > 0) {
for (var i = 0; i < nRows; i++) {
this[i] = new Array(nColumns);
}
} else {
throw new TypeError('nColumns must be a positive number');
throw new TypeError('nColumns must be a positive integer');
}
} else if (Array.isArray(nRows)) { // Copy the values from the 2D array
var matrix = nRows;
Expand Down Expand Up @@ -240,6 +240,9 @@ class Matrix extends Array {
* @returns {Matrix} this
*/
apply(callback) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
let ii = this.rows;
let jj = this.columns;
for (var i = 0; i < ii; i++) {
Expand Down
70 changes: 64 additions & 6 deletions test/matrix/creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Matrix = require('../../');
var util = require('../util');
var assert = require('assert');

describe('Matrix creation', function () {
it('should create a new object', function () {
Expand All @@ -12,17 +13,25 @@ describe('Matrix creation', function () {
it('should extend Array', function () {
var array = util.getSquareArray();
var matrix = new Matrix(array);
Matrix.isMatrix(matrix).should.be.true();
matrix.should.be.instanceof(Matrix);
matrix.should.be.instanceof(Array);
Matrix.isMatrix(matrix).should.be.true();
Array.isArray(matrix).should.be.true();
});
it('should create a new array if asked', function () {
var array = util.getSquareArray();
var matrix = new Matrix(array, true);
matrix.should.not.equal(array);
it('should clone existing matrix', function () {
var original = util.getSquareMatrix();
var matrix = new Matrix(original);
matrix.should.not.equal(original);
matrix.should.eql(original);
});
it('should create an empty matrix', function () {
var matrix = new Matrix(3, 9);
matrix.rows.should.equal(3);
matrix.columns.should.equal(9);
assert.strictEqual(matrix[0][0], undefined);
});
it('should throw with wrong arguments', function () {
(function(){ new Matrix(6) }).should.throw(TypeError, /^nColumns must be a positive integer/);
(function(){ new Matrix(0) }).should.throw(TypeError, /^First argument must be a positive number or an array$/);
(function(){ new Matrix([[]]) }).should.throw(TypeError, /^Data must be a 2D array with at least one element$/);
(function(){ new Matrix([0, 1, 2, 3]) }).should.throw(TypeError, /^Data must be a 2D array/);
Expand Down Expand Up @@ -50,7 +59,56 @@ describe('Matrix creation', function () {
matrix.rows.should.exactly(3);
matrix.columns.should.exactly(2);
(function () {
var matrix = Matrix.from1DArray(3, 2, [0, 1, 2, 3]);
Matrix.from1DArray(3, 2, [0, 1, 2, 3]);
}).should.throw(RangeError, /^Data length does not match given dimensions$/);
});
it('row vector', function () {
var vector = Matrix.rowVector([0, 1, 2, 3]);
vector.rows.should.equal(1);
vector.columns.should.equal(4);
vector.to2DArray().should.eql([[0, 1, 2, 3]]);
});
it('column vector', function () {
var vector = Matrix.columnVector([0, 1, 2, 3]);
vector.rows.should.equal(4);
vector.columns.should.equal(1);
vector.to2DArray().should.eql([[0], [1], [2], [3]]);
});
it('empty', function () {
Matrix.empty(3, 3).should.eql(new Matrix(3, 3));
});
it('zeros', function () {
Matrix.zeros(2, 3).to2DArray().should.eql([[0, 0, 0], [0, 0, 0]]);
});
it('ones', function () {
Matrix.ones(2, 3).to2DArray().should.eql([[1, 1, 1], [1, 1, 1]]);
});
it('random', function () {
var random = Matrix.rand(2, 2);
var random2 = Matrix.rand(2, 2);
random.to2DArray().should.not.eql(random2.to2DArray());
random[0][0].should.be.within(0, 1);
});
it('random with custom RNG', function () {
function fakeRNG() { return 2; }
Matrix.rand(2, 2, fakeRNG).to2DArray().should.eql([[2, 2], [2, 2]]);
});
it('eye/identity', function () {
var eye1 = Matrix.eye(3);
eye1.should.eql(Matrix.identity(3));
eye1.to2DArray().should.eql([[1, 0, 0], [0, 1, 0], [0, 0, 1]]);

var eye2 = Matrix.eye(3, 2);
eye2.to2DArray().should.eql([[1, 0], [0, 1], [0, 0]]);
});
it('diag/diagonal', function () {
var arr = [1, 2, 3];
var diag = Matrix.diag(arr);
diag.should.eql(Matrix.diagonal(arr));
diag.to2DArray().should.eql([[1, 0, 0], [0, 2, 0], [0, 0, 3]]);

Matrix.diag(arr, 2).to2DArray().should.eql([[1, 0], [0, 2]]);
Matrix.diag(arr, 2, 4).to2DArray().should.eql([[1, 0, 0, 0], [0, 2, 0, 0]]);
Matrix.diag(arr, 4, 4).to2DArray().should.eql([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 0]]);
});
});
60 changes: 60 additions & 0 deletions test/matrix/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,70 @@ describe('utility methods', function () {
matrix = util.getSquareMatrix();
});

it('isMatrix', function () {
function notAMatrix(val) {
Matrix.isMatrix(val).should.be.false();
}
notAMatrix();
notAMatrix(1);
notAMatrix('string');
notAMatrix(null);
notAMatrix(new Array(6));
notAMatrix([[]]);
notAMatrix([[1, 2], [3, 4]]);

Matrix.isMatrix(new Matrix(4, 4)).should.be.true();
Matrix.isMatrix(Matrix.ones(4, 4)).should.be.true();
});

it('size', function () {
new Matrix(3, 4).size.should.equal(12);
new Matrix(5, 5).size.should.equal(25);
});

it('apply', function () {
var matrix = Matrix.ones(6, 5);
matrix[0][0] = 10;
var called = 0;
function cb(i, j) {
called++;
this.should.be.instanceOf(Matrix);
if (called === 1) {
this[i][j].should.equal(10);
} else if (called === 30) {
this[i][j] = 20;
}
}
matrix.apply(cb);
matrix[5][4].should.equal(20);
called.should.equal(30);
});

it('clone', function () {
var clone = matrix.clone();
clone.should.not.equal(matrix);
clone.should.eql(matrix);
clone.should.be.instanceOf(Matrix);
Matrix.isMatrix(clone).should.be.true();
});

it('to1DArray', function () {
var matrix = util.getSquareMatrix();
var array = matrix.to1DArray();
array.should.be.an.Array().with.lengthOf(9);
array.should.eql([9, 13, 5, 1, 11, 7, 2, 6, 3]);
array.should.not.be.instanceOf(Matrix);
Matrix.isMatrix(array).should.be.false();
});

it('to2DArray', function () {
var matrix = util.getSquareMatrix();
var array = matrix.to2DArray();
array.should.eql(util.getSquareArray());
array.should.be.an.Array().with.lengthOf(3);
array[0].should.be.an.Array().with.lengthOf(3);
array.should.not.be.instanceOf(Matrix);
Matrix.isMatrix(array).should.be.false();
});

it('transpose square', function () {
Expand Down

0 comments on commit 36e8a55

Please sign in to comment.