mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
98 lines
4.5 KiB
JavaScript
98 lines
4.5 KiB
JavaScript
// test data type Complex
|
|
|
|
var assert = require('assert');
|
|
var math = require('../../src/index.js');
|
|
|
|
describe('Complex', function () {
|
|
|
|
it('creation', function () {
|
|
var complex1 = math.complex(3, -4);
|
|
assert.equal(complex1.re, 3);
|
|
assert.equal(complex1.im, -4);
|
|
assert.equal(complex1.toString(), '3 - 4i');
|
|
assert.throws(function () { math.complex(3, -4, 5); });
|
|
assert.throws(function () { math.type.Complex(3, -4); }); // no new
|
|
});
|
|
|
|
it('constructor and parser', function () {
|
|
assert.equal(math.complex().toString(), '0');
|
|
assert.equal(math.complex(2, 3).toString(), '2 + 3i');
|
|
assert.equal(math.complex(2, 0).toString(), '2');
|
|
assert.equal(math.complex(0, 3).toString(), '3i');
|
|
assert.equal(math.complex('2 + 3i').toString(), '2 + 3i');
|
|
assert.equal(math.complex('2 +3i').toString(), '2 + 3i');
|
|
assert.equal(math.complex('2+3i').toString(), '2 + 3i');
|
|
assert.equal(math.complex(' 2+3i ').toString(), '2 + 3i');
|
|
assert.equal(math.complex('2-3i').toString(), '2 - 3i');
|
|
assert.equal(math.complex('2 + i').toString(), '2 + i');
|
|
assert.equal(math.complex('-2-3i').toString(), '-2 - 3i');
|
|
assert.equal(math.complex('-2+3i').toString(), '-2 + 3i');
|
|
assert.equal(math.complex('-2+-3i').toString(), '-2 - 3i');
|
|
assert.equal(math.complex('-2-+3i').toString(), '-2 - 3i');
|
|
assert.equal(math.complex('+2-+3i').toString(), '2 - 3i');
|
|
assert.equal(math.complex('+2-+3i').toString(), '2 - 3i');
|
|
assert.equal(math.complex('2 + 3i').toString(), '2 + 3i');
|
|
assert.equal(math.complex('2 - -3i').toString(), '2 + 3i');
|
|
assert.equal(math.complex(' 2 + 3i ').toString(), '2 + 3i');
|
|
assert.equal(math.complex('2 + i').toString(), '2 + i');
|
|
assert.equal(math.complex('2 - i').toString(), '2 - i');
|
|
assert.equal(math.complex('2 + -i').toString(), '2 - i');
|
|
assert.equal(math.complex('-2+3e-1i').toString(), '-2 + 0.3i');
|
|
assert.equal(math.complex('-2+3e+1i').toString(), '-2 + 30i');
|
|
assert.equal(math.complex('2+3e2i').toString(), '2 + 300i');
|
|
assert.equal(math.complex('2.2e-1-3.2e-1i').toString(), '0.22 - 0.32i');
|
|
assert.equal(math.complex('2.2e-1-+3.2e-1i').toString(), '0.22 - 0.32i');
|
|
assert.equal(math.complex('2').toString(), '2');
|
|
assert.equal(math.complex('i').toString(), 'i');
|
|
assert.equal(math.complex(' i ').toString(), 'i');
|
|
assert.equal(math.complex('-i').toString(), '-i');
|
|
assert.equal(math.complex(' -i ').toString(), '-i');
|
|
assert.equal(math.complex('+i').toString(), 'i');
|
|
assert.equal(math.complex(' +i ').toString(), 'i');
|
|
assert.equal(math.complex('-2').toString(), '-2');
|
|
assert.equal(math.complex('3I').toString(), '3i');
|
|
assert.equal(math.complex('-3i').toString(), '-3i');
|
|
assert.throws(function () { math.complex(1, 2, 3); });
|
|
assert.throws(function () { math.complex('str', 2); });
|
|
assert.throws(function () { math.complex(1, true); });
|
|
assert.throws(function () { math.complex(2); });
|
|
assert.throws(function () { math.complex(''); });
|
|
assert.throws(function () { math.complex('2r'); });
|
|
assert.throws(function () { math.complex('str'); });
|
|
assert.throws(function () { math.complex('2i+3i'); });
|
|
assert.throws(function () { math.complex('2ia'); });
|
|
assert.throws(function () { math.complex('3+4'); });
|
|
assert.throws(function () { math.complex('3i+4'); });
|
|
assert.throws(function () { math.complex('3e + 4i'); });
|
|
assert.throws(function () { math.complex('3e1.2 + 4i'); });
|
|
assert.throws(function () { math.complex('3e1.2i'); });
|
|
assert.throws(function () { math.complex('3e1.2i'); });
|
|
assert.throws(function () { math.complex('- i'); });
|
|
assert.throws(function () { math.complex('+ i'); });
|
|
});
|
|
|
|
it('toString', function () {
|
|
assert.equal(math.complex().toString(), '0');
|
|
assert.equal(math.complex(0, 2).toString(), '2i');
|
|
assert.equal(math.complex(1, 1).toString(), '1 + i');
|
|
assert.equal(math.complex(1, 2).toString(), '1 + 2i');
|
|
assert.equal(math.complex(1, -1).toString(), '1 - i');
|
|
assert.equal(math.complex(1, -2).toString(), '1 - 2i');
|
|
assert.equal(math.complex(1, 0).toString(), '1');
|
|
assert.equal(math.complex(-1, 2).toString(), '-1 + 2i');
|
|
assert.equal(math.complex(-1, 1).toString(), '-1 + i');
|
|
});
|
|
|
|
it('clone', function () {
|
|
var complex1 = math.complex(3, -4);
|
|
var clone = complex1.clone();
|
|
clone.re = 100;
|
|
clone.im = 200;
|
|
assert.notEqual(complex1, clone);
|
|
assert.equal(complex1.re, 3);
|
|
assert.equal(complex1.im, -4);
|
|
assert.equal(clone.re, 100);
|
|
assert.equal(clone.im, 200);
|
|
});
|
|
|
|
}); |