Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some test cases to deepForEach #3211

Merged
merged 5 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion test/unit-tests/type/matrix/utils/deepForEach.test.js
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
// TODO: test deepForEach
import assert from 'assert'
import math from '../../../../../src/defaultInstance.js'
import { deepForEach } from '../../../../../src/utils/collection.js'

const DenseMatrix = math.DenseMatrix

describe('deepForEach', function () {
it('should iterate over all elements in a simple array', function () {
const array = [1, 2, 3]
const result = []
deepForEach(array, value => result.push(value))
assert.deepStrictEqual(result, [1, 2, 3])
})

it('should iterate over all elements in a nested array', function () {
const array = [[1, 2], [3, [4, 5]]]
const result = []
deepForEach(array, value => result.push(value))
assert.deepStrictEqual(result, [1, 2, 3, 4, 5])
})

it('should iterate over all elements in a mixed type array', function () {
const array = [1, 'two', [3, null, undefined, true]]
const result = []
deepForEach(array, value => result.push(value))
assert.deepStrictEqual(result, [1, 'two', 3, null, undefined, true])
})

it('should handle an empty array', function () {
const array = []
const result = []
deepForEach(array, value => result.push(value))
assert.deepStrictEqual(result, [])
})

it('should handle an array with empty nested arrays', function () {
const array = [[], [1, []], [2, [3, []]]]
const result = []
deepForEach(array, value => result.push(value))
assert.deepStrictEqual(result, [1, 2, 3])
})

it('should iterate over all elements in a DenseMatrix', function () {
const matrix = new DenseMatrix([[1, 2], [3, 4]])
const result = []
deepForEach(matrix, value => result.push(value))
assert.deepStrictEqual(result, [1, 2, 3, 4])
})

it('should call the callback with each element of a matrix after converting to array', function () {
const matrix = math.matrix([[1, 2], [3, 4]])
const result = []
deepForEach(matrix, value => result.push(value))
assert.deepStrictEqual(result, [1, 2, 3, 4])
})

it('should work with arrays containing complex numbers', function () {
const array = [math.complex(2, 3), [math.complex(4, 5)]]
const result = []
deepForEach(array, value => result.push(value))
assert.deepStrictEqual(result, [math.complex(2, 3), math.complex(4, 5)])
})
})