]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/stress/map-constructor.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / map-constructor.js
1 // Map constructor behaviors.
2
3 if (typeof Map !== 'function')
4 throw "Error: bad value" + typeof Map;
5
6 function testCallTypeError(item) {
7 var error = null;
8 try {
9 var map = Map(item);
10 } catch (e) {
11 error = e;
12 }
13 if (!error)
14 throw "Error: error not thrown";
15 if (String(error) !== "TypeError: Map cannot be called as a function")
16 throw "Error: bad error " + String(error);
17 }
18
19 var pass = [
20 [ null, 0 ],
21 [ undefined, 0 ],
22 [ [], 0 ],
23 [ new Set(), 0],
24 [ new Map(), 0],
25 [ "", 0],
26
27 [
28 [
29 [0, 1],
30 [1, 2],
31 [1, 3],
32 ],
33 2
34 ],
35
36 [
37 [
38 [1, 1],
39 [1, 2],
40 [1, 3],
41 ],
42 1
43 ],
44
45 [
46 new Map([
47 { 0: 'C', 1: 'O' },
48 { 0: 'C', 1: 'K' },
49 { 0: 'V', 1: 'K' },
50 ]),
51 2
52 ],
53
54 [
55 new Map([
56 [0, 1],
57 [1, 2],
58 [1, 3],
59 ]),
60 2
61 ],
62
63 [
64 new Map([
65 [1, 1],
66 [1, 2],
67 [1, 3],
68 ]),
69 1
70 ],
71
72 [
73 new Map([
74 { 0: 'C', 1: 'O' },
75 { 0: 'C', 1: 'K' },
76 { 0: 'V', 1: 'K' },
77 ]),
78 2
79 ],
80 ];
81
82 for (var pair of pass) {
83 var map = new Map(pair[0]);
84 if (map.size !== pair[1])
85 throw "Error: bad map size " + map.size;
86 testCallTypeError(pair[0]);
87 }
88
89 function testTypeError(item) {
90 var error = null;
91 try {
92 var map = new Map(item);
93 } catch (e) {
94 error = e;
95 }
96 if (!error)
97 throw "Error: error not thrown";
98 if (String(error) !== "TypeError: Type error")
99 throw "Error: bad error " + String(error);
100 }
101
102 var nonIterable = [
103 42,
104 Symbol("Cappuccino"),
105 true,
106 false,
107 {},
108 new Date(),
109 new Error(),
110 Object(Symbol("Matcha")),
111 (function () { }),
112 ];
113
114 for (var item of nonIterable) {
115 testTypeError(item);
116 testCallTypeError(item);
117 }
118
119 var notContainNextItem = [
120 "Cocoa",
121 [0, 1, 2, 3, 4],
122 [0, 0, 0, 1, 0],
123 ["A", "B", "A"],
124 new String("cocoa"),
125 new String("Cocoa"),
126 new Set([0,1,2,3,4]),
127 new Set([1,1,1,1]),
128 ];
129
130 for (var item of notContainNextItem) {
131 testTypeError(item);
132 testCallTypeError(item);
133 }