-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
TypedArray.prototype.with.js
59 lines (54 loc) 路 1.73 KB
/
TypedArray.prototype.with.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const TYPED_ARRAYS = [
Uint8Array,
Uint8ClampedArray,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
];
const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array];
describe("normal behavior", () => {
test("length is 2", () => {
TYPED_ARRAYS.forEach(T => {
expect(T.prototype.with).toHaveLength(2);
});
BIGINT_TYPED_ARRAYS.forEach(T => {
expect(T.prototype.with).toHaveLength(2);
});
});
test("basic functionality", () => {
TYPED_ARRAYS.forEach(T => {
const a = new T([1, 2, 3, 4, 5]);
const values = [
[0, 10, [10, 2, 3, 4, 5]],
[-5, 10, [10, 2, 3, 4, 5]],
[4, 10, [1, 2, 3, 4, 10]],
[-1, 10, [1, 2, 3, 4, 10]],
];
for (const [index, value, expected] of values) {
const b = a.with(index, value);
expect(a).not.toBe(b);
expect(a).toEqual([1, 2, 3, 4, 5]);
expect(b).toEqual(expected);
}
});
BIGINT_TYPED_ARRAYS.forEach(T => {
const a = new T([1n, 2n, 3n, 4n, 5n]);
const values = [
[0, 10n, [10n, 2n, 3n, 4n, 5n]],
[-5, 10n, [10n, 2n, 3n, 4n, 5n]],
[4, 10n, [1n, 2n, 3n, 4n, 10n]],
[-1, 10n, [1n, 2n, 3n, 4n, 10n]],
];
for (const [index, value, expected] of values) {
const b = a.with(index, value);
expect(a).not.toBe(b);
expect(a).toEqual([1n, 2n, 3n, 4n, 5n]);
expect(b).toEqual(expected);
}
});
});
});