forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitXor.test.js
More file actions
161 lines (137 loc) · 6.97 KB
/
bitXor.test.js
File metadata and controls
161 lines (137 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// test bitXor
import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
const matrix = math.matrix
const sparse = math.sparse
const bignumber = math.bignumber
const bitXor = math.bitXor
describe('bitXor', function () {
it('should xor two numbers', function () {
assert.strictEqual(bitXor(53, 131), 182)
assert.strictEqual(bitXor(2, 3), 1)
assert.strictEqual(bitXor(-2, 3), -3)
assert.strictEqual(bitXor(2, -3), -1)
assert.strictEqual(bitXor(-5, -3), 6)
})
it('should xor booleans', function () {
assert.strictEqual(bitXor(true, true), 0)
assert.strictEqual(bitXor(true, false), 1)
assert.strictEqual(bitXor(false, true), 1)
assert.strictEqual(bitXor(false, false), 0)
})
it('should xor mixed numbers and booleans', function () {
assert.strictEqual(bitXor(0, true), 1)
assert.strictEqual(bitXor(0, false), 0)
assert.strictEqual(bitXor(true, 0), 1)
assert.strictEqual(bitXor(false, 0), 0)
assert.strictEqual(bitXor(true, 1), 0)
assert.strictEqual(bitXor(false, 1), 1)
})
it('should bitwise xor bignumbers', function () {
assert.deepStrictEqual(bitXor(bignumber(1), bignumber(2)), bignumber(3))
assert.deepStrictEqual(bitXor(bignumber('-1.0e+31'), bignumber('-1.0e+32')), bignumber('92546795970570634164073698164736'))
assert.deepStrictEqual(bitXor(bignumber('1.0e+31'), bignumber('1.0e+32')), bignumber('92546795970570634164077993132032'))
assert.deepStrictEqual(bitXor(bignumber('-1.0e+31'), bignumber('1.0e+32')), bignumber('-92546795970570634164077993132032'))
assert.deepStrictEqual(bitXor(bignumber('1.0e+31'), bignumber('-1.0e+32')), bignumber('-92546795970570634164073698164736'))
})
it('should bitwise xor mixed numbers and bignumbers', function () {
assert.deepStrictEqual(bitXor(bignumber(1), 2), bignumber(3))
assert.deepStrictEqual(bitXor(1, bignumber(2)), bignumber(3))
assert.deepStrictEqual(bitXor(bignumber(7), 9), bignumber(14))
assert.deepStrictEqual(bitXor(7, bignumber(9)), bignumber(14))
})
it('should bitwise xor mixed booleans and bignumbers', function () {
assert.deepStrictEqual(bitXor(bignumber(1), true), bignumber(0))
assert.deepStrictEqual(bitXor(bignumber(1), false), bignumber(1))
assert.deepStrictEqual(bitXor(true, bignumber(3)), bignumber(2))
assert.deepStrictEqual(bitXor(false, bignumber(3)), bignumber(3))
})
it('should throw an error if used with a unit', function () {
assert.throws(function () { bitXor(math.unit('5cm'), 2) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(2, math.unit('5cm')) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(math.unit('2cm'), math.unit('5cm')) }, /TypeError: Unexpected type of argument/)
})
it('should throw an error if the parameters are not integers', function () {
assert.throws(function () {
bitXor(1.1, 1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(1, 1.1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(1.1, 1.1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(bignumber(1.1), 1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(1, bignumber(1.1))
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(bignumber(1.1), bignumber(1))
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(bignumber(1), bignumber(1.1))
}, /Integers expected in function bitXor/)
})
describe('Array', function () {
it('should bitwise xor array - scalar', function () {
assert.deepStrictEqual(bitXor(12, [3, 9]), [15, 5])
assert.deepStrictEqual(bitXor([3, 9], 12), [15, 5])
})
it('should bitwise xor array - array', function () {
assert.deepStrictEqual(bitXor([[1, 2], [3, 4]], [[5, 6], [7, 8]]), [[4, 4], [4, 12]])
})
it('should bitwise xor array - dense matrix', function () {
assert.deepStrictEqual(bitXor([[1, 2], [3, 4]], matrix([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor array - sparse matrix', function () {
assert.deepStrictEqual(bitXor([[1, 2], [3, 4]], sparse([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
})
describe('DenseMatrix', function () {
it('should bitwise xor dense matrix - scalar', function () {
assert.deepStrictEqual(bitXor(12, matrix([3, 9])), matrix([15, 5]))
assert.deepStrictEqual(bitXor(matrix([3, 9]), 12), matrix([15, 5]))
})
it('should bitwise xor dense matrix - array', function () {
assert.deepStrictEqual(bitXor(matrix([[1, 2], [3, 4]]), [[5, 6], [7, 8]]), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor dense matrix - dense matrix', function () {
assert.deepStrictEqual(bitXor(matrix([[1, 2], [3, 4]]), matrix([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor dense matrix - sparse matrix', function () {
assert.deepStrictEqual(bitXor(matrix([[1, 2], [3, 4]]), sparse([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
})
describe('SparseMatrix', function () {
it('should bitwise xor sparse matrix - scalar', function () {
assert.deepStrictEqual(bitXor(12, sparse([[3, 9], [9, 3]])), matrix([[15, 5], [5, 15]]))
assert.deepStrictEqual(bitXor(sparse([[3, 9], [9, 3]]), 12), matrix([[15, 5], [5, 15]]))
})
it('should bitwise xor sparse matrix - array', function () {
assert.deepStrictEqual(bitXor(sparse([[1, 2], [3, 4]]), [[5, 6], [7, 8]]), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor sparse matrix - dense matrix', function () {
assert.deepStrictEqual(bitXor(sparse([[1, 2], [3, 4]]), matrix([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor sparse matrix - sparse matrix', function () {
assert.deepStrictEqual(bitXor(sparse([[1, 2], [3, 4]]), sparse([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
})
it('should throw an error in case of invalid number of arguments', function () {
assert.throws(function () { bitXor(1) }, /TypeError: Too few arguments/)
assert.throws(function () { bitXor(1, 2, 3) }, /TypeError: Too many arguments/)
})
it('should throw an error in case of invalid type of arguments', function () {
assert.throws(function () { bitXor(2, null) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(new Date(), true) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(true, new Date()) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(true, undefined) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(undefined, true) }, /TypeError: Unexpected type of argument/)
})
it('should LaTeX bitXor', function () {
const expression = math.parse('bitXor(2,3)')
assert.strictEqual(expression.toTex(), '\\left(2\\underline{|}3\\right)')
})
})