MCPcopy Create free account
hub / github.com/dop251/goja / TestSparseArraySwitch

Function TestSparseArraySwitch

array_sparse_test.go:25–130  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

23}
24
25func TestSparseArraySwitch(t *testing.T) {
26 vm := New()
27 _, err := vm.RunString(`
28 var a = [];
29 a[20470] = 5; // switch to sparse`)
30 if err != nil {
31 t.Fatal(err)
32 }
33 a := vm.Get("a").(*Object)
34 if _, ok := a.self.(*sparseArrayObject); !ok {
35 t.Fatal("1: array is not sparse")
36 }
37 _, err = vm.RunString(`
38 var cutoffIdx = Math.round(20470 - 20470/8);
39 for (var i = a.length - 1; i >= cutoffIdx; i--) {
40 a[i] = i;
41 }
42
43 // At this point it will have switched to a normal array
44 if (a.length != 20471) {
45 throw new Error("Invalid length: " + a.length);
46 }
47
48 for (var i = 0; i < cutoffIdx; i++) {
49 if (a[i] !== undefined) {
50 throw new Error("Invalid value at " + i + ": " + a[i]);
51 }
52 }
53
54 for (var i = cutoffIdx; i < a.length; i++) {
55 if (a[i] !== i) {
56 throw new Error("Invalid value at " + i + ": " + a[i]);
57 }
58 }`)
59 if err != nil {
60 t.Fatal(err)
61 }
62 if _, ok := a.self.(*arrayObject); !ok {
63 t.Fatal("2: array is not normal")
64 }
65 _, err = vm.RunString(`
66 // Now try to expand. Should stay a normal array
67 a[20471] = 20471;
68 if (a.length != 20472) {
69 throw new Error("Invalid length: " + a.length);
70 }
71
72 for (var i = 0; i < cutoffIdx; i++) {
73 if (a[i] !== undefined) {
74 throw new Error("Invalid value at " + i + ": " + a[i]);
75 }
76 }
77
78 for (var i = cutoffIdx; i < a.length; i++) {
79 if (a[i] !== i) {
80 throw new Error("Invalid value at " + i + ": " + a[i]);
81 }
82 }`)

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
RunStringMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…