Skip to content

Commit 104330d

Browse files
beesaferootlinusg
authored andcommitted
LibJS: Add tests cases for %TypedArray%.prototype.toSorted function
1 parent 4dbb2c2 commit 104330d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const TYPED_ARRAYS = [
2+
Uint8Array,
3+
Uint8ClampedArray,
4+
Uint16Array,
5+
Uint32Array,
6+
Int8Array,
7+
Int16Array,
8+
Int32Array,
9+
Float32Array,
10+
Float64Array,
11+
];
12+
13+
const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array];
14+
15+
test("basic functionality", () => {
16+
TYPED_ARRAYS.forEach(T => {
17+
expect(T.prototype.toSorted).toHaveLength(1);
18+
19+
const typedArray = new T([3, 1, 2]);
20+
let sortedtypedArray = typedArray.toSorted();
21+
expect(sortedtypedArray).not.toBe(typedArray);
22+
expect(sortedtypedArray[0]).toBe(1);
23+
expect(sortedtypedArray[1]).toBe(2);
24+
expect(sortedtypedArray[2]).toBe(3);
25+
26+
sortedtypedArray = typedArray.toSorted(() => 0);
27+
expect(sortedtypedArray).not.toBe(typedArray);
28+
expect(sortedtypedArray[0]).toBe(3);
29+
expect(sortedtypedArray[1]).toBe(1);
30+
expect(sortedtypedArray[2]).toBe(2);
31+
});
32+
33+
BIGINT_TYPED_ARRAYS.forEach(T => {
34+
expect(T.prototype.toSorted).toHaveLength(1);
35+
36+
const typedArray = new T([3n, 1n, 2n]);
37+
38+
let sortedtypedArray = typedArray.toSorted();
39+
expect(sortedtypedArray).not.toBe(typedArray);
40+
expect(sortedtypedArray[0]).toBe(1n);
41+
expect(sortedtypedArray[1]).toBe(2n);
42+
expect(sortedtypedArray[2]).toBe(3n);
43+
44+
sortedtypedArray = typedArray.toSorted(() => 0);
45+
expect(sortedtypedArray).not.toBe(typedArray);
46+
expect(sortedtypedArray[0]).toBe(3n);
47+
expect(sortedtypedArray[1]).toBe(1n);
48+
expect(sortedtypedArray[2]).toBe(2n);
49+
});
50+
});
51+
52+
describe("errors", () => {
53+
test("null or undefined this value", () => {
54+
TYPED_ARRAYS.forEach(T => {
55+
expect(() => {
56+
T.prototype.toSorted.call();
57+
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
58+
59+
expect(() => {
60+
T.prototype.toSorted.call(undefined);
61+
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
62+
63+
expect(() => {
64+
T.prototype.toSorted.call(null);
65+
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
66+
});
67+
68+
BIGINT_TYPED_ARRAYS.forEach(T => {});
69+
});
70+
71+
test("invalid compare function", () => {
72+
TYPED_ARRAYS.forEach(T => {
73+
expect(() => {
74+
new T([]).toSorted("foo");
75+
}).toThrowWithMessage(TypeError, "foo is not a function");
76+
});
77+
78+
BIGINT_TYPED_ARRAYS.forEach(T => {
79+
expect(() => {
80+
new T([]).toSorted("foo");
81+
}).toThrowWithMessage(TypeError, "foo is not a function");
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)