From fd2b5f246f9d675a74486225d4da8055d551b859 Mon Sep 17 00:00:00 2001 From: Sebastien Piquemal Date: Wed, 7 Aug 2013 21:27:20 +0400 Subject: [PATCH 1/4] refactored tests/expr --- test/expr/parser.test.js | 630 +++++++++++++++++++++------------------ test/expr/scope.test.js | 192 +++++++----- 2 files changed, 458 insertions(+), 364 deletions(-) diff --git a/test/expr/parser.test.js b/test/expr/parser.test.js index 73cee2c19..4dd70de1d 100644 --- a/test/expr/parser.test.js +++ b/test/expr/parser.test.js @@ -3,314 +3,364 @@ var assert = require('assert'), approx = require('../../tools/approx.js'), math = require('../../src/index.js'); - parser = math.parser(), matrix = math.matrix, range = math.range, round = math.round; -// test precedence -assert.equal(parser.eval('4-2+3'), 5); -assert.equal(parser.eval('4-(2+3)'), -1); -assert.equal(parser.eval('4-2-3'), -1); -assert.equal(parser.eval('4-(2-3)'), 5); +describe('parser', function() { -assert.equal(parser.eval('2+3*4'), 14); -assert.equal(parser.eval('2*3+4'), 10); -assert.equal(parser.eval('2*3^2'), 18); + var parser -assert.equal(parser.eval('2^3'), 8); -assert.equal(parser.eval('2^3^4'), Math.pow(2, Math.pow(3, 4))); -assert.equal(parser.eval('1.5^1.5^1.5'), parser.eval('1.5^(1.5^1.5)')); -assert.equal(parser.eval('1.5^1.5^1.5^1.5'), parser.eval('1.5^(1.5^(1.5^1.5))')); + beforeEach(function() { + parser = math.parser(); + }) -assert.equal(parser.eval('-3^2'), -9); -assert.equal(parser.eval('(-3)^2'), 9); + it('should respect operator precedence', function() { + assert.equal(parser.eval('4-2+3'), 5); + assert.equal(parser.eval('4-(2+3)'), -1); + assert.equal(parser.eval('4-2-3'), -1); + assert.equal(parser.eval('4-(2-3)'), 5); -assert.equal(parser.eval('2^3!'), 64); -assert.equal(parser.eval('2^(3!)'), 64); + assert.equal(parser.eval('2+3*4'), 14); + assert.equal(parser.eval('2*3+4'), 10); + assert.equal(parser.eval('2*3^2'), 18); -assert.equal(parser.eval('-4!'), -24); -assert.equal(parser.eval('3!+2'), 8); + assert.equal(parser.eval('2^3'), 8); + assert.equal(parser.eval('2^3^4'), Math.pow(2, Math.pow(3, 4))); + assert.equal(parser.eval('1.5^1.5^1.5'), parser.eval('1.5^(1.5^1.5)')); + assert.equal(parser.eval('1.5^1.5^1.5^1.5'), parser.eval('1.5^(1.5^(1.5^1.5))')); -assert.deepEqual(parser.eval('[1,2;3,4]\' * 2').valueOf(), [[2,6],[4,8]]); -assert.deepEqual(parser.eval('[1,2;3,4]\' * [5,6;7,8]').valueOf(), [[26,30],[38,44]]); -assert.deepEqual(parser.eval('[1,2;3,4] * [5,6;7,8]\'').valueOf(), [[17,23],[39,53]]); -assert.deepEqual(parser.eval('[1,2;3,4]\'+2').valueOf(), [[3,5],[4,6]]); + assert.equal(parser.eval('-3^2'), -9); + assert.equal(parser.eval('(-3)^2'), 9); -// test numbers -assert.equal(parser.eval('3'), 3); -assert.equal(parser.eval('3.2'), 3.2); -assert.equal(parser.eval('003.2'), 3.2); -assert.equal(parser.eval('003.200'), 3.2); -assert.equal(parser.eval('.2'), 0.2); -assert.equal(parser.eval('2.'), 2); -assert.throws(function () {parser.eval('.'); }); -assert.throws(function () {parser.eval('3.2.2'); }); -assert.equal(parser.eval('3e2'), 300); -assert.equal(parser.eval('300e2'), 30000); -assert.equal(parser.eval('300e+2'), 30000); -assert.equal(parser.eval('300e-2'), 3); -assert.equal(parser.eval('300E-2'), 3); -assert.equal(parser.eval('3.2e2'), 320); -assert.throws(function () {parser.eval('3.2e2.2'); }); + assert.equal(parser.eval('2^3!'), 64); + assert.equal(parser.eval('2^(3!)'), 64); + assert.equal(parser.eval('-4!'), -24); + assert.equal(parser.eval('3!+2'), 8); -// test constants -assert.deepEqual(parser.eval('i'), math.complex(0, 1)); -assert.deepEqual(parser.eval('pi'), Math.PI); - - -// test function calls -assert.equal(parser.eval('sqrt(4)'), 2); -assert.equal(parser.eval('sqrt(6+3)'), 3); -assert.equal(parser.eval('atan2(2,2)'), 0.7853981633974483); - -// test variables -assert.equal(parser.eval('a = 0.75'), 0.75); -assert.equal(parser.eval('a + 2'), 2.75); -assert.equal(parser.eval('a = 2'), 2); -assert.equal(parser.eval('a + 2'), 4); -approx.equal(parser.eval('pi * 2'), 6.283185307179586); -parser.remove('a'); -assert.throws(function() {parser.eval('a + 2'); }); // TODO: throws an stack overflow - -// test nested variable assignments -assert.equal(parser.eval('c = d = (e = 4.5)'), 4.5); -assert.equal(parser.get('c'), 4.5); -assert.equal(parser.get('d'), 4.5); -assert.equal(parser.get('e'), 4.5); -assert.deepEqual(parser.eval('a = [1,2,f=3]'), matrix([[1,2,3]])); -assert.equal(parser.get('f'), 3); -assert.equal(parser.eval('2 + (g = 3 + 4)'), 9); -assert.equal(parser.get('g'), 7); -assert.throws(function () {parser.eval('a(j = 3)')}, SyntaxError); - - -// test function assignments -parser.eval('x=100'); // for testing scoping of the function variables -assert.equal(parser.eval('function f(x) = x^2'), 'f(x)'); -assert.equal(parser.eval('f(3)'), 9); -assert.equal(parser.eval('x'), 100); -assert.equal(parser.eval('function g(x, y) = x^y'), 'g(x, y)'); -assert.equal(parser.eval('g(4,5)'), 1024); -var g = parser.eval('g'); - - -// test range -assert.ok(parser.eval('2:5') instanceof math.type.Range); -assert.deepEqual(parser.eval('2:5').toArray(), [2,3,4,5]); -assert.deepEqual(parser.eval('10:-2:2').toArray(), [10,8,6,4,2]); - -// test matrix -parser.set('a', matrix([ - [1,2,3], - [4,5,6], - [7,8,9] -])); -assert.deepEqual(parser.eval('a(1, :)'), matrix([[4,5,6]])); -assert.deepEqual(parser.eval('a(1, :1)'), matrix([[4,5]])); -assert.deepEqual(parser.eval('a(1, :end-1)'), matrix([[4,5]])); -assert.deepEqual(parser.eval('a(1, 1:)'), matrix([[5,6]])); -assert.deepEqual(parser.eval('a(1, 1:2)'), matrix([[5,6]])); -assert.deepEqual(parser.eval('a(1, 0:2:2)'), matrix([[4,6]])); -assert.deepEqual(parser.eval('a(:, 1)'), matrix([[2],[5],[8]])); -assert.deepEqual(parser.eval('a(:1, 1)'), matrix([[2],[5]])); -assert.deepEqual(parser.eval('a(:end-1, 1)'), matrix([[2],[5]])); -assert.deepEqual(parser.eval('a(1:, 1)'), matrix([[5],[8]])); -assert.deepEqual(parser.eval('a(1:2, 1)'), matrix([[5],[8]])); -assert.deepEqual(parser.eval('a(0:2:2, 1)'), matrix([[2],[8]])); -// TODO: implement and test support for Array (instead of Matrix) - -// test matrix resizing -assert.deepEqual(parser.eval('a = []'), matrix([[]])); -assert.deepEqual(parser.eval('a(0:2,0) = [1;2;3]'), matrix([[1],[2],[3]])); -assert.deepEqual(parser.eval('a(:,1) = [4;5;6]'), matrix([[1,4],[2,5],[3,6]])); - -assert.deepEqual(parser.eval('a = []'), matrix([[]])); -assert.deepEqual(parser.eval('a(0,2) = 3'), matrix([[0,0,3]])); -assert.deepEqual(parser.eval('a(1,:) = [4,5,6]'), matrix([[0,0,3],[4,5,6]])); - -assert.deepEqual(parser.eval('a = []'), matrix([[]])); -assert.deepEqual(parser.eval('a(2,0) = 3'), matrix([[0],[0],[3]])); -assert.deepEqual(parser.eval('a(:,1) = [4;5;6]'), matrix([[0,4],[0,5],[3,6]])); - -assert.deepEqual(parser.eval('a = []'), matrix([[]])); -assert.deepEqual(parser.eval('a(0,0:2) = [1,2,3]'), matrix([[1,2,3]])); -assert.deepEqual(parser.eval('a(1,:) = [4,5,6]'), matrix([[1,2,3],[4,5,6]])); - -// test matrix sizes -assert.ok(parser.eval('[1,2;3,4]') instanceof math.type.Matrix); -var m = parser.eval('[1,2,3;4,5,6]'); -assert.deepEqual(m.size(), [2,3]); -assert.deepEqual(m.valueOf(), [[1,2,3],[4,5,6]]); -var b = parser.eval('[5, 6; 1, 1]'); -assert.deepEqual(b.size(), [2,2]); -assert.deepEqual(b.valueOf(), [[5,6],[1,1]]); -b.set([1, [0, 1]], [[7, 8]]); -assert.deepEqual(b.size(), [2,2]); -assert.deepEqual(b.valueOf(), [[5,6],[7,8]]); -assert.deepEqual(parser.eval('[ ]').valueOf(), [[]]); - -// test matrix get/set submatrix -parser.eval('a=[1,2;3,4]'); -parser.eval('a(0,0) = 100'); -assert.deepEqual(parser.get('a').size(), [2,2]); -assert.deepEqual(parser.get('a').valueOf(), [[100,2],[3,4]]); -parser.eval('a(1:2,1:2) = [10,11;12,13]'); -assert.deepEqual(parser.get('a').size(), [3,3]); -assert.deepEqual(parser.get('a').valueOf(), [[100,2,0],[3,10,11],[0,12,13]]); -var a = parser.get('a'); -assert.deepEqual(a.get([math.range('0:2'), math.range('0:1')]).valueOf(), [[100,2],[3,10],[0,12]]); -assert.deepEqual(parser.eval('a(0:2,0:1)').valueOf(), [[100,2],[3,10],[0,12]]); - -// test get/set matrix for 3d matrix -assert.deepEqual(parser.eval('f=[1,2;3,4]'), matrix([[1,2],[3,4]])); -assert.deepEqual(parser.eval('size(f)'), matrix([2,2])); -/* TODO: doesn't work correctly - assert.deepEqual(parser.eval('f(:,:,1)=[5,6;7,8]'), matrix([ - [ - [1,2], - [3,4] - ], - [ - [5,6], - [7,8] - ] - ])); - */ -parser.set('f', matrix([ - [ - [1,5], - [2,6] - ], - [ - [3,7], - [4,8] - ] -])); -assert.deepEqual(parser.eval('size(f)'), matrix([2,2,2])); -assert.deepEqual(parser.eval('f(:,:,0)'), matrix([[[1],[2]],[[3],[4]]])); // TODO: last dimension should be squeezed -assert.deepEqual(parser.eval('f(:,:,1)'), matrix([[[5],[6]],[[7],[8]]])); // TODO: last dimension should be squeezed -assert.deepEqual(parser.eval('f(:,1,:)'), matrix([[[2,6]],[[4,8]]])); -assert.deepEqual(parser.eval('f(1,:,:)'), matrix([[[3,7],[4,8]]])); - -parser.eval('a=diag([1,2,3,4])'); -assert.deepEqual(parser.eval('a(2:end, 2:end)'), matrix([[3,0],[0,4]])); -assert.deepEqual(parser.eval('a(2:end, 1:end)=9*ones(2,3)'), matrix([ - [1,0,0,0], - [0,2,0,0], - [0,9,9,9], - [0,9,9,9] -])); -assert.deepEqual(parser.eval('a(1:end-1, 1:end-1)'), matrix([[2,0],[9,9]])); - - -// test matrix concatenation -parser = math.parser(); -parser.eval('a=[1,2;3,4]'); -parser.eval('b=[5,6;7,8]'); -assert.deepEqual(parser.eval('c=[a,b]'), matrix([[1,2,5,6],[3,4,7,8]])); -assert.deepEqual(parser.eval('c=[a;b]'), matrix([[1,2],[3,4],[5,6],[7,8]])); -assert.deepEqual(parser.eval('c=[a,b;b,a]'), matrix([[1,2,5,6],[3,4,7,8],[5,6,1,2],[7,8,3,4]])); -assert.deepEqual(parser.eval('c=[[1,2]; [3,4]]'), matrix([[1,2],[3,4]])); -assert.deepEqual(parser.eval('c=[1; [2;3]]'), matrix([[1],[2],[3]])); -assert.deepEqual(parser.eval('d=1:3'), range(1,3)); // d is a Range -assert.deepEqual(parser.eval('[d,d]'), matrix([[1,2,3,1,2,3]])); -assert.deepEqual(parser.eval('[d;d]'), matrix([[1,2,3],[1,2,3]])); -assert.deepEqual(parser.eval('e=1+d'), [2,3,4]); // e is an Array -assert.deepEqual(parser.eval('size(e)'), [3]); -assert.deepEqual(parser.eval('[e,e]'), matrix([[2,3,4,2,3,4]])); -assert.deepEqual(parser.eval('[e;e]'), matrix([[2,3,4],[2,3,4]])); -assert.deepEqual(parser.eval('[[],[]]'), matrix([[]])); -assert.deepEqual(parser.eval('[[],[]]').size(), [0, 0]); -assert.throws(function () {parser.eval('c=[a; [1,2,3] ]')}); - -// test matrix transpose -assert.deepEqual(parser.eval('[1,2,3;4,5,6]\'').valueOf(), [[1,4],[2,5],[3,6]]); -assert.ok(parser.eval('[1,2,3;4,5,6]\'') instanceof math.type.Matrix); -assert.deepEqual(parser.eval('23\'').valueOf(), 23); -assert.deepEqual(parser.eval('[1:4]').valueOf(), [[1,2,3,4]]); -assert.deepEqual(parser.eval('[1:4]\'').valueOf(), [[1],[2],[3],[4]]); -assert.deepEqual(parser.eval('size([1:4])').valueOf(), [1, 4]); - -// test element wise operators -assert.deepEqual(parser.eval('2.*3'), 6); -assert.deepEqual(parser.eval('2 .* 3'), 6); -assert.deepEqual(parser.eval('2. * 3'), 6); -assert.deepEqual(parser.eval('2 .^ 3'), 8); -assert.deepEqual(parser.eval('4./2'), 2); -assert.deepEqual(parser.eval('4 ./ 2'), 2); -assert.deepEqual(parser.eval('a=3; a.*4'), [12]); -assert.deepEqual(parser.eval('[1,2,3] .* [1,2,3]'), matrix([[1,4,9]])); -assert.deepEqual(parser.eval('[1,2,3] ./ [1,2,3]'), matrix([[1,1,1]])); -assert.deepEqual(parser.eval('[2,3] .^ [2,3]'), matrix([[4,27]])); - - -// test unit -assert.equal(parser.eval('5cm').toString(), '50 mm'); -assert.ok(parser.eval('5cm') instanceof math.type.Unit); -//assert.equal(parser.eval('5.08 cm * 1000 in inch').toString(), '2000 inch'); // TODO: this gives an error -assert.equal(parser.eval('(5.08 cm * 1000) in inch').toString(), '2000 inch'); -assert.equal(parser.eval('(5.08 cm * 1000) in mm').toString(), '50800 mm'); -assert.equal(parser.eval('ans in inch').toString(), '2000 inch'); - -parser = math.parser(); -assert.equal(parser.eval('a = 3'), 3); -assert.equal(parser.eval('function f(x) = a * x'), 'f(x)'); -assert.equal(parser.eval('f(2)'), 6); -assert.equal(parser.eval('a = 5'), 5); -assert.equal(parser.eval('f(2)'), 10); -assert.equal(parser.eval('function g(x) = x^q'), 'g(x)'); -assert.throws(function () { - parser.eval('g(3)') -}, function (err) { - return (err instanceof Error) && (err.toString() == 'Error: Undefined symbol q'); -}); -assert.equal(parser.eval('q = 4/2'), 2); -assert.equal(parser.eval('g(3)'), 9); - -// test undefined symbols, defining symbols, and removing symbols -parser = math.parser(); -var n = parser.parse('q'); -assert.throws(function () { n.eval(); }); -parser.eval('q=33'); -assert.equal(n.eval(), 33); -parser.remove('q'); -assert.throws(function () { n.eval(); }); - -n = parser.parse('qq(0,0)=33'); -assert.throws(function () { n.eval(); }); -parser.eval('qq=[1,2;3,4]'); -assert.deepEqual(n.eval(), matrix([[33,2],[3,4]])); -parser.eval('qq=[4]'); -assert.deepEqual(n.eval(), matrix([[33]])); -parser.remove('qq'); -assert.throws(function () { n.eval(); }); - - -// test a custom node handler -function CustomNode (params, paramScopes) { - this.params = params; - this.paramScopes = paramScopes; -} -CustomNode.prototype = new math.expr.node.Node(); -CustomNode.prototype.toString = function () { - return 'CustomNode'; -}; -CustomNode.prototype.eval = function () { - var strParams = []; - this.params.forEach(function (param) { - strParams.push(param.toString()); + assert.deepEqual(parser.eval('[1,2;3,4]\' * 2').valueOf(), [[2,6],[4,8]]); + assert.deepEqual(parser.eval('[1,2;3,4]\' * [5,6;7,8]').valueOf(), [[26,30],[38,44]]); + assert.deepEqual(parser.eval('[1,2;3,4] * [5,6;7,8]\'').valueOf(), [[17,23],[39,53]]); + assert.deepEqual(parser.eval('[1,2;3,4]\'+2').valueOf(), [[3,5],[4,6]]); }); - return 'CustomNode(' + strParams.join(', ') + ')'; -}; -math.expr.node.handlers['custom'] = CustomNode; + it('should parse valid numbers', function() { + assert.equal(parser.eval('3'), 3); + assert.equal(parser.eval('3.2'), 3.2); + assert.equal(parser.eval('003.2'), 3.2); + assert.equal(parser.eval('003.200'), 3.2); + assert.equal(parser.eval('.2'), 0.2); + assert.equal(parser.eval('2.'), 2); + assert.equal(parser.eval('3e2'), 300); + assert.equal(parser.eval('300e2'), 30000); + assert.equal(parser.eval('300e+2'), 30000); + assert.equal(parser.eval('300e-2'), 3); + assert.equal(parser.eval('300E-2'), 3); + assert.equal(parser.eval('3.2e2'), 320); + }); -var node = math.parse('custom(x, (2+x), sin(x))'); -assert.equal(node.eval(), 'CustomNode(x, 2 + x, sin(x))'); + + it('should throw an error with invalid numbers', function() { + assert.throws(function () {parser.eval('.'); }); + assert.throws(function () {parser.eval('3.2.2'); }); + assert.throws(function () {parser.eval('3.2e2.2'); }); + }); + + it('should parse constants', function() { + assert.deepEqual(parser.eval('i'), math.complex(0, 1)); + assert.deepEqual(parser.eval('pi'), Math.PI); + }); + + + it('should parse function calls', function() { + assert.equal(parser.eval('sqrt(4)'), 2); + assert.equal(parser.eval('sqrt(6+3)'), 3); + assert.equal(parser.eval('atan2(2,2)'), 0.7853981633974483); + }); + + + it('should parse valid assignments', function() { + assert.equal(parser.eval('a = 0.75'), 0.75); + assert.equal(parser.eval('a + 2'), 2.75); + assert.equal(parser.eval('a = 2'), 2); + assert.equal(parser.eval('a + 2'), 4); + approx.equal(parser.eval('pi * 2'), 6.283185307179586); + }); + + it('should throw an error for invalid assignments', function() { + assert.throws(function() {parser.eval('a + 2'); }); // TODO: throws an stack overflow + }); + + + it('should parse nested assignments', function() { + assert.equal(parser.eval('c = d = (e = 4.5)'), 4.5); + assert.equal(parser.get('c'), 4.5); + assert.equal(parser.get('d'), 4.5); + assert.equal(parser.get('e'), 4.5); + assert.deepEqual(parser.eval('a = [1,2,f=3]'), matrix([[1,2,3]])); + assert.equal(parser.get('f'), 3); + assert.equal(parser.eval('2 + (g = 3 + 4)'), 9); + assert.equal(parser.get('g'), 7); + }); + + it('should throw an error for invalid nested assignments', function() { + assert.throws(function () {parser.eval('a(j = 3)')}, SyntaxError); + }); + + + it('should parse function assignments', function() { + parser.eval('x=100'); // for testing scoping of the function variables + assert.equal(parser.eval('function f(x) = x^2'), 'f(x)'); + assert.equal(parser.eval('f(3)'), 9); + assert.equal(parser.eval('x'), 100); + assert.equal(parser.eval('function g(x, y) = x^y'), 'g(x, y)'); + assert.equal(parser.eval('g(4,5)'), 1024); + var g = parser.eval('g'); + }); + + + it('should parse ranges', function() { + assert.ok(parser.eval('2:5') instanceof math.type.Range); + assert.deepEqual(parser.eval('2:5').toArray(), [2,3,4,5]); + assert.deepEqual(parser.eval('10:-2:2').toArray(), [10,8,6,4,2]); + }); + + + it('should parse matrices', function() { + parser.set('a', matrix([ + [1,2,3], + [4,5,6], + [7,8,9] + ])); + assert.deepEqual(parser.eval('a(1, :)'), matrix([[4,5,6]])); + assert.deepEqual(parser.eval('a(1, :1)'), matrix([[4,5]])); + assert.deepEqual(parser.eval('a(1, :end-1)'), matrix([[4,5]])); + assert.deepEqual(parser.eval('a(1, 1:)'), matrix([[5,6]])); + assert.deepEqual(parser.eval('a(1, 1:2)'), matrix([[5,6]])); + assert.deepEqual(parser.eval('a(1, 0:2:2)'), matrix([[4,6]])); + assert.deepEqual(parser.eval('a(:, 1)'), matrix([[2],[5],[8]])); + assert.deepEqual(parser.eval('a(:1, 1)'), matrix([[2],[5]])); + assert.deepEqual(parser.eval('a(:end-1, 1)'), matrix([[2],[5]])); + assert.deepEqual(parser.eval('a(1:, 1)'), matrix([[5],[8]])); + assert.deepEqual(parser.eval('a(1:2, 1)'), matrix([[5],[8]])); + assert.deepEqual(parser.eval('a(0:2:2, 1)'), matrix([[2],[8]])); + // TODO: implement and test support for Array (instead of Matrix) + }); + + + it('should parse matrix resizings', function() { + assert.deepEqual(parser.eval('a = []'), matrix([[]])); + assert.deepEqual(parser.eval('a(0:2,0) = [1;2;3]'), matrix([[1],[2],[3]])); + assert.deepEqual(parser.eval('a(:,1) = [4;5;6]'), matrix([[1,4],[2,5],[3,6]])); + + assert.deepEqual(parser.eval('a = []'), matrix([[]])); + assert.deepEqual(parser.eval('a(0,2) = 3'), matrix([[0,0,3]])); + assert.deepEqual(parser.eval('a(1,:) = [4,5,6]'), matrix([[0,0,3],[4,5,6]])); + + assert.deepEqual(parser.eval('a = []'), matrix([[]])); + assert.deepEqual(parser.eval('a(2,0) = 3'), matrix([[0],[0],[3]])); + assert.deepEqual(parser.eval('a(:,1) = [4;5;6]'), matrix([[0,4],[0,5],[3,6]])); + + assert.deepEqual(parser.eval('a = []'), matrix([[]])); + assert.deepEqual(parser.eval('a(0,0:2) = [1,2,3]'), matrix([[1,2,3]])); + assert.deepEqual(parser.eval('a(1,:) = [4,5,6]'), matrix([[1,2,3],[4,5,6]])); + }); + + + it('should get the right matrix size', function() { + assert.ok(parser.eval('[1,2;3,4]') instanceof math.type.Matrix); + var m = parser.eval('[1,2,3;4,5,6]'); + assert.deepEqual(m.size(), [2,3]); + assert.deepEqual(m.valueOf(), [[1,2,3],[4,5,6]]); + var b = parser.eval('[5, 6; 1, 1]'); + assert.deepEqual(b.size(), [2,2]); + assert.deepEqual(b.valueOf(), [[5,6],[1,1]]); + b.set([1, [0, 1]], [[7, 8]]); + assert.deepEqual(b.size(), [2,2]); + assert.deepEqual(b.valueOf(), [[5,6],[7,8]]); + assert.deepEqual(parser.eval('[ ]').valueOf(), [[]]); + }); + + + it('should get/set the matrix correctly', function() { + parser.eval('a=[1,2;3,4]'); + parser.eval('a(0,0) = 100'); + assert.deepEqual(parser.get('a').size(), [2,2]); + assert.deepEqual(parser.get('a').valueOf(), [[100,2],[3,4]]); + parser.eval('a(1:2,1:2) = [10,11;12,13]'); + assert.deepEqual(parser.get('a').size(), [3,3]); + assert.deepEqual(parser.get('a').valueOf(), [[100,2,0],[3,10,11],[0,12,13]]); + var a = parser.get('a'); + assert.deepEqual(a.get([math.range('0:2'), math.range('0:1')]).valueOf(), [[100,2],[3,10],[0,12]]); + assert.deepEqual(parser.eval('a(0:2,0:1)').valueOf(), [[100,2],[3,10],[0,12]]); + }); + + + it('should get/set the matrix correctly for 3d matrices', function() { + assert.deepEqual(parser.eval('f=[1,2;3,4]'), matrix([[1,2],[3,4]])); + assert.deepEqual(parser.eval('size(f)'), matrix([2,2])); + /* TODO: doesn't work correctly + assert.deepEqual(parser.eval('f(:,:,1)=[5,6;7,8]'), matrix([ + [ + [1,2], + [3,4] + ], + [ + [5,6], + [7,8] + ] + ])); + */ + parser.set('f', matrix([ + [ + [1,5], + [2,6] + ], + [ + [3,7], + [4,8] + ] + ])); + assert.deepEqual(parser.eval('size(f)'), matrix([2,2,2])); + assert.deepEqual(parser.eval('f(:,:,0)'), matrix([[[1],[2]],[[3],[4]]])); // TODO: last dimension should be squeezed + assert.deepEqual(parser.eval('f(:,:,1)'), matrix([[[5],[6]],[[7],[8]]])); // TODO: last dimension should be squeezed + assert.deepEqual(parser.eval('f(:,1,:)'), matrix([[[2,6]],[[4,8]]])); + assert.deepEqual(parser.eval('f(1,:,:)'), matrix([[[3,7],[4,8]]])); + + parser.eval('a=diag([1,2,3,4])'); + assert.deepEqual(parser.eval('a(2:end, 2:end)'), matrix([[3,0],[0,4]])); + assert.deepEqual(parser.eval('a(2:end, 1:end)=9*ones(2,3)'), matrix([ + [1,0,0,0], + [0,2,0,0], + [0,9,9,9], + [0,9,9,9] + ])); + assert.deepEqual(parser.eval('a(1:end-1, 1:end-1)'), matrix([[2,0],[9,9]])); + }); + + + it('should parse matrix concatenations', function() { + parser.eval('a=[1,2;3,4]'); + parser.eval('b=[5,6;7,8]'); + assert.deepEqual(parser.eval('c=[a,b]'), matrix([[1,2,5,6],[3,4,7,8]])); + assert.deepEqual(parser.eval('c=[a;b]'), matrix([[1,2],[3,4],[5,6],[7,8]])); + assert.deepEqual(parser.eval('c=[a,b;b,a]'), matrix([[1,2,5,6],[3,4,7,8],[5,6,1,2],[7,8,3,4]])); + assert.deepEqual(parser.eval('c=[[1,2]; [3,4]]'), matrix([[1,2],[3,4]])); + assert.deepEqual(parser.eval('c=[1; [2;3]]'), matrix([[1],[2],[3]])); + assert.deepEqual(parser.eval('d=1:3'), range(1,3)); // d is a Range + assert.deepEqual(parser.eval('[d,d]'), matrix([[1,2,3,1,2,3]])); + assert.deepEqual(parser.eval('[d;d]'), matrix([[1,2,3],[1,2,3]])); + assert.deepEqual(parser.eval('e=1+d'), [2,3,4]); // e is an Array + assert.deepEqual(parser.eval('size(e)'), [3]); + assert.deepEqual(parser.eval('[e,e]'), matrix([[2,3,4,2,3,4]])); + assert.deepEqual(parser.eval('[e;e]'), matrix([[2,3,4],[2,3,4]])); + assert.deepEqual(parser.eval('[[],[]]'), matrix([[]])); + assert.deepEqual(parser.eval('[[],[]]').size(), [0, 0]); + }); + + it('should throw an error for invalid matrix concatenations', function() { + assert.throws(function () {parser.eval('c=[a; [1,2,3] ]')}); + }); + + it('should parse matrix transpositions', function() { + assert.deepEqual(parser.eval('[1,2,3;4,5,6]\'').valueOf(), [[1,4],[2,5],[3,6]]); + assert.ok(parser.eval('[1,2,3;4,5,6]\'') instanceof math.type.Matrix); + assert.deepEqual(parser.eval('23\'').valueOf(), 23); + assert.deepEqual(parser.eval('[1:4]').valueOf(), [[1,2,3,4]]); + assert.deepEqual(parser.eval('[1:4]\'').valueOf(), [[1],[2],[3],[4]]); + assert.deepEqual(parser.eval('size([1:4])').valueOf(), [1, 4]); + }); + + + it('parse element wise operators', function() { + assert.deepEqual(parser.eval('2.*3'), 6); + assert.deepEqual(parser.eval('2 .* 3'), 6); + assert.deepEqual(parser.eval('2. * 3'), 6); + assert.deepEqual(parser.eval('2 .^ 3'), 8); + assert.deepEqual(parser.eval('4./2'), 2); + assert.deepEqual(parser.eval('4 ./ 2'), 2); + assert.deepEqual(parser.eval('a=3; a.*4'), [12]); + assert.deepEqual(parser.eval('[1,2,3] .* [1,2,3]'), matrix([[1,4,9]])); + assert.deepEqual(parser.eval('[1,2,3] ./ [1,2,3]'), matrix([[1,1,1]])); + assert.deepEqual(parser.eval('[2,3] .^ [2,3]'), matrix([[4,27]])); + }); + + + it('should parse measurment units', function() { + assert.equal(parser.eval('5cm').toString(), '50 mm'); + assert.ok(parser.eval('5cm') instanceof math.type.Unit); + //assert.equal(parser.eval('5.08 cm * 1000 in inch').toString(), '2000 inch'); // TODO: this gives an error + assert.equal(parser.eval('(5.08 cm * 1000) in inch').toString(), '2000 inch'); + assert.equal(parser.eval('(5.08 cm * 1000) in mm').toString(), '50800 mm'); + assert.equal(parser.eval('ans in inch').toString(), '2000 inch'); + + parser = math.parser(); + assert.equal(parser.eval('a = 3'), 3); + assert.equal(parser.eval('function f(x) = a * x'), 'f(x)'); + assert.equal(parser.eval('f(2)'), 6); + assert.equal(parser.eval('a = 5'), 5); + assert.equal(parser.eval('f(2)'), 10); + assert.equal(parser.eval('function g(x) = x^q'), 'g(x)'); + assert.equal(parser.eval('q = 4/2'), 2); + assert.equal(parser.eval('g(3)'), 9); + }); + + it('should throw an error for invalid units', function() { + assert.equal(parser.eval('function g(x) = x^q'), 'g(x)'); + assert.throws(function () { + parser.eval('g(3)'); + }, function (err) { + return (err instanceof Error) && (err.toString() == 'Error: Undefined symbol q'); + }); + }); + + + it('should parse undefined symbols, defining symbols, and removing symbols', function() { + var n; + var n = parser.parse('q'); + assert.throws(function () { n.eval(); }); + parser.eval('q=33'); + assert.equal(n.eval(), 33); + parser.remove('q'); + assert.throws(function () { n.eval(); }); + + n = parser.parse('qq(0,0)=33'); + assert.throws(function () { n.eval(); }); + parser.eval('qq=[1,2;3,4]'); + assert.deepEqual(n.eval(), matrix([[33,2],[3,4]])); + parser.eval('qq=[4]'); + assert.deepEqual(n.eval(), matrix([[33]])); + parser.remove('qq'); + assert.throws(function () { n.eval(); }); + }); + it('should support custom node handlers', function() { + function CustomNode (params, paramScopes) { + this.params = params; + this.paramScopes = paramScopes; + } + CustomNode.prototype = new math.expr.node.Node(); + CustomNode.prototype.toString = function () { + return 'CustomNode'; + }; + CustomNode.prototype.eval = function () { + var strParams = []; + this.params.forEach(function (param) { + strParams.push(param.toString()); + }); + return 'CustomNode(' + strParams.join(', ') + ')'; + }; + + math.expr.node.handlers['custom'] = CustomNode; + + var node = math.parse('custom(x, (2+x), sin(x))'); + assert.equal(node.eval(), 'CustomNode(x, 2 + x, sin(x))'); + + }); // TODO: extensively test the Parser + +}); diff --git a/test/expr/scope.test.js b/test/expr/scope.test.js index 8c5cd0d23..4f6631c8c 100644 --- a/test/expr/scope.test.js +++ b/test/expr/scope.test.js @@ -4,84 +4,128 @@ var assert = require('assert'), math = require('../../src/index.js'), Scope = math.expr.Scope; -var scope1 = new Scope(); -var scope2 = new Scope(scope1); -var symbols3 = {myvar: 3}; -var scope3 = new Scope(scope2, symbols3); +describe('Scope', function() { -assert.equal(scope1.get('sin'), math.sin); -assert.equal(scope3.get('sin'), math.sin); -assert.equal(scope3.get('myvar'), 3); -assert.equal(scope2.get('myvar'), undefined); -assert.equal(scope1.get('myvar'), undefined); + describe('get', function() { -scope3.set('d', 4); -assert.equal(scope3.get('d'), 4); -assert.equal(symbols3['d'], 4); -assert.equal(scope2.get('d'), undefined); + var scope1 = new Scope(); + var scope2 = new Scope(scope1); + var symbols3 = {myvar: 3}; + var scope3 = new Scope(scope2, symbols3); -symbols3['pi'] = 3; -assert.equal(scope3.get('pi'), 3); -approx.equal(scope2.get('pi'), Math.PI); -approx.equal(scope1.get('pi'), Math.PI); + it('should get variables in the scope', function () { + assert.equal(scope1.get('sin'), math.sin); + assert.equal(scope3.get('sin'), math.sin); + assert.equal(scope3.get('myvar'), 3); + assert.equal(scope2.get('myvar'), undefined); + assert.equal(scope1.get('myvar'), undefined); + }) + + it('should support subscopes', function() { + var value5 = { + 'aaa': 'bbb' + }; + var scope5 = new Scope(); + scope5.set('value5', value5); + var sub5 = scope5.createSubScope(); + assert.equal(scope5.get('value5'), value5); + assert.equal(sub5.get('value5'), value5); + sub5.set('subValue5', 5); + assert.equal(scope5.get('subValue5'), undefined); + assert.equal(sub5.get('subValue5'), 5); + }) + }) + + describe('set', function() { + + var scope1 = new Scope(); + var scope2 = new Scope(scope1); + var symbols3 = {myvar: 3}; + var scope3 = new Scope(scope2, symbols3); + + it('should set variables in the scope', function() { + scope3.set('d', 4); + assert.equal(scope3.get('d'), 4); + assert.equal(symbols3['d'], 4); + assert.equal(scope2.get('d'), undefined); + + symbols3['pi'] = 3; + assert.equal(scope3.get('pi'), 3); + approx.equal(scope2.get('pi'), Math.PI); + approx.equal(scope1.get('pi'), Math.PI); + }) + + }) + + describe('clear', function() { + + it('should restore the constants', function() { + var symbols4 = {e: 5}; + var scope4 = new Scope(symbols4); + assert.equal(scope4.get('myvar'), undefined); + assert.equal(scope4.get('e'), 5); + scope4.clear(); + approx.equal(scope4.get('e'), Math.E); + assert.equal(symbols4['e'], undefined); + }) + + it('should clear a scope and its subscopes', function() { + var value5 = { + 'aaa': 'bbb' + }; + var scope5 = new Scope(); + scope5.set('value5', value5); + var sub5 = scope5.createSubScope(); + sub5.set('subValue5', 5); + + scope5.clear(); + assert.equal(scope5.get('value5'), undefined); + assert.equal(sub5.get('value5'), undefined); + assert.equal(scope5.get('subValue5'), undefined); + assert.equal(sub5.get('subValue5'), undefined); + }); + + }) + + describe('remove', function() { + + it('should remove variables', function () { + var symbols6 = { + aa: 'aa', + bb: 'bb', + cc: 'cc' + }; + var scope6 = new Scope(symbols6); + assert.equal(scope6.get('aa'), 'aa'); + assert.equal(scope6.get('bb'), 'bb'); + assert.equal(scope6.get('cc'), 'cc'); + scope6.remove('bb'); + assert.equal(scope6.get('aa'), 'aa'); + assert.equal(scope6.get('bb'), undefined); + assert.equal(scope6.get('cc'), 'cc'); + assert.deepEqual(symbols6, { + aa: 'aa', + cc: 'cc' + }); + }) + + }) -var symbols4 = {e: 5}; -var scope4 = new Scope(symbols4); -assert.equal(scope4.get('myvar'), undefined); -assert.equal(scope4.get('e'), 5); -scope4.clear(); -approx.equal(scope4.get('e'), Math.E); -assert.equal(symbols4['e'], undefined); + describe('clearCache', function() { -// test sub scope -var value5 = { - 'aaa': 'bbb' -}; -var scope5 = new Scope(); -scope5.set('value5', value5); -var sub5 = scope5.createSubScope(); -assert.equal(scope5.get('value5'), value5); -assert.equal(sub5.get('value5'), value5); -sub5.set('subValue5', 5); -assert.equal(scope5.get('subValue5'), undefined); -assert.equal(sub5.get('subValue5'), 5); + it('should clear cached variables', function() { + var scope7 = new Scope(); + scope7.set('a', 123); + assert.equal(scope7.get('a'), 123); + var scope8 = new Scope(scope7); + assert.equal(scope8.get('a'), 123); + // 'a' is now in the cache of scope8, refering to scope7 + scope8.parentScope = null; // remove scope7 as parent + assert.equal(scope8.get('a'), 123); // whoops! still in the cache + scope8.clearCache(); // clear the cache of scope8 + assert.equal(scope8.get('a'), undefined); // ah, that's better + }) + }) -// clear must clear a scope and its subscopes -scope5.clear(); -assert.equal(scope5.get('value5'), undefined); -assert.equal(sub5.get('value5'), undefined); -assert.equal(scope5.get('subValue5'), undefined); -assert.equal(sub5.get('subValue5'), undefined); - -// test remove -var symbols6 = { - aa: 'aa', - bb: 'bb', - cc: 'cc' -}; -var scope6 = new Scope(symbols6); -assert.equal(scope6.get('aa'), 'aa'); -assert.equal(scope6.get('bb'), 'bb'); -assert.equal(scope6.get('cc'), 'cc'); -scope6.remove('bb'); -assert.equal(scope6.get('aa'), 'aa'); -assert.equal(scope6.get('bb'), undefined); -assert.equal(scope6.get('cc'), 'cc'); -assert.deepEqual(symbols6, { - aa: 'aa', - cc: 'cc' -}); - - -// test cache -var scope7 = new Scope(); -scope7.set('a', 123); -assert.equal(scope7.get('a'), 123); -var scope8 = new Scope(scope7); -assert.equal(scope8.get('a'), 123); -// 'a' is now in the cache of scope8, refering to scope7 -scope8.parentScope = null; // remove scope7 as parent -assert.equal(scope8.get('a'), 123); // whoops! still in the cache -scope8.clearCache(); // clear the cache of scope8 -assert.equal(scope8.get('a'), undefined); // ah, that's better +}) \ No newline at end of file From 1e7603488c3f50ce50e36e8a971d3506441ce8dd Mon Sep 17 00:00:00 2001 From: Sebastien Piquemal Date: Thu, 8 Aug 2013 15:31:37 +0400 Subject: [PATCH 2/4] refactored refactored tests/functions/arithmetic --- test/function/arithmetic/abs.test.js | 32 ++-- test/function/arithmetic/add.test.js | 83 ++++++---- test/function/arithmetic/ceil.test.js | 69 ++++---- test/function/arithmetic/cube.test.js | 58 ++++--- test/function/arithmetic/divide.test.js | 158 ++++++++++-------- test/function/arithmetic/edivide.test.js | 111 ++++++++----- test/function/arithmetic/emultiply.test.js | 104 +++++++----- test/function/arithmetic/epow.test.js | 144 ++++++++-------- test/function/arithmetic/equal.test.js | 85 ++++++---- test/function/arithmetic/exp.test.js | 94 ++++++----- test/function/arithmetic/fix.test.js | 70 ++++---- test/function/arithmetic/floor.test.js | 66 ++++---- test/function/arithmetic/gcd.test.js | 63 ++++--- test/function/arithmetic/larger.test.js | 101 ++++++----- test/function/arithmetic/largereq.test.js | 101 ++++++----- test/function/arithmetic/lcm.test.js | 45 +++-- test/function/arithmetic/log.test.js | 88 +++++----- test/function/arithmetic/log10.test.js | 90 +++++----- test/function/arithmetic/mod.test.js | 64 ++++--- test/function/arithmetic/multiply.test.js | 175 +++++++++++--------- test/function/arithmetic/pow.test.js | 148 +++++++++-------- test/function/arithmetic/round.test.js | 57 ++++--- test/function/arithmetic/sign.test.js | 46 +++-- test/function/arithmetic/smaller.test.js | 100 ++++++----- test/function/arithmetic/smallereq.test.js | 104 +++++++----- test/function/arithmetic/sqrt.test.js | 52 +++--- test/function/arithmetic/square.test.js | 56 ++++--- test/function/arithmetic/subtract.test.js | 97 ++++++----- test/function/arithmetic/unaryminus.test.js | 74 +++++---- test/function/arithmetic/unequal.test.js | 85 ++++++---- test/function/arithmetic/xgcd.test.js | 69 ++++---- 31 files changed, 1557 insertions(+), 1132 deletions(-) diff --git a/test/function/arithmetic/abs.test.js b/test/function/arithmetic/abs.test.js index 974a1bdb9..f52561d0e 100644 --- a/test/function/arithmetic/abs.test.js +++ b/test/function/arithmetic/abs.test.js @@ -4,34 +4,22 @@ var math = require('../../../src/index.js'); describe('abs', function () { - it('parser', function () { + it('should be parsed correctly', function () { assert.equal(math.eval('abs(-4.2)'), 4.2); }); - it('number', function () { + it('should return the abs value of a number', function () { assert.equal(math.abs(-4.2), 4.2); assert.equal(math.abs(-3.5), 3.5); assert.equal(math.abs(100), 100); assert.equal(math.abs(0), 0); }); - it('complex', function () { + it('should return the modulus of a complex number', function () { assert.equal(math.abs(math.complex(3, -4)), 5); }); - it('unit', function () { - assert.throws(function () { - math.abs(math.unit(5, 'km')); - }); - }); - - it('string', function () { - assert.throws(function () { - math.abs('a string'); - }); - }); - - it('matrix, array', function () { + it('should return the absolute value of all elements in a matrix', function () { var a1 = math.abs(math.matrix([1,-2,3])); assert.ok(a1 instanceof math.type.Matrix); assert.deepEqual(a1.size(), [3]); @@ -42,4 +30,16 @@ describe('abs', function () { assert.deepEqual(a1, [2,1,0,1,2]) }); + it('should throw an error with a measurment unit', function () { + assert.throws(function () { + math.abs(math.unit(5, 'km')); + }); + }); + + it('should throw an error with a string', function () { + assert.throws(function () { + math.abs('a string'); + }); + }); + }); diff --git a/test/function/arithmetic/add.test.js b/test/function/arithmetic/add.test.js index b5318100f..47d8551bd 100644 --- a/test/function/arithmetic/add.test.js +++ b/test/function/arithmetic/add.test.js @@ -2,41 +2,58 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// parser -assert.equal(math.eval('add(2, 3)'), 5); -assert.equal(math.eval('2 + 3'), 5); -assert.equal(math.eval('2 + 3 + 4'), 9); +describe('add', function() { -// number -assert.equal(math.add(2, 3), 5); -assert.equal(math.add(-2, 3), 1); -assert.equal(math.add(2, -3), -1); -assert.equal(math.add(-5, -3), -8); + it('should be parsed correctly', function() { + assert.equal(math.eval('add(2, 3)'), 5); + assert.equal(math.eval('2 + 3'), 5); + assert.equal(math.eval('2 + 3 + 4'), 9); + }); -// complex -assert.equal(math.add(math.complex(3, -4), math.complex(8, 2)), '11 - 2i'); -assert.equal(math.add(math.complex(3, -4), 10), '13 - 4i'); -assert.equal(math.add(10, math.complex(3, -4)), '13 - 4i'); + it('should add two numbers', function() { + assert.equal(math.add(2, 3), 5); + assert.equal(math.add(-2, 3), 1); + assert.equal(math.add(2, -3), -1); + assert.equal(math.add(-5, -3), -8); + }); -// unit -assert.equal(math.add(math.unit(5, 'km'), math.unit(100, 'mile')).toString(), '165.93 km'); -assert.throws(function () { - math.add(math.unit(5, 'km'), math.unit(100, 'gram')); -}); -// string -assert.equal(math.add('hello ', 'world'), 'hello world'); -assert.equal(math.add('str', 123), 'str123'); -assert.equal(math.add(123, 'str'), '123str'); + it('should add two complex numbers', function() { + assert.equal(math.add(math.complex(3, -4), math.complex(8, 2)), '11 - 2i'); + assert.equal(math.add(math.complex(3, -4), 10), '13 - 4i'); + assert.equal(math.add(10, math.complex(3, -4)), '13 - 4i'); + }); -// matrix, array -var a2 = math.matrix([[1,2],[3,4]]); -var a3 = math.matrix([[5,6],[7,8]]); -var a4 = math.add(a2, a3); -assert.ok(a4 instanceof math.type.Matrix); -assert.deepEqual(a4.size(), [2,2]); -assert.deepEqual(a4.valueOf(), [[6,8],[10,12]]); -var a5 = math.pow(a2, 2); -assert.ok(a5 instanceof math.type.Matrix); -assert.deepEqual(a5.size(), [2,2]); -assert.deepEqual(a5.valueOf(), [[7,10],[15,22]]); + + it('should add two measures of the same unit', function() { + assert.equal(math.add(math.unit(5, 'km'), math.unit(100, 'mile')).toString(), '165.93 km'); + }); + + it('should throw an error for two measures of different units', function() { + assert.throws(function () { + math.add(math.unit(5, 'km'), math.unit(100, 'gram')); + }); + }); + + it('should concatenate two strings', function() { + assert.equal(math.add('hello ', 'world'), 'hello world'); + assert.equal(math.add('str', 123), 'str123'); + assert.equal(math.add(123, 'str'), '123str'); + }); + + + it('should add matrices correctly', function() { + var a2 = math.matrix([[1,2],[3,4]]); + var a3 = math.matrix([[5,6],[7,8]]); + var a4 = math.add(a2, a3); + assert.ok(a4 instanceof math.type.Matrix); + assert.deepEqual(a4.size(), [2,2]); + assert.deepEqual(a4.valueOf(), [[6,8],[10,12]]); + var a5 = math.pow(a2, 2); + assert.ok(a5 instanceof math.type.Matrix); + assert.deepEqual(a5.size(), [2,2]); + assert.deepEqual(a5.valueOf(), [[7,10],[15,22]]); + }); + + +}) \ No newline at end of file diff --git a/test/function/arithmetic/ceil.test.js b/test/function/arithmetic/ceil.test.js index 0935ac497..73c83b3b2 100644 --- a/test/function/arithmetic/ceil.test.js +++ b/test/function/arithmetic/ceil.test.js @@ -8,36 +8,49 @@ var assert = require('assert'), range = math.range, ceil = math.ceil; -// parser -assert.equal(math.eval('ceil(1.3)'), 2); -assert.equal(math.eval('ceil(1.8)'), 2); +describe('ceil', function() { -// number -approx.equal(ceil(0), 0); -approx.equal(ceil(1), 1); -approx.equal(ceil(1.3), 2); -approx.equal(ceil(1.8), 2); -approx.equal(ceil(2), 2); -approx.equal(ceil(-1), -1); -approx.equal(ceil(-1.3), -1); -approx.equal(ceil(-1.8), -1); -approx.equal(ceil(-2), -2); -approx.equal(ceil(-2.1), -2); -approx.deepEqual(ceil(math.pi), 4); + it('should be parsed correctly', function() { + assert.equal(math.eval('ceil(1.3)'), 2); + assert.equal(math.eval('ceil(1.8)'), 2); + }); -// complex -approx.deepEqual(ceil(complex(0, 0)), complex(0, 0)); -approx.deepEqual(ceil(complex(1.3, 1.8)), complex(2, 2)); -approx.deepEqual(ceil(math.i), complex(0, 1)); -approx.deepEqual(ceil(complex(-1.3, -1.8)), complex(-1, -1)); + it('should return the ceil of a number', function() { + approx.equal(ceil(0), 0); + approx.equal(ceil(1), 1); + approx.equal(ceil(1.3), 2); + approx.equal(ceil(1.8), 2); + approx.equal(ceil(2), 2); + approx.equal(ceil(-1), -1); + approx.equal(ceil(-1.3), -1); + approx.equal(ceil(-1.8), -1); + approx.equal(ceil(-2), -2); + approx.equal(ceil(-2.1), -2); + approx.deepEqual(ceil(math.pi), 4); + }); -// unit -assert.throws(function () {ceil(unit('5cm'))}, TypeError, 'Function ceil(unit) not supported'); -// string -assert.throws(function () {ceil('hello world')}, TypeError, 'Function ceil(string) not supported'); + it('should return the ceil of real and imag part of a complex', function() { + approx.deepEqual(ceil(complex(0, 0)), complex(0, 0)); + approx.deepEqual(ceil(complex(1.3, 1.8)), complex(2, 2)); + approx.deepEqual(ceil(math.i), complex(0, 1)); + approx.deepEqual(ceil(complex(-1.3, -1.8)), complex(-1, -1)); + }); -// matrix, array, range -approx.deepEqual(ceil([1.2, 3.4, 5.6, 7.8, 10.0]), [2, 4, 6, 8, 10]); -approx.deepEqual(ceil(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([2, 4, 6, 8, 10])); -approx.deepEqual(ceil(range(1.2, 2.2, 10)), [2, 4, 6, 8, 10]); + + it('should throw an error for units', function() { + assert.throws(function () {ceil(unit('5cm'))}, TypeError, 'Function ceil(unit) not supported'); + }); + + + it('should throw an error for strings', function() { + assert.throws(function () {ceil('hello world')}, TypeError, 'Function ceil(string) not supported'); + }); + + it('should ceil each element in a matrix, array or range', function() { + approx.deepEqual(ceil([1.2, 3.4, 5.6, 7.8, 10.0]), [2, 4, 6, 8, 10]); + approx.deepEqual(ceil(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([2, 4, 6, 8, 10])); + approx.deepEqual(ceil(range(1.2, 2.2, 10)), [2, 4, 6, 8, 10]); + }); + +}); diff --git a/test/function/arithmetic/cube.test.js b/test/function/arithmetic/cube.test.js index 502c11acc..42b83d503 100644 --- a/test/function/arithmetic/cube.test.js +++ b/test/function/arithmetic/cube.test.js @@ -6,30 +6,44 @@ var assert = require('assert'), range = math.range, cube = math.cube; -// parser -assert.equal(math.eval('cube(4)'), 64); +describe('cube', function() { -// number -assert.equal(cube(4), 64); -assert.equal(cube(-2), -8); -assert.equal(cube(0), 0); -assert.throws(function () {cube()}, SyntaxError, 'Wrong number of arguments in function cube (0 provided, 1 expected)'); -assert.throws(function () {cube(1, 2)}, SyntaxError, 'Wrong number of arguments in function cube (2 provided, 1 expected)'); + it('should be parsed correctly', function() { + assert.equal(math.eval('cube(4)'), 64); + }); -// complex -assert.deepEqual(cube(math.complex('2i')), math.complex('-8i')); -assert.deepEqual(cube(math.complex('2+3i')), math.complex('-46+9i')); -assert.deepEqual(cube(math.complex('2')), 8); + it('should return the cube of a number', function() { + assert.equal(cube(4), 64); + assert.equal(cube(-2), -8); + assert.equal(cube(0), 0); + }); -// unit -assert.throws(function () {cube(unit('5cm'))}); + it('should return the cube of a complex number', function() { + assert.deepEqual(cube(math.complex('2i')), math.complex('-8i')); + assert.deepEqual(cube(math.complex('2+3i')), math.complex('-46+9i')); + assert.deepEqual(cube(math.complex('2')), 8); + }); -// string -assert.throws(function () {cube('text')}); + it('should throw an error with strings', function() { + assert.throws(function () {cube('text')}); + }); -// array, matrix, range -// arrays are evaluated element wise -assert.deepEqual(cube([2,3,4,5]), [8,27,64,125]); -assert.deepEqual(cube(range(2,5)), [8,27,64,125]); -assert.deepEqual(cube(matrix([2,3,4,5])), matrix([8,27,64,125])); -assert.deepEqual(cube(matrix([[1,2],[3,4]])), matrix([[1,8],[27,64]])); + it('should throw an error with units', function() { + assert.throws(function () {cube(unit('5cm'))}); + }); + + it('should throw an error if there\'s wrong number of args', function() { + assert.throws(function () {cube()}, SyntaxError, 'Wrong number of arguments in function cube (0 provided, 1 expected)'); + assert.throws(function () {cube(1, 2)}, SyntaxError, 'Wrong number of arguments in function cube (2 provided, 1 expected)'); + }); + + it('should cube each element in a matrix, array or range', function() { + // array, matrix, range + // arrays are evaluated element wise + assert.deepEqual(cube([2,3,4,5]), [8,27,64,125]); + assert.deepEqual(cube(range(2,5)), [8,27,64,125]); + assert.deepEqual(cube(matrix([2,3,4,5])), matrix([8,27,64,125])); + assert.deepEqual(cube(matrix([[1,2],[3,4]])), matrix([[1,8],[27,64]])); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/divide.test.js b/test/function/arithmetic/divide.test.js index 7c336a0c6..06598bc58 100644 --- a/test/function/arithmetic/divide.test.js +++ b/test/function/arithmetic/divide.test.js @@ -5,80 +5,100 @@ math = require('../../../src/index.js'), divide = math.divide, complex = math.complex; +describe('divide', function() { -// parser -assert.equal(math.eval('4 / 2'), 2); -assert.equal(math.eval('8 / 2 / 2'), 2); -assert.equal(math.eval('divide(4, 2)'), 2); + it('should be parsed correctly', function() { + assert.equal(math.eval('4 / 2'), 2); + assert.equal(math.eval('8 / 2 / 2'), 2); + assert.equal(math.eval('divide(4, 2)'), 2); + }); -// number -assert.equal(divide(4, 2), 2); -assert.equal(divide(-4, 2), -2); -assert.equal(divide(4, -2), -2); -assert.equal(divide(-4, -2), 2); -assert.equal(divide(4, 0), Infinity); -assert.equal(divide(-4, 0), -Infinity); -assert.equal(divide(0, -5), 0); -assert.ok(isNaN(divide(0, 0))); + it('should divide two numbers', function() { + assert.equal(divide(4, 2), 2); + assert.equal(divide(-4, 2), -2); + assert.equal(divide(4, -2), -2); + assert.equal(divide(-4, -2), 2); + assert.equal(divide(4, 0), Infinity); + assert.equal(divide(-4, 0), -Infinity); + assert.equal(divide(0, -5), 0); + assert.ok(isNaN(divide(0, 0))); + }); -// wrong arguments -assert.throws(function () {divide(2,3,4); }); -assert.throws(function () {divide(2); }); + it('should divide two complex numbers', function() { + approx.deepEqual(divide(complex('2+3i'), 2), complex('1+1.5i')); + approx.deepEqual(divide(complex('2+3i'), complex('4i')), complex('0.75 - 0.5i')); + approx.deepEqual(divide(complex('2i'), complex('4i')), complex('0.5')); + approx.deepEqual(divide(4, complex('1+2i')), complex('0.8 - 1.6i')); + approx.deepEqual(divide(math.i, 0), complex(0, Infinity)); + approx.deepEqual(divide(complex(0,1), 0), complex(0, Infinity)); + approx.deepEqual(divide(complex(1,0), 0), complex(Infinity, 0)); + approx.deepEqual(divide(complex(0,1), complex(0,0)), complex(0, Infinity)); + approx.deepEqual(divide(complex(1,1), complex(0,0)), complex(Infinity, Infinity)); + approx.deepEqual(divide(complex(1,-1), complex(0,0)), complex(Infinity, -Infinity)); + approx.deepEqual(divide(complex(-1,1), complex(0,0)), complex(-Infinity, Infinity)); + approx.deepEqual(divide(complex(1,1), complex(0,1)), complex(1, -1)); + approx.deepEqual(divide(complex(1,1), complex(1,0)), complex(1, 1)); -// complex -approx.deepEqual(divide(complex('2+3i'), 2), complex('1+1.5i')); -approx.deepEqual(divide(complex('2+3i'), complex('4i')), complex('0.75 - 0.5i')); -approx.deepEqual(divide(complex('2i'), complex('4i')), complex('0.5')); -approx.deepEqual(divide(4, complex('1+2i')), complex('0.8 - 1.6i')); -approx.deepEqual(divide(math.i, 0), complex(0, Infinity)); -approx.deepEqual(divide(complex(0,1), 0), complex(0, Infinity)); -approx.deepEqual(divide(complex(1,0), 0), complex(Infinity, 0)); -approx.deepEqual(divide(complex(0,1), complex(0,0)), complex(0, Infinity)); -approx.deepEqual(divide(complex(1,1), complex(0,0)), complex(Infinity, Infinity)); -approx.deepEqual(divide(complex(1,-1), complex(0,0)), complex(Infinity, -Infinity)); -approx.deepEqual(divide(complex(-1,1), complex(0,0)), complex(-Infinity, Infinity)); -approx.deepEqual(divide(complex(1,1), complex(0,1)), complex(1, -1)); -approx.deepEqual(divide(complex(1,1), complex(1,0)), complex(1, 1)); + approx.deepEqual(divide(complex(2, 3), complex(4, 5)), complex('0.5609756097560976 + 0.0487804878048781i')); + approx.deepEqual(divide(complex(2, 3), complex(4, -5)), complex('-0.170731707317073 + 0.536585365853659i')); + approx.deepEqual(divide(complex(2, 3), complex(-4, 5)), complex('0.170731707317073 - 0.536585365853659i')); + approx.deepEqual(divide(complex(2, 3), complex(-4, -5)), complex('-0.5609756097560976 - 0.0487804878048781i')); + approx.deepEqual(divide(complex(2, -3), complex(4, 5)), complex('-0.170731707317073 - 0.536585365853659i')); + approx.deepEqual(divide(complex(2, -3), complex(4, -5)), complex('0.5609756097560976 - 0.0487804878048781i')); + approx.deepEqual(divide(complex(2, -3), complex(-4, 5)), complex('-0.5609756097560976 + 0.0487804878048781i')); + approx.deepEqual(divide(complex(2, -3), complex(-4, -5)), complex('0.170731707317073 + 0.536585365853659i')); + approx.deepEqual(divide(complex(-2, 3), complex(4, 5)), complex('0.170731707317073 + 0.536585365853659i')); + approx.deepEqual(divide(complex(-2, 3), complex(4, -5)), complex('-0.5609756097560976 + 0.0487804878048781i')); + approx.deepEqual(divide(complex(-2, 3), complex(-4, 5)), complex('0.5609756097560976 - 0.0487804878048781i')); + approx.deepEqual(divide(complex(-2, 3), complex(-4, -5)), complex('-0.170731707317073 - 0.536585365853659i')); + approx.deepEqual(divide(complex(-2, -3), complex(4, 5)), complex('-0.5609756097560976 - 0.0487804878048781i')); + approx.deepEqual(divide(complex(-2, -3), complex(4, -5)), complex('0.170731707317073 - 0.536585365853659i')); + approx.deepEqual(divide(complex(-2, -3), complex(-4, 5)), complex('-0.170731707317073 + 0.536585365853659i')); + approx.deepEqual(divide(complex(-2, -3), complex(-4, -5)), complex('0.5609756097560976 + 0.0487804878048781i')); + }); -approx.deepEqual(divide(complex(2, 3), complex(4, 5)), complex('0.5609756097560976 + 0.0487804878048781i')); -approx.deepEqual(divide(complex(2, 3), complex(4, -5)), complex('-0.170731707317073 + 0.536585365853659i')); -approx.deepEqual(divide(complex(2, 3), complex(-4, 5)), complex('0.170731707317073 - 0.536585365853659i')); -approx.deepEqual(divide(complex(2, 3), complex(-4, -5)), complex('-0.5609756097560976 - 0.0487804878048781i')); -approx.deepEqual(divide(complex(2, -3), complex(4, 5)), complex('-0.170731707317073 - 0.536585365853659i')); -approx.deepEqual(divide(complex(2, -3), complex(4, -5)), complex('0.5609756097560976 - 0.0487804878048781i')); -approx.deepEqual(divide(complex(2, -3), complex(-4, 5)), complex('-0.5609756097560976 + 0.0487804878048781i')); -approx.deepEqual(divide(complex(2, -3), complex(-4, -5)), complex('0.170731707317073 + 0.536585365853659i')); -approx.deepEqual(divide(complex(-2, 3), complex(4, 5)), complex('0.170731707317073 + 0.536585365853659i')); -approx.deepEqual(divide(complex(-2, 3), complex(4, -5)), complex('-0.5609756097560976 + 0.0487804878048781i')); -approx.deepEqual(divide(complex(-2, 3), complex(-4, 5)), complex('0.5609756097560976 - 0.0487804878048781i')); -approx.deepEqual(divide(complex(-2, 3), complex(-4, -5)), complex('-0.170731707317073 - 0.536585365853659i')); -approx.deepEqual(divide(complex(-2, -3), complex(4, 5)), complex('-0.5609756097560976 - 0.0487804878048781i')); -approx.deepEqual(divide(complex(-2, -3), complex(4, -5)), complex('0.170731707317073 - 0.536585365853659i')); -approx.deepEqual(divide(complex(-2, -3), complex(-4, 5)), complex('-0.170731707317073 + 0.536585365853659i')); -approx.deepEqual(divide(complex(-2, -3), complex(-4, -5)), complex('0.5609756097560976 + 0.0487804878048781i')); + it('should divide units by a number', function() { + assert.equal(divide(math.unit('5 m'), 10).toString(), '500 mm'); + }); -// unit -assert.equal(divide(math.unit('5 m'), 10).toString(), '500 mm'); -assert.throws(function () {divide(10, math.unit('5 m')).toString()}); + it('should divide each elements in a matrix by a number', function() { + assert.deepEqual(divide(math.range(2,2,6), 2), [1,2,3]); + a = math.matrix([[1,2],[3,4]]); + assert.deepEqual(divide(a, 2), math.matrix([[0.5,1],[1.5,2]])); + assert.deepEqual(divide(a.valueOf(), 2), [[0.5,1],[1.5,2]]); + assert.deepEqual(divide([], 2), []); + assert.deepEqual(divide([], 2), []); + }); -// matrix, array, range -assert.deepEqual(divide(math.range(2,2,6), 2), [1,2,3]); -a = math.matrix([[1,2],[3,4]]); -assert.deepEqual(divide(a, 2), math.matrix([[0.5,1],[1.5,2]])); -assert.deepEqual(divide(a.valueOf(), 2), [[0.5,1],[1.5,2]]); -assert.deepEqual(divide([], 2), []); -assert.deepEqual(divide([], 2), []); -assert.deepEqual(math.format(divide(1, [ - [ 1, 4, 7], - [ 3, 0, 5], - [-1, 9, 11] -])), math.format([ - [ 5.625, -2.375, -2.5], - [ 4.75, -2.25, -2], - [-3.375, 1.625, 1.5] -])); -a = math.matrix([[1,2],[3,4]]); -b = math.matrix([[5,6],[7,8]]); -assert.deepEqual(divide(a, b), math.matrix([[3,-2], [2,-1]])); -assert.throws(function () {divide(a, [[1]])}); + it('??? 1', function() { + assert.deepEqual(math.format(divide(1, [ + [ 1, 4, 7], + [ 3, 0, 5], + [-1, 9, 11] + ])), math.format([ + [ 5.625, -2.375, -2.5], + [ 4.75, -2.25, -2], + [-3.375, 1.625, 1.5] + ])); + }); + it('should perform matrix division', function() { + a = math.matrix([[1,2],[3,4]]); + b = math.matrix([[5,6],[7,8]]); + assert.deepEqual(divide(a, b), math.matrix([[3,-2], [2,-1]])); + }); + + it('??? 2', function() { + assert.throws(function () {divide(a, [[1]])}); + }); + + it('should throw an error if dividing a number by a unit', function() { + assert.throws(function () {divide(10, math.unit('5 m')).toString()}); + }); + + it('should throw an error if there\'s wrong number of arguments', function() { + assert.throws(function () {divide(2,3,4); }); + assert.throws(function () {divide(2); }); + }); + +}); diff --git a/test/function/arithmetic/edivide.test.js b/test/function/arithmetic/edivide.test.js index 95fbeebe8..257b8ea68 100644 --- a/test/function/arithmetic/edivide.test.js +++ b/test/function/arithmetic/edivide.test.js @@ -5,54 +5,75 @@ math = require('../../../src/index.js'), edivide = math.edivide, complex = math.complex; +describe('edivide', function() { -// parser -/* TODO: edivide for parser - assert.equal(math.eval('4 ./ 2'), 2); - assert.equal(math.eval('8 ./ 2 / 2'), 2); - */ -assert.equal(math.eval('edivide(4, 2)'), 2); + it('should be parsed correctly', function() { + /* TODO: edivide for parser + assert.equal(math.eval('4 ./ 2'), 2); + assert.equal(math.eval('8 ./ 2 / 2'), 2); + */ + assert.equal(math.eval('edivide(4, 2)'), 2); + }); -// number -assert.equal(edivide(4, 2), 2); -assert.equal(edivide(-4, 2), -2); -assert.equal(edivide(4, -2), -2); -assert.equal(edivide(-4, -2), 2); -assert.equal(edivide(4, 0), Infinity); -assert.equal(edivide(0, -5), 0); -assert.ok(isNaN(edivide(0, 0))); + it('should divide two numbers', function() { + assert.equal(edivide(4, 2), 2); + assert.equal(edivide(-4, 2), -2); + assert.equal(edivide(4, -2), -2); + assert.equal(edivide(-4, -2), 2); + assert.equal(edivide(4, 0), Infinity); + assert.equal(edivide(0, -5), 0); + assert.ok(isNaN(edivide(0, 0))); + }); -// wrong arguments -assert.throws(function () {edivide(2,3,4); }); -assert.throws(function () {edivide(2); }); + it('should throw an error if there\'s wrong number of arguments', function() { + assert.throws(function () {edivide(2,3,4); }); + assert.throws(function () {edivide(2); }); + }); -// complex -approx.deepEqual(edivide(complex('2+3i'), 2), complex('1+1.5i')); -approx.deepEqual(edivide(complex('2+3i'), complex('4i')), complex('0.75 - 0.5i')); -approx.deepEqual(edivide(complex('2i'), complex('4i')), 0.5); -approx.deepEqual(edivide(4, complex('1+2i')), complex('0.8 - 1.6i')); + it('should divide two complex numbers', function() { + approx.deepEqual(edivide(complex('2+3i'), 2), complex('1+1.5i')); + approx.deepEqual(edivide(complex('2+3i'), complex('4i')), complex('0.75 - 0.5i')); + approx.deepEqual(edivide(complex('2i'), complex('4i')), 0.5); + approx.deepEqual(edivide(4, complex('1+2i')), complex('0.8 - 1.6i')); + }); -// unit -assert.equal(edivide(math.unit('5 m'), 10).toString(), '500 mm'); -assert.throws(function () {edivide(10, math.unit('5 m')).toString()}); + it('should divide a unit by a number', function() { + assert.equal(edivide(math.unit('5 m'), 10).toString(), '500 mm'); + }); -// matrix, array, range -assert.deepEqual(edivide(math.range(2,2,6), 2), [1,2,3]); -a = math.matrix([[1,2],[3,4]]); -assert.deepEqual(edivide(a, 2), math.matrix([[0.5,1],[1.5,2]])); -assert.deepEqual(edivide(a.valueOf(), 2), [[0.5,1],[1.5,2]]); -assert.deepEqual(edivide([], 2), []); -assert.deepEqual(edivide([], 2), []); -approx.deepEqual(math.format(edivide(1, [ - [ 1, 4, 7], - [ 3, 0, 5], - [-1, 9, 11] -])), math.format([ - [ 1, 0.25, 1/7], - [ 1/3, Infinity, 0.2], - [-1, 1/9, 1/11] -])); -a = math.matrix([[1,2],[3,4]]); -b = math.matrix([[5,6],[7,8]]); -assert.deepEqual(edivide(a, b), math.matrix([[1/5, 2/6], [3/7,4/8]])); -assert.throws(function () {edivide(a, [[1]])}); + it('should throw an error if dividing a number by a unit', function() { + assert.throws(function () {edivide(10, math.unit('5 m')).toString()}); + }); + + it('should divide all the elements of a matrix by one number', function() { + assert.deepEqual(edivide(math.range(2,2,6), 2), [1,2,3]); + a = math.matrix([[1,2],[3,4]]); + assert.deepEqual(edivide(a, 2), math.matrix([[0.5,1],[1.5,2]])); + assert.deepEqual(edivide(a.valueOf(), 2), [[0.5,1],[1.5,2]]); + assert.deepEqual(edivide([], 2), []); + assert.deepEqual(edivide([], 2), []); + }); + + it('??? 1', function() { + approx.deepEqual(math.format(edivide(1, [ + [ 1, 4, 7], + [ 3, 0, 5], + [-1, 9, 11] + ])), math.format([ + [ 1, 0.25, 1/7], + [ 1/3, Infinity, 0.2], + [-1, 1/9, 1/11] + ])); + }); + + it('should perform matrix element-wise matrix division', function() { + a = math.matrix([[1,2],[3,4]]); + b = math.matrix([[5,6],[7,8]]); + assert.deepEqual(edivide(a, b), math.matrix([[1/5, 2/6], [3/7,4/8]])); + }); + + it('??? 2', function() { + assert.throws(function () {edivide(a, [[1]])}); + }); + +}); diff --git a/test/function/arithmetic/emultiply.test.js b/test/function/arithmetic/emultiply.test.js index 7c719ab38..59d5444ac 100644 --- a/test/function/arithmetic/emultiply.test.js +++ b/test/function/arithmetic/emultiply.test.js @@ -10,52 +10,72 @@ var assert = require('assert'), i = math.i, unit = math.unit; -// parser -/* TODO: emultiply for parser - approx.equal(math.eval('4 .* 2'), 8); - approx.equal(math.eval('8 .* 2 .* 2'), 32); - */ -approx.equal(math.eval('emultiply(4, 2)'), 8); +describe('emultiply', function() { -// number -approx.equal(emultiply(2, 3), 6); -approx.equal(emultiply(-2, 3), -6); -approx.equal(emultiply(-2, -3), 6); -approx.equal(emultiply(5, 0), 0); -approx.equal(emultiply(0, 5), 0); + it('should be parsed correctly', function() { + /* TODO: emultiply for parser + approx.equal(math.eval('4 .* 2'), 8); + approx.equal(math.eval('8 .* 2 .* 2'), 32); + */ + approx.equal(math.eval('emultiply(4, 2)'), 8); + }); -// complex -approx.deepEqual(emultiply(complex(2, 3), 2), complex(4, 6)); -approx.deepEqual(emultiply(complex(2, -3), 2), complex(4, -6)); -approx.deepEqual(emultiply(complex(0, 1), complex(2, 3)), complex(-3, 2)); -approx.deepEqual(emultiply(complex(2, 3), complex(2, 3)), complex(-5, 12)); -approx.deepEqual(emultiply(2, complex(2, 3)), complex(4, 6)); -approx.deepEqual(divide(complex(-5, 12), complex(2, 3)), complex(2, 3)); + it('should multiply 2 numbers', function() { + // number + approx.equal(emultiply(2, 3), 6); + approx.equal(emultiply(-2, 3), -6); + approx.equal(emultiply(-2, -3), 6); + approx.equal(emultiply(5, 0), 0); + approx.equal(emultiply(0, 5), 0); + }); -// unit -assert.equal(emultiply(2, unit('5 mm')).toString(), '10 mm'); -assert.equal(emultiply(2, unit('5 mm')).toString(), '10 mm'); -assert.equal(emultiply(unit('5 mm'), 2).toString(), '10 mm'); -assert.equal(emultiply(unit('5 mm'), 0).toString(), '0 m'); + it('should multiply 2 complex numbers', function() { + // complex + approx.deepEqual(emultiply(complex(2, 3), 2), complex(4, 6)); + approx.deepEqual(emultiply(complex(2, -3), 2), complex(4, -6)); + approx.deepEqual(emultiply(complex(0, 1), complex(2, 3)), complex(-3, 2)); + approx.deepEqual(emultiply(complex(2, 3), complex(2, 3)), complex(-5, 12)); + approx.deepEqual(emultiply(2, complex(2, 3)), complex(4, 6)); + approx.deepEqual(divide(complex(-5, 12), complex(2, 3)), complex(2, 3)); + }); -// string -assert.throws(function () {emultiply("hello", "world")}); -assert.throws(function () {emultiply("hello", 2)}); + it('should multiply a unit by a number', function() { + // unit + assert.equal(emultiply(2, unit('5 mm')).toString(), '10 mm'); + assert.equal(emultiply(2, unit('5 mm')).toString(), '10 mm'); + assert.equal(emultiply(unit('5 mm'), 2).toString(), '10 mm'); + assert.equal(emultiply(unit('5 mm'), 0).toString(), '0 m'); + }); -// matrix, array, range -var a = matrix([[1,2],[3,4]]); -var b = matrix([[5,6],[7,8]]); -var c = matrix([[5],[6]]); -var d = matrix([[5,6]]); -approx.deepEqual(emultiply(a, 3), matrix([[3,6],[9,12]])); -approx.deepEqual(emultiply(3, a), matrix([[3,6],[9,12]])); -approx.deepEqual(emultiply(a, b), matrix([[5,12],[21,32]])); + it('should throw an error with strings', function() { + // string + assert.throws(function () {emultiply("hello", "world")}); + assert.throws(function () {emultiply("hello", 2)}); + }); -approx.deepEqual(emultiply([[1,2],[3,4]], [[5,6],[7,8]]), [[5,12],[21,32]]); -approx.deepEqual(emultiply(range(1, 4), 2), [2, 4, 6, 8]); + var a = matrix([[1,2],[3,4]]); + var b = matrix([[5,6],[7,8]]); + var c = matrix([[5],[6]]); + var d = matrix([[5,6]]); -assert.throws(function () {emultiply(a, c)}); -assert.throws(function () {emultiply(d, a)}); -assert.throws(function () {emultiply(d, b)}); -assert.throws(function () {emultiply(d, c)}); -assert.throws(function () {emultiply(c, b)}); + it('should multiply a all elements in a matrix by a number', function() { + // matrix, array, range + approx.deepEqual(emultiply(a, 3), matrix([[3,6],[9,12]])); + approx.deepEqual(emultiply(3, a), matrix([[3,6],[9,12]])); + }); + + it('should perform element-wise matrix multiplication', function() { + approx.deepEqual(emultiply(a, b), matrix([[5,12],[21,32]])); + approx.deepEqual(emultiply([[1,2],[3,4]], [[5,6],[7,8]]), [[5,12],[21,32]]); + approx.deepEqual(emultiply(range(1, 4), 2), [2, 4, 6, 8]); + }); + + it('should throw an error if matrices are of different sizes', function() { + assert.throws(function () {emultiply(a, c)}); + assert.throws(function () {emultiply(d, a)}); + assert.throws(function () {emultiply(d, b)}); + assert.throws(function () {emultiply(d, c)}); + assert.throws(function () {emultiply(c, b)}); + }); + +}); diff --git a/test/function/arithmetic/epow.test.js b/test/function/arithmetic/epow.test.js index 66d8e6a0b..0819ad2f7 100644 --- a/test/function/arithmetic/epow.test.js +++ b/test/function/arithmetic/epow.test.js @@ -8,77 +8,89 @@ var assert = require('assert'), range = math.range, epow = math.epow; -// parser -/* TODO: epow for parser - approx.equal(math.eval('2.^3'), 8); - approx.equal(math.eval('-2.^2'), -4); // -(2^2) - approx.equal(math.eval('2.^3.^4'), 2.41785163922926e+24); // 2^(3^4) - */ -approx.equal(math.eval('epow(2,3)'), 8); -approx.equal(math.eval('epow(-2,2)'), 4); +describe('epow', function() { -// number -approx.deepEqual(epow(2,3), 8); -approx.deepEqual(epow(2,4), 16); -approx.deepEqual(epow(-2,2), 4); -approx.deepEqual(epow(3,3), 27); -approx.deepEqual(epow(3,-2), 0.111111111111111); -approx.deepEqual(epow(-3,-2), 0.111111111111111); -approx.deepEqual(epow(3,-3), 0.0370370370370370); -approx.deepEqual(epow(-3,-3), -0.0370370370370370); -approx.deepEqual(epow(2,1.5), 2.82842712474619); -approx.deepEqual(epow(-2,1.5), complex(0, -2.82842712474619)); + it('should be parsed correctly', function() { + /* TODO: epow for parser + approx.equal(math.eval('2.^3'), 8); + approx.equal(math.eval('-2.^2'), -4); // -(2^2) + approx.equal(math.eval('2.^3.^4'), 2.41785163922926e+24); // 2^(3^4) + */ + approx.equal(math.eval('epow(2,3)'), 8); + approx.equal(math.eval('epow(-2,2)'), 4); + }); -assert.throws(function () {epow(1)}, SyntaxError, 'Wrong number of arguments in function epow (1 provided, 2 expected)'); -assert.throws(function () {epow(1, 2, 3)}, SyntaxError, 'Wrong number of arguments in function epow (3 provided, 2 expected)'); + it('should elevate a number to the given power', function() { + approx.deepEqual(epow(2,3), 8); + approx.deepEqual(epow(2,4), 16); + approx.deepEqual(epow(-2,2), 4); + approx.deepEqual(epow(3,3), 27); + approx.deepEqual(epow(3,-2), 0.111111111111111); + approx.deepEqual(epow(-3,-2), 0.111111111111111); + approx.deepEqual(epow(3,-3), 0.0370370370370370); + approx.deepEqual(epow(-3,-3), -0.0370370370370370); + approx.deepEqual(epow(2,1.5), 2.82842712474619); + approx.deepEqual(epow(-2,1.5), complex(0, -2.82842712474619)); + }); -// complex -approx.deepEqual(epow(complex(-1,-1),complex(-1,-1)), complex('-0.0284750589322119 + 0.0606697332231795i')); -approx.deepEqual(epow(complex(-1,-1),complex(-1,1)), complex('-6.7536199239765713 + 3.1697803027015614i')); -approx.deepEqual(epow(complex(-1,-1),complex(0,-1)), complex('0.0891447921553914 - 0.0321946742909677i')); -approx.deepEqual(epow(complex(-1,-1),complex(0,1)), complex('9.92340022667813 + 3.58383962127501i')); -approx.deepEqual(epow(complex(-1,-1),complex(1,-1)), complex('-0.1213394664463591 - 0.0569501178644237i')); -approx.deepEqual(epow(complex(-1,-1),complex(1,1)), complex('-6.3395606054031211 - 13.5072398479531426i')); -approx.deepEqual(epow(complex(-1,1),complex(-1,-1)), complex('-6.7536199239765713 - 3.1697803027015614i')); -approx.deepEqual(epow(complex(-1,1),complex(-1,1)), complex('-0.0284750589322119 - 0.0606697332231795i')); -approx.deepEqual(epow(complex(-1,1),complex(0,-1)), complex('9.92340022667813 - 3.58383962127501i')); -approx.deepEqual(epow(complex(-1,1),complex(0,1)), complex('0.0891447921553914 + 0.0321946742909677i')); -approx.deepEqual(epow(complex(-1,1),complex(1,-1)), complex('-6.3395606054031211 + 13.5072398479531426i')); -approx.deepEqual(epow(complex(-1,1),complex(1,1)), complex('-0.1213394664463591 + 0.0569501178644237i')); + it('should throw an error if invalid number of arguments', function() { + assert.throws(function () {epow(1)}, SyntaxError, 'Wrong number of arguments in function epow (1 provided, 2 expected)'); + assert.throws(function () {epow(1, 2, 3)}, SyntaxError, 'Wrong number of arguments in function epow (3 provided, 2 expected)'); + }); -approx.deepEqual(epow(complex(0,-1),complex(-1,-1)), complex('0.000000000000000 + 0.207879576350762i')); -approx.deepEqual(epow(complex(0,-1),complex(-1,1)), complex('0.000000000000000 + 4.810477380965351i')); -approx.deepEqual(epow(complex(0,-1),complex(1,-1)), complex('0.000000000000000 - 0.207879576350762i')); -approx.deepEqual(epow(complex(0,-1),complex(1,1)), complex('0.000000000000000 - 4.810477380965351i')); -approx.deepEqual(epow(complex(0,1),complex(-1,-1)), complex('0.000000000000000 - 4.810477380965351i')); -approx.deepEqual(epow(complex(0,1),complex(-1,1)), complex('0.000000000000000 - 0.207879576350762i')); -approx.deepEqual(epow(complex(0,1),complex(1,-1)), complex('0.000000000000000 + 4.810477380965351i')); -approx.deepEqual(epow(complex(0,1),complex(1,1)), complex('0.000000000000000 + 0.207879576350762i')); + it('should elevate a complex number to the given power', function() { + approx.deepEqual(epow(complex(-1,-1),complex(-1,-1)), complex('-0.0284750589322119 + 0.0606697332231795i')); + approx.deepEqual(epow(complex(-1,-1),complex(-1,1)), complex('-6.7536199239765713 + 3.1697803027015614i')); + approx.deepEqual(epow(complex(-1,-1),complex(0,-1)), complex('0.0891447921553914 - 0.0321946742909677i')); + approx.deepEqual(epow(complex(-1,-1),complex(0,1)), complex('9.92340022667813 + 3.58383962127501i')); + approx.deepEqual(epow(complex(-1,-1),complex(1,-1)), complex('-0.1213394664463591 - 0.0569501178644237i')); + approx.deepEqual(epow(complex(-1,-1),complex(1,1)), complex('-6.3395606054031211 - 13.5072398479531426i')); + approx.deepEqual(epow(complex(-1,1),complex(-1,-1)), complex('-6.7536199239765713 - 3.1697803027015614i')); + approx.deepEqual(epow(complex(-1,1),complex(-1,1)), complex('-0.0284750589322119 - 0.0606697332231795i')); + approx.deepEqual(epow(complex(-1,1),complex(0,-1)), complex('9.92340022667813 - 3.58383962127501i')); + approx.deepEqual(epow(complex(-1,1),complex(0,1)), complex('0.0891447921553914 + 0.0321946742909677i')); + approx.deepEqual(epow(complex(-1,1),complex(1,-1)), complex('-6.3395606054031211 + 13.5072398479531426i')); + approx.deepEqual(epow(complex(-1,1),complex(1,1)), complex('-0.1213394664463591 + 0.0569501178644237i')); -approx.deepEqual(epow(complex(1,-1),complex(-1,-1)), complex('0.2918503793793073 + 0.1369786269150605i')); -approx.deepEqual(epow(complex(1,-1),complex(-1,1)), complex('0.6589325864505904 + 1.4039396486303144i')); -approx.deepEqual(epow(complex(1,-1),complex(0,-1)), complex('0.428829006294368 - 0.154871752464247i')); -approx.deepEqual(epow(complex(1,-1),complex(0,1)), complex('2.062872235080905 + 0.745007062179724i')); -approx.deepEqual(epow(complex(1,-1),complex(1,-1)), complex('0.2739572538301211 - 0.5837007587586147i')); -approx.deepEqual(epow(complex(1,-1),complex(1,1)), complex('2.8078792972606288 - 1.3178651729011805i')); -approx.deepEqual(epow(complex(1,1),complex(-1,-1)), complex('0.6589325864505904 - 1.4039396486303144i')); -approx.deepEqual(epow(complex(1,1),complex(-1,1)), complex('0.2918503793793073 - 0.1369786269150605i')); -approx.deepEqual(epow(complex(1,1),complex(0,-1)), complex('2.062872235080905 - 0.745007062179724i')); -approx.deepEqual(epow(complex(1,1),complex(0,1)), complex('0.428829006294368 + 0.154871752464247i')); -approx.deepEqual(epow(complex(1,1),complex(1,-1)), complex('2.8078792972606288 + 1.3178651729011805i')); -approx.deepEqual(epow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i')); + approx.deepEqual(epow(complex(0,-1),complex(-1,-1)), complex('0.000000000000000 + 0.207879576350762i')); + approx.deepEqual(epow(complex(0,-1),complex(-1,1)), complex('0.000000000000000 + 4.810477380965351i')); + approx.deepEqual(epow(complex(0,-1),complex(1,-1)), complex('0.000000000000000 - 0.207879576350762i')); + approx.deepEqual(epow(complex(0,-1),complex(1,1)), complex('0.000000000000000 - 4.810477380965351i')); + approx.deepEqual(epow(complex(0,1),complex(-1,-1)), complex('0.000000000000000 - 4.810477380965351i')); + approx.deepEqual(epow(complex(0,1),complex(-1,1)), complex('0.000000000000000 - 0.207879576350762i')); + approx.deepEqual(epow(complex(0,1),complex(1,-1)), complex('0.000000000000000 + 4.810477380965351i')); + approx.deepEqual(epow(complex(0,1),complex(1,1)), complex('0.000000000000000 + 0.207879576350762i')); -// unit -assert.throws(function () {epow(unit('5cm'))}); + approx.deepEqual(epow(complex(1,-1),complex(-1,-1)), complex('0.2918503793793073 + 0.1369786269150605i')); + approx.deepEqual(epow(complex(1,-1),complex(-1,1)), complex('0.6589325864505904 + 1.4039396486303144i')); + approx.deepEqual(epow(complex(1,-1),complex(0,-1)), complex('0.428829006294368 - 0.154871752464247i')); + approx.deepEqual(epow(complex(1,-1),complex(0,1)), complex('2.062872235080905 + 0.745007062179724i')); + approx.deepEqual(epow(complex(1,-1),complex(1,-1)), complex('0.2739572538301211 - 0.5837007587586147i')); + approx.deepEqual(epow(complex(1,-1),complex(1,1)), complex('2.8078792972606288 - 1.3178651729011805i')); + approx.deepEqual(epow(complex(1,1),complex(-1,-1)), complex('0.6589325864505904 - 1.4039396486303144i')); + approx.deepEqual(epow(complex(1,1),complex(-1,1)), complex('0.2918503793793073 - 0.1369786269150605i')); + approx.deepEqual(epow(complex(1,1),complex(0,-1)), complex('2.062872235080905 - 0.745007062179724i')); + approx.deepEqual(epow(complex(1,1),complex(0,1)), complex('0.428829006294368 + 0.154871752464247i')); + approx.deepEqual(epow(complex(1,1),complex(1,-1)), complex('2.8078792972606288 + 1.3178651729011805i')); + approx.deepEqual(epow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i')); + }); -// string -assert.throws(function () {epow('text')}); + it('should throw an error with units', function() { + assert.throws(function () {epow(unit('5cm'))}); + }); -// matrix, array, range -var a = [[1,2],[3,4]]; -var res = [[1,4],[9,16]]; -approx.deepEqual(epow(a, 2), res); -approx.deepEqual(epow(a, 2.5), [[1,5.65685424949238], [15.58845726811990, 32]]); -approx.deepEqual(epow(3, [2,3]), [9,27]); -approx.deepEqual(epow(matrix(a), 2), matrix(res)); -approx.deepEqual(epow([[1,2,3],[4,5,6]],2), [[1,4,9],[16,25,36]]); + it('should throw an error with strings', function() { + assert.throws(function () {epow('text')}); + }); + + it('should elevate each element in a matrix to the given power', function() { + var a = [[1,2],[3,4]]; + var res = [[1,4],[9,16]]; + approx.deepEqual(epow(a, 2), res); + approx.deepEqual(epow(a, 2.5), [[1,5.65685424949238], [15.58845726811990, 32]]); + approx.deepEqual(epow(3, [2,3]), [9,27]); + approx.deepEqual(epow(matrix(a), 2), matrix(res)); + approx.deepEqual(epow([[1,2,3],[4,5,6]],2), [[1,4,9],[16,25,36]]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/equal.test.js b/test/function/arithmetic/equal.test.js index 74611ddc9..d82c5e3b5 100644 --- a/test/function/arithmetic/equal.test.js +++ b/test/function/arithmetic/equal.test.js @@ -6,42 +6,59 @@ var assert = require('assert'), unit = math.unit, equal = math.equal; -// parser -assert.equal(math.eval('2 == 3'), false); -assert.equal(math.eval('2 == 2'), true); -assert.equal(math.eval('equal(2, 3)'), false); -assert.equal(math.eval('equal(2, 2)'), true); +describe('equal', function() { -// number -assert.equal(equal(2, 3), false); -assert.equal(equal(2, 2), true); -assert.equal(equal(0, 0), true); -assert.equal(equal(-2, 2), false); + it('should be parsed correctly', function() { + assert.equal(math.eval('2 == 3'), false); + assert.equal(math.eval('2 == 2'), true); + assert.equal(math.eval('equal(2, 3)'), false); + assert.equal(math.eval('equal(2, 2)'), true); + }); -// complex -assert.equal(equal(complex(2,3), complex(2,4)), false); -assert.equal(equal(complex(2,3), complex(2,3)), true); -assert.equal(equal(complex(1,3), complex(2,3)), false); -assert.equal(equal(complex(1,3), complex(2,4)), false); -assert.equal(equal(complex(2,0), 2), true); -assert.equal(equal(complex(2,1), 2), false); -assert.equal(equal(2, complex(2, 0)), true); -assert.equal(equal(2, complex(2, 1)), false); -assert.equal(equal(complex(2,0), 3), false); + it('should compare two numbers correctly', function() { + assert.equal(equal(2, 3), false); + assert.equal(equal(2, 2), true); + assert.equal(equal(0, 0), true); + assert.equal(equal(-2, 2), false); + }); -// unit -assert.equal(equal(unit('100cm'), unit('10inch')), false); -assert.equal(equal(unit('100cm'), unit('1m')), true); -//assert.equal(equal(unit('12inch'), unit('1foot')), true); // round-off error :( -//assert.equal(equal(unit('2.54cm'), unit('1inch')), true); // round-off error :( -assert.throws(function () {equal(unit('100cm'), 22)}); + it('should compare two complex numbers correctly', function() { + assert.equal(equal(complex(2,3), complex(2,4)), false); + assert.equal(equal(complex(2,3), complex(2,3)), true); + assert.equal(equal(complex(1,3), complex(2,3)), false); + assert.equal(equal(complex(1,3), complex(2,4)), false); + assert.equal(equal(complex(2,0), 2), true); + assert.equal(equal(complex(2,1), 2), false); + assert.equal(equal(2, complex(2, 0)), true); + assert.equal(equal(2, complex(2, 1)), false); + assert.equal(equal(complex(2,0), 3), false); + }); -// string -assert.equal(equal('0', 0), true); -assert.equal(equal('Hello', 'hello'), false); -assert.equal(equal('hello', 'hello'), true); + it('should compare two units correctly', function() { + assert.equal(equal(unit('100cm'), unit('10inch')), false); + assert.equal(equal(unit('100cm'), unit('1m')), true); + //assert.equal(equal(unit('12inch'), unit('1foot')), true); // round-off error :( + //assert.equal(equal(unit('2.54cm'), unit('1inch')), true); // round-off error :( + }); -// array, matrix -assert.deepEqual(equal([1,4,5], [3,4,5]), [false, true, true]); -assert.deepEqual(equal([1,4,5], matrix([3,4,5])), matrix([false, true, true])); -assert.throws(function () {equal([1,4,5], [3,4])}); + it('should throw an error when comparing a unit with a number', function() { + assert.throws(function () {equal(unit('100cm'), 22)}); + }); + + it('should compare two strings correctly', function() { + assert.equal(equal('0', 0), true); + assert.equal(equal('Hello', 'hello'), false); + assert.equal(equal('hello', 'hello'), true); + }); + + it('should compare two matrices correctly', function() { + assert.deepEqual(equal([1,4,5], [3,4,5]), [false, true, true]); + assert.deepEqual(equal([1,4,5], matrix([3,4,5])), matrix([false, true, true])); + }); + + it('should throw an error if matrices have different sizes', function() { + assert.throws(function () {equal([1,4,5], [3,4])}); + }); + + +}); diff --git a/test/function/arithmetic/exp.test.js b/test/function/arithmetic/exp.test.js index f6376daaf..473cd707a 100644 --- a/test/function/arithmetic/exp.test.js +++ b/test/function/arithmetic/exp.test.js @@ -8,50 +8,64 @@ var assert = require('assert'), range = math.range, exp = math.exp; -// parser -approx.equal(math.eval('exp(1)'), Math.E); -approx.deepEqual(math.eval('1+exp(pi*i)'), complex('0')); +describe('exp', function() { -// number -approx.equal(exp(-3), 0.0497870683678639); -approx.equal(exp(-2), 0.1353352832366127); -approx.equal(exp(-1), 0.3678794411714423); -approx.equal(exp(0), 1); -approx.equal(exp(1), 2.71828182845905); -approx.equal(exp(2), 7.38905609893065); -approx.equal(exp(3), 20.0855369231877); -approx.equal(exp(math.log(100)), 100); + it('should be parsed correctly', function() { + approx.equal(math.eval('exp(1)'), Math.E); + approx.deepEqual(math.eval('1+exp(pi*i)'), complex('0')); + }); -assert.throws(function () {exp()}, SyntaxError, 'Wrong number of arguments in function exp (0 provided, 1 expected)'); -assert.throws(function () {exp(1, 2)}, SyntaxError, 'Wrong number of arguments in function exp (2 provided, 1 expected)'); + it('should exponentiate a number correctly', function() { + // number + approx.equal(exp(-3), 0.0497870683678639); + approx.equal(exp(-2), 0.1353352832366127); + approx.equal(exp(-1), 0.3678794411714423); + approx.equal(exp(0), 1); + approx.equal(exp(1), 2.71828182845905); + approx.equal(exp(2), 7.38905609893065); + approx.equal(exp(3), 20.0855369231877); + approx.equal(exp(math.log(100)), 100); + }); -// complex -approx.deepEqual(exp(math.i), complex('0.540302305868140 + 0.841470984807897i')); -approx.deepEqual(exp(complex(0, -1)), complex('0.540302305868140 - 0.841470984807897i')); -approx.deepEqual(exp(complex(1, 1)), complex('1.46869393991589 + 2.28735528717884i')); -approx.deepEqual(exp(complex(1, -1)), complex('1.46869393991589 - 2.28735528717884i')); -approx.deepEqual(exp(complex(-1, -1)), complex('0.198766110346413 - 0.309559875653112i')); -approx.deepEqual(exp(complex(-1, 1)), complex('0.198766110346413 + 0.309559875653112i')); -approx.deepEqual(exp(complex(1, 0)), 2.71828182845905); + it('should throw an error if there\'s wrong number of arguments', function() { + assert.throws(function () {exp()}, SyntaxError, 'Wrong number of arguments in function exp (0 provided, 1 expected)'); + assert.throws(function () {exp(1, 2)}, SyntaxError, 'Wrong number of arguments in function exp (2 provided, 1 expected)'); + }); -approx.deepEqual(math.eval('exp(0.5 * pi * i)'), complex(0, 1)); -approx.deepEqual(math.eval('exp(1 * pi * i)'), complex(-1, 0)); -approx.deepEqual(math.eval('exp(1.5 * pi * i)'), complex(0, -1)); -approx.deepEqual(math.eval('exp(2 * pi * i)'), complex(1, 0)); -approx.deepEqual(math.eval('exp(-0.5 * pi * i)'), complex(0, -1)); -approx.deepEqual(math.eval('exp(-1 * pi * i)'), complex(-1, 0)); -approx.deepEqual(math.eval('exp(-1.5 * pi * i)'), complex(0, 1)); + it('should exponentiate a complex number correctly', function() { + approx.deepEqual(exp(math.i), complex('0.540302305868140 + 0.841470984807897i')); + approx.deepEqual(exp(complex(0, -1)), complex('0.540302305868140 - 0.841470984807897i')); + approx.deepEqual(exp(complex(1, 1)), complex('1.46869393991589 + 2.28735528717884i')); + approx.deepEqual(exp(complex(1, -1)), complex('1.46869393991589 - 2.28735528717884i')); + approx.deepEqual(exp(complex(-1, -1)), complex('0.198766110346413 - 0.309559875653112i')); + approx.deepEqual(exp(complex(-1, 1)), complex('0.198766110346413 + 0.309559875653112i')); + approx.deepEqual(exp(complex(1, 0)), 2.71828182845905); -// unit -assert.throws(function () {exp(unit('5cm'))}); + approx.deepEqual(math.eval('exp(0.5 * pi * i)'), complex(0, 1)); + approx.deepEqual(math.eval('exp(1 * pi * i)'), complex(-1, 0)); + approx.deepEqual(math.eval('exp(1.5 * pi * i)'), complex(0, -1)); + approx.deepEqual(math.eval('exp(2 * pi * i)'), complex(1, 0)); + approx.deepEqual(math.eval('exp(-0.5 * pi * i)'), complex(0, -1)); + approx.deepEqual(math.eval('exp(-1 * pi * i)'), complex(-1, 0)); + approx.deepEqual(math.eval('exp(-1.5 * pi * i)'), complex(0, 1)); + }); -// string -assert.throws(function () {exp('text')}); + it('should throw an error on a unit', function() { + assert.throws(function () {exp(unit('5cm'))}); + }); -// matrix, array, range -var res = [1, 2.71828182845905, 7.38905609893065, 20.0855369231877]; -approx.deepEqual(exp([0,1,2,3]), res); -approx.deepEqual(exp(range('0:3')), res); -approx.deepEqual(exp(matrix([0,1,2,3])), matrix(res)); -approx.deepEqual(exp(matrix([[0,1],[2,3]])), - matrix([[1, 2.71828182845905], [7.38905609893065, 20.0855369231877]])); + it('should throw an error with a string', function() { + assert.throws(function () {exp('text')}); + }); + + it('should exponentiate matrices, arrays and ranges correctly', function() { + var res = [1, 2.71828182845905, 7.38905609893065, 20.0855369231877]; + approx.deepEqual(exp([0,1,2,3]), res); + approx.deepEqual(exp(range('0:3')), res); + approx.deepEqual(exp(matrix([0,1,2,3])), matrix(res)); + approx.deepEqual(exp(matrix([[0,1],[2,3]])), + matrix([[1, 2.71828182845905], [7.38905609893065, 20.0855369231877]])); + }); + + +}); \ No newline at end of file diff --git a/test/function/arithmetic/fix.test.js b/test/function/arithmetic/fix.test.js index 4c167b9ae..0a218b3f0 100644 --- a/test/function/arithmetic/fix.test.js +++ b/test/function/arithmetic/fix.test.js @@ -8,36 +8,50 @@ var assert = require('assert'), range = math.range, fix = math.fix; -// parser -assert.equal(math.eval('fix(1.3)'), 1); -assert.equal(math.eval('fix(1.8)'), 1); +describe('fix ???', function() { -// number -approx.equal(fix(0), 0); -approx.equal(fix(1), 1); -approx.equal(fix(1.3), 1); -approx.equal(fix(1.8), 1); -approx.equal(fix(2), 2); -approx.equal(fix(-1), -1); -approx.equal(fix(-1.3), -1); -approx.equal(fix(-1.8), -1); -approx.equal(fix(-2), -2); -approx.equal(fix(-2.1), -2); -approx.deepEqual(fix(math.pi), 3); + it('should be parsed correctly', function() { + assert.equal(math.eval('fix(1.3)'), 1); + assert.equal(math.eval('fix(1.8)'), 1); + }); -// complex -approx.deepEqual(fix(complex(0, 0)), complex(0, 0)); -approx.deepEqual(fix(complex(1.3, 1.8)), complex(1, 1)); -approx.deepEqual(fix(math.i), complex(0, 1)); -approx.deepEqual(fix(complex(-1.3, -1.8)), complex(-1, -1)); + it('', function() { + approx.equal(fix(0), 0); + approx.equal(fix(1), 1); + approx.equal(fix(1.3), 1); + approx.equal(fix(1.8), 1); + approx.equal(fix(2), 2); + approx.equal(fix(-1), -1); + approx.equal(fix(-1.3), -1); + approx.equal(fix(-1.8), -1); + approx.equal(fix(-2), -2); + approx.equal(fix(-2.1), -2); + approx.deepEqual(fix(math.pi), 3); + }); -// unit -assert.throws(function () {fix(unit('5cm'))}, TypeError, 'Function fix(unit) not supported'); + it('should', function() { + // complex + approx.deepEqual(fix(complex(0, 0)), complex(0, 0)); + approx.deepEqual(fix(complex(1.3, 1.8)), complex(1, 1)); + approx.deepEqual(fix(math.i), complex(0, 1)); + approx.deepEqual(fix(complex(-1.3, -1.8)), complex(-1, -1)); + }); -// string -assert.throws(function () {fix('hello world')}, TypeError, 'Function fix(string) not supported'); + it('should', function() { + // unit + assert.throws(function () {fix(unit('5cm'))}, TypeError, 'Function fix(unit) not supported'); + }); -// matrix, array, range -approx.deepEqual(fix([1.2, 3.4, 5.6, 7.8, 10.0]), [1, 3, 5, 7, 10]); -approx.deepEqual(fix(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([1, 3, 5, 7, 10])); -approx.deepEqual(fix(range(1.2, 2.2, 10)), [1, 3, 5, 7, 10]); + it('should', function() { + // string + assert.throws(function () {fix('hello world')}, TypeError, 'Function fix(string) not supported'); + }); + + it('should', function() { + // matrix, array, range + approx.deepEqual(fix([1.2, 3.4, 5.6, 7.8, 10.0]), [1, 3, 5, 7, 10]); + approx.deepEqual(fix(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([1, 3, 5, 7, 10])); + approx.deepEqual(fix(range(1.2, 2.2, 10)), [1, 3, 5, 7, 10]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/floor.test.js b/test/function/arithmetic/floor.test.js index c300f2670..82e89e9e8 100644 --- a/test/function/arithmetic/floor.test.js +++ b/test/function/arithmetic/floor.test.js @@ -8,36 +8,46 @@ var assert = require('assert'), range = math.range, floor = math.floor; -// parser -assert.equal(math.eval('floor(1.3)'), 1); -assert.equal(math.eval('floor(1.8)'), 1); +describe('floor', function() { -// number -approx.equal(floor(0), 0); -approx.equal(floor(1), 1); -approx.equal(floor(1.3), 1); -approx.equal(floor(1.8), 1); -approx.equal(floor(2), 2); -approx.equal(floor(-1), -1); -approx.equal(floor(-1.3), -2); -approx.equal(floor(-1.8), -2); -approx.equal(floor(-2), -2); -approx.equal(floor(-2.1), -3); -approx.deepEqual(floor(math.pi), 3); + it('should be parsed correctly', function() { + assert.equal(math.eval('floor(1.3)'), 1); + assert.equal(math.eval('floor(1.8)'), 1); + }); -// complex -approx.deepEqual(floor(complex(0, 0)), complex(0, 0)); -approx.deepEqual(floor(complex(1.3, 1.8)), complex(1, 1)); -approx.deepEqual(floor(math.i), complex(0, 1)); -approx.deepEqual(floor(complex(-1.3, -1.8)), complex(-2, -2)); + it('should floor numbers correctly', function() { + approx.equal(floor(0), 0); + approx.equal(floor(1), 1); + approx.equal(floor(1.3), 1); + approx.equal(floor(1.8), 1); + approx.equal(floor(2), 2); + approx.equal(floor(-1), -1); + approx.equal(floor(-1.3), -2); + approx.equal(floor(-1.8), -2); + approx.equal(floor(-2), -2); + approx.equal(floor(-2.1), -3); + approx.deepEqual(floor(math.pi), 3); + }); -// unit -assert.throws(function () {floor(unit('5cm'))}, TypeError, 'Function floor(unit) not supported'); + it('should floor complex numbers correctly', function() { + approx.deepEqual(floor(complex(0, 0)), complex(0, 0)); + approx.deepEqual(floor(complex(1.3, 1.8)), complex(1, 1)); + approx.deepEqual(floor(math.i), complex(0, 1)); + approx.deepEqual(floor(complex(-1.3, -1.8)), complex(-2, -2)); + }); -// string -assert.throws(function () {floor('hello world')}, TypeError, 'Function floor(string) not supported'); + it('should throw an error with a unit', function() { + assert.throws(function () {floor(unit('5cm'))}, TypeError, 'Function floor(unit) not supported'); + }); -// matrix, array, range -approx.deepEqual(floor([1.2, 3.4, 5.6, 7.8, 10.0]), [1, 3, 5, 7, 10]); -approx.deepEqual(floor(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([1, 3, 5, 7, 10])); -approx.deepEqual(floor(range(1.2, 2.2, 10)), [1, 3, 5, 7, 10]); + it('should throw an error with a string', function() { + assert.throws(function () {floor('hello world')}, TypeError, 'Function floor(string) not supported'); + }); + + it('should floor all elements in a matrix', function() { + approx.deepEqual(floor([1.2, 3.4, 5.6, 7.8, 10.0]), [1, 3, 5, 7, 10]); + approx.deepEqual(floor(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([1, 3, 5, 7, 10])); + approx.deepEqual(floor(range(1.2, 2.2, 10)), [1, 3, 5, 7, 10]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/gcd.test.js b/test/function/arithmetic/gcd.test.js index ab8ae4d90..9eb9351a9 100644 --- a/test/function/arithmetic/gcd.test.js +++ b/test/function/arithmetic/gcd.test.js @@ -2,33 +2,46 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// parser -assert.equal(math.eval('gcd(12, 8)'), 4); +describe('gcd', function() { -// number -assert.equal(math.gcd(12, 8), 4); -assert.equal(math.gcd(8, 12), 4); -assert.equal(math.gcd(8, -12), 4); -assert.equal(math.gcd(-12, 8), 4); -assert.equal(math.gcd(12, -8), 4); -assert.equal(math.gcd(15, 3), 3); -assert.equal(math.gcd(3, 0), 3); -assert.equal(math.gcd(-3, 0), 3); -assert.equal(math.gcd(0, 3), 3); -assert.equal(math.gcd(0, -3), 3); -assert.equal(math.gcd(0, 0), 0); -assert.equal(math.gcd(25, 15, -10, 30), 5); -assert.throws(function () {math.gcd(1); }, SyntaxError, 'Wrong number of arguments in function gcd (3 provided, 1-2 expected)'); + it('should be parsed correctly', function() { + assert.equal(math.eval('gcd(12, 8)'), 4); + }); -// complex -assert.throws(function () {math.gcd(math.complex(1,3),2); }, TypeError, 'Function gcd(complex, number) not supported'); + it('should find the greatest common divisor of two or more numbers', function() { + assert.equal(math.gcd(12, 8), 4); + assert.equal(math.gcd(8, 12), 4); + assert.equal(math.gcd(8, -12), 4); + assert.equal(math.gcd(-12, 8), 4); + assert.equal(math.gcd(12, -8), 4); + assert.equal(math.gcd(15, 3), 3); + assert.equal(math.gcd(3, 0), 3); + assert.equal(math.gcd(-3, 0), 3); + assert.equal(math.gcd(0, 3), 3); + assert.equal(math.gcd(0, -3), 3); + assert.equal(math.gcd(0, 0), 0); + assert.equal(math.gcd(25, 15, -10, 30), 5); + }); -// string -assert.throws(function () {math.gcd('a', 2); }, TypeError, 'Function gcd(string, number) not supported'); -assert.throws(function () {math.gcd(2, 'a'); }, TypeError, 'Function gcd(number, string) not supported'); + it('should throw an error if only one argument', function() { + assert.throws(function () {math.gcd(1); }, SyntaxError, 'Wrong number of arguments in function gcd (3 provided, 1-2 expected)'); + }) -// unit -assert.throws(function () { math.gcd(math.unit('5cm'), 2); }, TypeError, 'Function gcd(unit, number) not supported'); + it('should throw an error with complex numbers', function() { + assert.throws(function () {math.gcd(math.complex(1,3),2); }, TypeError, 'Function gcd(complex, number) not supported'); + }); -// array, matrix, range -assert.deepEqual(math.gcd([5,2,3], [25,3,6]), [5, 1, 3]); + it('should throw an error with strings', function() { + assert.throws(function () {math.gcd('a', 2); }, TypeError, 'Function gcd(string, number) not supported'); + assert.throws(function () {math.gcd(2, 'a'); }, TypeError, 'Function gcd(number, string) not supported'); + }); + + it('should throw an error with units', function() { + assert.throws(function () { math.gcd(math.unit('5cm'), 2); }, TypeError, 'Function gcd(unit, number) not supported'); + }); + + it('should find the greatest common divisor element-wise in a matrix', function() { + assert.deepEqual(math.gcd([5,2,3], [25,3,6]), [5, 1, 3]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/larger.test.js b/test/function/arithmetic/larger.test.js index 18bac90ee..3004e8184 100644 --- a/test/function/arithmetic/larger.test.js +++ b/test/function/arithmetic/larger.test.js @@ -6,50 +6,67 @@ var assert = require('assert'), unit = math.unit, larger = math.larger; -// parser -assert.equal(math.eval('2 > 3'), false); -assert.equal(math.eval('2 > 2'), false); -assert.equal(math.eval('2 > 1'), true); -assert.equal(math.eval('larger(2, 3)'), false); -assert.equal(math.eval('larger(2, 2)'), false); -assert.equal(math.eval('larger(2, 1)'), true); +describe('larger', function() { -// number -assert.equal(larger(2, 3), false); -assert.equal(larger(2, 2), false); -assert.equal(larger(2, 1), true); -assert.equal(larger(0, 0), false); -assert.equal(larger(-2, 2), false); -assert.equal(larger(-2, -3), true); -assert.equal(larger(-3, -2), false); + it('should be parsed correctly', function() { + assert.equal(math.eval('2 > 3'), false); + assert.equal(math.eval('2 > 2'), false); + assert.equal(math.eval('2 > 1'), true); + assert.equal(math.eval('larger(2, 3)'), false); + assert.equal(math.eval('larger(2, 2)'), false); + assert.equal(math.eval('larger(2, 1)'), true); + }); -// complex -assert.equal(larger(complex(1,1), complex(1,2)), false); -assert.equal(larger(complex(1,1), complex(1,1)), false); -assert.equal(larger(complex(1,1), complex(2,1)), false); -assert.equal(larger(complex(1,6), complex(7,1)), false); -assert.equal(larger(complex(4,1), complex(2,2)), true); -assert.equal(larger(complex(2,0), 3), false); -assert.equal(larger(complex(2,0), 2), false); -assert.equal(larger(complex(2,0), 1), true); -assert.equal(larger(3, complex(2,0)), true); -assert.equal(larger(2, complex(2,0)), false); -assert.equal(larger(1, complex(2,0)), false); + it('should compare two numbers correctly', function() { + assert.equal(larger(2, 3), false); + assert.equal(larger(2, 2), false); + assert.equal(larger(2, 1), true); + assert.equal(larger(0, 0), false); + assert.equal(larger(-2, 2), false); + assert.equal(larger(-2, -3), true); + assert.equal(larger(-3, -2), false); + }); -// unit -assert.equal(larger(unit('100cm'), unit('10inch')), true); -assert.equal(larger(unit('99cm'), unit('1m')), false); -//assert.equal(larger(unit('100cm'), unit('1m')), false); // dangerous, round-off errors -assert.equal(larger(unit('101cm'), unit('1m')), true); -assert.throws(function () {larger(unit('100cm'), 22)}); + it('should compare two complex numbers correctly', function() { + assert.equal(larger(complex(1,1), complex(1,2)), false); + assert.equal(larger(complex(1,1), complex(1,1)), false); + assert.equal(larger(complex(1,1), complex(2,1)), false); + assert.equal(larger(complex(1,6), complex(7,1)), false); + assert.equal(larger(complex(4,1), complex(2,2)), true); + assert.equal(larger(complex(2,0), 3), false); + assert.equal(larger(complex(2,0), 2), false); + assert.equal(larger(complex(2,0), 1), true); + assert.equal(larger(3, complex(2,0)), true); + assert.equal(larger(2, complex(2,0)), false); + assert.equal(larger(1, complex(2,0)), false); + }); -// string -assert.equal(larger('0', 0), false); -assert.equal(larger('abd', 'abc'), true); -assert.equal(larger('abc', 'abc'), false); -assert.equal(larger('abc', 'abd'), false); + it('should add two measures of the same unit', function() { + assert.equal(larger(unit('100cm'), unit('10inch')), true); + assert.equal(larger(unit('99cm'), unit('1m')), false); + //assert.equal(larger(unit('100cm'), unit('1m')), false); // dangerous, round-off errors + assert.equal(larger(unit('101cm'), unit('1m')), true); + }); -// array, matrix -assert.deepEqual(larger([1,4,6], [3,4,5]), [false, false, true]); -assert.deepEqual(larger([1,4,6], matrix([3,4,5])), matrix([false, false, true])); -assert.throws(function () {larger([1,4,6], [3,4])}); + it('should throw an error if comparing a unit with a number', function() { + assert.throws(function () {larger(unit('100cm'), 22)}); + }); + + it('should perform lexical comparison for two strings', function() { + assert.equal(larger('0', 0), false); + + assert.equal(larger('abd', 'abc'), true); + assert.equal(larger('abc', 'abc'), false); + assert.equal(larger('abc', 'abd'), false); + }); + + it('should perform element-wise comparison for two matrices of same size', function() { + assert.deepEqual(larger([1,4,6], [3,4,5]), [false, false, true]); + assert.deepEqual(larger([1,4,6], matrix([3,4,5])), matrix([false, false, true])); + }); + + it('should throw an error if matrices are different sizes', function() { + assert.throws(function () {larger([1,4,6], [3,4])}); + }); + +}); diff --git a/test/function/arithmetic/largereq.test.js b/test/function/arithmetic/largereq.test.js index fb31967cb..3cc6288f0 100644 --- a/test/function/arithmetic/largereq.test.js +++ b/test/function/arithmetic/largereq.test.js @@ -6,50 +6,67 @@ var assert = require('assert'), unit = math.unit, largereq = math.largereq; -// parser -assert.equal(math.eval('2 >= 3'), false); -assert.equal(math.eval('2 >= 2'), true); -assert.equal(math.eval('2 >= 1'), true); -assert.equal(math.eval('largereq(2, 3)'), false); -assert.equal(math.eval('largereq(2, 2)'), true); -assert.equal(math.eval('largereq(2, 1)'), true); +describe('largereq', function() { -// number -assert.equal(largereq(2, 3), false); -assert.equal(largereq(2, 2), true); -assert.equal(largereq(2, 1), true); -assert.equal(largereq(0, 0), true); -assert.equal(largereq(-2, 2), false); -assert.equal(largereq(-2, -3), true); -assert.equal(largereq(-3, -2), false); + it('should be parsed correctly', function() { + assert.equal(math.eval('2 >= 3'), false); + assert.equal(math.eval('2 >= 2'), true); + assert.equal(math.eval('2 >= 1'), true); + assert.equal(math.eval('largereq(2, 3)'), false); + assert.equal(math.eval('largereq(2, 2)'), true); + assert.equal(math.eval('largereq(2, 1)'), true); + }); -// complex -assert.equal(largereq(complex(1,1), complex(1,2)), false); -assert.equal(largereq(complex(1,1), complex(1,1)), true); -assert.equal(largereq(complex(1,1), complex(2,1)), false); -assert.equal(largereq(complex(1,6), complex(7,1)), false); -assert.equal(largereq(complex(4,1), complex(2,2)), true); -assert.equal(largereq(complex(2,0), 3), false); -assert.equal(largereq(complex(2,0), 2), true); -assert.equal(largereq(complex(2,0), 1), true); -assert.equal(largereq(3, complex(2,0)), true); -assert.equal(largereq(2, complex(2,0)), true); -assert.equal(largereq(1, complex(2,0)), false); + it('should compare two numbers correctly', function() { + assert.equal(largereq(2, 3), false); + assert.equal(largereq(2, 2), true); + assert.equal(largereq(2, 1), true); + assert.equal(largereq(0, 0), true); + assert.equal(largereq(-2, 2), false); + assert.equal(largereq(-2, -3), true); + assert.equal(largereq(-3, -2), false); + }); -// unit -assert.equal(largereq(unit('100cm'), unit('10inch')), true); -assert.equal(largereq(unit('99cm'), unit('1m')), false); -//assert.equal(largereq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors -assert.equal(largereq(unit('101cm'), unit('1m')), true); -assert.throws(function () {largereq(unit('100cm'), 22)}); + it('should compare two complex numbers correctly', function() { + assert.equal(largereq(complex(1,1), complex(1,2)), false); + assert.equal(largereq(complex(1,1), complex(1,1)), true); + assert.equal(largereq(complex(1,1), complex(2,1)), false); + assert.equal(largereq(complex(1,6), complex(7,1)), false); + assert.equal(largereq(complex(4,1), complex(2,2)), true); + assert.equal(largereq(complex(2,0), 3), false); + assert.equal(largereq(complex(2,0), 2), true); + assert.equal(largereq(complex(2,0), 1), true); + assert.equal(largereq(3, complex(2,0)), true); + assert.equal(largereq(2, complex(2,0)), true); + assert.equal(largereq(1, complex(2,0)), false); + }); -// string -assert.equal(largereq('0', 0), true); -assert.equal(largereq('abd', 'abc'), true); -assert.equal(largereq('abc', 'abc'), true); -assert.equal(largereq('abc', 'abd'), false); + it('should compare two units correctly', function() { + assert.equal(largereq(unit('100cm'), unit('10inch')), true); + assert.equal(largereq(unit('99cm'), unit('1m')), false); + //assert.equal(largereq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors + assert.equal(largereq(unit('101cm'), unit('1m')), true); + }); -// array, matrix -assert.deepEqual(largereq([1,4,6], [3,4,5]), [false, true, true]); -assert.deepEqual(largereq([1,4,6], matrix([3,4,5])), matrix([false, true, true])); -assert.throws(function () {largereq([1,4,6], [3,4])}); + it('should throw an error if comparing a unit with a number', function() { + assert.throws(function () {largereq(unit('100cm'), 22)}); + }); + + it('should perform lexical comparison for 2 strings', function() { + assert.equal(largereq('0', 0), true); + assert.equal(largereq('abd', 'abc'), true); + assert.equal(largereq('abc', 'abc'), true); + assert.equal(largereq('abc', 'abd'), false); + }); + + it('should perform element-wise comparison for two matrices of the same size', function() { + assert.deepEqual(largereq([1,4,6], [3,4,5]), [false, true, true]); + assert.deepEqual(largereq([1,4,6], matrix([3,4,5])), matrix([false, true, true])); + }); + + it('should throw an error if comparing two matrices of different sizes', function() { + assert.throws(function () {largereq([1,4,6], [3,4])}); + }); + + +}); \ No newline at end of file diff --git a/test/function/arithmetic/lcm.test.js b/test/function/arithmetic/lcm.test.js index e9540d9d0..30879fd6f 100644 --- a/test/function/arithmetic/lcm.test.js +++ b/test/function/arithmetic/lcm.test.js @@ -1,25 +1,34 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// test lcm -assert.equal(math.lcm(4, 6), 12); -assert.equal(math.lcm(4, -6), 12); -assert.equal(math.lcm(6, 4), 12); -assert.equal(math.lcm(-6, 4), 12); -assert.equal(math.lcm(-6, -4), 12); -assert.equal(math.lcm(21, 6), 42); -assert.equal(math.lcm(3, -4, 24), 24); -assert.throws(function () {math.lcm(1); }, SyntaxError, 'Wrong number of arguments in function lcm (3 provided, 1-2 expected)'); +describe('lcm', function() { -// complex -assert.throws(function () {math.lcm(math.complex(1,3),2); }, TypeError, 'Function lcm(complex, number) not supported'); + it('should find the lowest common multiple of tow or more numbers', function() { + assert.equal(math.lcm(4, 6), 12); + assert.equal(math.lcm(4, -6), 12); + assert.equal(math.lcm(6, 4), 12); + assert.equal(math.lcm(-6, 4), 12); + assert.equal(math.lcm(-6, -4), 12); + assert.equal(math.lcm(21, 6), 42); + assert.equal(math.lcm(3, -4, 24), 24); + assert.throws(function () {math.lcm(1); }, SyntaxError, 'Wrong number of arguments in function lcm (3 provided, 1-2 expected)'); + }); -// string -assert.throws(function () {math.lcm('a', 2); }, TypeError, 'Function lcm(string, number) not supported'); -assert.throws(function () {math.lcm(2, 'a'); }, TypeError, 'Function lcm(number, string) not supported'); + it('should throw an error with complex numbers', function() { + assert.throws(function () {math.lcm(math.complex(1,3),2); }, TypeError, 'Function lcm(complex, number) not supported'); + }); -// unit -assert.throws(function () { math.lcm(math.unit('5cm'), 2); }, TypeError, 'Function lcm(unit, number) not supported'); + it('should throw an error with strings', function() { + assert.throws(function () {math.lcm('a', 2); }, TypeError, 'Function lcm(string, number) not supported'); + assert.throws(function () {math.lcm(2, 'a'); }, TypeError, 'Function lcm(number, string) not supported'); + }); -// array, matrix, range -assert.deepEqual(math.lcm([5,2,3], [25,3,6]), [25, 6, 6]); + it('should throw an error with units', function() { + assert.throws(function () { math.lcm(math.unit('5cm'), 2); }, TypeError, 'Function lcm(unit, number) not supported'); + }); + + it('should perform element-wise lcm on two or more matrices of the same size', function() { + assert.deepEqual(math.lcm([5,2,3], [25,3,6]), [25, 6, 6]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/log.test.js b/test/function/arithmetic/log.test.js index 49282b08c..b94c7d65f 100644 --- a/test/function/arithmetic/log.test.js +++ b/test/function/arithmetic/log.test.js @@ -8,47 +8,61 @@ var assert = require('assert'), range = math.range, log = math.log; -// parser -approx.equal(math.eval('log(e)'), 1); -approx.equal(math.eval('log(2.71828182845905)'), 1); +describe('log', function() { -// number -approx.deepEqual(log(-3), complex('1.098612288668110 + 3.141592653589793i')); -approx.deepEqual(log(-2), complex('0.693147180559945 + 3.141592653589793i')); -approx.deepEqual(log(-1), complex('0.000000000000000 + 3.141592653589793i')); -approx.deepEqual(log(0), -Infinity); -approx.deepEqual(log(1), 0); -approx.deepEqual(log(2), 0.693147180559945); -approx.deepEqual(log(3), 1.098612288668110); -approx.deepEqual(math.exp(log(100)), 100); + it('should be parsed correctly', function() { + approx.equal(math.eval('log(e)'), 1); + approx.equal(math.eval('log(2.71828182845905)'), 1); + }); -approx.deepEqual(log(100, 10), 2); -approx.deepEqual(log(1000, 10), 3); -approx.deepEqual(log(8, 2), 3); -approx.deepEqual(log(16, 2), 4); + it('should return the log of a number', function() { + approx.deepEqual(log(-3), complex('1.098612288668110 + 3.141592653589793i')); + approx.deepEqual(log(-2), complex('0.693147180559945 + 3.141592653589793i')); + approx.deepEqual(log(-1), complex('0.000000000000000 + 3.141592653589793i')); + approx.deepEqual(log(0), -Infinity); + approx.deepEqual(log(1), 0); + approx.deepEqual(log(2), 0.693147180559945); + approx.deepEqual(log(3), 1.098612288668110); + approx.deepEqual(math.exp(log(100)), 100); + }); -assert.throws(function () {log()}, SyntaxError, 'Wrong number of arguments in function log (0 provided, 1-2 expected)'); -assert.throws(function () {log(1, 2, 3)}, SyntaxError, 'Wrong number of arguments in function log (3 provided, 1-2 expected)'); + it('should return the log base N of a number', function() { + approx.deepEqual(log(100, 10), 2); + approx.deepEqual(log(1000, 10), 3); + approx.deepEqual(log(8, 2), 3); + approx.deepEqual(log(16, 2), 4); + }); -// complex -approx.deepEqual(log(math.i), complex('1.570796326794897i')); -approx.deepEqual(log(complex(0, -1)), complex('-1.570796326794897i')); -approx.deepEqual(log(complex(1, 1)), complex('0.346573590279973 + 0.785398163397448i')); -approx.deepEqual(log(complex(1, -1)), complex('0.346573590279973 - 0.785398163397448i')); -approx.deepEqual(log(complex(-1, -1)), complex('0.346573590279973 - 2.356194490192345i')); -approx.deepEqual(log(complex(-1, 1)), complex('0.346573590279973 + 2.356194490192345i')); -approx.deepEqual(log(complex(1, 0)), complex(0, 0)); + it('should throw an error if invalid number of arguments', function() { + assert.throws(function () {log()}, SyntaxError, 'Wrong number of arguments in function log (0 provided, 1-2 expected)'); + assert.throws(function () {log(1, 2, 3)}, SyntaxError, 'Wrong number of arguments in function log (3 provided, 1-2 expected)'); + }); -// unit -assert.throws(function () {log(unit('5cm'))}); + it('should return the log of a complex number', function() { + approx.deepEqual(log(math.i), complex('1.570796326794897i')); + approx.deepEqual(log(complex(0, -1)), complex('-1.570796326794897i')); + approx.deepEqual(log(complex(1, 1)), complex('0.346573590279973 + 0.785398163397448i')); + approx.deepEqual(log(complex(1, -1)), complex('0.346573590279973 - 0.785398163397448i')); + approx.deepEqual(log(complex(-1, -1)), complex('0.346573590279973 - 2.356194490192345i')); + approx.deepEqual(log(complex(-1, 1)), complex('0.346573590279973 + 2.356194490192345i')); + approx.deepEqual(log(complex(1, 0)), complex(0, 0)); + }); -// string -assert.throws(function () {log('text')}); + it('should throw an error when used on a unit', function() { + assert.throws(function () {log(unit('5cm'))}); + }); -// matrix, array, range -var res = [0, 0.693147180559945, 1.098612288668110, 1.386294361119891]; -approx.deepEqual(log([1,2,3,4]), res); -approx.deepEqual(log(range('1:4')), res); -approx.deepEqual(log(matrix([1,2,3,4])), matrix(res)); -approx.deepEqual(log(matrix([[1,2],[3,4]])), - matrix([[0, 0.693147180559945], [1.098612288668110, 1.386294361119891]])); + it('should throw an error when used on a string', function() { + assert.throws(function () {log('text')}); + }); + + it('should return the log of each element of a matrix', function() { + var res = [0, 0.693147180559945, 1.098612288668110, 1.386294361119891]; + approx.deepEqual(log([1,2,3,4]), res); + approx.deepEqual(log(range('1:4')), res); + approx.deepEqual(log(matrix([1,2,3,4])), matrix(res)); + approx.deepEqual(log(matrix([[1,2],[3,4]])), + matrix([[0, 0.693147180559945], [1.098612288668110, 1.386294361119891]])); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/log10.test.js b/test/function/arithmetic/log10.test.js index 32d2be17b..9058b4c3f 100644 --- a/test/function/arithmetic/log10.test.js +++ b/test/function/arithmetic/log10.test.js @@ -8,49 +8,61 @@ var assert = require('assert'), range = math.range, log10 = math.log10; -// parser -approx.equal(math.eval('log10(1000)'), 3); +describe('log10', function() { -// number -approx.deepEqual(log10(-3), complex('0.477121254719662 + 1.364376353841841i')); -approx.deepEqual(log10(-2), complex('0.301029995663981 + 1.364376353841841i')); -approx.deepEqual(log10(-1), complex('0.000000000000000 + 1.364376353841841i')); -approx.deepEqual(log10(0), -Infinity); -approx.deepEqual(log10(1), 0); -approx.deepEqual(log10(2), 0.301029995663981); -approx.deepEqual(log10(3), 0.477121254719662); + it('should be parsed correctly', function() { + approx.equal(math.eval('log10(1000)'), 3); + }); -approx.deepEqual(log10(0.01), -2); -approx.deepEqual(log10(0.1), -1); -approx.deepEqual(log10(1), 0); -approx.deepEqual(log10(10), 1); -approx.deepEqual(log10(100), 2); -approx.deepEqual(log10(1000), 3); + it('should return the log base 10 of a number', function() { + approx.deepEqual(log10(-3), complex('0.477121254719662 + 1.364376353841841i')); + approx.deepEqual(log10(-2), complex('0.301029995663981 + 1.364376353841841i')); + approx.deepEqual(log10(-1), complex('0.000000000000000 + 1.364376353841841i')); + approx.deepEqual(log10(0), -Infinity); + approx.deepEqual(log10(1), 0); + approx.deepEqual(log10(2), 0.301029995663981); + approx.deepEqual(log10(3), 0.477121254719662); -assert.throws(function () {log10()}, SyntaxError, 'Wrong number of arguments in function log10 (0 provided, 1 expected)'); -assert.throws(function () {log10(1, 2)}, SyntaxError, 'Wrong number of arguments in function log10 (2 provided, 1 expected)'); + approx.deepEqual(log10(0.01), -2); + approx.deepEqual(log10(0.1), -1); + approx.deepEqual(log10(1), 0); + approx.deepEqual(log10(10), 1); + approx.deepEqual(log10(100), 2); + approx.deepEqual(log10(1000), 3); + }); -// complex -approx.deepEqual(log10(complex(0, 1)), complex('0.000000000000000 + 0.682188176920921i')); -approx.deepEqual(log10(complex(0, -1)), complex('0.000000000000000 - 0.682188176920921i')); -approx.deepEqual(log10(complex(1, 1)), complex('0.150514997831991 + 0.341094088460460i')); -approx.deepEqual(log10(complex(1, -1)), complex('0.150514997831991 - 0.341094088460460i')); -approx.deepEqual(log10(complex(-1, -1)), complex('0.150514997831991 - 1.023282265381381i')); -approx.deepEqual(log10(complex(-1, 1)), complex('0.150514997831991 + 1.023282265381381i')); -approx.deepEqual(log10(complex(1, 0)), complex(0, 0)); + it('should throw an error if used with a wrong number of arguments', function() { + assert.throws(function () {log10()}, SyntaxError, 'Wrong number of arguments in function log10 (0 provided, 1 expected)'); + assert.throws(function () {log10(1, 2)}, SyntaxError, 'Wrong number of arguments in function log10 (2 provided, 1 expected)'); + }); -// unit -assert.throws(function () {log10(unit('5cm'))}); + it('should return the log base 10 of a complex number', function() { + approx.deepEqual(log10(complex(0, 1)), complex('0.000000000000000 + 0.682188176920921i')); + approx.deepEqual(log10(complex(0, -1)), complex('0.000000000000000 - 0.682188176920921i')); + approx.deepEqual(log10(complex(1, 1)), complex('0.150514997831991 + 0.341094088460460i')); + approx.deepEqual(log10(complex(1, -1)), complex('0.150514997831991 - 0.341094088460460i')); + approx.deepEqual(log10(complex(-1, -1)), complex('0.150514997831991 - 1.023282265381381i')); + approx.deepEqual(log10(complex(-1, 1)), complex('0.150514997831991 + 1.023282265381381i')); + approx.deepEqual(log10(complex(1, 0)), complex(0, 0)); + }); -// string -assert.throws(function () {log10('text')}); + it('should throw an error when used on a unit', function() { + assert.throws(function () {log10(unit('5cm'))}); + }); -// matrix, array, range -var res = [0, 0.301029995663981, 0.477121254719662, 0.602059991327962]; -approx.deepEqual(log10([1,2,3,4]), res); -approx.deepEqual(log10(range('1:4')), res); -approx.deepEqual(log10(matrix([1,2,3,4])), matrix(res)); -approx.deepEqual(math.divide(log10(matrix([1,2,3,4])), math.LOG10E), - matrix([0, 0.693147180559945, 1.098612288668110, 1.386294361119891])); -approx.deepEqual(log10(matrix([[1,2],[3,4]])), - matrix([[0, 0.301029995663981], [0.477121254719662, 0.602059991327962]])); + it('should throw an error when used on a string', function() { + assert.throws(function () {log10('text')}); + }); + + it('should return the log base 10 of each element of a matrix', function() { + var res = [0, 0.301029995663981, 0.477121254719662, 0.602059991327962]; + approx.deepEqual(log10([1,2,3,4]), res); + approx.deepEqual(log10(range('1:4')), res); + approx.deepEqual(log10(matrix([1,2,3,4])), matrix(res)); + approx.deepEqual(math.divide(log10(matrix([1,2,3,4])), math.LOG10E), + matrix([0, 0.693147180559945, 1.098612288668110, 1.386294361119891])); + approx.deepEqual(log10(matrix([[1,2],[3,4]])), + matrix([[0, 0.301029995663981], [0.477121254719662, 0.602059991327962]])); + }); + +}); diff --git a/test/function/arithmetic/mod.test.js b/test/function/arithmetic/mod.test.js index 986773649..dc7733197 100644 --- a/test/function/arithmetic/mod.test.js +++ b/test/function/arithmetic/mod.test.js @@ -6,34 +6,46 @@ var assert = require('assert'), range = math.range, mod = math.mod; -// test parser -approx.equal(math.eval('8 % 3'), 2); -approx.equal(math.eval('mod(8, 3)'), 2); +describe('mod', function() { -// test number -approx.equal(mod(7, 2), 1); -approx.equal(mod(9, 3), 0); -approx.equal(mod(10, 4), 2); -assert.throws(function () {mod(10, -4)}); -approx.equal(mod(-10, 4), 2); -assert.throws(function () {mod(-10, -4)}); -approx.equal(mod(8.2, 3), 2.2); -approx.equal(mod(4, 1.5), 1); -approx.equal(mod(0, 3), 0); + it('should be parsed correctly', function() { + approx.equal(math.eval('8 % 3'), 2); + approx.equal(math.eval('mod(8, 3)'), 2); + }); -// test wrong number of arguments -assert.throws(function () {mod(1)}, SyntaxError); -assert.throws(function () {mod(1,2,3)}, SyntaxError); + it('should perform the modulus of two numbers', function() { + approx.equal(mod(7, 2), 1); + approx.equal(mod(9, 3), 0); + approx.equal(mod(10, 4), 2); + approx.equal(mod(-10, 4), 2); + approx.equal(mod(8.2, 3), 2.2); + approx.equal(mod(4, 1.5), 1); + approx.equal(mod(0, 3), 0); + }); -// test complex -assert.throws(function () {mod(math.complex(1,2), 3)}, TypeError); -assert.throws(function () {mod(3, math.complex(1,2))}, TypeError); + it('should throw an error if the modulus is negative', function() { + assert.throws(function () {mod(10, -4)}); + }); -// test string -assert.throws(function () {mod('string', 3)}, TypeError); -assert.throws(function () {mod(5, 'string')}, TypeError); + it('should throw an error if used with wrong number of arguments', function() { + assert.throws(function () {mod(1)}, SyntaxError); + assert.throws(function () {mod(1,2,3)}, SyntaxError); + }); -// test array, matrix, range -approx.deepEqual(mod([-4,-3,-2,-1,0,1,2,3,4], 3), [2,0,1,2,0,1,2,0,1]); -approx.deepEqual(mod(matrix([-4,-3,-2,-1,0,1,2,3,4]), 3), matrix([2,0,1,2,0,1,2,0,1])); -approx.deepEqual(mod(range(-4,4), 3), [2,0,1,2,0,1,2,0,1]); + it('should throw an error if used on complex numbers', function() { + assert.throws(function () {mod(math.complex(1,2), 3)}, TypeError); + assert.throws(function () {mod(3, math.complex(1,2))}, TypeError); + }); + + it('should an throw an error if used on a string', function() { + assert.throws(function () {mod('string', 3)}, TypeError); + assert.throws(function () {mod(5, 'string')}, TypeError); + }); + + it('should perform element-wise modulus on a matrix', function() { + approx.deepEqual(mod([-4,-3,-2,-1,0,1,2,3,4], 3), [2,0,1,2,0,1,2,0,1]); + approx.deepEqual(mod(matrix([-4,-3,-2,-1,0,1,2,3,4]), 3), matrix([2,0,1,2,0,1,2,0,1])); + approx.deepEqual(mod(range(-4,4), 3), [2,0,1,2,0,1,2,0,1]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/multiply.test.js b/test/function/arithmetic/multiply.test.js index d4615835a..c696b4b6a 100644 --- a/test/function/arithmetic/multiply.test.js +++ b/test/function/arithmetic/multiply.test.js @@ -10,89 +10,106 @@ var assert = require('assert'), i = math.i, unit = math.unit; -// parser -approx.equal(math.eval('4 * 2'), 8); -approx.equal(math.eval('8 * 2 * 2'), 32); -approx.equal(math.eval('multiply(4, 2)'), 8); +describe('multiply', function() { -// number -approx.equal(multiply(2, 3), 6); -approx.equal(multiply(-2, 3), -6); -approx.equal(multiply(-2, -3), 6); -approx.equal(multiply(5, 0), 0); -approx.equal(multiply(0, 5), 0); -approx.deepEqual(multiply(0, Infinity), NaN); -approx.deepEqual(multiply(2, Infinity), Infinity); -approx.deepEqual(multiply(-2, Infinity), -Infinity); + it('should be parsed correctly', function() { + approx.equal(math.eval('4 * 2'), 8); + approx.equal(math.eval('8 * 2 * 2'), 32); + approx.equal(math.eval('multiply(4, 2)'), 8); + }); -// complex -approx.deepEqual(multiply(complex(2, 3), 2), complex(4, 6)); -approx.deepEqual(multiply(complex(2, -3), -2), complex(-4, 6)); -approx.deepEqual(multiply(complex(2, -3), 2), complex(4, -6)); -approx.deepEqual(multiply(complex(-2, 3), 2), complex(-4, 6)); -approx.deepEqual(multiply(complex(-2, -3), 2), complex(-4, -6)); -approx.deepEqual(multiply(2, complex(2, 3)), complex(4, 6)); -approx.deepEqual(multiply(i, complex(2, 3)), complex(-3, 2)); -approx.deepEqual(multiply(complex(0, 1), complex(2, 3)), complex(-3, 2)); -approx.deepEqual(multiply(complex(1, 1), complex(2, 3)), complex(-1, 5)); -approx.deepEqual(multiply(complex(2, 3), complex(1, 1)), complex(-1, 5)); -approx.deepEqual(multiply(complex(2, 3), complex(2, 3)), complex(-5, 12)); -approx.deepEqual(divide(complex(-5, 12), complex(2, 3)), complex(2, 3)); -approx.deepEqual(multiply(complex(2, 3), 0), complex(0, 0)); -approx.deepEqual(multiply(complex(0, 3), complex(0, -4)), complex(12, 0)); -approx.deepEqual(multiply(multiply(3, i), multiply(-4, i)), complex(12, 0)); -approx.deepEqual(multiply(math.i, Infinity), complex(0, Infinity)); -approx.deepEqual(multiply(Infinity, math.i), complex(0, Infinity)); + it('should multiply two numbers correctly', function() { + approx.equal(multiply(2, 3), 6); + approx.equal(multiply(-2, 3), -6); + approx.equal(multiply(-2, -3), 6); + approx.equal(multiply(5, 0), 0); + approx.equal(multiply(0, 5), 0); + approx.deepEqual(multiply(0, Infinity), NaN); + approx.deepEqual(multiply(2, Infinity), Infinity); + approx.deepEqual(multiply(-2, Infinity), -Infinity); + }); -approx.deepEqual(multiply(complex(2,0), complex(0,2)), complex(0, 4)); -approx.deepEqual(multiply(complex(0,2), complex(0,2)), -4); -approx.deepEqual(multiply(complex(2,2), complex(0,2)), complex(-4, 4)); -approx.deepEqual(multiply(complex(2,0), complex(2,2)), complex(4, 4)); -approx.deepEqual(multiply(complex(0,2), complex(2,2)), complex(-4, 4)); -approx.deepEqual(multiply(complex(2,2), complex(2,2)), complex(0, 8)); -approx.deepEqual(multiply(complex(2,0), complex(2,0)), 4); -approx.deepEqual(multiply(complex(0,2), complex(2,0)), complex(0, 4)); -approx.deepEqual(multiply(complex(2,2), complex(2,0)), complex(4, 4)); + it('should multiply two complex numbers correctly', function() { + approx.deepEqual(multiply(complex(2, 3), 2), complex(4, 6)); + approx.deepEqual(multiply(complex(2, -3), -2), complex(-4, 6)); + approx.deepEqual(multiply(complex(2, -3), 2), complex(4, -6)); + approx.deepEqual(multiply(complex(-2, 3), 2), complex(-4, 6)); + approx.deepEqual(multiply(complex(-2, -3), 2), complex(-4, -6)); + approx.deepEqual(multiply(2, complex(2, 3)), complex(4, 6)); + approx.deepEqual(multiply(i, complex(2, 3)), complex(-3, 2)); + approx.deepEqual(multiply(complex(0, 1), complex(2, 3)), complex(-3, 2)); + approx.deepEqual(multiply(complex(1, 1), complex(2, 3)), complex(-1, 5)); + approx.deepEqual(multiply(complex(2, 3), complex(1, 1)), complex(-1, 5)); + approx.deepEqual(multiply(complex(2, 3), complex(2, 3)), complex(-5, 12)); + approx.deepEqual(divide(complex(-5, 12), complex(2, 3)), complex(2, 3)); + approx.deepEqual(multiply(complex(2, 3), 0), complex(0, 0)); + approx.deepEqual(multiply(complex(0, 3), complex(0, -4)), complex(12, 0)); + approx.deepEqual(multiply(multiply(3, i), multiply(-4, i)), complex(12, 0)); + approx.deepEqual(multiply(math.i, Infinity), complex(0, Infinity)); + approx.deepEqual(multiply(Infinity, math.i), complex(0, Infinity)); -approx.deepEqual(multiply(complex(2, 3), complex(4, 5)), complex(-7, 22)); -approx.deepEqual(multiply(complex(2, 3), complex(4, -5)), complex(23, 2)); -approx.deepEqual(multiply(complex(2, 3), complex(-4, 5)), complex(-23, -2)); -approx.deepEqual(multiply(complex(2, 3), complex(-4, -5)), complex(7, -22)); -approx.deepEqual(multiply(complex(2, -3), complex(4, 5)), complex(23, -2)); -approx.deepEqual(multiply(complex(2, -3), complex(4, -5)), complex(-7, -22)); -approx.deepEqual(multiply(complex(2, -3), complex(-4, 5)), complex(7, 22)); -approx.deepEqual(multiply(complex(2, -3), complex(-4, -5)), complex(-23, 2)); -approx.deepEqual(multiply(complex(-2, 3), complex(4, 5)), complex(-23, 2)); -approx.deepEqual(multiply(complex(-2, 3), complex(4, -5)), complex(7, 22)); -approx.deepEqual(multiply(complex(-2, 3), complex(-4, 5)), complex(-7, -22)); -approx.deepEqual(multiply(complex(-2, 3), complex(-4, -5)), complex(23, -2)); -approx.deepEqual(multiply(complex(-2, -3), complex(4, 5)), complex(7, -22)); -approx.deepEqual(multiply(complex(-2, -3), complex(4, -5)), complex(-23, -2)); -approx.deepEqual(multiply(complex(-2, -3), complex(-4, 5)), complex(23, 2)); -approx.deepEqual(multiply(complex(-2, -3), complex(-4, -5)), complex(-7, 22)); + approx.deepEqual(multiply(complex(2,0), complex(0,2)), complex(0, 4)); + approx.deepEqual(multiply(complex(0,2), complex(0,2)), -4); + approx.deepEqual(multiply(complex(2,2), complex(0,2)), complex(-4, 4)); + approx.deepEqual(multiply(complex(2,0), complex(2,2)), complex(4, 4)); + approx.deepEqual(multiply(complex(0,2), complex(2,2)), complex(-4, 4)); + approx.deepEqual(multiply(complex(2,2), complex(2,2)), complex(0, 8)); + approx.deepEqual(multiply(complex(2,0), complex(2,0)), 4); + approx.deepEqual(multiply(complex(0,2), complex(2,0)), complex(0, 4)); + approx.deepEqual(multiply(complex(2,2), complex(2,0)), complex(4, 4)); -// unit -assert.equal(multiply(2, unit('5 mm')).toString(), '10 mm'); -assert.equal(multiply(2, unit('5 mm')).toString(), '10 mm'); -assert.equal(multiply(unit('5 mm'), 2).toString(), '10 mm'); -assert.equal(multiply(unit('5 mm'), 0).toString(), '0 m'); + approx.deepEqual(multiply(complex(2, 3), complex(4, 5)), complex(-7, 22)); + approx.deepEqual(multiply(complex(2, 3), complex(4, -5)), complex(23, 2)); + approx.deepEqual(multiply(complex(2, 3), complex(-4, 5)), complex(-23, -2)); + approx.deepEqual(multiply(complex(2, 3), complex(-4, -5)), complex(7, -22)); + approx.deepEqual(multiply(complex(2, -3), complex(4, 5)), complex(23, -2)); + approx.deepEqual(multiply(complex(2, -3), complex(4, -5)), complex(-7, -22)); + approx.deepEqual(multiply(complex(2, -3), complex(-4, 5)), complex(7, 22)); + approx.deepEqual(multiply(complex(2, -3), complex(-4, -5)), complex(-23, 2)); + approx.deepEqual(multiply(complex(-2, 3), complex(4, 5)), complex(-23, 2)); + approx.deepEqual(multiply(complex(-2, 3), complex(4, -5)), complex(7, 22)); + approx.deepEqual(multiply(complex(-2, 3), complex(-4, 5)), complex(-7, -22)); + approx.deepEqual(multiply(complex(-2, 3), complex(-4, -5)), complex(23, -2)); + approx.deepEqual(multiply(complex(-2, -3), complex(4, 5)), complex(7, -22)); + approx.deepEqual(multiply(complex(-2, -3), complex(4, -5)), complex(-23, -2)); + approx.deepEqual(multiply(complex(-2, -3), complex(-4, 5)), complex(23, 2)); + approx.deepEqual(multiply(complex(-2, -3), complex(-4, -5)), complex(-7, 22)); + }); -// string -assert.throws(function () {multiply("hello", "world")}); -assert.throws(function () {multiply("hello", 2)}); + it('should multiply a number and a unit correctly', function() { + assert.equal(multiply(2, unit('5 mm')).toString(), '10 mm'); + assert.equal(multiply(2, unit('5 mm')).toString(), '10 mm'); + assert.equal(multiply(unit('5 mm'), 2).toString(), '10 mm'); + assert.equal(multiply(unit('5 mm'), 0).toString(), '0 m'); + }); -// matrix, array, range -var a = matrix([[1,2],[3,4]]); -var b = matrix([[5,6],[7,8]]); -var c = matrix([[5],[6]]); -var d = matrix([[5,6]]); -approx.deepEqual(multiply(a, 3), matrix([[3,6],[9,12]])); -approx.deepEqual(multiply(3, a), matrix([[3,6],[9,12]])); -approx.deepEqual(multiply(a, b), matrix([[19,22],[43,50]])); -approx.deepEqual(multiply(a, c), matrix([[17],[39]])); -approx.deepEqual(multiply(d, a), matrix([[23,34]])); -approx.deepEqual(multiply(d, b), matrix([[67,78]])); -approx.deepEqual(multiply(d, c), matrix([[61]])); -approx.deepEqual(multiply([[1,2],[3,4]], [[5,6],[7,8]]), [[19,22],[43,50]]); -approx.deepEqual(multiply(range(1, 4), 2), [2, 4, 6, 8]); -assert.throws(function () {multiply(c, b)}); + it('should throw an error if used with strings', function() { + assert.throws(function () {multiply("hello", "world")}); + assert.throws(function () {multiply("hello", 2)}); + }); + + var a = matrix([[1,2],[3,4]]); + var b = matrix([[5,6],[7,8]]); + var c = matrix([[5],[6]]); + var d = matrix([[5,6]]); + + it('should perform element-wise multiplication if multiplying a matrix and a number', function() { + approx.deepEqual(multiply(a, 3), matrix([[3,6],[9,12]])); + approx.deepEqual(multiply(3, a), matrix([[3,6],[9,12]])); + }); + + it('should perform matrix multiplication', function () { + approx.deepEqual(multiply(a, b), matrix([[19,22],[43,50]])); + approx.deepEqual(multiply(a, c), matrix([[17],[39]])); + approx.deepEqual(multiply(d, a), matrix([[23,34]])); + approx.deepEqual(multiply(d, b), matrix([[67,78]])); + approx.deepEqual(multiply(d, c), matrix([[61]])); + approx.deepEqual(multiply([[1,2],[3,4]], [[5,6],[7,8]]), [[19,22],[43,50]]); + approx.deepEqual(multiply(range(1, 4), 2), [2, 4, 6, 8]); + }); + + it('should throw an error if multiplying matrices with incompatible sizes', function() { + assert.throws(function () {multiply(c, b)}); + }); + +}); diff --git a/test/function/arithmetic/pow.test.js b/test/function/arithmetic/pow.test.js index 83304e677..431aa36b7 100644 --- a/test/function/arithmetic/pow.test.js +++ b/test/function/arithmetic/pow.test.js @@ -8,81 +8,93 @@ var assert = require('assert'), range = math.range, pow = math.pow; -// parser -approx.equal(math.eval('2^3'), 8); -approx.equal(math.eval('-2^2'), -4); // -(2^2) -approx.equal(math.eval('2^3^4'), 2.41785163922926e+24); // 2^(3^4) -approx.equal(math.eval('pow(2,3)'), 8); -approx.equal(math.eval('pow(-2,2)'), 4); +describe('pow', function() { -// number -approx.deepEqual(pow(2,3), 8); -approx.deepEqual(pow(2,4), 16); -approx.deepEqual(pow(-2,2), 4); -approx.deepEqual(pow(3,3), 27); -approx.deepEqual(pow(3,-2), 0.111111111111111); -approx.deepEqual(pow(-3,-2), 0.111111111111111); -approx.deepEqual(pow(3,-3), 0.0370370370370370); -approx.deepEqual(pow(-3,-3), -0.0370370370370370); -approx.deepEqual(pow(2,1.5), 2.82842712474619); -approx.deepEqual(pow(-2,1.5), complex(0, -2.82842712474619)); + it('should be parsed correctly', function() { + approx.equal(math.eval('2^3'), 8); + approx.equal(math.eval('-2^2'), -4); // -(2^2) + approx.equal(math.eval('2^3^4'), 2.41785163922926e+24); // 2^(3^4) + approx.equal(math.eval('pow(2,3)'), 8); + approx.equal(math.eval('pow(-2,2)'), 4); + }); -assert.throws(function () {pow(1)}, SyntaxError, 'Wrong number of arguments in function pow (1 provided, 2 expected)'); -assert.throws(function () {pow(1, 2, 3)}, SyntaxError, 'Wrong number of arguments in function pow (3 provided, 2 expected)'); + it('should elevate a number to the given power', function() { + approx.deepEqual(pow(2,3), 8); + approx.deepEqual(pow(2,4), 16); + approx.deepEqual(pow(-2,2), 4); + approx.deepEqual(pow(3,3), 27); + approx.deepEqual(pow(3,-2), 0.111111111111111); + approx.deepEqual(pow(-3,-2), 0.111111111111111); + approx.deepEqual(pow(3,-3), 0.0370370370370370); + approx.deepEqual(pow(-3,-3), -0.0370370370370370); + approx.deepEqual(pow(2,1.5), 2.82842712474619); + approx.deepEqual(pow(-2,1.5), complex(0, -2.82842712474619)); + }); -// complex -approx.deepEqual(pow(complex(3, 0), 2), 9); -approx.deepEqual(pow(complex(0, 2), 2), complex(-4, 0)); + it('should throw an error if used with wrong number of arguments', function() { + assert.throws(function () {pow(1)}, SyntaxError, 'Wrong number of arguments in function pow (1 provided, 2 expected)'); + assert.throws(function () {pow(1, 2, 3)}, SyntaxError, 'Wrong number of arguments in function pow (3 provided, 2 expected)'); + }); -approx.deepEqual(pow(complex(-1,-1),complex(-1,-1)), complex('-0.0284750589322119 + 0.0606697332231795i')); -approx.deepEqual(pow(complex(-1,-1),complex(-1,1)), complex('-6.7536199239765713 + 3.1697803027015614i')); -approx.deepEqual(pow(complex(-1,-1),complex(0,-1)), complex('0.0891447921553914 - 0.0321946742909677i')); -approx.deepEqual(pow(complex(-1,-1),complex(0,1)), complex('9.92340022667813 + 3.58383962127501i')); -approx.deepEqual(pow(complex(-1,-1),complex(1,-1)), complex('-0.1213394664463591 - 0.0569501178644237i')); -approx.deepEqual(pow(complex(-1,-1),complex(1,1)), complex('-6.3395606054031211 - 13.5072398479531426i')); -approx.deepEqual(pow(complex(-1,1),complex(-1,-1)), complex('-6.7536199239765713 - 3.1697803027015614i')); -approx.deepEqual(pow(complex(-1,1),complex(-1,1)), complex('-0.0284750589322119 - 0.0606697332231795i')); -approx.deepEqual(pow(complex(-1,1),complex(0,-1)), complex('9.92340022667813 - 3.58383962127501i')); -approx.deepEqual(pow(complex(-1,1),complex(0,1)), complex('0.0891447921553914 + 0.0321946742909677i')); -approx.deepEqual(pow(complex(-1,1),complex(1,-1)), complex('-6.3395606054031211 + 13.5072398479531426i')); -approx.deepEqual(pow(complex(-1,1),complex(1,1)), complex('-0.1213394664463591 + 0.0569501178644237i')); + it('should elevate a complex number to the given power', function() { + approx.deepEqual(pow(complex(3, 0), 2), 9); + approx.deepEqual(pow(complex(0, 2), 2), complex(-4, 0)); -approx.deepEqual(pow(complex(0,-1),complex(-1,-1)), complex('0.000000000000000 + 0.207879576350762i')); -approx.deepEqual(pow(complex(0,-1),complex(-1,1)), complex('0.000000000000000 + 4.810477380965351i')); -approx.deepEqual(pow(complex(0,-1),complex(1,-1)), complex('0.000000000000000 - 0.207879576350762i')); -approx.deepEqual(pow(complex(0,-1),complex(1,1)), complex('0.000000000000000 - 4.810477380965351i')); -approx.deepEqual(pow(complex(0,1),complex(-1,-1)), complex('0.000000000000000 - 4.810477380965351i')); -approx.deepEqual(pow(complex(0,1),complex(-1,1)), complex('0.000000000000000 - 0.207879576350762i')); -approx.deepEqual(pow(complex(0,1),complex(1,-1)), complex('0.000000000000000 + 4.810477380965351i')); -approx.deepEqual(pow(complex(0,1),complex(1,1)), complex('0.000000000000000 + 0.207879576350762i')); + approx.deepEqual(pow(complex(-1,-1),complex(-1,-1)), complex('-0.0284750589322119 + 0.0606697332231795i')); + approx.deepEqual(pow(complex(-1,-1),complex(-1,1)), complex('-6.7536199239765713 + 3.1697803027015614i')); + approx.deepEqual(pow(complex(-1,-1),complex(0,-1)), complex('0.0891447921553914 - 0.0321946742909677i')); + approx.deepEqual(pow(complex(-1,-1),complex(0,1)), complex('9.92340022667813 + 3.58383962127501i')); + approx.deepEqual(pow(complex(-1,-1),complex(1,-1)), complex('-0.1213394664463591 - 0.0569501178644237i')); + approx.deepEqual(pow(complex(-1,-1),complex(1,1)), complex('-6.3395606054031211 - 13.5072398479531426i')); + approx.deepEqual(pow(complex(-1,1),complex(-1,-1)), complex('-6.7536199239765713 - 3.1697803027015614i')); + approx.deepEqual(pow(complex(-1,1),complex(-1,1)), complex('-0.0284750589322119 - 0.0606697332231795i')); + approx.deepEqual(pow(complex(-1,1),complex(0,-1)), complex('9.92340022667813 - 3.58383962127501i')); + approx.deepEqual(pow(complex(-1,1),complex(0,1)), complex('0.0891447921553914 + 0.0321946742909677i')); + approx.deepEqual(pow(complex(-1,1),complex(1,-1)), complex('-6.3395606054031211 + 13.5072398479531426i')); + approx.deepEqual(pow(complex(-1,1),complex(1,1)), complex('-0.1213394664463591 + 0.0569501178644237i')); -approx.deepEqual(pow(complex(1,-1),complex(-1,-1)), complex('0.2918503793793073 + 0.1369786269150605i')); -approx.deepEqual(pow(complex(1,-1),complex(-1,1)), complex('0.6589325864505904 + 1.4039396486303144i')); -approx.deepEqual(pow(complex(1,-1),complex(0,-1)), complex('0.428829006294368 - 0.154871752464247i')); -approx.deepEqual(pow(complex(1,-1),complex(0,1)), complex('2.062872235080905 + 0.745007062179724i')); -approx.deepEqual(pow(complex(1,-1),complex(1,-1)), complex('0.2739572538301211 - 0.5837007587586147i')); -approx.deepEqual(pow(complex(1,-1),complex(1,1)), complex('2.8078792972606288 - 1.3178651729011805i')); -approx.deepEqual(pow(complex(1,1),complex(-1,-1)), complex('0.6589325864505904 - 1.4039396486303144i')); -approx.deepEqual(pow(complex(1,1),complex(-1,1)), complex('0.2918503793793073 - 0.1369786269150605i')); -approx.deepEqual(pow(complex(1,1),complex(0,-1)), complex('2.062872235080905 - 0.745007062179724i')); -approx.deepEqual(pow(complex(1,1),complex(0,1)), complex('0.428829006294368 + 0.154871752464247i')); -approx.deepEqual(pow(complex(1,1),complex(1,-1)), complex('2.8078792972606288 + 1.3178651729011805i')); -approx.deepEqual(pow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i')); + approx.deepEqual(pow(complex(0,-1),complex(-1,-1)), complex('0.000000000000000 + 0.207879576350762i')); + approx.deepEqual(pow(complex(0,-1),complex(-1,1)), complex('0.000000000000000 + 4.810477380965351i')); + approx.deepEqual(pow(complex(0,-1),complex(1,-1)), complex('0.000000000000000 - 0.207879576350762i')); + approx.deepEqual(pow(complex(0,-1),complex(1,1)), complex('0.000000000000000 - 4.810477380965351i')); + approx.deepEqual(pow(complex(0,1),complex(-1,-1)), complex('0.000000000000000 - 4.810477380965351i')); + approx.deepEqual(pow(complex(0,1),complex(-1,1)), complex('0.000000000000000 - 0.207879576350762i')); + approx.deepEqual(pow(complex(0,1),complex(1,-1)), complex('0.000000000000000 + 4.810477380965351i')); + approx.deepEqual(pow(complex(0,1),complex(1,1)), complex('0.000000000000000 + 0.207879576350762i')); -// unit -assert.throws(function () {pow(unit('5cm'))}); + approx.deepEqual(pow(complex(1,-1),complex(-1,-1)), complex('0.2918503793793073 + 0.1369786269150605i')); + approx.deepEqual(pow(complex(1,-1),complex(-1,1)), complex('0.6589325864505904 + 1.4039396486303144i')); + approx.deepEqual(pow(complex(1,-1),complex(0,-1)), complex('0.428829006294368 - 0.154871752464247i')); + approx.deepEqual(pow(complex(1,-1),complex(0,1)), complex('2.062872235080905 + 0.745007062179724i')); + approx.deepEqual(pow(complex(1,-1),complex(1,-1)), complex('0.2739572538301211 - 0.5837007587586147i')); + approx.deepEqual(pow(complex(1,-1),complex(1,1)), complex('2.8078792972606288 - 1.3178651729011805i')); + approx.deepEqual(pow(complex(1,1),complex(-1,-1)), complex('0.6589325864505904 - 1.4039396486303144i')); + approx.deepEqual(pow(complex(1,1),complex(-1,1)), complex('0.2918503793793073 - 0.1369786269150605i')); + approx.deepEqual(pow(complex(1,1),complex(0,-1)), complex('2.062872235080905 - 0.745007062179724i')); + approx.deepEqual(pow(complex(1,1),complex(0,1)), complex('0.428829006294368 + 0.154871752464247i')); + approx.deepEqual(pow(complex(1,1),complex(1,-1)), complex('2.8078792972606288 + 1.3178651729011805i')); + approx.deepEqual(pow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i')); + }); -// string -assert.throws(function () {pow('text')}); + it('should throw an error if used with a unit', function() { + assert.throws(function () {pow(unit('5cm'))}); + }); -// matrix, array, range -var a = [[1,2],[3,4]]; -var res = [[7,10],[15,22]]; -approx.deepEqual(pow(a, 2), res); -approx.deepEqual(pow(matrix(a), 2), matrix(res)); + it('should throw an error if used with a string', function() { + assert.throws(function () {pow('text')}); + }); -assert.throws(function () {pow([1,2,3,4],2);}); -assert.throws(function () {pow([[1,2,3],[4,5,6]],2);}); -assert.throws(function () {pow([[1,2,3],[4,5,6]],2);}); -assert.throws(function () {pow(a, 2.5);}); -assert.throws(function () {pow(a, [2,3]);}); + it('???', function() { + var a = [[1,2],[3,4]]; + var res = [[7,10],[15,22]]; + approx.deepEqual(pow(a, 2), res); + approx.deepEqual(pow(matrix(a), 2), matrix(res)); + + assert.throws(function () {pow([1,2,3,4],2);}); + assert.throws(function () {pow([[1,2,3],[4,5,6]],2);}); + assert.throws(function () {pow([[1,2,3],[4,5,6]],2);}); + assert.throws(function () {pow(a, 2.5);}); + assert.throws(function () {pow(a, [2,3]);}); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/round.test.js b/test/function/arithmetic/round.test.js index 0fcfefd84..db9f88c06 100644 --- a/test/function/arithmetic/round.test.js +++ b/test/function/arithmetic/round.test.js @@ -2,30 +2,43 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// parser -assert.equal(math.eval('round(pi)'), 3); -assert.equal(math.eval('round(pi, 3)'), 3.142); +describe('round', function() { -// number -assert.equal(math.round(math.pi), 3); -assert.equal(math.round(math.pi * 1000), 3142); -assert.equal(math.round(math.pi, 3), 3.142); -assert.equal(math.round(math.pi, 6), 3.141593); -assert.equal(math.round(1234.5678, 2), 1234.57); -assert.throws(function () {math.round();}, SyntaxError, 'Wrong number of arguments in function round (3 provided, 1-2 expected)'); -assert.throws(function () {math.round(1,2,3);}, SyntaxError, 'Wrong number of arguments in function round (3 provided, 1-2 expected)'); + it('should be parsed correctly', function() { + assert.equal(math.eval('round(pi)'), 3); + assert.equal(math.eval('round(pi, 3)'), 3.142); + }); -// complex -assert.deepEqual(math.round(math.complex(2.2, math.pi)), math.complex(2,3)); + it('should round a number to te given number of decimals', function() { + assert.equal(math.round(math.pi), 3); + assert.equal(math.round(math.pi * 1000), 3142); + assert.equal(math.round(math.pi, 3), 3.142); + assert.equal(math.round(math.pi, 6), 3.141593); + assert.equal(math.round(1234.5678, 2), 1234.57); + }); -// unit -assert.throws(function () { math.round(math.unit('5cm')); }, TypeError, 'Function round(unit) not supported'); + it('should throw an error if used with wrong number of arguments', function() { + assert.throws(function () {math.round();}, SyntaxError, 'Wrong number of arguments in function round (3 provided, 1-2 expected)'); + assert.throws(function () {math.round(1,2,3);}, SyntaxError, 'Wrong number of arguments in function round (3 provided, 1-2 expected)'); + }); -// string -assert.throws(function () { math.round("hello world"); }, TypeError, 'Function round(unit) not supported'); + it('should round real and imag part of a complex number', function() { + assert.deepEqual(math.round(math.complex(2.2, math.pi)), math.complex(2,3)); + }); -// matrix, array, range -assert.deepEqual(math.round(math.range(0,1/3,2), 2), [0,0.33,0.67,1,1.33,1.67,2]); -assert.deepEqual(math.round(math.range(0,1/3,2)), [0,0,1,1,1,2,2]); -assert.deepEqual(math.round([1.7,2.3]), [2,2]); -assert.deepEqual(math.round(math.matrix([1.7,2.3])).valueOf(), [2, 2]); + it('should throw an error if used with a unit', function() { + assert.throws(function () { math.round(math.unit('5cm')); }, TypeError, 'Function round(unit) not supported'); + }); + + it('should throw an error if used with a string', function() { + assert.throws(function () { math.round("hello world"); }, TypeError, 'Function round(unit) not supported'); + }); + + it('should round each element in a matrix, array, range', function() { + assert.deepEqual(math.round(math.range(0,1/3,2), 2), [0,0.33,0.67,1,1.33,1.67,2]); + assert.deepEqual(math.round(math.range(0,1/3,2)), [0,0,1,1,1,2,2]); + assert.deepEqual(math.round([1.7,2.3]), [2,2]); + assert.deepEqual(math.round(math.matrix([1.7,2.3])).valueOf(), [2, 2]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/sign.test.js b/test/function/arithmetic/sign.test.js index 95e561e5a..7c1fb3490 100644 --- a/test/function/arithmetic/sign.test.js +++ b/test/function/arithmetic/sign.test.js @@ -2,26 +2,36 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// parser -assert.equal(math.eval('sign(3)'), 1); -assert.equal(math.eval('sign(-3)'), -1); -assert.equal(math.eval('sign(0)'), 0); +describe('sign', function() { -// number -assert.equal(math.sign(3), 1); -assert.equal(math.sign(-3), -1); -assert.equal(math.sign(0), 0); + it('should be parsed correctly', function() { + assert.equal(math.eval('sign(3)'), 1); + assert.equal(math.eval('sign(-3)'), -1); + assert.equal(math.eval('sign(0)'), 0); + }); -// complex -assert.equal(math.sign(math.complex(2,-3)).toString(), '0.5547 - 0.83205i'); + it('should return the sign of a number as 1, -1 or 0', function() { + assert.equal(math.sign(3), 1); + assert.equal(math.sign(-3), -1); + assert.equal(math.sign(0), 0); + }); -// unit -assert.throws(function () { math.sign(math.unit('5cm')); }); + it('???', function() { + assert.equal(math.sign(math.complex(2,-3)).toString(), '0.5547 - 0.83205i'); + }); -// string -assert.throws(function () { math.sign("hello world"); }); + it('should throw an error when used with a unit', function() { + assert.throws(function () { math.sign(math.unit('5cm')); }); + }); -// matrix, range -assert.deepEqual(math.sign(math.range(-2,2)), [-1,-1,0,1,1]); -assert.deepEqual(math.sign(math.matrix(math.range(-2,2))).valueOf(), [-1,-1,0,1,1]); -assert.deepEqual(math.sign([-2, -1, 0, 1, 2]), [-1,-1,0,1,1]); + it('should throw an error when used with a string', function() { + assert.throws(function () { math.sign("hello world"); }); + }); + + it('should return a matrix of the signs of each elements in the given matrix', function() { + assert.deepEqual(math.sign(math.range(-2,2)), [-1,-1,0,1,1]); + assert.deepEqual(math.sign(math.matrix(math.range(-2,2))).valueOf(), [-1,-1,0,1,1]); + assert.deepEqual(math.sign([-2, -1, 0, 1, 2]), [-1,-1,0,1,1]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/smaller.test.js b/test/function/arithmetic/smaller.test.js index e5843b55f..3a7136a4f 100644 --- a/test/function/arithmetic/smaller.test.js +++ b/test/function/arithmetic/smaller.test.js @@ -6,50 +6,66 @@ var assert = require('assert'), unit = math.unit, smaller = math.smaller; -// parser -assert.equal(math.eval('2 < 3'), true); -assert.equal(math.eval('2 < 2'), false); -assert.equal(math.eval('2 < 1'), false); -assert.equal(math.eval('smaller(2, 3)'), true); -assert.equal(math.eval('smaller(2, 2)'), false); -assert.equal(math.eval('smaller(2, 1)'), false); +describe('smaller', function() { -// number -assert.equal(smaller(2, 3), true); -assert.equal(smaller(2, 2), false); -assert.equal(smaller(2, 1), false); -assert.equal(smaller(0, 0), false); -assert.equal(smaller(-2, 2), true); -assert.equal(smaller(-2, -3), false); -assert.equal(smaller(-3, -2), true); + it('should be parsed correctly', function() { + assert.equal(math.eval('2 < 3'), true); + assert.equal(math.eval('2 < 2'), false); + assert.equal(math.eval('2 < 1'), false); + assert.equal(math.eval('smaller(2, 3)'), true); + assert.equal(math.eval('smaller(2, 2)'), false); + assert.equal(math.eval('smaller(2, 1)'), false); + }); -// complex -assert.equal(smaller(complex(1,1), complex(1,2)), true); -assert.equal(smaller(complex(1,1), complex(1,1)), false); -assert.equal(smaller(complex(1,1), complex(2,1)), true); -assert.equal(smaller(complex(1,6), complex(7,1)), true); -assert.equal(smaller(complex(4,1), complex(2,2)), false); -assert.equal(smaller(complex(2,0), 3), true); -assert.equal(smaller(complex(2,0), 2), false); -assert.equal(smaller(complex(2,0), 1), false); -assert.equal(smaller(3, complex(2,0)), false); -assert.equal(smaller(2, complex(2,0)), false); -assert.equal(smaller(1, complex(2,0)), true); + it('should compare two numbers correctly', function() { + assert.equal(smaller(2, 3), true); + assert.equal(smaller(2, 2), false); + assert.equal(smaller(2, 1), false); + assert.equal(smaller(0, 0), false); + assert.equal(smaller(-2, 2), true); + assert.equal(smaller(-2, -3), false); + assert.equal(smaller(-3, -2), true); + }); -// unit -assert.equal(smaller(unit('100cm'), unit('10inch')), false); -assert.equal(smaller(unit('99cm'), unit('1m')), true); -//assert.equal(smaller(unit('100cm'), unit('1m')), false); // dangerous, round-off errors -assert.equal(smaller(unit('101cm'), unit('1m')), false); -assert.throws(function () {smaller(unit('100cm'), 22)}); + it('should compare two complex numbers correctly', function() { + assert.equal(smaller(complex(1,1), complex(1,2)), true); + assert.equal(smaller(complex(1,1), complex(1,1)), false); + assert.equal(smaller(complex(1,1), complex(2,1)), true); + assert.equal(smaller(complex(1,6), complex(7,1)), true); + assert.equal(smaller(complex(4,1), complex(2,2)), false); + assert.equal(smaller(complex(2,0), 3), true); + assert.equal(smaller(complex(2,0), 2), false); + assert.equal(smaller(complex(2,0), 1), false); + assert.equal(smaller(3, complex(2,0)), false); + assert.equal(smaller(2, complex(2,0)), false); + assert.equal(smaller(1, complex(2,0)), true); + }); -// string -assert.equal(smaller('0', 0), false); -assert.equal(smaller('abd', 'abc'), false); -assert.equal(smaller('abc', 'abc'), false); -assert.equal(smaller('abc', 'abd'), true); + it('should compare two measures of the same unit correctly', function() { + assert.equal(smaller(unit('100cm'), unit('10inch')), false); + assert.equal(smaller(unit('99cm'), unit('1m')), true); + //assert.equal(smaller(unit('100cm'), unit('1m')), false); // dangerous, round-off errors + assert.equal(smaller(unit('101cm'), unit('1m')), false); + }); -// array, matrix -assert.deepEqual(smaller([1,4,6], [3,4,5]), [true, false, false]); -assert.deepEqual(smaller([1,4,6], matrix([3,4,5])), matrix([true, false, false])); -assert.throws(function () {smaller([1,4,6], [3,4])}); + it('should throw an error if comparing a unit and a number', function() { + assert.throws(function () {smaller(unit('100cm'), 22)}); + }) + + it('should perform lexical comparison on two strings', function() { + assert.equal(smaller('0', 0), false); + assert.equal(smaller('abd', 'abc'), false); + assert.equal(smaller('abc', 'abc'), false); + assert.equal(smaller('abc', 'abd'), true); + }); + + it('should perform element-wise comparison on two matrices of same size', function() { + assert.deepEqual(smaller([1,4,6], [3,4,5]), [true, false, false]); + assert.deepEqual(smaller([1,4,6], matrix([3,4,5])), matrix([true, false, false])); + }); + + it('should throw an error with two matrices of different sizes', function () { + assert.throws(function () {smaller([1,4,6], [3,4])}); + }); + +}); diff --git a/test/function/arithmetic/smallereq.test.js b/test/function/arithmetic/smallereq.test.js index 695c81dbc..950954d09 100644 --- a/test/function/arithmetic/smallereq.test.js +++ b/test/function/arithmetic/smallereq.test.js @@ -6,52 +6,68 @@ var assert = require('assert'), unit = math.unit, smallereq = math.smallereq; -// parser -assert.equal(math.eval('2 <= 3'), true); -assert.equal(math.eval('2 <= 2'), true); -assert.equal(math.eval('2 <= 1'), false); -assert.equal(math.eval('smallereq(2, 3)'), true); -assert.equal(math.eval('smallereq(2, 2)'), true); -assert.equal(math.eval('smallereq(2, 1)'), false); +describe('smallereq', function() { -// number -assert.equal(smallereq(2, 3), true); -assert.equal(smallereq(2, 2), true); -assert.equal(smallereq(2, 1), false); -assert.equal(smallereq(0, 0), true); -assert.equal(smallereq(-2, 2), true); -assert.equal(smallereq(-2, -3), false); -assert.equal(smallereq(-2, -2), true); -assert.equal(smallereq(-3, -2), true); + it('should be parsed correctly', function() { + assert.equal(math.eval('2 <= 3'), true); + assert.equal(math.eval('2 <= 2'), true); + assert.equal(math.eval('2 <= 1'), false); + assert.equal(math.eval('smallereq(2, 3)'), true); + assert.equal(math.eval('smallereq(2, 2)'), true); + assert.equal(math.eval('smallereq(2, 1)'), false); + }); -// complex -assert.equal(smallereq(complex(1,1), complex(1,2)), true); -assert.equal(smallereq(complex(1,1), complex(1,1)), true); -assert.equal(smallereq(complex(2,4), complex(4,2)), true); -assert.equal(smallereq(complex(1,1), complex(2,1)), true); -assert.equal(smallereq(complex(1,6), complex(7,1)), true); -assert.equal(smallereq(complex(4,1), complex(2,2)), false); -assert.equal(smallereq(complex(2,0), 3), true); -assert.equal(smallereq(complex(2,0), 2), true); -assert.equal(smallereq(complex(2,0), 1), false); -assert.equal(smallereq(3, complex(2,0)), false); -assert.equal(smallereq(2, complex(2,0)), true); -assert.equal(smallereq(1, complex(2,0)), true); + it('should compare two numbers correctly', function() { + assert.equal(smallereq(2, 3), true); + assert.equal(smallereq(2, 2), true); + assert.equal(smallereq(2, 1), false); + assert.equal(smallereq(0, 0), true); + assert.equal(smallereq(-2, 2), true); + assert.equal(smallereq(-2, -3), false); + assert.equal(smallereq(-2, -2), true); + assert.equal(smallereq(-3, -2), true); + }); -// unit -assert.equal(smallereq(unit('100cm'), unit('10inch')), false); -assert.equal(smallereq(unit('99cm'), unit('1m')), true); -//assert.equal(smallereq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors -assert.equal(smallereq(unit('101cm'), unit('1m')), false); -assert.throws(function () {smallereq(unit('100cm'), 22)}); + it('should compare two complex numbers correctly', function() { + assert.equal(smallereq(complex(1,1), complex(1,2)), true); + assert.equal(smallereq(complex(1,1), complex(1,1)), true); + assert.equal(smallereq(complex(2,4), complex(4,2)), true); + assert.equal(smallereq(complex(1,1), complex(2,1)), true); + assert.equal(smallereq(complex(1,6), complex(7,1)), true); + assert.equal(smallereq(complex(4,1), complex(2,2)), false); + assert.equal(smallereq(complex(2,0), 3), true); + assert.equal(smallereq(complex(2,0), 2), true); + assert.equal(smallereq(complex(2,0), 1), false); + assert.equal(smallereq(3, complex(2,0)), false); + assert.equal(smallereq(2, complex(2,0)), true); + assert.equal(smallereq(1, complex(2,0)), true); + }); -// string -assert.equal(smallereq('0', 0), true); -assert.equal(smallereq('abd', 'abc'), false); -assert.equal(smallereq('abc', 'abc'), true); -assert.equal(smallereq('abc', 'abd'), true); + it('should compare two measures of the same unit correctly', function() { + assert.equal(smallereq(unit('100cm'), unit('10inch')), false); + assert.equal(smallereq(unit('99cm'), unit('1m')), true); + //assert.equal(smallereq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors + assert.equal(smallereq(unit('101cm'), unit('1m')), false); + }); -// array, matrix -assert.deepEqual(smallereq([1,4,6], [3,4,5]), [true, true, false]); -assert.deepEqual(smallereq([1,4,6], matrix([3,4,5])), matrix([true, true, false])); -assert.throws(function () {smallereq([1,4,6], [3,4])}); + it('should throw an error if comparing a unit with a number', function() { + assert.throws(function () {smallereq(unit('100cm'), 22)}); + }); + + it('should perform lexical comprison of two strings', function() { + assert.equal(smallereq('0', 0), true); + assert.equal(smallereq('abd', 'abc'), false); + assert.equal(smallereq('abc', 'abc'), true); + assert.equal(smallereq('abc', 'abd'), true); + }); + + it('should perform element-wise comparison on two matrices', function() { + assert.deepEqual(smallereq([1,4,6], [3,4,5]), [true, true, false]); + assert.deepEqual(smallereq([1,4,6], matrix([3,4,5])), matrix([true, true, false])); + }); + + it('should throw an error with two matrices of different sizes', function () { + assert.throws(function () {smallereq([1,4,6], [3,4])}); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/sqrt.test.js b/test/function/arithmetic/sqrt.test.js index d5d0b206f..6689e88ee 100644 --- a/test/function/arithmetic/sqrt.test.js +++ b/test/function/arithmetic/sqrt.test.js @@ -3,29 +3,39 @@ var assert = require('assert'), approx = require('../../../tools/approx.js'), math = require('../../../src/index.js'); -// parser -assert.equal(math.eval('sqrt(25)'), 5); +describe('sqrt', function() { -// number -assert.equal(math.sqrt(25), 5); -assert.equal(math.sqrt(-4), '2i'); -assert.equal(math.sqrt(0), ''); + it('should be parsed correctly', function() { + assert.equal(math.eval('sqrt(25)'), 5); + }); -// complex -assert.equal(math.sqrt(math.complex(3, -4)), '2 - i'); + it('should return the square root of a number', function() { + assert.equal(math.sqrt(25), 5); + assert.equal(math.sqrt(-4), '2i'); + assert.equal(math.sqrt(0), ''); + }); -// unit -assert.throws(function () { - math.sqrt(math.unit(5, 'km')); -}); + it('should return the square root of a complex number', function() { + assert.equal(math.sqrt(math.complex(3, -4)), '2 - i'); + }); -// string -assert.throws(function () { - math.sqrt('a string'); -}); + it('should throw an error when used with a unit', function() { + assert.throws(function () { + math.sqrt(math.unit(5, 'km')); + }); + }); -// array -assert.deepEqual(math.sqrt([4,9,16,25]), [2,3,4,5]); -assert.deepEqual(math.sqrt([[4,9],[16,25]]), [[2,3],[4,5]]); -assert.deepEqual(math.sqrt(math.matrix([[4,9],[16,25]])), math.matrix([[2,3],[4,5]])); -approx.deepEqual(math.sqrt(math.range('4:2:8')), [2, 2.44948974278318, 2.82842712474619]); + it('should throw an error when used with a string', function() { + assert.throws(function () { + math.sqrt('a string'); + }); + }); + + it('should return the square root of each element of a matrix', function() { + assert.deepEqual(math.sqrt([4,9,16,25]), [2,3,4,5]); + assert.deepEqual(math.sqrt([[4,9],[16,25]]), [[2,3],[4,5]]); + assert.deepEqual(math.sqrt(math.matrix([[4,9],[16,25]])), math.matrix([[2,3],[4,5]])); + approx.deepEqual(math.sqrt(math.range('4:2:8')), [2, 2.44948974278318, 2.82842712474619]); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/square.test.js b/test/function/arithmetic/square.test.js index 55141cb65..dad16a536 100644 --- a/test/function/arithmetic/square.test.js +++ b/test/function/arithmetic/square.test.js @@ -6,30 +6,42 @@ var assert = require('assert'), range = math.range, square = math.square; -// parser -assert.equal(math.eval('square(4)'), 16); +describe('square', function() { -// number -assert.equal(square(4), 16); -assert.equal(square(-2), 4); -assert.equal(square(0), 0); -assert.throws(function () {square()}, SyntaxError, 'Wrong number of arguments in function square (0 provided, 1 expected)'); -assert.throws(function () {square(1, 2)}, SyntaxError, 'Wrong number of arguments in function square (2 provided, 1 expected)'); + it('should be parsed correctly', function() { + assert.equal(math.eval('square(4)'), 16); + }); -// complex -assert.deepEqual(square(math.complex('2i')), -4); -assert.deepEqual(square(math.complex('2+3i')), math.complex('-5+12i')); -assert.deepEqual(square(math.complex('2')), 4); + it('should return the square of a number', function() { + assert.equal(square(4), 16); + assert.equal(square(-2), 4); + assert.equal(square(0), 0); + }); -// unit -assert.throws(function () {square(unit('5cm'))}); + it('should throw an error if used with wrong number of arguments', function() { + assert.throws(function () {square()}, SyntaxError, 'Wrong number of arguments in function square (0 provided, 1 expected)'); + assert.throws(function () {square(1, 2)}, SyntaxError, 'Wrong number of arguments in function square (2 provided, 1 expected)'); + }); -// string -assert.throws(function () {square('text')}); + it('should return the square of a complex number', function() { + assert.deepEqual(square(math.complex('2i')), -4); + assert.deepEqual(square(math.complex('2+3i')), math.complex('-5+12i')); + assert.deepEqual(square(math.complex('2')), 4); + }); -// array, matrix, range -// arrays are evaluated element wise -assert.deepEqual(square([2,3,4,5]), [4,9,16,25]); -assert.deepEqual(square(range(2,5)), [4,9,16,25]); -assert.deepEqual(square(matrix([2,3,4,5])), matrix([4,9,16,25])); -assert.deepEqual(square(matrix([[1,2],[3,4]])), matrix([[1,4],[9,16]])); + it('should throw an error when used with a unit', function() { + assert.throws(function () {square(unit('5cm'))}); + }); + + it('should throw an error when used with a string', function() { + assert.throws(function () {square('text')}); + }); + + it('should return the square of each element in a matrix', function() { + assert.deepEqual(square([2,3,4,5]), [4,9,16,25]); + assert.deepEqual(square(range(2,5)), [4,9,16,25]); + assert.deepEqual(square(matrix([2,3,4,5])), matrix([4,9,16,25])); + assert.deepEqual(square(matrix([[1,2],[3,4]])), matrix([[1,4],[9,16]])); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/subtract.test.js b/test/function/arithmetic/subtract.test.js index 9e92ba48b..c82debd7d 100644 --- a/test/function/arithmetic/subtract.test.js +++ b/test/function/arithmetic/subtract.test.js @@ -2,49 +2,62 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// parser -assert.equal(math.eval('4 - 2'), 2); -assert.equal(math.eval('8 - 2 - 2'), 4); -assert.equal(math.eval('subtract(4, 2)'), 2); +describe('subtract', function() { -// number -assert.deepEqual(math.subtract(4, 2), 2); -assert.deepEqual(math.subtract(4, -4), 8); -assert.deepEqual(math.subtract(-4, -4), 0); -assert.deepEqual(math.subtract(-4, 4), -8); -assert.deepEqual(math.subtract(2, 4), -2); -assert.deepEqual(math.subtract(3, 0), 3); -assert.deepEqual(math.subtract(0, 3), -3); -assert.deepEqual(math.subtract(0, 3), -3); -assert.deepEqual(math.subtract(0, 3), -3); + it('should be parsed correctly', function() { + assert.equal(math.eval('4 - 2'), 2); + assert.equal(math.eval('8 - 2 - 2'), 4); + assert.equal(math.eval('subtract(4, 2)'), 2); + }); -// complex -assert.equal(math.subtract(math.complex(3, 2), math.complex(8, 4)), '-5 - 2i'); -assert.equal(math.subtract(math.complex(6, 3), math.complex(-2, -2)), '8 + 5i'); -assert.equal(math.subtract(math.complex(3, 4), 10), '-7 + 4i'); -assert.equal(math.subtract(math.complex(3, 4), -2), '5 + 4i'); -assert.equal(math.subtract(math.complex(-3, -4), 10), '-13 - 4i'); -assert.equal(math.subtract(10, math.complex(3, 4)), '7 - 4i'); -assert.equal(math.subtract(10, math.i), '10 - i'); -assert.equal(math.subtract(0, math.i), '-i'); -assert.equal(math.subtract(10, math.complex(0, 1)), '10 - i'); + it('should subtract two numbers correctly', function() { + assert.deepEqual(math.subtract(4, 2), 2); + assert.deepEqual(math.subtract(4, -4), 8); + assert.deepEqual(math.subtract(-4, -4), 0); + assert.deepEqual(math.subtract(-4, 4), -8); + assert.deepEqual(math.subtract(2, 4), -2); + assert.deepEqual(math.subtract(3, 0), 3); + assert.deepEqual(math.subtract(0, 3), -3); + assert.deepEqual(math.subtract(0, 3), -3); + assert.deepEqual(math.subtract(0, 3), -3); + }); + + it('should subtract two complex numbers correctly', function() { + assert.equal(math.subtract(math.complex(3, 2), math.complex(8, 4)), '-5 - 2i'); + assert.equal(math.subtract(math.complex(6, 3), math.complex(-2, -2)), '8 + 5i'); + assert.equal(math.subtract(math.complex(3, 4), 10), '-7 + 4i'); + assert.equal(math.subtract(math.complex(3, 4), -2), '5 + 4i'); + assert.equal(math.subtract(math.complex(-3, -4), 10), '-13 - 4i'); + assert.equal(math.subtract(10, math.complex(3, 4)), '7 - 4i'); + assert.equal(math.subtract(10, math.i), '10 - i'); + assert.equal(math.subtract(0, math.i), '-i'); + assert.equal(math.subtract(10, math.complex(0, 1)), '10 - i'); + }); + + it('should subtract two quantities of the same unit', function() { + assert.equal(math.subtract(math.unit(5, 'km'), math.unit(100, 'mile')).toString(), '-155.93 km'); + }); + + it('should throw an error if subtracting two quantities of different units', function() { + assert.throws(function () { + math.subtract(math.unit(5, 'km'), math.unit(100, 'gram')); + }); + }) + + it('should throw an error when used with a string', function() { + assert.throws(function () {math.subtract('hello ', 'world'); }); + assert.throws(function () {math.subtract('str', 123)}); + assert.throws(function () {math.subtract(123, 'str')}); + }); + + it('should perform element-wise subtraction of two matrices', function() { + a2 = math.matrix([[1,2],[3,4]]); + a3 = math.matrix([[5,6],[7,8]]); + var a6 = math.subtract(a2, a3); + assert.ok(a6 instanceof math.type.Matrix); + assert.deepEqual(a6.size(), [2,2]); + assert.deepEqual(a6.valueOf(), [[-4,-4],[-4,-4]]); + assert.deepEqual(math.subtract(math.range(1,5), 2), math.range(-1,3).valueOf()); + }); -// unit -assert.equal(math.subtract(math.unit(5, 'km'), math.unit(100, 'mile')).toString(), '-155.93 km'); -assert.throws(function () { - math.subtract(math.unit(5, 'km'), math.unit(100, 'gram')); }); - -// string -assert.throws(function () {math.subtract('hello ', 'world'); }); -assert.throws(function () {math.subtract('str', 123)}); -assert.throws(function () {math.subtract(123, 'str')}); - -// array, matrix, range -a2 = math.matrix([[1,2],[3,4]]); -a3 = math.matrix([[5,6],[7,8]]); -var a6 = math.subtract(a2, a3); -assert.ok(a6 instanceof math.type.Matrix); -assert.deepEqual(a6.size(), [2,2]); -assert.deepEqual(a6.valueOf(), [[-4,-4],[-4,-4]]); -assert.deepEqual(math.subtract(math.range(1,5), 2), math.range(-1,3).valueOf()); diff --git a/test/function/arithmetic/unaryminus.test.js b/test/function/arithmetic/unaryminus.test.js index 2e15c6bb9..e3df3ad3f 100644 --- a/test/function/arithmetic/unaryminus.test.js +++ b/test/function/arithmetic/unaryminus.test.js @@ -2,40 +2,50 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// parser -assert.equal(math.eval('-2'), -2); -assert.equal(math.eval('4*-2'), -8); -assert.equal(math.eval('4 * -2'), -8); -assert.equal(math.eval('4+-2'), 2); -assert.equal(math.eval('4 + -2'), 2); -assert.equal(math.eval('4--2'), 6); -assert.equal(math.eval('4 - -2'), 6); -assert.equal(math.eval('unary(4)'), -4); +describe('unaryminus', function() { -// number -assert.deepEqual(math.unary(2), -2); -assert.deepEqual(math.unary(-2), 2); -assert.deepEqual(math.unary(0), 0); + it('should be parsed correctly', function() { + assert.equal(math.eval('-2'), -2); + assert.equal(math.eval('4*-2'), -8); + assert.equal(math.eval('4 * -2'), -8); + assert.equal(math.eval('4+-2'), 2); + assert.equal(math.eval('4 + -2'), 2); + assert.equal(math.eval('4--2'), 6); + assert.equal(math.eval('4 - -2'), 6); + assert.equal(math.eval('unary(4)'), -4); + }); -// complex -assert.equal(math.unary(math.complex(3, 2)), '-3 - 2i'); -assert.equal(math.unary(math.complex(3, -2)), '-3 + 2i'); -assert.equal(math.unary(math.complex(-3, 2)), '3 - 2i'); -assert.equal(math.unary(math.complex(-3, -2)), '3 + 2i'); + it('should perform unary minus of a number', function() { + assert.deepEqual(math.unary(2), -2); + assert.deepEqual(math.unary(-2), 2); + assert.deepEqual(math.unary(0), 0); + }); -// unit -assert.equal(math.unary(math.unit(5, 'km')).toString(), '-5 km'); + it('should perform unary minus of a complex number', function() { + assert.equal(math.unary(math.complex(3, 2)), '-3 - 2i'); + assert.equal(math.unary(math.complex(3, -2)), '-3 + 2i'); + assert.equal(math.unary(math.complex(-3, 2)), '3 - 2i'); + assert.equal(math.unary(math.complex(-3, -2)), '3 + 2i'); + }); -// string -assert.throws(function () {math.subtract('hello ', 'world'); }); -assert.throws(function () {math.subtract('str', 123)}); -assert.throws(function () {math.subtract(123, 'str')}); + it('should perform unary minus of a unit', function() { + assert.equal(math.unary(math.unit(5, 'km')).toString(), '-5 km'); + }); -// array, matrix, range -a2 = math.matrix([[1,2],[3,4]]); -var a7 = math.unary(a2); -assert.ok(a7 instanceof math.type.Matrix); -assert.deepEqual(a7.size(), [2,2]); -assert.deepEqual(a7.valueOf(), [[-1,-2],[-3,-4]]); -assert.deepEqual(math.unary([[1,2],[3,4]]), [[-1,-2],[-3,-4]]); -assert.deepEqual(math.unary(math.range(1,5)), math.range(-1,-1,-5).valueOf()); + it('should throw an error when used with a string', function() { + assert.throws(function () {math.subtract('hello ', 'world'); }); + assert.throws(function () {math.subtract('str', 123)}); + assert.throws(function () {math.subtract(123, 'str')}); + }); + + it('should perform element-wise unary minus on a matrix', function() { + a2 = math.matrix([[1,2],[3,4]]); + var a7 = math.unary(a2); + assert.ok(a7 instanceof math.type.Matrix); + assert.deepEqual(a7.size(), [2,2]); + assert.deepEqual(a7.valueOf(), [[-1,-2],[-3,-4]]); + assert.deepEqual(math.unary([[1,2],[3,4]]), [[-1,-2],[-3,-4]]); + assert.deepEqual(math.unary(math.range(1,5)), math.range(-1,-1,-5).valueOf()); + }); + +}); \ No newline at end of file diff --git a/test/function/arithmetic/unequal.test.js b/test/function/arithmetic/unequal.test.js index 78137aab6..7790518cb 100644 --- a/test/function/arithmetic/unequal.test.js +++ b/test/function/arithmetic/unequal.test.js @@ -6,42 +6,59 @@ var assert = require('assert'), unit = math.unit, unequal = math.unequal; -// parser -assert.equal(math.eval('2 != 3'), true); -assert.equal(math.eval('2 != 2'), false); -assert.equal(math.eval('unequal(2, 3)'), true); -assert.equal(math.eval('unequal(2, 2)'), false); +describe('unequal', function() { -// number -assert.equal(unequal(2, 3), true); -assert.equal(unequal(2, 2), false); -assert.equal(unequal(0, 0), false); -assert.equal(unequal(-2, 2), true); + it('should be parsed correctly', function() { + assert.equal(math.eval('2 != 3'), true); + assert.equal(math.eval('2 != 2'), false); + assert.equal(math.eval('unequal(2, 3)'), true); + assert.equal(math.eval('unequal(2, 2)'), false); + }); -// complex -assert.equal(unequal(complex(2,3), complex(2,4)), true); -assert.equal(unequal(complex(2,3), complex(2,3)), false); -assert.equal(unequal(complex(1,3), complex(2,3)), true); -assert.equal(unequal(complex(1,3), complex(2,4)), true); -assert.equal(unequal(complex(2,0), 2), false); -assert.equal(unequal(complex(2,1), 2), true); -assert.equal(unequal(2, complex(2, 0)), false); -assert.equal(unequal(2, complex(2, 1)), true); -assert.equal(unequal(complex(2,0), 3), true); -// unit -assert.equal(unequal(unit('100cm'), unit('10inch')), true); -assert.equal(unequal(unit('100cm'), unit('1m')), false); -//assert.equal(unequal(unit('12inch'), unit('1foot')), false); // round-off error :( -//assert.equal(unequal(unit('2.54cm'), unit('1inch')), false); // round-off error :( -assert.throws(function () {unequal(unit('100cm'), 22)}); + it('should compare two numbers correctly', function() { + assert.equal(unequal(2, 3), true); + assert.equal(unequal(2, 2), false); + assert.equal(unequal(0, 0), false); + assert.equal(unequal(-2, 2), true); + }); -// string -assert.equal(unequal('0', 0), false); -assert.equal(unequal('Hello', 'hello'), true); -assert.equal(unequal('hello', 'hello'), false); + it('should compare two complex numbers correctly', function() { + assert.equal(unequal(complex(2,3), complex(2,4)), true); + assert.equal(unequal(complex(2,3), complex(2,3)), false); + assert.equal(unequal(complex(1,3), complex(2,3)), true); + assert.equal(unequal(complex(1,3), complex(2,4)), true); + assert.equal(unequal(complex(2,0), 2), false); + assert.equal(unequal(complex(2,1), 2), true); + assert.equal(unequal(2, complex(2, 0)), false); + assert.equal(unequal(2, complex(2, 1)), true); + assert.equal(unequal(complex(2,0), 3), true); + }); -// array, matrix -assert.deepEqual(unequal([1,4,5], [3,4,5]), [true, false, false]); -assert.deepEqual(unequal([1,4,5], matrix([3,4,5])), matrix([true, false, false])); -assert.throws(function () {unequal([1,4,5], [3,4])}); + it('should compare two quantitites of the same unit correctly', function() { + assert.equal(unequal(unit('100cm'), unit('10inch')), true); + assert.equal(unequal(unit('100cm'), unit('1m')), false); + //assert.equal(unequal(unit('12inch'), unit('1foot')), false); // round-off error :( + //assert.equal(unequal(unit('2.54cm'), unit('1inch')), false); // round-off error :( + }); + + it('should throw an error when compating two different units', function() { + assert.throws(function () {unequal(unit('100cm'), 22)}); + }); + + it('should compare two strings correctly', function() { + assert.equal(unequal('0', 0), false); + assert.equal(unequal('Hello', 'hello'), true); + assert.equal(unequal('hello', 'hello'), false); + }); + + it('should perform element-wise comparison of two matrices of the same size', function() { + assert.deepEqual(unequal([1,4,5], [3,4,5]), [true, false, false]); + assert.deepEqual(unequal([1,4,5], matrix([3,4,5])), matrix([true, false, false])); + }); + + it('should throw an error when comparing two matrices of different sizes', function() { + assert.throws(function () {unequal([1,4,5], [3,4])}); + }); + +}); diff --git a/test/function/arithmetic/xgcd.test.js b/test/function/arithmetic/xgcd.test.js index 922d89dea..43f7cddc2 100644 --- a/test/function/arithmetic/xgcd.test.js +++ b/test/function/arithmetic/xgcd.test.js @@ -2,40 +2,53 @@ var assert = require('assert'); var math = require('../../../src/index.js'); -// parser -assert.deepEqual([1247, -7, 12], math.eval('xgcd(36163, 21199)')); +describe('xgcd', function() { -// xgcd(36163, 21199) = 1247 => -7(36163) + 12(21199) = 1247 -assert.deepEqual([1247, -7, 12], math.xgcd(36163, 21199)); + it('should be parsed correctly', function() { + // parser + assert.deepEqual([1247, -7, 12], math.eval('xgcd(36163, 21199)')); + }); -// xgcd(120, 23) = 1 => -9(120) + 47(23) = 1 -assert.deepEqual([1, -9, 47], math.xgcd(120, 23)); + it('should return extended greatest common divisor of two numbers', function() { + // xgcd(36163, 21199) = 1247 => -7(36163) + 12(21199) = 1247 + assert.deepEqual([1247, -7, 12], math.xgcd(36163, 21199)); + // xgcd(120, 23) = 1 => -9(120) + 47(23) = 1 + assert.deepEqual([1, -9, 47], math.xgcd(120, 23)); + // some unit tests from: https://github.com/sjkaliski/numbers.js/blob/master/test/basic.test.js + assert.deepEqual([5, -3, 5], math.xgcd(65, 40)); + assert.deepEqual([5, 5, -3], math.xgcd(40, 65)); + assert.deepEqual([21, -16, 27], math.xgcd(1239, 735)); + assert.deepEqual([21, 5, -2], math.xgcd(105, 252)); + assert.deepEqual([21, -2, 5], math.xgcd(252, 105)); + }); -// some unit tests from: https://github.com/sjkaliski/numbers.js/blob/master/test/basic.test.js -assert.deepEqual([5, -3, 5], math.xgcd(65, 40)); -assert.deepEqual([5, 5, -3], math.xgcd(40, 65)); -assert.deepEqual([21, -16, 27], math.xgcd(1239, 735)); -assert.deepEqual([21, 5, -2], math.xgcd(105, 252)); -assert.deepEqual([21, -2, 5], math.xgcd(252, 105)); + it('should give same results as gcd', function() { + assert.equal(math.gcd(1239, 735), math.xgcd(1239, 735)[0]); + assert.equal(math.gcd(105, 252), math.xgcd(105, 252)[0]); + assert.equal(math.gcd(7, 13), math.xgcd(7, 13)[0]); + }); -// check if gcd and xgcd give the same result -assert.equal(math.gcd(1239, 735), math.xgcd(1239, 735)[0]); -assert.equal(math.gcd(105, 252), math.xgcd(105, 252)[0]); -assert.equal(math.gcd(7, 13), math.xgcd(7, 13)[0]); + it('should throw an error if used with wrong number of arguments', function() { + assert.throws(function () {math.xgcd(1)}); + assert.throws(function () {math.xgcd(1, 2, 3)}); + }); -// invalid input (only 2 params are permitted!) -assert.throws(function () {math.xgcd(1)}); -assert.throws(function () {math.xgcd(1, 2, 3)}); + it('should throw an error when used with a complex number', function() { + assert.throws(function () {math.xgcd(math.complex(1,3),2); }, TypeError, 'Function xgcd(complex, number) not supported'); + }); -// complex -assert.throws(function () {math.xgcd(math.complex(1,3),2); }, TypeError, 'Function xgcd(complex, number) not supported'); + it('should throw an error when used with a string', function() { + assert.throws(function () {math.xgcd('a', 2); }, TypeError, 'Function xgcd(string, number) not supported'); + assert.throws(function () {math.xgcd(2, 'a'); }, TypeError, 'Function xgcd(number, string) not supported'); + }); -// string -assert.throws(function () {math.xgcd('a', 2); }, TypeError, 'Function xgcd(string, number) not supported'); -assert.throws(function () {math.xgcd(2, 'a'); }, TypeError, 'Function xgcd(number, string) not supported'); + it('should throw an error when used with a unit', function() { + assert.throws(function () { math.xgcd(math.unit('5cm'), 2); }, TypeError, 'Function xgcd(unit, number) not supported'); + }); -// unit -assert.throws(function () { math.xgcd(math.unit('5cm'), 2); }, TypeError, 'Function xgcd(unit, number) not supported'); + it('should throw an error when used with a matrix', function() { + assert.throws(function () { math.xgcd([5,2,3], [25,3,6]); }, TypeError, 'Function xgcd(array, array) not supported'); + }); -// array, matrix, range -assert.throws(function () { math.xgcd([5,2,3], [25,3,6]); }, TypeError, 'Function xgcd(array, array) not supported'); + +}); \ No newline at end of file From f4e1ad3d6d4925f0308a2a6472343ff58c804014 Mon Sep 17 00:00:00 2001 From: Sebastien Piquemal Date: Fri, 9 Aug 2013 13:36:29 +0400 Subject: [PATCH 3/4] refactored test/function/complex to mocha --- test/function/complex/arg.test.js | 67 +++++++++++++++++++----------- test/function/complex/conj.test.js | 39 +++++++++++------ test/function/complex/im.test.js | 35 ++++++++++++---- test/function/complex/re.test.js | 35 ++++++++++++---- 4 files changed, 120 insertions(+), 56 deletions(-) diff --git a/test/function/complex/arg.test.js b/test/function/complex/arg.test.js index b3d3cb5d4..1d3eb2dca 100644 --- a/test/function/complex/arg.test.js +++ b/test/function/complex/arg.test.js @@ -1,28 +1,45 @@ -// test arg var assert = require('assert'); var math = require('../../../src/index.js'); -assert.equal(math.arg(math.complex('0')) / math.pi, 0); -assert.equal(math.arg(math.complex('1 + 0i')) / math.pi, 0); -assert.equal(math.arg(math.complex('1 + i')) / math.pi, 0.25); -assert.equal(math.arg(math.complex('0 + i')) / math.pi, 0.5); -assert.equal(math.arg(math.complex('-1 + i')) / math.pi, 0.75); -assert.equal(math.arg(math.complex('-1 + 0i')) / math.pi, 1); -assert.equal(math.arg(math.complex('-1 - i')) / math.pi, -0.75); -assert.equal(math.arg(math.complex('0 - i')) / math.pi, -0.5); -assert.equal(math.arg(math.complex('1 - i')) / math.pi, -0.25); -assert.equal(math.arg(math.i) / math.pi, 0.5); -assert.deepEqual(math.divide(math.arg([ - math.i, math.unary(math.i), math.add(1,math.i) -]), math.pi), [ - 0.5, -0.5, 0.25 -]); -assert.deepEqual(math.matrix(math.divide(math.arg([ - math.i, math.unary(math.i), math.add(1,math.i) -]), math.pi)).valueOf(), [ - 0.5, -0.5, 0.25 -]); -assert.equal(math.arg(2) / math.pi, 0); -assert.equal(math.arg(-2) / math.pi, 1); -assert.throws(function () {math.arg('string')}); -assert.throws(function () {math.arg(math.unit('5cm'))}); +describe('arg', function() { + + it('should compute the argument of a complex number correctly', function() { + assert.equal(math.arg(math.complex('0')) / math.pi, 0); + assert.equal(math.arg(math.complex('1 + 0i')) / math.pi, 0); + assert.equal(math.arg(math.complex('1 + i')) / math.pi, 0.25); + assert.equal(math.arg(math.complex('0 + i')) / math.pi, 0.5); + assert.equal(math.arg(math.complex('-1 + i')) / math.pi, 0.75); + assert.equal(math.arg(math.complex('-1 + 0i')) / math.pi, 1); + assert.equal(math.arg(math.complex('-1 - i')) / math.pi, -0.75); + assert.equal(math.arg(math.complex('0 - i')) / math.pi, -0.5); + assert.equal(math.arg(math.complex('1 - i')) / math.pi, -0.25); + assert.equal(math.arg(math.i) / math.pi, 0.5); + }); + + it('should calculate the argument for each element in a matrix', function() { + assert.deepEqual(math.divide(math.arg([ + math.i, math.unary(math.i), math.add(1,math.i) + ]), math.pi), [ + 0.5, -0.5, 0.25 + ]); + assert.deepEqual(math.matrix(math.divide(math.arg([ + math.i, math.unary(math.i), math.add(1,math.i) + ]), math.pi)).valueOf(), [ + 0.5, -0.5, 0.25 + ]); + }); + + it('should compute the argument of a real number correctly', function() { + assert.equal(math.arg(2) / math.pi, 0); + assert.equal(math.arg(-2) / math.pi, 1); + }); + + it('should throw an error if used with a string', function() { + assert.throws(function () {math.arg('string')}); + }); + + it('should throw an error if used with a unit', function() { + assert.throws(function () {math.arg(math.unit('5cm'))}); + }); + +}); \ No newline at end of file diff --git a/test/function/complex/conj.test.js b/test/function/complex/conj.test.js index 01292bd81..8343e928f 100644 --- a/test/function/complex/conj.test.js +++ b/test/function/complex/conj.test.js @@ -1,17 +1,30 @@ -// test conj var assert = require('assert'); var math = require('../../../src/index.js'); -assert.equal(math.conj(math.complex('2 + 3i')).toString(), '2 - 3i'); -assert.equal(math.conj(123).toString(), '123'); -assert.equal(math.conj(math.complex('2 - 3i')).toString(), '2 + 3i'); -assert.equal(math.conj(math.complex('2')).toString(), '2'); -assert.equal(math.conj(math.complex('-4i')).toString(), '4i'); -assert.equal(math.conj(math.i).toString(), '-i'); -assert.equal(math.format(math.conj([math.complex('2+3i'), math.complex('3-4i')])), - '[2 - 3i, 3 + 4i]'); -assert.equal(math.conj(math.matrix([math.complex('2+3i'), math.complex('3-4i')])).toString(), - '[2 - 3i, 3 + 4i]'); +describe('conj', function() { -assert.equal(math.conj('string'), 'string'); -assert.deepEqual(math.conj(math.unit('5cm')), math.unit('5cm')); + it('should calculate the conjugate of a complex number correctly', function() { + assert.equal(math.conj(math.complex('2 + 3i')).toString(), '2 - 3i'); + assert.equal(math.conj(123).toString(), '123'); + assert.equal(math.conj(math.complex('2 - 3i')).toString(), '2 + 3i'); + assert.equal(math.conj(math.complex('2')).toString(), '2'); + assert.equal(math.conj(math.complex('-4i')).toString(), '4i'); + assert.equal(math.conj(math.i).toString(), '-i'); + }); + + it('should calculate the conjugate for each element in a matrix', function() { + assert.equal(math.format(math.conj([math.complex('2+3i'), math.complex('3-4i')])), + '[2 - 3i, 3 + 4i]'); + assert.equal(math.conj(math.matrix([math.complex('2+3i'), math.complex('3-4i')])).toString(), + '[2 - 3i, 3 + 4i]'); + }); + + it('should be identity if used with a string', function() { + assert.equal(math.conj('string'), 'string'); + }); + + it('should be identity if used with a unit', function() { + assert.deepEqual(math.conj(math.unit('5cm')), math.unit('5cm')); + }); + +}); \ No newline at end of file diff --git a/test/function/complex/im.test.js b/test/function/complex/im.test.js index 7c460f806..e723c61b2 100644 --- a/test/function/complex/im.test.js +++ b/test/function/complex/im.test.js @@ -1,12 +1,29 @@ -// test im var assert = require('assert'); var math = require('../../../src/index.js'); -assert.equal(math.im(math.complex(2,3)), 3); -assert.equal(math.im(math.complex(-2,-3)), -3); -assert.equal(math.im(math.i), 1); -assert.equal(math.im(2), 0); -assert.equal(math.im('string'), 0); -assert.equal(math.im(true), 0); -assert.deepEqual(math.im([2, math.complex('3-6i')]), [0, -6]); -assert.deepEqual(math.im(math.matrix([2, math.complex('3-6i')])).valueOf(), [0, -6]); +describe('im', function() { + + it('should return the imaginary part of a complex number', function() { + assert.equal(math.im(math.complex(2,3)), 3); + assert.equal(math.im(math.complex(-2,-3)), -3); + assert.equal(math.im(math.i), 1); + }); + + it('should return 0 for a real number', function() { + assert.equal(math.im(2), 0); + }); + + it('should return 0 for a string', function() { + assert.equal(math.im('string'), 0); + }); + + it('should return 0 for a boolean', function() { + assert.equal(math.im(true), 0); + }); + + it('should return the imaginary part for each element in a matrix', function() { + assert.deepEqual(math.im([2, math.complex('3-6i')]), [0, -6]); + assert.deepEqual(math.im(math.matrix([2, math.complex('3-6i')])).valueOf(), [0, -6]); + }); + +}); \ No newline at end of file diff --git a/test/function/complex/re.test.js b/test/function/complex/re.test.js index d5a16c31a..acbbb9199 100644 --- a/test/function/complex/re.test.js +++ b/test/function/complex/re.test.js @@ -1,12 +1,29 @@ -// test re var assert = require('assert'); var math = require('../../../src/index.js'); -assert.equal(math.re(math.complex(2,3)), 2); -assert.equal(math.re(math.complex(-2,-3)), -2); -assert.equal(math.re(math.i), 0); -assert.equal(math.re(2), 2); -assert.equal(math.re('string'), 'string'); -assert.equal(math.re(true), true); -assert.deepEqual(math.re([2, math.complex('3-6i')]), [2, 3]); -assert.deepEqual(math.re(math.matrix([2, math.complex('3-6i')])).valueOf(), [2, 3]); +describe('re', function() { + + it('should return the real part of a complex number', function() { + assert.equal(math.re(math.complex(2,3)), 2); + assert.equal(math.re(math.complex(-2,-3)), -2); + assert.equal(math.re(math.i), 0); + }); + + it('should be the identity for a real number', function() { + assert.equal(math.re(2), 2); + }); + + it('should be the identity for a string', function() { + assert.equal(math.re('string'), 'string'); + }); + + it('should be the identity for a boolean', function() { + assert.equal(math.re(true), true); + }); + + it('should return the real part for each element in a matrix', function() { + assert.deepEqual(math.re([2, math.complex('3-6i')]), [2, 3]); + assert.deepEqual(math.re(math.matrix([2, math.complex('3-6i')])).valueOf(), [2, 3]); + }); + +}); \ No newline at end of file From 6a6f522a934007c35d31fb03542582c4d77ec1e8 Mon Sep 17 00:00:00 2001 From: Sebastien Piquemal Date: Fri, 9 Aug 2013 14:13:53 +0400 Subject: [PATCH 4/4] refactored test/function/construction to mocha --- test/function/construction/boolean.test.js | 88 +++++++++++++--------- test/function/construction/complex.test.js | 70 +++++++++++------ test/function/construction/matrix.test.js | 58 ++++++++------ test/function/construction/number.test.js | 79 ++++++++++++------- test/function/construction/range.test.js | 77 +++++++++++-------- test/function/construction/string.test.js | 74 ++++++++++-------- test/function/construction/unit.test.js | 59 ++++++++++----- 7 files changed, 313 insertions(+), 192 deletions(-) diff --git a/test/function/construction/boolean.test.js b/test/function/construction/boolean.test.js index e0144f7df..5a0f8e248 100644 --- a/test/function/construction/boolean.test.js +++ b/test/function/construction/boolean.test.js @@ -1,44 +1,62 @@ -// test bool construction var assert = require('assert'), math = require('../../../src/index.js'), bool = math.boolean; -// parser -assert.equal(math.eval('boolean("true")'), true); -assert.equal(math.eval('boolean("false")'), false); -assert.equal(math.eval('boolean(true)'), true); -assert.equal(math.eval('boolean(false)'), false); -assert.equal(math.eval('boolean(1)'), true); -assert.equal(math.eval('boolean(2)'), true); -assert.equal(math.eval('boolean(0)'), false); +describe('boolean', function() { -// bool input -assert.equal(bool(true), true); -assert.equal(bool(false), false); + it('should be parsed correctly', function() { + assert.equal(math.eval('boolean("true")'), true); + assert.equal(math.eval('boolean("false")'), false); + assert.equal(math.eval('boolean(true)'), true); + assert.equal(math.eval('boolean(false)'), false); + assert.equal(math.eval('boolean(1)'), true); + assert.equal(math.eval('boolean(2)'), true); + assert.equal(math.eval('boolean(0)'), false); + }); -// bool input -assert.equal(bool(-2), true); -assert.equal(bool(-1), true); -assert.equal(bool(0), false); -assert.equal(bool(1), true); -assert.equal(bool(2), true); + it('should be the identity with a boolean', function() { + assert.equal(bool(true), true); + assert.equal(bool(false), false); + }); -// string input -assert.equal(bool('2'), true); -assert.equal(bool(' 4e2 '), true); -assert.equal(bool(' -4e2 '), true); -assert.equal(bool('0'), false); -assert.equal(bool(' 0 '), false); -assert.throws(function () {bool('')}, SyntaxError); -assert.throws(function () {bool('23a')}, SyntaxError); + it('should return false for 0, true for any other number', function() { + assert.equal(bool(-2), true); + assert.equal(bool(-1), true); + assert.equal(bool(0), false); + assert.equal(bool(1), true); + assert.equal(bool(2), true); + }); -// wrong bool of arguments -assert.throws(function () {bool(1,2)}, SyntaxError); -assert.throws(function () {bool(1,2,3)}, SyntaxError); + it('should return false for \'0\', true for any other valid number string', function() { + assert.equal(bool('2'), true); + assert.equal(bool(' 4e2 '), true); + assert.equal(bool(' -4e2 '), true); + assert.equal(bool('0'), false); + assert.equal(bool(' 0 '), false); + }); -// wrong type of arguments -assert.throws(function () {bool(math.complex(2,3))}, SyntaxError); -assert.throws(function () {bool(math.unit('5cm'))}, SyntaxError); -assert.throws(function () {bool(math.range(1,3))}, SyntaxError); -assert.throws(function () {bool(math.matrix([1,3]))}, SyntaxError); -assert.throws(function () {bool([1,3])}, SyntaxError); + it('should throw an error if the string is not a valid number', function() { + assert.throws(function () {bool('')}, SyntaxError); + assert.throws(function () {bool('23a')}, SyntaxError); + }); + + it('should throw an error if there\'s a wrong number of arguments', function() { + assert.throws(function () {bool(1,2)}, SyntaxError); + assert.throws(function () {bool(1,2,3)}, SyntaxError); + }); + + it('should throw an error if used with a complex', function() { + assert.throws(function () {bool(math.complex(2,3))}, SyntaxError); + }); + + it('should throw an error if used with a unit', function() { + assert.throws(function () {bool(math.unit('5cm'))}, SyntaxError); + }); + + it('should throw an error if used with a range, matrix or array', function() { + assert.throws(function () {bool(math.range(1,3))}, SyntaxError); + assert.throws(function () {bool(math.matrix([1,3]))}, SyntaxError); + assert.throws(function () {bool([1,3])}, SyntaxError); + }); + +}); \ No newline at end of file diff --git a/test/function/construction/complex.test.js b/test/function/construction/complex.test.js index 57eac69d5..27e89b11e 100644 --- a/test/function/construction/complex.test.js +++ b/test/function/construction/complex.test.js @@ -1,34 +1,54 @@ -// test complex construction var assert = require('assert'), math = require('../../../src/index.js'), complex = math.complex; -// 0 arguments -assert.deepEqual(complex(), new math.type.Complex(0, 0)); -assert.ok(complex() instanceof math.type.Complex); +describe('complex', function() { -// 1 argument (only string or Complex accepted) -assert.deepEqual(complex('2+3i'), new math.type.Complex(2, 3)); -assert.deepEqual(complex('2-3i'), new math.type.Complex(2, -3)); -assert.ok(complex('2+3i') instanceof math.type.Complex); + it('should return 0 + 0i if called with no argument', function() { + assert.deepEqual(complex(), new math.type.Complex(0, 0)); + assert.ok(complex() instanceof math.type.Complex); + }); -var a = complex(2,3); -var b = complex(a); -a.re = 4; -assert.deepEqual(b, new math.type.Complex(2,3)); + it('should parse a valid string and create the complex number accordingly', function() { + assert.deepEqual(complex('2+3i'), new math.type.Complex(2, 3)); + assert.deepEqual(complex('2-3i'), new math.type.Complex(2, -3)); + assert.ok(complex('2+3i') instanceof math.type.Complex); + }); -assert.throws(function () {complex('no valid complex number')}, SyntaxError); -assert.throws(function () {complex(123)}, TypeError); -assert.throws(function () {complex(math.unit('5cm'))}, TypeError); -assert.throws(function () {complex(math.matrix())}, TypeError); + it('should be the identity if called with a complex number', function() { + var b = complex(complex(2,3)); + assert.deepEqual(b, new math.type.Complex(2,3)); + }); -// 2 arguments -assert.deepEqual(complex(2, 3), new math.type.Complex(2, 3)); -assert.deepEqual(complex(2, -3), new math.type.Complex(2, -3)); -assert.deepEqual(complex(-2, 3), new math.type.Complex(-2, 3)); -assert.ok(complex(2, 3) instanceof math.type.Complex); -assert.throws(function () {complex('string', 2)}, TypeError); -assert.throws(function () {complex(2, 'string')}, TypeError); + it('should throw an error if called with a string', function() { + assert.throws(function () {complex('no valid complex number')}, SyntaxError); + }); -// more than 2 arguments -assert.throws(function () {complex(2,3,4)}, SyntaxError); + it('should throw an error if called with a single number', function() { + assert.throws(function () {complex(123)}, TypeError); + }); + + it('should throw an error if called with a unit', function() { + assert.throws(function () {complex(math.unit('5cm'))}, TypeError); + }); + + it('should throw an error if called with a matrix', function() { + assert.throws(function () {complex(math.matrix())}, TypeError); + }); + + it('should accept two numbers as arguments', function() { + assert.deepEqual(complex(2, 3), new math.type.Complex(2, 3)); + assert.deepEqual(complex(2, -3), new math.type.Complex(2, -3)); + assert.deepEqual(complex(-2, 3), new math.type.Complex(-2, 3)); + assert.ok(complex(2, 3) instanceof math.type.Complex); + }); + + it('should throw an error if passed two argument, one is invalid', function() { + assert.throws(function () {complex('string', 2)}, TypeError); + assert.throws(function () {complex(2, 'string')}, TypeError); + }); + + it('should throw an error if called with more than 2 arguments', function() { + assert.throws(function () {complex(2,3,4)}, SyntaxError); + }); +}); \ No newline at end of file diff --git a/test/function/construction/matrix.test.js b/test/function/construction/matrix.test.js index c31a9e21e..11c31f016 100644 --- a/test/function/construction/matrix.test.js +++ b/test/function/construction/matrix.test.js @@ -3,30 +3,46 @@ var assert = require('assert'), math = require('../../../src/index.js'), matrix = math.matrix; -// 0 arguments -var a = matrix(); -assert.ok(a instanceof math.type.Matrix); -assert.deepEqual(math.size(a), matrix([0])); // TODO: wouldn't it be nicer if an empty matrix has zero dimensions? +describe('matrix', function() { + it('should create an empty matrix with one dimension if called without argument', function() { + var a = matrix(); + assert.ok(a instanceof math.type.Matrix); + assert.deepEqual(math.size(a), matrix([0])); // TODO: wouldn't it be nicer if an empty matrix has zero dimensions? + }); -// 1 argument -var b = matrix([[1,2],[3,4]]); -assert.ok(b instanceof math.type.Matrix); -assert.deepEqual(b, new math.type.Matrix([[1,2],[3,4]])); -assert.deepEqual(math.size(b), matrix([2,2])); + it('should create a matrix from an array', function() { + var b = matrix([[1,2],[3,4]]); + assert.ok(b instanceof math.type.Matrix); + assert.deepEqual(b, new math.type.Matrix([[1,2],[3,4]])); + assert.deepEqual(math.size(b), matrix([2,2])); + }); -var c = matrix(b); -assert.ok(c._data != b._data); // data should be cloned -assert.deepEqual(c, new math.type.Matrix([[1,2],[3,4]])); -assert.deepEqual(math.size(c), matrix([2,2])); + it('should be the identity if called with a matrix', function() { + var b = matrix([[1,2],[3,4]]); + var c = matrix(b); + assert.ok(c._data != b._data); // data should be cloned + assert.deepEqual(c, new math.type.Matrix([[1,2],[3,4]])); + assert.deepEqual(math.size(c), matrix([2,2])); + }); -var d = matrix(math.range(1,5)); -assert.ok(d instanceof math.type.Matrix); -assert.deepEqual(d, new math.type.Matrix([1,2,3,4,5])); -assert.deepEqual(math.size(d), matrix([5])); + it('should create a matrix from a range correctly', function() { + var d = matrix(math.range(1,5)); + assert.ok(d instanceof math.type.Matrix); + assert.deepEqual(d, new math.type.Matrix([1,2,3,4,5])); + assert.deepEqual(math.size(d), matrix([5])); + }); -assert.throws(function () {matrix(123)}, TypeError); -assert.throws(function () {matrix(math.unit('5cm'))}, TypeError); + it('should throw an error if called with a single number', function() { + assert.throws(function () {matrix(123)}, TypeError); + }); -// more than 1 argument -assert.throws(function () {matrix(2, 3)}, SyntaxError); + it('should throw an error if called with a unit', function() { + assert.throws(function () {matrix(math.unit('5cm'))}, TypeError); + }); + + it('should throw an error if called with 2 numbers', function() { + assert.throws(function () {matrix(2, 3)}, SyntaxError); + }); + +}); \ No newline at end of file diff --git a/test/function/construction/number.test.js b/test/function/construction/number.test.js index f13a9ca91..2d81381ca 100644 --- a/test/function/construction/number.test.js +++ b/test/function/construction/number.test.js @@ -1,43 +1,64 @@ -// test number construction var assert = require('assert'), math = require('../../../src/index.js'), approx = require('../../../tools/approx.js'), number = math.number; -// parser -assert.equal(math.eval('number("123")'), 123); -assert.equal(math.eval('number()'), 0); +describe('number', function() { -// 0 arguments -approx.equal(number(), 0); + it('should be parsed correctly', function() { + assert.equal(math.eval('number("123")'), 123); + assert.equal(math.eval('number()'), 0); + }); -// 1 argument + it('should be 0 if called with no argument', function() { + approx.equal(number(), 0); + }); -// boolean input -approx.equal(number(true), 1); -approx.equal(number(false), 0); + it('should be 1 if called with true, 0 if called with false', function() { + approx.equal(number(true), 1); + approx.equal(number(false), 0); + }); -// number input -approx.equal(number(3), 3); -approx.equal(number(-3), -3); + it('should be the identity if called with a number', function() { + approx.equal(number(3), 3); + approx.equal(number(-3), -3); + }); -// string input -approx.equal(number('2.1e3'), 2100); -approx.equal(number(' 2.1e-3 '), 0.0021); -approx.equal(number(''), 0); -approx.equal(number(' '), 0); -assert.throws(function () {number('2.3.4')}, SyntaxError); -assert.throws(function () {number('23a')}, SyntaxError); + it('should parse the string if called with a valid string', function() { + approx.equal(number('2.1e3'), 2100); + approx.equal(number(' 2.1e-3 '), 0.0021); + approx.equal(number(''), 0); + approx.equal(number(' '), 0); + }); -// wrong number of arguments -assert.throws(function () {number(1,2)}, SyntaxError); -assert.throws(function () {number(1,2,3)}, SyntaxError); + it('should throw an error if called with an invalid string', function() { + assert.throws(function () {number('2.3.4')}, SyntaxError); + assert.throws(function () {number('23a')}, SyntaxError); + }); -// wrong type of arguments -assert.throws(function () {number(math.complex(2,3))}, SyntaxError); -assert.throws(function () {number(math.unit('5cm'))}, SyntaxError); -assert.throws(function () {number(math.range(1,3))}, SyntaxError); -assert.throws(function () {number(math.matrix([1,3]))}, SyntaxError); -assert.throws(function () {number([1,3])}, SyntaxError); + it('should throw an error if called with a wrong number of arguments', function() { + assert.throws(function () {number(1,2)}, SyntaxError); + assert.throws(function () {number(1,2,3)}, SyntaxError); + }); + it('should throw an error if called with a complex number', function() { + assert.throws(function () {number(math.complex(2,3))}, SyntaxError); + }); + + it('should throw an error if called with a unit', function() { + assert.throws(function () {number(math.unit('5cm'))}, SyntaxError); + }); + + it('should throw an error if called with a range', function() { + assert.throws(function () {number(math.range(1,3))}, SyntaxError); + }); + + it('should throw an error if called with a matrix', function() { + assert.throws(function () {number(math.matrix([1,3]))}, SyntaxError); + }); + + it('should throw an error if called with an array', function() { + assert.throws(function () {number([1,3])}, SyntaxError); + }); +}); diff --git a/test/function/construction/range.test.js b/test/function/construction/range.test.js index a26e07b64..ece6adc82 100644 --- a/test/function/construction/range.test.js +++ b/test/function/construction/range.test.js @@ -1,44 +1,59 @@ -// test range construction var assert = require('assert'), math = require('../../../src/index.js'), range = math.range; +describe('range', function() { -// 1 argument -assert.deepEqual(range('1:5').valueOf(), [1,2,3,4,5]); -assert.deepEqual(range('0:2:8').valueOf(), [0,2,4,6,8]); -assert.deepEqual(range('5:-1:1').valueOf(), [5,4,3,2,1]); -assert.deepEqual(range('2:-2:-2').valueOf(), [2,0,-2]); + it('should parse a valid string correctly', function() { + assert.deepEqual(range('1:5').valueOf(), [1,2,3,4,5]); + assert.deepEqual(range('0:2:8').valueOf(), [0,2,4,6,8]); + assert.deepEqual(range('5:-1:1').valueOf(), [5,4,3,2,1]); + assert.deepEqual(range('2:-2:-2').valueOf(), [2,0,-2]); + }); -var a = range(1,5); -var b = range(a); -a.start = 3; -assert.deepEqual(a.valueOf(), [3,4,5]); -assert.deepEqual(b.valueOf(), [1,2,3,4,5]); + it('should create a range start:1:end if called with 2 numbers', function() { + var a = range(1,5); + var b = range(a); + a.start = 3; + assert.deepEqual(a.valueOf(), [3,4,5]); + assert.deepEqual(b.valueOf(), [1,2,3,4,5]); + assert.deepEqual(range(1,5).valueOf(), [1,2,3,4,5]); + assert.deepEqual(range(5,1).valueOf(), []); + }); -assert.throws(function () {range('invalid range')}, SyntaxError); + it('should throw an error if called with an invalid string', function() { + assert.throws(function () {range('invalid range')}, SyntaxError); + }); -assert.throws(function () {range(2)}, TypeError); -assert.throws(function () {range(math.unit('5cm'))}, TypeError); -assert.throws(function () {range(math.complex(2,3))}, TypeError); + it('should throw an error if called with a single number', function() { + assert.throws(function () {range(2)}, TypeError); + }); -// 2 arguments -assert.deepEqual(range(1,5).valueOf(), [1,2,3,4,5]); -assert.deepEqual(range(5,1).valueOf(), []); + it('should throw an error if called with a unit', function() { + assert.throws(function () {range(math.unit('5cm'))}, TypeError); + }); -assert.throws(function () {range(2, 'string')}, TypeError); -assert.throws(function () {range(math.unit('5cm'), 2)}, TypeError); -assert.throws(function () {range(2, math.complex(2,3))}, TypeError); + it('should throw an error if called with a complex number', function() { + assert.throws(function () {range(math.complex(2,3))}, TypeError); + }); -// 3 arguments -assert.deepEqual(range(0,2,8).valueOf(), [0,2,4,6,8]); -assert.deepEqual(range(5,-1,1).valueOf(), [5,4,3,2,1]); -assert.deepEqual(range(2,-2,-2).valueOf(), [2,0,-2]); + it('should create a range start:step:end if called with 3 numbers', function() { + assert.deepEqual(range(0,2,8).valueOf(), [0,2,4,6,8]); + assert.deepEqual(range(5,-1,1).valueOf(), [5,4,3,2,1]); + assert.deepEqual(range(2,-2,-2).valueOf(), [2,0,-2]); + }); -assert.throws(function () {range(2, 'string', 3)}, TypeError); -assert.throws(function () {range(2, 1, math.unit('5cm'))}, TypeError); -assert.throws(function () {range(math.complex(2,3), 1, 3)}, TypeError); + it('should throw an error if called with one invalid argument', function() { + assert.throws(function () {range(2, 'string')}, TypeError); + assert.throws(function () {range(math.unit('5cm'), 2)}, TypeError); + assert.throws(function () {range(2, math.complex(2,3))}, TypeError); + assert.throws(function () {range(2, 'string', 3)}, TypeError); + assert.throws(function () {range(2, 1, math.unit('5cm'))}, TypeError); + assert.throws(function () {range(math.complex(2,3), 1, 3)}, TypeError); + }); -// 0 or 4+ arguments -assert.throws(function () {range()}, SyntaxError); -assert.throws(function () {range(1,2,3,4)}, SyntaxError); + it('should throw an error if called with an invalid number of arguments', function() { + assert.throws(function () {range()}, SyntaxError); + assert.throws(function () {range(1,2,3,4)}, SyntaxError); + }); +}); \ No newline at end of file diff --git a/test/function/construction/string.test.js b/test/function/construction/string.test.js index 83208525b..d5659ce6a 100644 --- a/test/function/construction/string.test.js +++ b/test/function/construction/string.test.js @@ -1,45 +1,55 @@ -// test string construction var assert = require('assert'), math = require('../../../src/index.js'), string = math.string; -// parser -assert.equal(math.eval('string(123)'), '123'); -assert.equal(math.eval('string(2+3i)'), '2 + 3i'); -assert.equal(math.eval('string(1:5)'), '[1, 2, 3, 4, 5]'); -assert.equal(math.eval('string(2 inch)'), '2 inch'); -assert.equal(math.eval('string([1,2;3,4])'), '[[1, 2], [3, 4]]'); +describe('string', function() { -// 0 arguments -assert.equal(string(), ''); + it('should be parsed correctly', function() { + assert.equal(math.eval('string(123)'), '123'); + assert.equal(math.eval('string(2+3i)'), '2 + 3i'); + assert.equal(math.eval('string(1:5)'), '[1, 2, 3, 4, 5]'); + assert.equal(math.eval('string(2 inch)'), '2 inch'); + assert.equal(math.eval('string([1,2;3,4])'), '[[1, 2], [3, 4]]'); + }); -// boolean -assert.equal(string(true), 'true'); -assert.equal(string(false), 'false'); + it('should be \'\' if called with no argument', function() { + assert.equal(string(), ''); + }); -// string -assert.equal(string('hello'), 'hello'); -assert.equal(string(''), ''); -assert.equal(string(' '), ' '); + it('should be \'true\' if called with true, \'false\' if called with false', function() { + assert.equal(string(true), 'true'); + assert.equal(string(false), 'false'); + }); -// number -assert.equal(string(1/8), '0.125'); -assert.equal(string(2.1e-3), '0.0021'); -assert.equal(string(123456789), '1.2346e8'); // TODO: is it desirable that value is rounded? -assert.equal(string(2000000), '2e6'); + it('should be the identity if called with a string', function() { + assert.equal(string('hello'), 'hello'); + assert.equal(string(''), ''); + assert.equal(string(' '), ' '); + }); -// complex -assert.equal(string(math.complex(2,3)), '2 + 3i'); + it('should convert a number to string', function() { + assert.equal(string(1/8), '0.125'); + assert.equal(string(2.1e-3), '0.0021'); + assert.equal(string(123456789), '1.2346e8'); // TODO: is it desirable that value is rounded? + assert.equal(string(2000000), '2e6'); + }); -// unit -assert.equal(string(math.unit('5cm')), '50 mm'); + it('should convert a complex number to string', function() { + assert.equal(string(math.complex(2,3)), '2 + 3i'); + }); -// array, matrix, range -assert.equal(string([[1,2],[3,4]]), '[[1, 2], [3, 4]]'); -assert.equal(string(math.matrix([[1,2],[3,4]])), '[[1, 2], [3, 4]]'); -assert.equal(string(math.range(1,5)), '[1, 2, 3, 4, 5]'); + it('should convert a unit to string', function() { + assert.equal(string(math.unit('5cm')), '50 mm'); + }); -// wrong number of arguments -assert.throws(function () {string(1,2)}, SyntaxError); -assert.throws(function () {string(1,2,3)}, SyntaxError); + it('should convert a matrix/range/array to string', function() { + assert.equal(string([[1,2],[3,4]]), '[[1, 2], [3, 4]]'); + assert.equal(string(math.matrix([[1,2],[3,4]])), '[[1, 2], [3, 4]]'); + assert.equal(string(math.range(1,5)), '[1, 2, 3, 4, 5]'); + }); + it('should throw an error if called with wrong number of arguments', function() { + assert.throws(function () {string(1,2)}, SyntaxError); + assert.throws(function () {string(1,2,3)}, SyntaxError); + }); +}); diff --git a/test/function/construction/unit.test.js b/test/function/construction/unit.test.js index b0f3c861c..eaefe92bf 100644 --- a/test/function/construction/unit.test.js +++ b/test/function/construction/unit.test.js @@ -1,30 +1,51 @@ -// test unit construction var assert = require('assert'), math = require('../../../src/index.js'), unit = math.unit; +describe('unit', function() { -// 1 argument -assert.deepEqual(unit('5 cm').toString(), '50 mm'); -assert.deepEqual(unit('5000 cm').toString(), '50 m'); -assert.deepEqual(unit('10 kg').toString(), '10 kg'); + it('should parse a valid string to a unit', function() { + assert.deepEqual(unit('5 cm').toString(), '50 mm'); + assert.deepEqual(unit('5000 cm').toString(), '50 m'); + assert.deepEqual(unit('10 kg').toString(), '10 kg'); + }); -var a = math.unit('5cm'); -var b = math.unit(a); -assert.deepEqual(b.toString(), '50 mm'); + it('should be the identity if called with a unit', function() { + var a = math.unit('5cm'); + var b = math.unit(a); + assert.deepEqual(b.toString(), '50 mm'); + }); -assert.throws(function () {unit('invalid unit')}, SyntaxError); + it('should throw an error if called with an invalid string', function() { + assert.throws(function () {unit('invalid unit')}, SyntaxError); + }); -assert.throws(function () {unit(2)}, TypeError); -assert.throws(function () {unit(math.complex(2,3))}, TypeError); + it('should throw an error if called with a number', function() { + assert.throws(function () {unit(2)}, TypeError); + }); -// 2 arguments -assert.deepEqual(unit(5, 'cm').toString(), '50 mm'); -assert.deepEqual(unit(10, 'kg').toString(), '10 kg'); + it('should throw an error if called with a complex', function() { + assert.throws(function () {unit(math.complex(2,3))}, TypeError); + }); -assert.throws(function () {unit('2', 'cm')}, TypeError); -assert.throws(function () {unit(2, math.complex(2,3))}, TypeError); + it('should take a number as the quantity and a string as the unit', function() { + assert.deepEqual(unit(5, 'cm').toString(), '50 mm'); + assert.deepEqual(unit(10, 'kg').toString(), '10 kg'); + }); -// 0 or 3+ arguments -assert.throws(function () {unit()}, SyntaxError); -assert.throws(function () {unit(1,2,3)}, SyntaxError); + it('should throw an error if called with 2 strings', function() { + assert.throws(function () {unit('2', 'cm')}, TypeError); + }); + + it('should throw an error if called with one invalid argument', function() { + assert.throws(function () {unit(2, math.complex(2,3))}, TypeError); + }); + + it('should throw an error if called with no argument', function() { + assert.throws(function () {unit()}, SyntaxError); + }); + + it('should throw an error if called with an invalid number of arguments', function() { + assert.throws(function () {unit(1,2,3)}, SyntaxError); + }); +}); \ No newline at end of file