Skip to content

Commit 128411e

Browse files
committed
test: comprehensive tests for new features and minor bugfixes they revealed
1 parent 91057df commit 128411e

File tree

16 files changed

+182
-9
lines changed

16 files changed

+182
-9
lines changed

src/function/arithmetic/subtract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const createSubtract = /* #__PURE__ */ factory(name, dependencies, ({ typ
6666
return r.createRange({
6767
start: self(r.start, p.start),
6868
length: r.length,
69-
step: self(r.ste, p.step)
69+
step: self(r.step, p.step)
7070
})
7171
}),
7272
'Range, Matrix': typed.referToSelf(

src/type/matrix/Matrix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const createMatrixClass = /* #__PURE__ */ factory(name, dependencies, ()
144144
fields[key] = val
145145
}
146146
if (fields.start === '') fields.start = index.shiftPosition
147-
if (fields.end === '') fields.end = size[dim] + index.shiftPosition - 1
147+
if (fields.end === '') fields.end = size[dim] + index.shiftPosition
148148
fields.start -= index.shiftPosition
149149
fields.end -= index.shiftPosition
150150
const attributes = index.includeEnd

src/type/matrix/Range.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ export const createRangeClass = /* #__PURE__ */ factory(name, dependencies, ({
209209
// if the span is computed in bigints, we want a bigint increment
210210
let bigi = isBigInt(span)
211211
bigi ||= isMatrix(span) && span.datatype() === 'bigint'
212-
bigi ||= Array.isArray(span) && getArrayDataType(span) === 'bigint'
212+
bigi ||= Array.isArray(span) &&
213+
getArrayDataType(span, typeOf) === 'bigint'
213214
const denominator = bigi ? BigInt(segments) : segments
214215
attributes.step = divide(span, denominator)
215216
}
@@ -428,7 +429,7 @@ export const createRangeClass = /* #__PURE__ */ factory(name, dependencies, ({
428429
*
429430
* @return {string} The storage format.
430431
*/
431-
Matrix.prototype.storage = function () {
432+
Range.prototype.storage = function () {
432433
return 'range' // neither 'sparse' or 'dense' seemed fair
433434
}
434435

src/type/matrix/function/matrix.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const name = 'matrix'
55
const dependencies = ['typed', 'Matrix', 'DenseMatrix', 'SparseMatrix', 'Range']
66

77
export const createMatrix = /* #__PURE__ */ factory(name, dependencies, ({ typed, Matrix, DenseMatrix, SparseMatrix, Range }) => {
8+
const rangeCreator = new Range() // dummy to use to get access to .create()
89
/**
910
* Create a Matrix. The function creates a new `math.Matrix` object from
1011
* an `Array`. A Matrix has utility functions to manipulate the data in the
@@ -88,7 +89,7 @@ export const createMatrix = /* #__PURE__ */ factory(name, dependencies, ({ typed
8889
start: data.start, step: data.step, length: data.length
8990
})
9091
}
91-
return new Range(data)
92+
return rangeCreator.create(data)
9293
}
9394

9495
throw new TypeError('Unknown matrix type ' + JSON.stringify(format) + '.')

test/unit-tests/function/arithmetic/add.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ describe('add', function () {
248248
})
249249
})
250250

251+
describe('Range', function () {
252+
it('should keep the result a Range when possible', function () {
253+
assert.deepStrictEqual(add(math.range(2, 5), 7), new math.Range(9, 12))
254+
assert.deepStrictEqual(
255+
add(-3, math.range(6, 11, 2)), new math.Range(3, 8, 2))
256+
assert.deepStrictEqual(
257+
add(math.range(0.5, 4.5), math.range(0, 2.1, 0.5)),
258+
new math.Range({ start: 0.5, step: 1.5, length: 4 }))
259+
assert.deepStrictEqual(
260+
add(math.range(2, 5), [1, 3, 6]), math.matrix([3, 6, 10]))
261+
})
262+
})
263+
251264
describe('multiple arguments', function () {
252265
it('should add more than two arguments', function () {
253266
assert.deepStrictEqual(add(2, 3, 4), 9)

test/unit-tests/function/arithmetic/divide.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ describe('divide', function () {
210210
assert.throws(function () { divide(a, [[1]]) })
211211
})
212212

213+
describe('Range', function () {
214+
it('should keep the result a Range when possible', function () {
215+
assert.deepStrictEqual(
216+
divide(math.range(2, 5), 7),
217+
new math.Range({ start: 2 / 7, step: 1 / 7, length: 3 }))
218+
})
219+
})
220+
213221
/*
214222
// These are supported now --ericman314
215223
it('should throw an error if dividing a number by a unit', function() {

test/unit-tests/function/arithmetic/multiply.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,18 @@ describe('multiply', function () {
899899
})
900900
})
901901

902+
describe('Range', function () {
903+
it('should keep the result a Range when possible', function () {
904+
assert.deepStrictEqual(
905+
multiply(math.range(2, 5), 7),
906+
new math.Range({ start: 14, step: 7, length: 3 }))
907+
const frac = math.fraction
908+
assert.deepStrictEqual(
909+
multiply(frac(1, 3), math.range(2n, 5n)),
910+
new math.Range(frac(2, 3), frac(5, 3), frac(1, 3)))
911+
})
912+
})
913+
902914
describe('multiple arguments', function () {
903915
it('should multiply more than two arguments', function () {
904916
assert.deepStrictEqual(multiply(2, 3, 4), 24)

test/unit-tests/function/arithmetic/subtract.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,20 @@ describe('subtract', function () {
245245
})
246246
})
247247

248+
describe('Range', function () {
249+
it('should keep the result a Range when possible', function () {
250+
assert.deepStrictEqual(
251+
subtract(math.range(2, 5), 7), new math.Range(-5, -2))
252+
assert.deepStrictEqual(
253+
subtract(-3, math.range(6, 11, 2)), new math.Range(-9, -14, -2))
254+
assert.deepStrictEqual(
255+
subtract(math.range(0.5, 4.5), math.range(0, 2.1, 0.5)),
256+
new math.Range({ start: 0.5, step: 0.5, length: 4 }))
257+
assert.deepStrictEqual(
258+
subtract(math.range(2, 5), [1, 3, 6]), math.matrix([1, 0, -2]))
259+
})
260+
})
261+
248262
it('should throw an error in case of invalid number of arguments', function () {
249263
assert.throws(function () { subtract(1) }, /TypeError: Too few arguments/)
250264
assert.throws(function () { subtract(1, 2, 3) }, /TypeError: Too many arguments/)

test/unit-tests/function/matrix/concat.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ describe('concat', function () {
7979
])
8080
})
8181

82+
it('should keep Ranges if possible', function () {
83+
assert.deepStrictEqual(
84+
math.concat(math.range(2, 10), math.range(10, 20)), new math.Range(2, 20))
85+
})
86+
8287
it('should concatenate strings', function () {
8388
assert.strictEqual(math.concat('a', 'b'), 'ab')
8489
assert.strictEqual(math.concat('a', 'b', 'c'), 'abc')

test/unit-tests/function/matrix/range.test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('range', function () {
2323
})
2424

2525
it('should create a range start:1:end if called with 2 numbers', function () {
26+
assert.deepStrictEqual(range(3, 6), new Range(3, 6))
2627
assert.deepStrictEqual(range(3, 6).valueOf(), [3, 4, 5])
2728
assert.deepStrictEqual(range(1, 6).valueOf(), [1, 2, 3, 4, 5])
2829
assert.deepStrictEqual(range(1, 6.1).valueOf(), [1, 2, 3, 4, 5, 6])
@@ -31,6 +32,7 @@ describe('range', function () {
3132
})
3233

3334
it('should create a range start:step:end if called with 3 numbers', function () {
35+
assert.deepStrictEqual(range(0, 10, 2), new Range(0, 10, 2))
3436
assert.deepStrictEqual(range(0, 10, 2).valueOf(), [0, 2, 4, 6, 8])
3537
assert.deepStrictEqual(range(5, 0, -1).valueOf(), [5, 4, 3, 2, 1])
3638
assert.deepStrictEqual(range(2, -4, -2).valueOf(), [2, 0, -2])
@@ -78,13 +80,15 @@ describe('range', function () {
7880
})
7981

8082
it('should create a range with bigints', function () {
83+
assert.deepStrictEqual(range(1n, 3n), new Range(1n, 3n))
8184
assert.deepStrictEqual(range(1n, 3n).valueOf(), [1n, 2n])
8285
assert.deepStrictEqual(range(3n, 1n, -1n).valueOf(), [3n, 2n])
8386
assert.deepStrictEqual(range(1n, 3n, true).valueOf(), [1n, 2n, 3n])
8487
assert.deepStrictEqual(range(3n, 1n, -1n, true).valueOf(), [3n, 2n, 1n])
8588
})
8689

8790
it('should handle mixed numbers and bigints appropriately', function () {
91+
assert.deepStrictEqual(range(1n, 3), new Range(1n, 3n))
8892
assert.deepStrictEqual(range(1n, 3).valueOf(), [1n, 2n])
8993
assert.deepStrictEqual(range(3, 1n, -1n).valueOf(), [3, 2])
9094
assert.deepStrictEqual(range(3n, 1, -1).valueOf(), [3, 2])
@@ -96,8 +100,9 @@ describe('range', function () {
96100
})
97101

98102
it('should create a range with bignumbers', function () {
99-
assert.deepStrictEqual(
100-
range(bignumber(1), bignumber(3)).valueOf(), [bignumber(1), bignumber(2)])
103+
const bigRange = range(bignumber(1), bignumber(3))
104+
assert.deepStrictEqual(bigRange, new Range(bignumber(1), bignumber(3)))
105+
assert.deepStrictEqual(bigRange.valueOf(), [bignumber(1), bignumber(2)])
101106
assert.deepStrictEqual(
102107
range(bignumber(3), bignumber(1), bignumber(-1)).valueOf(),
103108
[bignumber(3), bignumber(2)])
@@ -220,9 +225,10 @@ describe('range', function () {
220225

221226
it('should handle Fractions', function () {
222227
const frac = math.fraction
228+
const fRange = range(frac(1, 3), frac(10, 3))
229+
assert.deepStrictEqual(fRange, new Range(frac(1, 3), frac(10, 3)))
223230
assert.deepStrictEqual(
224-
range(frac(1, 3), frac(10, 3)).valueOf(),
225-
[frac(1, 3), frac(4, 3), frac(7, 3)])
231+
fRange.valueOf(), [frac(1, 3), frac(4, 3), frac(7, 3)])
226232
assert.deepStrictEqual(
227233
range(frac(1, 3), frac(7, 3), true).valueOf(),
228234
[frac(1, 3), frac(4, 3), frac(7, 3)])

0 commit comments

Comments
 (0)