Josef Wittmann bf0eedfdd5
Implement count based on size (#2092)
* Implement count based on size

Closes #2085

* Improve count implementation

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
2021-01-31 11:02:26 +01:00

41 lines
1.4 KiB
JavaScript

import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
const { count, matrix, sparse } = math
describe('count', function () {
it('should count arrays', function () {
assert.strictEqual(count([[1, 2, 3], [4, 5, 6]]), 6)
assert.strictEqual(count([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), 8)
assert.strictEqual(count([]), 0)
assert.strictEqual(count([[]]), 0)
})
it('should count dense matrices', function () {
assert.strictEqual(count(matrix([[1, 2, 3], [4, 5, 6]])), 6)
assert.strictEqual(count(matrix([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])), 8)
assert.strictEqual(count(matrix([])), 0)
assert.strictEqual(count(matrix([[]])), 0)
})
it('should count sparse matrices', function () {
assert.strictEqual(count(sparse([[1, 2, 3], [4, 5, 6]])), 6)
assert.strictEqual(count(sparse([])), 0)
assert.strictEqual(count(sparse([[]])), 0)
})
it('should count strings', function () {
assert.strictEqual(count('123456'), 6)
assert.strictEqual(count(''), 0)
})
it('should throw an error if called with an invalid number of arguments', function () {
assert.throws(function () { count() }, /TypeError: Too few arguments/)
assert.throws(function () { count(1, 2) }, /TypeError: Too many arguments/)
})
it('should throw an error if called with invalid type of arguments', function () {
assert.throws(function () { count(new Date()) }, /TypeError: Unexpected type of argument/)
})
})