summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/typedarray/typed-array-change-by-copy.js
blob: 4aa12a615d9b60878e57d3990b284ce6fe445da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// |jit-test|

load(libdir + 'array-compare.js');
load(libdir + "asserts.js");

let typedArray123 = new Uint8Array([1, 2, 3]);
let typedArray12345 = new Uint8Array([1, 2, 3, 4, 5]);
let typedArray = new Uint8Array([1, 2, 3]);
let typedArray2 = new Uint8Array([3, 2, 1]);

let a_with = typedArray.with(1, 42);
assertEq(arraysEqual(typedArray, new Uint8Array([1, 2, 3])), true);
assertEq(arraysEqual(a_with, new Uint8Array([1, 42, 3])), true);

let tarray1 = new Uint8Array([42, 2, 3]);
let tarray2 = new Uint8Array([1, 2, 42]);

assertEq(arraysEqual(typedArray.with(-0, 42), tarray1), true);

/* null => 0 */
assertEq(arraysEqual(typedArray.with(null, 42), tarray1), true);
/* [] => 0 */
assertEq(arraysEqual(typedArray.with([], 42), tarray1), true);

assertEq(arraysEqual(typedArray.with("2", 42), tarray2), true);

/* Non-numeric indices => 0 */
assertEq(arraysEqual(typedArray.with("monkeys", 42), tarray1), true);
assertEq(arraysEqual(typedArray.with(undefined, 42), tarray1), true);
assertEq(arraysEqual(typedArray.with(function() {}, 42), tarray1), true);

assertThrowsInstanceOf(() => typedArray.with(3, 42), RangeError);
assertThrowsInstanceOf(() => typedArray.with(5, 42), RangeError);
assertThrowsInstanceOf(() => typedArray.with(-10, 42), RangeError);
assertThrowsInstanceOf(() => typedArray.with(Infinity, 42), RangeError);

let reversedIntArray = typedArray.toReversed();
assertEq(arraysEqual(typedArray, typedArray123), true);
assertEq(arraysEqual(reversedIntArray, typedArray2), true);

let sortedIntArray = typedArray2.toSorted();
assertEq(arraysEqual(typedArray2, new Uint8Array([3, 2, 1])), true);
assertEq(arraysEqual(sortedIntArray, typedArray), true);