forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_sparse_test.go
103 lines (85 loc) · 2.28 KB
/
array_sparse_test.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package goja
import "testing"
func TestSparseArraySetLengthWithPropItems(t *testing.T) {
const SCRIPT = `
var a = [1,2,3,4];
a[100000] = 5;
var thrown = false;
Object.defineProperty(a, "2", {value: 42, configurable: false, writable: false});
try {
Object.defineProperty(a, "length", {value: 0, writable: false});
} catch (e) {
thrown = e instanceof TypeError;
}
thrown && a.length === 3;
`
testScript1(SCRIPT, valueTrue, t)
}
func TestSparseArraySwitch(t *testing.T) {
const SCRIPT = `
var a = [];
a[20470] = 5; // switch to sparse
var cutoffIdx = Math.round(20470 - 20470/8);
for (var i = a.length - 1; i >= cutoffIdx; i--) {
a[i] = i;
}
// At this point it will have switched to a normal array
if (a.length != 20471) {
throw new Error("Invalid length: " + a.length);
}
for (var i = 0; i < cutoffIdx; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = cutoffIdx; i < a.length; i++) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
// Now try to expand. Should stay a normal array
a[20471] = 20471;
if (a.length != 20472) {
throw new Error("Invalid length: " + a.length);
}
for (var i = 0; i < cutoffIdx; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = cutoffIdx; i < a.length; i++) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
// Delete enough elements for it to become sparse again.
var cutoffIdx1 = Math.round(20472 - 20472/10);
for (var i = cutoffIdx; i < cutoffIdx1; i++) {
delete a[i];
}
// This should switch it back to sparse.
a[25590] = 25590;
if (a.length != 25591) {
throw new Error("Invalid length: " + a.length);
}
for (var i = 0; i < cutoffIdx1; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = cutoffIdx1; i < 20472; i++) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = 20472; i < 25590; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
if (a[25590] !== 25590) {
throw new Error("Invalid value at 25590: " + a[25590]);
}
`
testScript1(SCRIPT, _undefined, t)
}