mathjs/test/unit-tests/function/set/setDistinct.test.js
dhimansachit 8a9e1beab1
fix: #3602 let function setDistinct keep the original order (#3603)
* fix: #3597 added nullish type definitions (#3601)

* chore: update HISTORY.md and AUTHORS

* docs: describe that `setDistinct` sorts the elements (see #3602)

* [Issue-3602] - fixing setDistinct  order

* Reverting formatting

* fixing formatting

* fixing test cases and logic

---------

Co-authored-by: Ayomide Bamigbade <iamtryve@gmail.com>
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
2025-12-10 15:42:58 +01:00

35 lines
1.4 KiB
JavaScript

// test setDistinct
import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
describe('setDistinct', function () {
it('should return the elements of a set', function () {
assert.deepStrictEqual(math.setDistinct([1, 2]), [1, 2])
assert.deepStrictEqual(math.setDistinct([]), [])
})
it('should return the elements of a set in insertion order', function () {
assert.deepStrictEqual(math.setDistinct([3, 1, 2, 1, 2]), [3, 1, 2])
assert.deepStrictEqual(math.setDistinct(['h', 'e', 'l', 'l', 'o']), ['h', 'e', 'l', 'o'])
})
it('should return the distinct elements of a multiset', function () {
assert.deepStrictEqual(math.setDistinct([1, 1, 2, 2]), [1, 2])
assert.deepStrictEqual(math.setDistinct([1, 2, 1, 2]), [1, 2])
assert.deepStrictEqual(math.setDistinct([1, 2, math.complex(3, 3), 2, math.complex(3, 3)]), [1, 2, math.complex(3, 3)])
})
it('should return the same type of output as the inputs', function () {
assert.strictEqual(math.typeOf(math.setDistinct([1, 2, 3])), 'Array')
assert.strictEqual(
math.typeOf(math.setDistinct(math.matrix([1, 2, 3]))),
'DenseMatrix')
})
it('should throw an error in case of invalid number of arguments', function () {
assert.throws(function () { math.setDistinct() }, /TypeError: Too few arguments/)
assert.throws(function () { math.setDistinct([], []) }, /TypeError: Too many arguments/)
})
})