forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_map_test.go
101 lines (84 loc) · 1.75 KB
/
builtin_map_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
package goja
import (
"hash/maphash"
"testing"
)
func TestMapEvilIterator(t *testing.T) {
const SCRIPT = `
'use strict';
var o = {};
function Iter(value) {
this.value = value;
this.idx = 0;
}
Iter.prototype.next = function() {
var idx = this.idx;
if (idx === 0) {
this.idx++;
return this.value;
}
return {done: true};
}
o[Symbol.iterator] = function() {
return new Iter({});
}
assert.throws(TypeError, function() {
new Map(o);
});
o[Symbol.iterator] = function() {
return new Iter({value: []});
}
function t(prefix) {
var m = new Map(o);
assert.sameValue(1, m.size, prefix+": m.size");
assert.sameValue(true, m.has(undefined), prefix+": m.has(undefined)");
assert.sameValue(undefined, m.get(undefined), prefix+": m.get(undefined)");
}
t("standard adder");
var count = 0;
var origSet = Map.prototype.set;
Map.prototype.set = function() {
count++;
origSet.apply(this, arguments);
}
t("custom adder");
assert.sameValue(1, count, "count");
undefined;
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}
func BenchmarkMapDelete(b *testing.B) {
var key1 Value = asciiString("a")
var key2 Value = asciiString("b")
one := intToValue(1)
two := intToValue(2)
for i := 0; i < b.N; i++ {
m := newOrderedMap(&maphash.Hash{})
m.set(key1, one)
m.set(key2, two)
if !m.remove(key1) {
b.Fatal("remove() returned false")
}
}
}
func BenchmarkMapDeleteJS(b *testing.B) {
prg, err := Compile("test.js", `
var m = new Map([['a',1], ['b', 2]]);
var result = m.delete('a');
if (!result || m.size !== 1) {
throw new Error("Fail!");
}
`,
false)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
vm := New()
_, err := vm.RunProgram(prg)
if err != nil {
b.Fatal(err)
}
}
}