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
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"core-js": "3.37.0",
"del": "6.1.1",
"dtslint": "4.2.1",
"eslint": "8.57.0",
"eslint": "^8.57.0",
josdejong marked this conversation as resolved.
Show resolved Hide resolved
"eslint-config-prettier": "9.1.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.29.1",
Expand Down
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)])
})
})