added tests for map and forEach

This commit is contained in:
Sebastien Piquemal 2013-08-30 13:05:04 +04:00
parent fe2c2b59d2
commit bf005f41a2
2 changed files with 61 additions and 4 deletions

View File

@ -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(), [
[

View File

@ -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]]);