From bf005f41a2e977380c8a3f4dddec603aa16774e4 Mon Sep 17 00:00:00 2001 From: Sebastien Piquemal Date: Fri, 30 Aug 2013 13:05:04 +0400 Subject: [PATCH] added tests for map and forEach --- test/function/utils/map.test.js | 1 - test/type/matrix.test.js | 64 +++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/test/function/utils/map.test.js b/test/function/utils/map.test.js index 6629b08b9..8457ee2cd 100644 --- a/test/function/utils/map.test.js +++ b/test/function/utils/map.test.js @@ -19,7 +19,6 @@ describe('map', function() { var arr = [[1,2,3], [4,5,6]]; assert.deepEqual(math.map(arr, function (value, index, obj) { - debugger return math.clone([value, index, obj === arr]); }).valueOf(), [ [ diff --git a/test/type/matrix.test.js b/test/type/matrix.test.js index 03caca404..b2fc60695 100644 --- a/test/type/matrix.test.js +++ b/test/type/matrix.test.js @@ -231,9 +231,36 @@ describe('matrix', function() { describe('map', function() { it('should apply the given function to all elements in the matrix', function() { - var m = math.matrix([[1,2,3], [4,5,6]]); - var m2 = m.map(function (value) { return value * 2; }); - assert.deepEqual(m2.valueOf(), [[2,4,6],[8,10,12]]); + var m, m2; + + m = math.matrix([ + [[1,2],[3,4]], + [[5,6],[7,8]], + [[9,10],[11,12]], + [[13,14],[15,16]] + ]); + m2 = m.map(function (value) { return value * 2; }); + assert.deepEqual(m2.valueOf(), [ + [[2,4],[6,8]], + [[10,12],[14,16]], + [[18,20],[22,24]], + [[26,28],[30,32]] + ]); + + m = math.matrix([1]); + m2 = m.map(function (value) { return value * 2; }); + assert.deepEqual(m2.valueOf(), [2]); + + m = math.matrix([1,2,3]); + m2 = m.map(function (value) { return value * 2; }); + assert.deepEqual(m2.valueOf(), [2,4,6]); + }); + + it('should work on empty matrices', function() { + var m, m2; + m = math.matrix([]); + m2 = m.map(function (value) { return value * 2; }); + assert.deepEqual(m2.valueOf(), []); }); it('should invoke callback with parameters value, index, obj', function() { @@ -260,6 +287,37 @@ describe('matrix', function() { describe('forEach', function() { + it('should run on all elements of the matrix, last dimension first', function() { + var m, output; + + m = math.matrix([ + [[1,2],[3,4]], + [[5,6],[7,8]], + [[9,10],[11,12]], + [[13,14],[15,16]] + ]); + output = []; + m.forEach(function (value) { output.push(value); }); + assert.deepEqual(output, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]); + + m = math.matrix([1]); + output = []; + m.forEach(function (value) { output.push(value); }); + assert.deepEqual(output, [1]); + + m = math.matrix([1,2,3]); + output = []; + m.forEach(function (value) { output.push(value); }); + assert.deepEqual(output, [1,2,3]); + }); + + it('should work on empty matrices', function() { + m = math.matrix([]); + output = []; + m.forEach(function (value) { output.push(value); }); + assert.deepEqual(output, []); + }); + it('should invoke callback with parameters value, index, obj', function() { var m = math.matrix([[1,2,3], [4,5,6]]);