mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
Finished testing, and renamed some of the ops to avoid any future confusion with boolean ops.
This commit is contained in:
parent
37fe86d251
commit
a6fa55122d
@ -21,31 +21,31 @@ module.exports = function (math, config) {
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.and(x, y)
|
||||
* math.bitAnd(x, y)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.and(53, 131); // returns Number 1
|
||||
* math.bitAnd(53, 131); // returns Number 1
|
||||
*
|
||||
* math.and([1, 12, 31], 42); // returns Array [0, 8, 10]
|
||||
* math.bitAnd([1, 12, 31], 42); // returns Array [0, 8, 10]
|
||||
*
|
||||
* var b = math.unit('12 m');
|
||||
* var c = math.unit('12 cm');
|
||||
* var d = math.unit('52 mm');
|
||||
* math.and(c, d); // returns Unit 48 mm
|
||||
* math.and(b, d); // returns Unit 32 mm
|
||||
* math.bitAnd(c, d); // returns Unit 48 mm
|
||||
* math.bitAnd(b, d); // returns Unit 32 mm
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* not, or, xor, leftShift, rightArithShift, rightLogShift
|
||||
* bitNot, bitOr, xor, leftShift, rightArithShift, rightLogShift
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x First value to and
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} y Second value to and
|
||||
* @return {Integer | BigNumber | Unit | String | Array | Matrix} AND of `x` and `y`
|
||||
*/
|
||||
math.and = function and(x, y) {
|
||||
math.bitAnd = function bitAnd(x, y) {
|
||||
if (arguments.length != 2) {
|
||||
throw new math.error.ArgumentsError('and', arguments.length, 2);
|
||||
throw new math.error.ArgumentsError('bitAnd', arguments.length, 2);
|
||||
}
|
||||
|
||||
if (isNumber(x) && isNumber(y)) {
|
||||
@ -100,7 +100,7 @@ module.exports = function (math, config) {
|
||||
}
|
||||
|
||||
// downgrade to Number
|
||||
return and(x.toNumber(), y);
|
||||
return bitAnd(x.toNumber(), y);
|
||||
}
|
||||
if (y instanceof BigNumber) {
|
||||
// try to convert to big number
|
||||
@ -116,31 +116,31 @@ module.exports = function (math, config) {
|
||||
}
|
||||
|
||||
// downgrade to Number
|
||||
return and(x, y.toNumber());
|
||||
return bitAnd(x, y.toNumber());
|
||||
}*/
|
||||
|
||||
if (isCollection(x) || isCollection(y)) {
|
||||
return collection.deepMap2(x, y, and);
|
||||
return collection.deepMap2(x, y, bitAnd);
|
||||
}
|
||||
|
||||
if (isString(x)) {
|
||||
return and((config.number == 'bignumber')
|
||||
? new BigNumber(x)
|
||||
: parseInt(x), y);
|
||||
return bitAnd((config.number == 'bignumber')
|
||||
? new BigNumber(x)
|
||||
: parseInt(x), y);
|
||||
}
|
||||
if (isString(y)) {
|
||||
return and(x, (config.number == 'bignumber')
|
||||
? new BigNumber(y)
|
||||
: parseInt(y));
|
||||
return bitAnd(x, (config.number == 'bignumber')
|
||||
? new BigNumber(y)
|
||||
: parseInt(y));
|
||||
}
|
||||
|
||||
if (isBoolean(x) || x === null) {
|
||||
return and(+x, y);
|
||||
return bitAnd(+x, y);
|
||||
}
|
||||
if (isBoolean(y) || y === null) {
|
||||
return and(x, +y);
|
||||
return bitAnd(x, +y);
|
||||
}
|
||||
|
||||
throw new math.error.UnsupportedTypeError('and', math['typeof'](x), math['typeof'](y));
|
||||
throw new math.error.UnsupportedTypeError('bitAnd', math['typeof'](x), math['typeof'](y));
|
||||
};
|
||||
};
|
||||
@ -21,29 +21,29 @@ module.exports = function (math, config) {
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.not(x)
|
||||
* math.bitNot(x)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.not(1); // returns Number -2
|
||||
* math.bitNot(1); // returns Number -2
|
||||
*
|
||||
* math.not([2, -3, 4]); // returns Array [-3, 2, 5]
|
||||
* math.bitNot([2, -3, 4]); // returns Array [-3, 2, 5]
|
||||
*
|
||||
* var c = math.unit('-12 m');
|
||||
* var d = math.unit('52 mm');
|
||||
* math.not(c); // returns Unit 11 m
|
||||
* math.not(d); // returns Unit -53 mm
|
||||
* math.bitNot(c); // returns Unit 11 m
|
||||
* math.bitNot(d); // returns Unit -53 mm
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* and, or, xor, leftShift, rightArithShift, rightLogShift
|
||||
* bitAnd, bitOr, xor, leftShift, rightArithShift, rightLogShift
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x Value to not
|
||||
* @return {Integer | BigNumber | Unit | String | Array | Matrix} NOT of `x`
|
||||
*/
|
||||
math.not = function not(x) {
|
||||
math.bitNot = function bitNot(x) {
|
||||
if (arguments.length != 1) {
|
||||
throw new math.error.ArgumentsError('not', arguments.length, 1);
|
||||
throw new math.error.ArgumentsError('bitNot', arguments.length, 1);
|
||||
}
|
||||
|
||||
if (isNumber(x)) {
|
||||
@ -72,19 +72,19 @@ module.exports = function (math, config) {
|
||||
}*/
|
||||
|
||||
if (isCollection(x)) {
|
||||
return collection.deepMap(x, not);
|
||||
return collection.deepMap(x, bitNot);
|
||||
}
|
||||
|
||||
if (isString(x)) {
|
||||
return not((config.number == 'bignumber')
|
||||
? new BigNumber(x)
|
||||
: parseInt(x));
|
||||
return bitNot((config.number == 'bignumber')
|
||||
? new BigNumber(x)
|
||||
: parseInt(x));
|
||||
}
|
||||
|
||||
if (isBoolean(x) || x === null) {
|
||||
return not(+x);
|
||||
return bitNot(+x);
|
||||
}
|
||||
|
||||
throw new math.error.UnsupportedTypeError('not', math['typeof'](x));
|
||||
throw new math.error.UnsupportedTypeError('bitNot', math['typeof'](x));
|
||||
};
|
||||
};
|
||||
@ -21,31 +21,31 @@ module.exports = function (math, config) {
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.or(x, y)
|
||||
* math.bitOr(x, y)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.or(1, 2); // returns Number 3
|
||||
* math.bitOr(1, 2); // returns Number 3
|
||||
*
|
||||
* math.or([1, 2, 3], 4); // returns Array [5, 6, 7]
|
||||
* math.bitOr([1, 2, 3], 4); // returns Array [5, 6, 7]
|
||||
*
|
||||
* var b = math.unit('12 m');
|
||||
* var c = math.unit('12 cm');
|
||||
* var d = math.unit('52 mm');
|
||||
* math.or(c, d); // returns Unit 124 mm
|
||||
* math.or(b, d); // returns Unit 12.02 m
|
||||
* math.bitOr(c, d); // returns Unit 124 mm
|
||||
* math.bitOr(b, d); // returns Unit 12.02 m
|
||||
*
|
||||
* See also:
|
||||
*
|
||||
* and, not, xor, leftShift, rightArithShift, rightLogShift
|
||||
* bitAnd, bitNot, xor, leftShift, rightArithShift, rightLogShift
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x First value to or
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} y Second value to or
|
||||
* @return {Integer | BigNumber | Unit | String | Array | Matrix} OR of `x` and `y`
|
||||
*/
|
||||
math.or = function or(x, y) {
|
||||
math.bitOr = function bitOr(x, y) {
|
||||
if (arguments.length != 2) {
|
||||
throw new math.error.ArgumentsError('or', arguments.length, 2);
|
||||
throw new math.error.ArgumentsError('bitOr', arguments.length, 2);
|
||||
}
|
||||
|
||||
if (isNumber(x) && isNumber(y)) {
|
||||
@ -101,7 +101,7 @@ module.exports = function (math, config) {
|
||||
}
|
||||
|
||||
// downgrade to Number
|
||||
return or(x.toNumber(), y);
|
||||
return bitOr(x.toNumber(), y);
|
||||
}
|
||||
if (y instanceof BigNumber) {
|
||||
// try to convert to big number
|
||||
@ -117,31 +117,31 @@ module.exports = function (math, config) {
|
||||
}
|
||||
|
||||
// downgrade to Number
|
||||
return or(x, y.toNumber());
|
||||
return bitOr(x, y.toNumber());
|
||||
}*/
|
||||
|
||||
if (isCollection(x) || isCollection(y)) {
|
||||
return collection.deepMap2(x, y, or);
|
||||
return collection.deepMap2(x, y, bitOr);
|
||||
}
|
||||
|
||||
if (isString(x)) {
|
||||
return or((config.number == 'bignumber')
|
||||
? new BigNumber(x)
|
||||
: parseInt(x), y);
|
||||
return bitOr((config.number == 'bignumber')
|
||||
? new BigNumber(x)
|
||||
: parseInt(x), y);
|
||||
}
|
||||
if (isString(y)) {
|
||||
return or(x, (config.number == 'bignumber')
|
||||
? new BigNumber(y)
|
||||
: parseInt(y));
|
||||
return bitOr(x, (config.number == 'bignumber')
|
||||
? new BigNumber(y)
|
||||
: parseInt(y));
|
||||
}
|
||||
|
||||
if (isBoolean(x) || x === null) {
|
||||
return or(+x, y);
|
||||
return bitOr(+x, y);
|
||||
}
|
||||
if (isBoolean(y) || y === null) {
|
||||
return or(x, +y);
|
||||
return bitOr(x, +y);
|
||||
}
|
||||
|
||||
throw new math.error.UnsupportedTypeError('or', math['typeof'](x), math['typeof'](y));
|
||||
throw new math.error.UnsupportedTypeError('bitOr', math['typeof'](x), math['typeof'](y));
|
||||
};
|
||||
};
|
||||
@ -208,10 +208,10 @@ function create (config) {
|
||||
require('./function/arithmetic/xgcd')(math, _config);
|
||||
|
||||
// functions - bitwise
|
||||
require('./function/bitwise/and')(math, _config);
|
||||
require('./function/bitwise/bitAnd')(math, _config);
|
||||
require('./function/bitwise/bitNot')(math, _config);
|
||||
require('./function/bitwise/bitOr')(math, _config);
|
||||
require('./function/bitwise/leftShift')(math, _config);
|
||||
require('./function/bitwise/not')(math, _config);
|
||||
require('./function/bitwise/or')(math, _config);
|
||||
require('./function/bitwise/rightArithShift')(math, _config);
|
||||
require('./function/bitwise/rightLogShift')(math, _config);
|
||||
require('./function/bitwise/xor')(math, _config);
|
||||
|
||||
@ -1,163 +0,0 @@
|
||||
// test and
|
||||
var assert = require('assert'),
|
||||
approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
//bignumber = math.bignumber,
|
||||
and = math.and;
|
||||
|
||||
describe('and', function () {
|
||||
|
||||
it('should and two numbers', function () {
|
||||
assert.equal(and(53, 131), 1);
|
||||
assert.equal(and(2, 3), 2);
|
||||
assert.equal(and(-2, 3), 2);
|
||||
assert.equal(and(2, -3), 0);
|
||||
assert.equal(and(-5, -3), -7);
|
||||
});
|
||||
|
||||
it('should and booleans', function () {
|
||||
assert.equal(and(true, true), 1);
|
||||
assert.equal(and(true, false), 0);
|
||||
assert.equal(and(false, true), 0);
|
||||
assert.equal(and(false, false), 0);
|
||||
});
|
||||
|
||||
it('should and numbers and null', function () {
|
||||
assert.equal(math.and(null, null), 0);
|
||||
assert.equal(math.and(null, 1), 0);
|
||||
assert.equal(math.and(1, null), 0);
|
||||
});
|
||||
|
||||
it('should and mixed numbers and booleans', function () {
|
||||
assert.equal(and(1, true), 1);
|
||||
assert.equal(and(1, false), 0);
|
||||
assert.equal(and(true, 1), 1);
|
||||
assert.equal(and(false, 1), 0);
|
||||
});
|
||||
|
||||
/*it('should add bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), bignumber(0.2)), bignumber(0.3));
|
||||
assert.deepEqual(add(bignumber('2e5001'), bignumber('3e5000')), bignumber('2.3e5001'));
|
||||
assert.deepEqual(add(bignumber('9999999999999999999'), bignumber('1')), bignumber('1e19'));
|
||||
});
|
||||
|
||||
it('should add mixed numbers and bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), 0.2), bignumber(0.3));
|
||||
assert.deepEqual(add(0.1, bignumber(0.2)), bignumber(0.3));
|
||||
|
||||
approx.equal(add(1/3, bignumber(1)), 1.333333333333333);
|
||||
approx.equal(add(bignumber(1), 1/3), 1.333333333333333);
|
||||
});
|
||||
|
||||
it('should add mixed booleans and bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), true), bignumber(1.1));
|
||||
assert.deepEqual(add(bignumber(0.1), false), bignumber(0.1));
|
||||
assert.deepEqual(add(false, bignumber(0.2)), bignumber(0.2));
|
||||
assert.deepEqual(add(true, bignumber(0.2)), bignumber(1.2));
|
||||
});
|
||||
|
||||
it('should add mixed complex numbers and bignumbers', function () {
|
||||
assert.deepEqual(add(math.complex(3, -4), bignumber(2)), math.complex(5, -4));
|
||||
assert.deepEqual(add(bignumber(2), math.complex(3, -4)), math.complex(5, -4));
|
||||
});*/
|
||||
|
||||
it('should and two measures of the same unit', function () {
|
||||
approx.deepEqual(and(math.unit(33, 'km'), math.unit(100, 'mile')), math.unit(.16, 'km'));
|
||||
|
||||
var b = math.unit('12 m');
|
||||
var c = math.unit('12 cm');
|
||||
var d = math.unit('52 mm');
|
||||
approx.deepEqual(and(b, d), math.unit(.032, 'm'));
|
||||
approx.deepEqual(and(c, d), math.unit(4.8, 'cm'));
|
||||
});
|
||||
|
||||
it('should throw an error for two measures of different units', function () {
|
||||
assert.throws(function () {
|
||||
and(math.unit(5, 'km'), math.unit(100, 'gram'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when one of the two units has undefined value', function () {
|
||||
assert.throws(function () {
|
||||
and(math.unit('km'), math.unit('5gram'));
|
||||
}, /Parameter x contains a unit with undefined value/);
|
||||
assert.throws(function () {
|
||||
and(math.unit('5 km'), math.unit('gram'));
|
||||
}, /Parameter y contains a unit with undefined value/);
|
||||
});
|
||||
|
||||
it('should throw an error in case of a unit and non-unit argument', function () {
|
||||
assert.throws(function () {and(math.unit('5cm'), 2)}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {and(math.unit('5cm'), new Date())}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {and(new Date(), math.unit('5cm'))}, math.error.UnsupportedTypeError);
|
||||
});
|
||||
|
||||
it('should and two ints, even in string format', function () {
|
||||
assert.equal(and('120', '86'), 80);
|
||||
assert.equal(and('86', 120), 80);
|
||||
assert.equal(and('-120', '-86'), -120);
|
||||
assert.equal(and(-120, '-86'), -120);
|
||||
});
|
||||
|
||||
it('should throw an error in case of NaN argument', function () {
|
||||
assert.throws(function () {
|
||||
and(NaN, 10);
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
and('10', NaN);
|
||||
}, /Parameter y contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
and('This is not a number!', '12');
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
and(12, '');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
});
|
||||
|
||||
it('should and strings and matrices element wise', function () {
|
||||
assert.deepEqual(and('42', ['1', 12, '31']), [0, 8, 10]);
|
||||
assert.deepEqual(and(['1', 12, '31'], '42'), [0, 8, 10]);
|
||||
|
||||
assert.deepEqual(and('42', math.matrix(['1', 12, '31'])), math.matrix([0, 8, 10]));
|
||||
assert.deepEqual(and(math.matrix(['1', 12, '31']), '42'), math.matrix([0, 8, 10]));
|
||||
});
|
||||
|
||||
it('should and matrices correctly', function () {
|
||||
var a2 = math.matrix([[1,2],[3,4]]);
|
||||
var a3 = math.matrix([[5,6],[7,8]]);
|
||||
var a4 = and(a2, a3);
|
||||
assert.ok(a4 instanceof math.type.Matrix);
|
||||
assert.deepEqual(a4.size(), [2,2]);
|
||||
assert.deepEqual(a4.valueOf(), [[1,2],[3,0]]);
|
||||
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 and a scalar and a matrix correctly', function () {
|
||||
assert.deepEqual(and(12, math.matrix([3,9])), math.matrix([0,8]));
|
||||
assert.deepEqual(and(math.matrix([3,9]), 12), math.matrix([0,8]));
|
||||
});
|
||||
|
||||
it('should and a scalar and an array correctly', function () {
|
||||
assert.deepEqual(and(12, [3,9]), [0,8]);
|
||||
assert.deepEqual(and([3,9], 12), [0,8]);
|
||||
});
|
||||
|
||||
it('should and a matrix and an array correctly', function () {
|
||||
var a = [6,4,28];
|
||||
var b = math.matrix([13,92,101]);
|
||||
var c = and(a, b);
|
||||
|
||||
assert.ok(c instanceof math.type.Matrix);
|
||||
assert.deepEqual(c, math.matrix([4,4,4]));
|
||||
});
|
||||
|
||||
it('should throw an error in case of invalid number of arguments', function () {
|
||||
assert.throws(function () {and(1)}, error.ArgumentsError);
|
||||
assert.throws(function () {and(1, 2, 3)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
});
|
||||
159
test/function/bitwise/bitAnd.test.js
Normal file
159
test/function/bitwise/bitAnd.test.js
Normal file
@ -0,0 +1,159 @@
|
||||
// test bitAnd
|
||||
var assert = require('assert'),
|
||||
approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
//bignumber = math.bignumber,
|
||||
bitAnd = math.bitAnd;
|
||||
|
||||
describe('bitAnd', function () {
|
||||
|
||||
it('should bitwise and two numbers', function () {
|
||||
assert.equal(bitAnd(53, 131), 1);
|
||||
assert.equal(bitAnd(2, 3), 2);
|
||||
assert.equal(bitAnd(-2, 3), 2);
|
||||
assert.equal(bitAnd(2, -3), 0);
|
||||
assert.equal(bitAnd(-5, -3), -7);
|
||||
});
|
||||
|
||||
it('should bitwise and booleans', function () {
|
||||
assert.equal(bitAnd(true, true), 1);
|
||||
assert.equal(bitAnd(true, false), 0);
|
||||
assert.equal(bitAnd(false, true), 0);
|
||||
assert.equal(bitAnd(false, false), 0);
|
||||
});
|
||||
|
||||
it('should bitwise and numbers and null', function () {
|
||||
assert.equal(math.bitAnd(null, null), 0);
|
||||
assert.equal(math.bitAnd(null, 1), 0);
|
||||
assert.equal(math.bitAnd(1, null), 0);
|
||||
});
|
||||
|
||||
it('should bitwise and mixed numbers and booleans', function () {
|
||||
assert.equal(bitAnd(1, true), 1);
|
||||
assert.equal(bitAnd(1, false), 0);
|
||||
assert.equal(bitAnd(true, 1), 1);
|
||||
assert.equal(bitAnd(false, 1), 0);
|
||||
});
|
||||
|
||||
/*it('should bitwise and bignumbers', function () {
|
||||
assert.deepEqual(bitAnd(bignumber(1), bignumber(2)), bignumber(0));
|
||||
assert.deepEqual(bitAnd(bignumber('-1.0e+31'), bignumber('-1.0e+32')), bignumber('-1.0127339798528531708e+32'));
|
||||
assert.deepEqual(bitAnd(bignumber('1.0e+31'), bignumber('1.0e+32')), bignumber('8.726602014714682918e+30'));
|
||||
assert.deepEqual(bitAnd(bignumber('-1.0e+31'), bignumber('1.0e+32')), bignumber('9.1273397985285317082e+31'));
|
||||
assert.deepEqual(bitAnd(bignumber('1.0e+31'), bignumber('-1.0e+32')), bignumber('1.273397985285317082e+30'));
|
||||
});
|
||||
|
||||
it('should bitwise and mixed numbers and bignumbers', function () {
|
||||
assert.deepEqual(bitAnd(bignumber(1), 2), bignumber(0));
|
||||
assert.deepEqual(bitAnd(1, bignumber(2)), bignumber(0));
|
||||
assert.deepEqual(bitAnd(bignumber(7), 9), bignumber(1));
|
||||
assert.deepEqual(bitAnd(7, bignumber(9)), bignumber(1));
|
||||
});
|
||||
|
||||
it('should bitwise and mixed booleans and bignumbers', function () {
|
||||
assert.deepEqual(bitAnd(bignumber(1), true), bignumber(1));
|
||||
assert.deepEqual(bitAnd(bignumber(1), false), bignumber(0));
|
||||
assert.deepEqual(bitAnd(false, bignumber(3)), bignumber(0));
|
||||
assert.deepEqual(bitAnd(true, bignumber(3)), bignumber(1));
|
||||
});*/
|
||||
|
||||
it('should bitwise and two measures of the same unit', function () {
|
||||
approx.deepEqual(bitAnd(math.unit(33, 'km'), math.unit(100, 'mile')), math.unit(.16, 'km'));
|
||||
|
||||
var b = math.unit('12 m');
|
||||
var c = math.unit('12 cm');
|
||||
var d = math.unit('52 mm');
|
||||
approx.deepEqual(bitAnd(b, d), math.unit(.032, 'm'));
|
||||
approx.deepEqual(bitAnd(c, d), math.unit(4.8, 'cm'));
|
||||
});
|
||||
|
||||
it('should throw an error for two measures of different units', function () {
|
||||
assert.throws(function () {
|
||||
bitAnd(math.unit(5, 'km'), math.unit(100, 'gram'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when one of the two units has undefined value', function () {
|
||||
assert.throws(function () {
|
||||
bitAnd(math.unit('km'), math.unit('5gram'));
|
||||
}, /Parameter x contains a unit with undefined value/);
|
||||
assert.throws(function () {
|
||||
bitAnd(math.unit('5 km'), math.unit('gram'));
|
||||
}, /Parameter y contains a unit with undefined value/);
|
||||
});
|
||||
|
||||
it('should throw an error in case of a unit and non-unit argument', function () {
|
||||
assert.throws(function () {bitAnd(math.unit('5cm'), 2)}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {bitAnd(math.unit('5cm'), new Date())}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {bitAnd(new Date(), math.unit('5cm'))}, math.error.UnsupportedTypeError);
|
||||
});
|
||||
|
||||
it('should bitwise and two ints, even in string format', function () {
|
||||
assert.equal(bitAnd('120', '86'), 80);
|
||||
assert.equal(bitAnd('86', 120), 80);
|
||||
assert.equal(bitAnd('-120', '-86'), -120);
|
||||
assert.equal(bitAnd(-120, '-86'), -120);
|
||||
});
|
||||
|
||||
it('should throw an error in case of NaN argument', function () {
|
||||
assert.throws(function () {
|
||||
bitAnd(NaN, 10);
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
bitAnd('10', NaN);
|
||||
}, /Parameter y contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
bitAnd('This is not a number!', '12');
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
bitAnd(12, '');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
});
|
||||
|
||||
it('should bitwise and strings and matrices element wise', function () {
|
||||
assert.deepEqual(bitAnd('42', ['1', 12, '31']), [0, 8, 10]);
|
||||
assert.deepEqual(bitAnd(['1', 12, '31'], '42'), [0, 8, 10]);
|
||||
|
||||
assert.deepEqual(bitAnd('42', math.matrix(['1', 12, '31'])), math.matrix([0, 8, 10]));
|
||||
assert.deepEqual(bitAnd(math.matrix(['1', 12, '31']), '42'), math.matrix([0, 8, 10]));
|
||||
});
|
||||
|
||||
it('should bitwise and matrices correctly', function () {
|
||||
var a2 = math.matrix([[1,2],[3,4]]);
|
||||
var a3 = math.matrix([[5,6],[7,8]]);
|
||||
var a4 = bitAnd(a2, a3);
|
||||
assert.ok(a4 instanceof math.type.Matrix);
|
||||
assert.deepEqual(a4.size(), [2,2]);
|
||||
assert.deepEqual(a4.valueOf(), [[1,2],[3,0]]);
|
||||
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 bitwise and a scalar and a matrix correctly', function () {
|
||||
assert.deepEqual(bitAnd(12, math.matrix([3,9])), math.matrix([0,8]));
|
||||
assert.deepEqual(bitAnd(math.matrix([3,9]), 12), math.matrix([0,8]));
|
||||
});
|
||||
|
||||
it('should bitwise and a scalar and an array correctly', function () {
|
||||
assert.deepEqual(bitAnd(12, [3,9]), [0,8]);
|
||||
assert.deepEqual(bitAnd([3,9], 12), [0,8]);
|
||||
});
|
||||
|
||||
it('should bitwise and a matrix and an array correctly', function () {
|
||||
var a = [6,4,28];
|
||||
var b = math.matrix([13,92,101]);
|
||||
var c = bitAnd(a, b);
|
||||
|
||||
assert.ok(c instanceof math.type.Matrix);
|
||||
assert.deepEqual(c, math.matrix([4,4,4]));
|
||||
});
|
||||
|
||||
it('should throw an error in case of invalid number of arguments', function () {
|
||||
assert.throws(function () {bitAnd(1)}, error.ArgumentsError);
|
||||
assert.throws(function () {bitAnd(1, 2, 3)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
});
|
||||
71
test/function/bitwise/bitNot.test.js
Normal file
71
test/function/bitwise/bitNot.test.js
Normal file
@ -0,0 +1,71 @@
|
||||
// test bitNot
|
||||
var assert = require('assert'),
|
||||
math = require('../../../index'),
|
||||
error = require('../../../lib/error/index'),
|
||||
// bignumber = math.bignumber,
|
||||
bitNot = math.bitNot;
|
||||
|
||||
describe('bitNot', function () {
|
||||
it('should return bitwise not of a boolean', function () {
|
||||
assert.equal(bitNot(true), -2);
|
||||
assert.equal(bitNot(false), -1);
|
||||
});
|
||||
|
||||
/*it('should return bignumber bitwise not of a boolean', function () {
|
||||
var bigmath = math.create({number: 'bignumber'});
|
||||
assert.deepEqual(bigmath.bitNot(true), bigmath.bignumber(-2));
|
||||
assert.deepEqual(bigmath.bitNot(false), bigmath.bignumber(-1));
|
||||
});*/
|
||||
|
||||
it('should return bitwise not of null', function () {
|
||||
assert.equal(bitNot(null), -1);
|
||||
});
|
||||
|
||||
it('should return bitwise not of a string', function () {
|
||||
assert.equal(bitNot('2'), -3);
|
||||
assert.equal(bitNot('-2'), 1);
|
||||
});
|
||||
|
||||
/*it('should return bignumber bitwise not on a string', function() {
|
||||
var bigmath = math.create({number: 'bignumber'});
|
||||
assert.deepEqual(bigmath.bitNot('2'), bigmath.bignumber(-3));
|
||||
assert.deepEqual(bigmath.bitNot('-2'), bigmath.bignumber(1));
|
||||
});*/
|
||||
|
||||
it('should perform bitwise not of a number', function () {
|
||||
assert.deepEqual(bitNot(2), -3);
|
||||
assert.deepEqual(bitNot(-2), 1);
|
||||
assert.deepEqual(bitNot(0), -1);
|
||||
});
|
||||
|
||||
/*it('should perform bitwise not of a big number', function() {
|
||||
assert.deepEqual(bitNot(bignumber(2)), bignumber(-3));
|
||||
assert.deepEqual(bitNot(bignumber(-2)), bignumber(1));
|
||||
});*/
|
||||
|
||||
it('should perform bitwise not of a unit', function () {
|
||||
assert.equal(bitNot(math.unit(5, 'km')).toString(), '-6 km');
|
||||
});
|
||||
|
||||
it('should perform element-wise bitwise not on a matrix', function () {
|
||||
a2 = math.matrix([[1,2],[3,4]]);
|
||||
var a7 = bitNot(a2);
|
||||
assert.ok(a7 instanceof math.type.Matrix);
|
||||
assert.deepEqual(a7.size(), [2,2]);
|
||||
assert.deepEqual(a7.valueOf(), [[-2,-3],[-4,-5]]);
|
||||
});
|
||||
|
||||
it('should perform element-wise bitwise not on an array', function () {
|
||||
assert.deepEqual(bitNot([[1,2],[3,4]]), [[-2,-3],[-4,-5]]);
|
||||
});
|
||||
|
||||
it('should throw an error in case of invalid number of arguments', function () {
|
||||
assert.throws(function () {bitNot()}, error.ArgumentsError);
|
||||
assert.throws(function () {bitNot(1, 2)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
it('should throw an error in case of invalid type of argument', function () {
|
||||
assert.throws(function () {bitNot(new Date())}, error.UnsupportedTypeError);
|
||||
});
|
||||
|
||||
});
|
||||
137
test/function/bitwise/bitOr.test.js
Normal file
137
test/function/bitwise/bitOr.test.js
Normal file
@ -0,0 +1,137 @@
|
||||
// test bitOr
|
||||
var assert = require('assert'),
|
||||
approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
//bignumber = math.bignumber,
|
||||
bitOr = math.bitOr;
|
||||
|
||||
describe('bitOr', function () {
|
||||
|
||||
it('should bitwise or two numbers', function () {
|
||||
assert.equal(bitOr(53, 131), 183);
|
||||
assert.equal(bitOr(2, 3), 3);
|
||||
assert.equal(bitOr(-2, 3), -1);
|
||||
assert.equal(bitOr(2, -3), -1);
|
||||
assert.equal(bitOr(-5, -3), -1);
|
||||
});
|
||||
|
||||
it('should bitwise or booleans', function () {
|
||||
assert.equal(bitOr(true, true), 1);
|
||||
assert.equal(bitOr(true, false), 1);
|
||||
assert.equal(bitOr(false, true), 1);
|
||||
assert.equal(bitOr(false, false), 0);
|
||||
});
|
||||
|
||||
it('should bitwise or numbers and null', function () {
|
||||
assert.equal(math.bitOr(null, null), 0);
|
||||
assert.equal(math.bitOr(null, 1), 1);
|
||||
assert.equal(math.bitOr(1, null), 1);
|
||||
});
|
||||
|
||||
it('should bitwise or mixed numbers and booleans', function () {
|
||||
assert.equal(bitOr(0, true), 1);
|
||||
assert.equal(bitOr(0, false), 0);
|
||||
assert.equal(bitOr(true, 0), 1);
|
||||
assert.equal(bitOr(false, 0), 0);
|
||||
});
|
||||
|
||||
it('should bitwise or two measures of the same unit', function () {
|
||||
approx.deepEqual(bitOr(math.unit(33, 'km'), math.unit(100, 'mile')), math.unit(193.774, 'km'));
|
||||
|
||||
var b = math.unit('12 m');
|
||||
var c = math.unit('12 cm');
|
||||
var d = math.unit('52 mm');
|
||||
approx.deepEqual(bitOr(b, d), math.unit(12.02, 'm'));
|
||||
approx.deepEqual(bitOr(c, d), math.unit(12.4, 'cm'));
|
||||
});
|
||||
|
||||
it('should throw an error for two measures of different units', function () {
|
||||
assert.throws(function () {
|
||||
bitOr(math.unit(5, 'km'), math.unit(100, 'gram'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when one of the two units has undefined value', function () {
|
||||
assert.throws(function () {
|
||||
bitOr(math.unit('km'), math.unit('5gram'));
|
||||
}, /Parameter x contains a unit with undefined value/);
|
||||
assert.throws(function () {
|
||||
bitOr(math.unit('5 km'), math.unit('gram'));
|
||||
}, /Parameter y contains a unit with undefined value/);
|
||||
});
|
||||
|
||||
it('should throw an error in case of a unit and non-unit argument', function () {
|
||||
assert.throws(function () {bitOr(math.unit('5cm'), 2)}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {bitOr(math.unit('5cm'), new Date())}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {bitOr(new Date(), math.unit('5cm'))}, math.error.UnsupportedTypeError);
|
||||
});
|
||||
|
||||
it('should bitwise or two ints, even in string format', function () {
|
||||
assert.equal(bitOr('120', '86'), 126);
|
||||
assert.equal(bitOr('86', 120), 126);
|
||||
assert.equal(bitOr('-120', '-86'), -86);
|
||||
assert.equal(bitOr(-120, '-86'), -86);
|
||||
});
|
||||
|
||||
it('should throw an error in case of NaN argument', function () {
|
||||
assert.throws(function () {
|
||||
bitOr(NaN, 10);
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
bitOr('10', NaN);
|
||||
}, /Parameter y contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
bitOr('This is not a number!', '12');
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
bitOr(12, '');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
});
|
||||
|
||||
it('should bitwise or strings and matrices element wise', function () {
|
||||
assert.deepEqual(bitOr('42', ['1', 12, '31']), [43, 46, 63]);
|
||||
assert.deepEqual(bitOr(['1', 12, '31'], '42'), [43, 46, 63]);
|
||||
|
||||
assert.deepEqual(bitOr('42', math.matrix(['1', 12, '31'])), math.matrix([43, 46, 63]));
|
||||
assert.deepEqual(bitOr(math.matrix(['1', 12, '31']), '42'), math.matrix([43, 46, 63]));
|
||||
});
|
||||
|
||||
it('should bitwise or matrices correctly', function () {
|
||||
var a2 = math.matrix([[1,2],[3,4]]);
|
||||
var a3 = math.matrix([[5,6],[7,8]]);
|
||||
var a4 = bitOr(a2, a3);
|
||||
assert.ok(a4 instanceof math.type.Matrix);
|
||||
assert.deepEqual(a4.size(), [2,2]);
|
||||
assert.deepEqual(a4.valueOf(), [[5,6],[7,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 bitwise or a scalar and a matrix correctly', function () {
|
||||
assert.deepEqual(bitOr(12, math.matrix([3,9])), math.matrix([15,13]));
|
||||
assert.deepEqual(bitOr(math.matrix([3,9]), 12), math.matrix([15,13]));
|
||||
});
|
||||
|
||||
it('should bitwise or a scalar and an array correctly', function () {
|
||||
assert.deepEqual(bitOr(12, [3,9]), [15,13]);
|
||||
assert.deepEqual(bitOr([3,9], 12), [15,13]);
|
||||
});
|
||||
|
||||
it('should bitwise or a matrix and an array correctly', function () {
|
||||
var a = [6,4,28];
|
||||
var b = math.matrix([13,92,101]);
|
||||
var c = bitOr(a, b);
|
||||
|
||||
assert.ok(c instanceof math.type.Matrix);
|
||||
assert.deepEqual(c, math.matrix([15,92,125]));
|
||||
});
|
||||
|
||||
it('should throw an error in case of invalid number of arguments', function () {
|
||||
assert.throws(function () {bitOr(1)}, error.ArgumentsError);
|
||||
assert.throws(function () {bitOr(1, 2, 3)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
});
|
||||
109
test/function/bitwise/leftShift.test.js
Normal file
109
test/function/bitwise/leftShift.test.js
Normal file
@ -0,0 +1,109 @@
|
||||
// test leftShift
|
||||
var assert = require('assert'),
|
||||
approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
//bignumber = math.bignumber,
|
||||
leftShift = math.leftShift;
|
||||
|
||||
describe('leftShift', function () {
|
||||
|
||||
it('should left shift a number by a given amount', function () {
|
||||
assert.equal(leftShift(0, 1000), 0);
|
||||
assert.equal(leftShift(2, 0), 2);
|
||||
assert.equal(leftShift(2, 3), 16);
|
||||
assert.equal(leftShift(2, 4), 32);
|
||||
assert.equal(leftShift(-2, 2), -8);
|
||||
assert.equal(leftShift(3, 3), 24);
|
||||
assert.equal(leftShift(-3, 2), -12);
|
||||
assert.equal(leftShift(-3, 3), -24);
|
||||
});
|
||||
|
||||
it('should left shift booleans by a boolean amount', function () {
|
||||
assert.equal(leftShift(true, true), 2);
|
||||
assert.equal(leftShift(true, false), 1);
|
||||
assert.equal(leftShift(false, true), 0);
|
||||
assert.equal(leftShift(false, false), 0);
|
||||
});
|
||||
|
||||
it('should left shift with a mix of numbers and booleans', function () {
|
||||
assert.equal(leftShift(2, true), 4);
|
||||
assert.equal(leftShift(2, false), 2);
|
||||
assert.equal(leftShift(true, 2), 4);
|
||||
assert.equal(leftShift(false, 2), 0);
|
||||
});
|
||||
|
||||
it('should left shift numbers and null', function () {
|
||||
assert.equal(leftShift(1, null), 1);
|
||||
assert.equal(leftShift(null, 1), 0);
|
||||
});
|
||||
|
||||
/*it('should left shift bignumbers', function () {
|
||||
assert.deepEqual(leftShift(bignumber(2), bignumber(3)), bignumber(16));
|
||||
assert.deepEqual(leftShift(bignumber(500), bignumber(100)), bignumber('633825300114114700748351602688000'));
|
||||
assert.deepEqual(leftShift(bignumber(-1), bignumber(2)), bignumber('-1'));
|
||||
});
|
||||
|
||||
it('should left shift mixed numbers and bignumbers', function () {
|
||||
assert.deepEqual(leftShift(bignumber(2), 3), bignumber(16));
|
||||
assert.deepEqual(leftShift(2, bignumber(3)), bignumber(16));
|
||||
|
||||
approx.equal(leftShift(-1, bignumber(2)), bignumber(-4));
|
||||
approx.equal(leftShift(bignumber(-1), 2), bignumber(-4));
|
||||
});
|
||||
|
||||
it('should left shift mixed booleans and bignumbers', function () {
|
||||
assert.deepEqual(leftShift(true, bignumber(3)), bignumber(8));
|
||||
assert.deepEqual(leftShift(false, bignumber(3)), bignumber(0));
|
||||
assert.deepEqual(leftShift(bignumber(3), false), bignumber(3));
|
||||
assert.deepEqual(leftShift(bignumber(3), true), bignumber(6));
|
||||
});*/
|
||||
|
||||
it('should left shift by values given by strings', function () {
|
||||
assert.equal(leftShift('0', '1000'), 0);
|
||||
assert.equal(leftShift('2', 0), 2);
|
||||
assert.equal(leftShift(2, '3'), 16);
|
||||
assert.equal(leftShift('-2', 2), -8);
|
||||
});
|
||||
|
||||
it('should throw an error if string value is invalid', function () {
|
||||
assert.throws(function () {
|
||||
leftShift('This is not a number!', '12');
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
leftShift('This is still not a number!', 12);
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
leftShift(1, 'kung');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
leftShift('1', 'foo');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
});
|
||||
|
||||
it('should element-wise left shift a matrix', function () {
|
||||
var a = math.matrix([1,2]);
|
||||
var b = leftShift(a, 2);
|
||||
assert.ok(b instanceof math.type.Matrix);
|
||||
assert.deepEqual(b, math.matrix([4,8]));
|
||||
|
||||
a = math.matrix([[1,2],[3,4]]);
|
||||
b = leftShift(a, 2);
|
||||
assert.ok(b instanceof math.type.Matrix);
|
||||
assert.deepEqual(b, math.matrix([[4,8],[12,16]]));
|
||||
});
|
||||
|
||||
it('should element-wise left shift an array', function () {
|
||||
var a = [[1,2],[3,4]];
|
||||
assert.deepEqual(leftShift(a[0], 0), a[0]);
|
||||
assert.deepEqual(leftShift(a[0], 2), [4,8]);
|
||||
assert.deepEqual(leftShift(a, 0), a);
|
||||
assert.deepEqual(leftShift(a, 2), [[4,8],[12,16]]);
|
||||
});
|
||||
|
||||
it('should throw an error if used with wrong number of arguments', function () {
|
||||
assert.throws(function () {leftShift(1)}, error.ArgumentsError);
|
||||
assert.throws(function () {leftShift(1, 2, 3)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
// load math.js
|
||||
var math = require('../index');
|
||||
|
||||
/**
|
||||
* Helper function to output a value in the console. Value will be formatted.
|
||||
* @param {*} value
|
||||
*/
|
||||
function print (value) {
|
||||
var precision = 14;
|
||||
console.log(math.format(value, precision));
|
||||
}
|
||||
|
||||
print(math.not(1)); // returns Number -2
|
||||
|
||||
print(math.not([2, -3, 4])); // returns Array [-3, 2, 5]
|
||||
|
||||
var c = math.unit('-12 m');
|
||||
var d = math.unit('52 mm');
|
||||
print(math.not(c)); // returns Unit 11 m
|
||||
print(math.not(d)); // returns Unit -53 mm
|
||||
@ -1,163 +0,0 @@
|
||||
// test and
|
||||
var assert = require('assert'),
|
||||
approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
//bignumber = math.bignumber,
|
||||
or = math.or;
|
||||
|
||||
describe('or', function () {
|
||||
|
||||
it('should or two numbers', function () {
|
||||
assert.equal(or(53, 131), 183);
|
||||
assert.equal(or(2, 3), 3);
|
||||
assert.equal(or(-2, 3), -1);
|
||||
assert.equal(or(2, -3), -1);
|
||||
assert.equal(or(-5, -3), -1);
|
||||
});
|
||||
|
||||
it('should or booleans', function () {
|
||||
assert.equal(or(true, true), 1);
|
||||
assert.equal(or(true, false), 1);
|
||||
assert.equal(or(false, true), 1);
|
||||
assert.equal(or(false, false), 0);
|
||||
});
|
||||
|
||||
it('should or numbers and null', function () {
|
||||
assert.equal(math.or(null, null), 0);
|
||||
assert.equal(math.or(null, 1), 1);
|
||||
assert.equal(math.or(1, null), 1);
|
||||
});
|
||||
|
||||
it('should or mixed numbers and booleans', function () {
|
||||
assert.equal(or(0, true), 1);
|
||||
assert.equal(or(0, false), 0);
|
||||
assert.equal(or(true, 0), 1);
|
||||
assert.equal(or(false, 0), 0);
|
||||
});
|
||||
|
||||
/*it('should add bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), bignumber(0.2)), bignumber(0.3));
|
||||
assert.deepEqual(add(bignumber('2e5001'), bignumber('3e5000')), bignumber('2.3e5001'));
|
||||
assert.deepEqual(add(bignumber('9999999999999999999'), bignumber('1')), bignumber('1e19'));
|
||||
});
|
||||
|
||||
it('should add mixed numbers and bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), 0.2), bignumber(0.3));
|
||||
assert.deepEqual(add(0.1, bignumber(0.2)), bignumber(0.3));
|
||||
|
||||
approx.equal(add(1/3, bignumber(1)), 1.333333333333333);
|
||||
approx.equal(add(bignumber(1), 1/3), 1.333333333333333);
|
||||
});
|
||||
|
||||
it('should add mixed booleans and bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), true), bignumber(1.1));
|
||||
assert.deepEqual(add(bignumber(0.1), false), bignumber(0.1));
|
||||
assert.deepEqual(add(false, bignumber(0.2)), bignumber(0.2));
|
||||
assert.deepEqual(add(true, bignumber(0.2)), bignumber(1.2));
|
||||
});
|
||||
|
||||
it('should add mixed complex numbers and bignumbers', function () {
|
||||
assert.deepEqual(add(math.complex(3, -4), bignumber(2)), math.complex(5, -4));
|
||||
assert.deepEqual(add(bignumber(2), math.complex(3, -4)), math.complex(5, -4));
|
||||
});*/
|
||||
|
||||
it('should or two measures of the same unit', function () {
|
||||
approx.deepEqual(or(math.unit(33, 'km'), math.unit(100, 'mile')), math.unit(193.774, 'km'));
|
||||
|
||||
var b = math.unit('12 m');
|
||||
var c = math.unit('12 cm');
|
||||
var d = math.unit('52 mm');
|
||||
approx.deepEqual(or(b, d), math.unit(12.02, 'm'));
|
||||
approx.deepEqual(or(c, d), math.unit(12.4, 'cm'));
|
||||
});
|
||||
|
||||
it('should throw an error for two measures of different units', function () {
|
||||
assert.throws(function () {
|
||||
or(math.unit(5, 'km'), math.unit(100, 'gram'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when one of the two units has undefined value', function () {
|
||||
assert.throws(function () {
|
||||
or(math.unit('km'), math.unit('5gram'));
|
||||
}, /Parameter x contains a unit with undefined value/);
|
||||
assert.throws(function () {
|
||||
or(math.unit('5 km'), math.unit('gram'));
|
||||
}, /Parameter y contains a unit with undefined value/);
|
||||
});
|
||||
|
||||
it('should throw an error in case of a unit and non-unit argument', function () {
|
||||
assert.throws(function () {or(math.unit('5cm'), 2)}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {or(math.unit('5cm'), new Date())}, math.error.UnsupportedTypeError);
|
||||
assert.throws(function () {or(new Date(), math.unit('5cm'))}, math.error.UnsupportedTypeError);
|
||||
});
|
||||
|
||||
it('should or two ints, even in string format', function () {
|
||||
assert.equal(or('120', '86'), 126);
|
||||
assert.equal(or('86', 120), 126);
|
||||
assert.equal(or('-120', '-86'), -86);
|
||||
assert.equal(or(-120, '-86'), -86);
|
||||
});
|
||||
|
||||
it('should throw an error in case of NaN argument', function () {
|
||||
assert.throws(function () {
|
||||
or(NaN, 10);
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
or('10', NaN);
|
||||
}, /Parameter y contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
or('This is not a number!', '12');
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
or(12, '');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
});
|
||||
|
||||
it('should or strings and matrices element wise', function () {
|
||||
assert.deepEqual(or('42', ['1', 12, '31']), [43, 46, 63]);
|
||||
assert.deepEqual(or(['1', 12, '31'], '42'), [43, 46, 63]);
|
||||
|
||||
assert.deepEqual(or('42', math.matrix(['1', 12, '31'])), math.matrix([43, 46, 63]));
|
||||
assert.deepEqual(or(math.matrix(['1', 12, '31']), '42'), math.matrix([43, 46, 63]));
|
||||
});
|
||||
|
||||
it('should or matrices correctly', function () {
|
||||
var a2 = math.matrix([[1,2],[3,4]]);
|
||||
var a3 = math.matrix([[5,6],[7,8]]);
|
||||
var a4 = or(a2, a3);
|
||||
assert.ok(a4 instanceof math.type.Matrix);
|
||||
assert.deepEqual(a4.size(), [2,2]);
|
||||
assert.deepEqual(a4.valueOf(), [[5,6],[7,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 or a scalar and a matrix correctly', function () {
|
||||
assert.deepEqual(or(12, math.matrix([3,9])), math.matrix([15,13]));
|
||||
assert.deepEqual(or(math.matrix([3,9]), 12), math.matrix([15,13]));
|
||||
});
|
||||
|
||||
it('should or a scalar and an array correctly', function () {
|
||||
assert.deepEqual(or(12, [3,9]), [15,13]);
|
||||
assert.deepEqual(or([3,9], 12), [15,13]);
|
||||
});
|
||||
|
||||
it('should or a matrix and an array correctly', function () {
|
||||
var a = [6,4,28];
|
||||
var b = math.matrix([13,92,101]);
|
||||
var c = or(a, b);
|
||||
|
||||
assert.ok(c instanceof math.type.Matrix);
|
||||
assert.deepEqual(c, math.matrix([15,92,125]));
|
||||
});
|
||||
|
||||
it('should throw an error in case of invalid number of arguments', function () {
|
||||
assert.throws(function () {or(1)}, error.ArgumentsError);
|
||||
assert.throws(function () {or(1, 2, 3)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
});
|
||||
@ -1,19 +0,0 @@
|
||||
// load math.js
|
||||
var math = require('../index');
|
||||
|
||||
/**
|
||||
* Helper function to output a value in the console. Value will be formatted.
|
||||
* @param {*} value
|
||||
*/
|
||||
function print (value) {
|
||||
var precision = 14;
|
||||
console.log(math.format(value, precision));
|
||||
}
|
||||
|
||||
print(math.or(1, 2)); // returns Number 3
|
||||
print(math.or([1, 2, 3], 4)); // returns Array [5, 6, 7]
|
||||
var c = math.unit('12 cm');
|
||||
var d = math.unit('52 mm');
|
||||
print(c);
|
||||
print(d);
|
||||
print(math.or(c, d)); // returns Unit 124 mm
|
||||
90
test/function/bitwise/rightArithShift.test.js
Normal file
90
test/function/bitwise/rightArithShift.test.js
Normal file
@ -0,0 +1,90 @@
|
||||
// test rightArithShift
|
||||
var assert = require('assert'),
|
||||
//approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
//bignumber = math.bignumber,
|
||||
rightArithShift = math.rightArithShift;
|
||||
|
||||
describe('rightArithShift', function () {
|
||||
|
||||
it('should right arithmetically shift a number by a given amount', function () {
|
||||
assert.equal(rightArithShift(0, 1000), 0);
|
||||
assert.equal(rightArithShift(2, 0), 2);
|
||||
assert.equal(rightArithShift(12, 3), 1);
|
||||
assert.equal(rightArithShift(32, 4), 2);
|
||||
assert.equal(rightArithShift(-1, 1000), -1);
|
||||
assert.equal(rightArithShift(-12, 2), -3);
|
||||
assert.equal(rightArithShift(122, 3), 15);
|
||||
assert.equal(rightArithShift(-13, 2), -4);
|
||||
assert.equal(rightArithShift(-13, 3), -2);
|
||||
});
|
||||
|
||||
it('should right arithmetically shift booleans by a boolean amount', function () {
|
||||
assert.equal(rightArithShift(true, true), 0);
|
||||
assert.equal(rightArithShift(true, false), 1);
|
||||
assert.equal(rightArithShift(false, true), 0);
|
||||
assert.equal(rightArithShift(false, false), 0);
|
||||
});
|
||||
|
||||
it('should right arithmetically shift with a mix of numbers and booleans', function () {
|
||||
assert.equal(rightArithShift(2, true), 1);
|
||||
assert.equal(rightArithShift(2, false), 2);
|
||||
assert.equal(rightArithShift(true, 0), 1);
|
||||
assert.equal(rightArithShift(true, 1), 0);
|
||||
assert.equal(rightArithShift(false, 2), 0);
|
||||
});
|
||||
|
||||
it('should right arithmetically shift numbers and null', function () {
|
||||
assert.equal(rightArithShift(1, null), 1);
|
||||
assert.equal(rightArithShift(null, 1), 0);
|
||||
});
|
||||
|
||||
it('should right arithmetically shift by values given by strings', function () {
|
||||
assert.equal(rightArithShift('0', '1000'), 0);
|
||||
assert.equal(rightArithShift('2', 0), 2);
|
||||
assert.equal(rightArithShift(22, '3'), 2);
|
||||
assert.equal(rightArithShift('-256', 2), -64);
|
||||
});
|
||||
|
||||
it('should throw an error if string value is invalid', function () {
|
||||
assert.throws(function () {
|
||||
rightArithShift('This is not a number!', '12');
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
rightArithShift('This is still not a number!', 12);
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
rightArithShift(1, 'kung');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
rightArithShift('1', 'foo');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
});
|
||||
|
||||
it('should element-wise right arithmetically shift a matrix', function () {
|
||||
var a = math.matrix([4,8]);
|
||||
var b = rightArithShift(a, 2);
|
||||
assert.ok(b instanceof math.type.Matrix);
|
||||
assert.deepEqual(b, math.matrix([1,2]));
|
||||
|
||||
a = math.matrix([[4,8],[12,16]]);
|
||||
b = rightArithShift(a, 2);
|
||||
assert.ok(b instanceof math.type.Matrix);
|
||||
assert.deepEqual(b, math.matrix([[1,2],[3,4]]));
|
||||
});
|
||||
|
||||
it('should element-wise right arithmetically shift an array', function () {
|
||||
var a = [[4,8],[12,16]];
|
||||
assert.deepEqual(rightArithShift(a[0], 0), a[0]);
|
||||
assert.deepEqual(rightArithShift(a[0], 2), [1,2]);
|
||||
assert.deepEqual(rightArithShift(a, 0), a);
|
||||
assert.deepEqual(rightArithShift(a, 2), [[1,2],[3,4]]);
|
||||
});
|
||||
|
||||
it('should throw an error if used with wrong number of arguments', function () {
|
||||
assert.throws(function () {rightArithShift(1)}, error.ArgumentsError);
|
||||
assert.throws(function () {rightArithShift(1, 2, 3)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
});
|
||||
90
test/function/bitwise/rightLogShift.test.js
Normal file
90
test/function/bitwise/rightLogShift.test.js
Normal file
@ -0,0 +1,90 @@
|
||||
// test rightLogShift
|
||||
var assert = require('assert'),
|
||||
//approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
//bignumber = math.bignumber,
|
||||
rightLogShift = math.rightLogShift;
|
||||
|
||||
describe('rightLogShift', function () {
|
||||
|
||||
it('should right logically shift a number by a given amount', function () {
|
||||
assert.equal(rightLogShift(0, 1000), 0);
|
||||
assert.equal(rightLogShift(2, 0), 2);
|
||||
assert.equal(rightLogShift(12, 3), 1);
|
||||
assert.equal(rightLogShift(32, 4), 2);
|
||||
assert.equal(rightLogShift(-1, 1000), 16777215);
|
||||
assert.equal(rightLogShift(-12, 2), 1073741821);
|
||||
assert.equal(rightLogShift(122, 3), 15);
|
||||
assert.equal(rightLogShift(-13, 2), 1073741820);
|
||||
assert.equal(rightLogShift(-13, 3), 536870910);
|
||||
});
|
||||
|
||||
it('should right logically shift booleans by a boolean amount', function () {
|
||||
assert.equal(rightLogShift(true, true), 0);
|
||||
assert.equal(rightLogShift(true, false), 1);
|
||||
assert.equal(rightLogShift(false, true), 0);
|
||||
assert.equal(rightLogShift(false, false), 0);
|
||||
});
|
||||
|
||||
it('should right logically shift with a mix of numbers and booleans', function () {
|
||||
assert.equal(rightLogShift(2, true), 1);
|
||||
assert.equal(rightLogShift(2, false), 2);
|
||||
assert.equal(rightLogShift(true, 0), 1);
|
||||
assert.equal(rightLogShift(true, 1), 0);
|
||||
assert.equal(rightLogShift(false, 2), 0);
|
||||
});
|
||||
|
||||
it('should right logically shift numbers and null', function () {
|
||||
assert.equal(rightLogShift(1, null), 1);
|
||||
assert.equal(rightLogShift(null, 1), 0);
|
||||
});
|
||||
|
||||
it('should right logically shift by values given by strings', function () {
|
||||
assert.equal(rightLogShift('0', '1000'), 0);
|
||||
assert.equal(rightLogShift('2', 0), 2);
|
||||
assert.equal(rightLogShift(22, '3'), 2);
|
||||
assert.equal(rightLogShift('-256', 2), 1073741760);
|
||||
});
|
||||
|
||||
it('should throw an error if string value is invalid', function () {
|
||||
assert.throws(function () {
|
||||
rightLogShift('This is not a number!', '12');
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
rightLogShift('This is still not a number!', 12);
|
||||
}, /Parameter x contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
rightLogShift(1, 'kung');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
assert.throws(function () {
|
||||
rightLogShift('1', 'foo');
|
||||
}, /Parameter y contains a NaN value/);
|
||||
});
|
||||
|
||||
it('should element-wise right logically shift a matrix', function () {
|
||||
var a = math.matrix([4,8]);
|
||||
var b = rightLogShift(a, 2);
|
||||
assert.ok(b instanceof math.type.Matrix);
|
||||
assert.deepEqual(b, math.matrix([1,2]));
|
||||
|
||||
a = math.matrix([[4,8],[12,16]]);
|
||||
b = rightLogShift(a, 2);
|
||||
assert.ok(b instanceof math.type.Matrix);
|
||||
assert.deepEqual(b, math.matrix([[1,2],[3,4]]));
|
||||
});
|
||||
|
||||
it('should element-wise right logically shift an array', function () {
|
||||
var a = [[4,8],[12,16]];
|
||||
assert.deepEqual(rightLogShift(a[0], 0), a[0]);
|
||||
assert.deepEqual(rightLogShift(a[0], 2), [1,2]);
|
||||
assert.deepEqual(rightLogShift(a, 0), a);
|
||||
assert.deepEqual(rightLogShift(a, 2), [[1,2],[3,4]]);
|
||||
});
|
||||
|
||||
it('should throw an error if used with wrong number of arguments', function () {
|
||||
assert.throws(function () {rightLogShift(1)}, error.ArgumentsError);
|
||||
assert.throws(function () {rightLogShift(1, 2, 3)}, error.ArgumentsError);
|
||||
});
|
||||
|
||||
});
|
||||
@ -1,4 +1,4 @@
|
||||
// test and
|
||||
// test xor
|
||||
var assert = require('assert'),
|
||||
approx = require('../../../tools/approx'),
|
||||
error = require('../../../lib/error/index'),
|
||||
@ -38,32 +38,6 @@ describe('xor', function () {
|
||||
assert.equal(xor(false, 1), 1);
|
||||
});
|
||||
|
||||
/*it('should add bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), bignumber(0.2)), bignumber(0.3));
|
||||
assert.deepEqual(add(bignumber('2e5001'), bignumber('3e5000')), bignumber('2.3e5001'));
|
||||
assert.deepEqual(add(bignumber('9999999999999999999'), bignumber('1')), bignumber('1e19'));
|
||||
});
|
||||
|
||||
it('should add mixed numbers and bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), 0.2), bignumber(0.3));
|
||||
assert.deepEqual(add(0.1, bignumber(0.2)), bignumber(0.3));
|
||||
|
||||
approx.equal(add(1/3, bignumber(1)), 1.333333333333333);
|
||||
approx.equal(add(bignumber(1), 1/3), 1.333333333333333);
|
||||
});
|
||||
|
||||
it('should add mixed booleans and bignumbers', function () {
|
||||
assert.deepEqual(add(bignumber(0.1), true), bignumber(1.1));
|
||||
assert.deepEqual(add(bignumber(0.1), false), bignumber(0.1));
|
||||
assert.deepEqual(add(false, bignumber(0.2)), bignumber(0.2));
|
||||
assert.deepEqual(add(true, bignumber(0.2)), bignumber(1.2));
|
||||
});
|
||||
|
||||
it('should add mixed complex numbers and bignumbers', function () {
|
||||
assert.deepEqual(add(math.complex(3, -4), bignumber(2)), math.complex(5, -4));
|
||||
assert.deepEqual(add(bignumber(2), math.complex(3, -4)), math.complex(5, -4));
|
||||
});*/
|
||||
|
||||
it('should xor two measures of the same unit', function () {
|
||||
approx.deepEqual(xor(math.unit(33, 'km'), math.unit(100, 'mile')), math.unit(193.614, 'km'));
|
||||
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
// load math.js
|
||||
var math = require('../index');
|
||||
|
||||
/**
|
||||
* Helper function to output a value in the console. Value will be formatted.
|
||||
* @param {*} value
|
||||
*/
|
||||
function print (value) {
|
||||
var precision = 14;
|
||||
console.log(math.format(value, precision));
|
||||
}
|
||||
|
||||
print(math.xor(1, 2)); // returns Number 3
|
||||
print(math.xor([2, 3, 4], 4)); // returns Array [6, 7, 0]
|
||||
|
||||
var b = math.unit('12 m');
|
||||
var c = math.unit('12 cm');
|
||||
var d = math.unit('52 mm');
|
||||
print(c);
|
||||
print(d);
|
||||
print(math.xor(c, d)); // returns Unit 76 mm
|
||||
print(math.xor(b, d)); // returns Unit 48 mm
|
||||
Loading…
x
Reference in New Issue
Block a user