mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
added tests for map and forEach
This commit is contained in:
parent
fe2c2b59d2
commit
bf005f41a2
@ -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(), [
|
||||
[
|
||||
|
||||
@ -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]]);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user