Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / script-tests / switch-behaviour.js
blobc10874dbb453de05c7b9c85c1e828194629c2f10
1 description('This test covers the correctness and behaviour of switch statements.');
3 // To make sure that there are no jump table indexing problems each of these functions
4 // has multiple no-op switch statements to force not trivial jump table offsets for the
5 // primary switch being tested.
6 function characterSwitch(scrutinee) {
7 switch(null){
8 case '_':
10 switch(scrutinee) {
11 case '\0':
12 return '\0';
13 case 'A':
14 return 'A';
15 case 'a':
16 return 'a';
17 case 'A':
18 return 'Should not hit second \'A\'';
19 case '1':
20 return '1';
21 default:
22 return 'default';
23 case 'B':
24 return 'B';
25 switch(null){
26 case '1':
29 switch(null){
30 case '_':
32 return 'Did not reach default clause';
35 function sparseCharacterSwitch(scrutinee) {
36 switch(null){
37 case '1':
39 switch(null){
40 case '12':
42 switch(scrutinee) {
43 case '\0':
44 return '\0';
45 case 'A':
46 return 'A';
47 case 'a':
48 return 'a';
49 case 'A':
50 return 'Should not hit second \'A\'';
51 case '1':
52 return '1';
53 default:
54 return 'default';
55 case 'B':
56 return 'B';
57 case '\uffff':
58 return '\uffff';
59 switch(null){
60 case '1':
62 switch(null){
63 case '12':
66 return 'Did not reach default clause';
67 switch(null){
68 case '1':
70 switch(null){
71 case '12':
75 function stringSwitch(scrutinee) {
76 switch(null){
77 case '12':
79 switch(scrutinee) {
80 case '\0':
81 return '\0';
82 case 'A':
83 return 'A';
84 case 'a':
85 return 'a';
86 case 'A':
87 return 'Should not hit second \'A\'';
88 case '1':
89 return '1';
90 case '-1':
91 return '-1';
92 case '[object Object]':
93 return '[object Object]';
94 case 'some string':
95 return 'some string';
96 default:
97 return 'default';
98 case 'B':
99 return 'B';
100 case '\uffff':
101 return '\uffff'
102 switch(null){
103 case '12':
106 return 'Did not reach default clause';
107 switch(null){
108 case '12':
112 function numberSwitch(scrutinee) {
113 switch(null){
114 case 1:
116 switch(scrutinee) {
117 case 0:
118 return 0;
119 case 1:
120 return 1;
121 case 1:
122 return 'Should not hit second 1';
123 default:
124 return 'default';
125 case -1:
126 return -1;
127 switch(null){
128 case 1:
131 return 'Did not reach default clause';
132 switch(null){
133 case 1:
137 function sparseNumberSwitch(scrutinee) {
138 switch(null){
139 case 1:
141 switch(scrutinee) {
142 case 0:
143 return 0;
144 case 1:
145 return 1;
146 case 1:
147 return 'Should not hit second 1';
148 default:
149 return 'default';
150 case -1:
151 return -1;
152 case -1000000000:
153 return -1000000000;
154 case 1000000000:
155 return 1000000000;
156 switch(null){
157 case 1:
160 return 'Did not reach default clause';
161 switch(null){
162 case 1:
166 function generalSwitch(scrutinee) {
167 switch(null){
168 case 1:
170 switch(null){
171 case '1':
173 switch(null){
174 case '12':
176 switch(scrutinee) {
177 case 0:
178 return 0;
179 case 1:
180 return 1;
181 case 1:
182 return 'Should not hit second 1';
183 default:
184 return 'default';
185 case -1:
186 return -1;
187 case -1000000000:
188 return -1000000000;
189 case 1000000000:
190 return 1000000000;
191 case '\0':
192 return '\0';
193 case 'A':
194 return 'A';
195 case 'a':
196 return 'a';
197 case 'A':
198 return 'Should not hit second \'A\'';
199 case '1':
200 return '1';
201 case '-1':
202 return '-1';
203 case '[object Object]':
204 return '[object Object]';
205 case 'some string':
206 return 'some string';
207 case 'B':
208 return 'B';
209 case '\uffff':
210 return '\uffff'
211 switch(null){
212 case 1:
214 switch(null){
215 case '1':
217 switch(null){
218 case '12':
221 return 'Did not reach default clause';
222 switch(null){
223 case 1:
225 switch(null){
226 case '1':
228 switch(null){
229 case '12':
233 // Character switch
234 var emptyString1 = "";
235 var emptyString2 = "";
236 shouldBe("characterSwitch('A' + emptyString1)", '"A"');
237 shouldBe("characterSwitch('A' + emptyString1 + emptyString2)", '"A"');
238 shouldBe("characterSwitch(emptyString1 + emptyString2)", '"default"');
240 shouldBe("characterSwitch('\0')", '"\0"');
241 shouldBe("characterSwitch('A')", '"A"');
242 shouldBe("characterSwitch('a')", '"a"');
243 shouldBe("characterSwitch('1')", '"1"');
244 shouldBe("characterSwitch('-1')", '"default"');
245 shouldBe("characterSwitch('B')", '"B"');
246 shouldBe("characterSwitch('\uffff')", '"default"');
247 shouldBe("characterSwitch({toString: function(){return 'B'}})", '"default"');
248 shouldBe("characterSwitch(0)", '"default"');
249 shouldBe("characterSwitch(-0)", '"default"');
250 shouldBe("characterSwitch(1)", '"default"');
251 shouldBe("characterSwitch(1.1)", '"default"');
252 shouldBe("characterSwitch(-1)", '"default"');
253 shouldBe("characterSwitch(-1000000000)", '"default"');
254 shouldBe("characterSwitch(1000000000)", '"default"');
255 shouldBe("characterSwitch({})", '"default"');
257 // Sparse character switch
258 shouldBe("sparseCharacterSwitch('\0')", '"\0"');
259 shouldBe("sparseCharacterSwitch('A')", '"A"');
260 shouldBe("sparseCharacterSwitch('a')", '"a"');
261 shouldBe("sparseCharacterSwitch('1')", '"1"');
262 shouldBe("sparseCharacterSwitch('-1')", '"default"');
263 shouldBe("sparseCharacterSwitch('B')", '"B"');
264 shouldBe("sparseCharacterSwitch('\uffff')", '"\uffff"');
265 shouldBe("sparseCharacterSwitch({toString: function(){return 'B'}})", '"default"');
266 shouldBe("sparseCharacterSwitch(0)", '"default"');
267 shouldBe("sparseCharacterSwitch(-0)", '"default"');
268 shouldBe("sparseCharacterSwitch(1)", '"default"');
269 shouldBe("sparseCharacterSwitch(1.1)", '"default"');
270 shouldBe("sparseCharacterSwitch(-1)", '"default"');
271 shouldBe("sparseCharacterSwitch(-1000000000)", '"default"');
272 shouldBe("sparseCharacterSwitch(1000000000)", '"default"');
273 shouldBe("sparseCharacterSwitch({})", '"default"');
275 // String switch
276 shouldBe("stringSwitch('\0')", '"\0"');
277 shouldBe("stringSwitch('A')", '"A"');
278 shouldBe("stringSwitch('a')", '"a"');
279 shouldBe("stringSwitch('1')", '"1"');
280 shouldBe("stringSwitch('-1')", '"-1"');
281 shouldBe("stringSwitch('B')", '"B"');
282 shouldBe("stringSwitch('\uffff')", '"\uffff"');
283 shouldBe("stringSwitch('some string')", '"some string"');
284 shouldBe("stringSwitch({toString: function(){return 'some string'}})", '"default"');
285 shouldBe("stringSwitch('s')", '"default"');
286 shouldBe("stringSwitch(0)", '"default"');
287 shouldBe("stringSwitch(-0)", '"default"');
288 shouldBe("stringSwitch(1)", '"default"');
289 shouldBe("stringSwitch(1.1)", '"default"');
290 shouldBe("stringSwitch(-1)", '"default"');
291 shouldBe("stringSwitch(-1000000000)", '"default"');
292 shouldBe("stringSwitch(1000000000)", '"default"');
293 shouldBe("stringSwitch({})", '"default"');
295 // Number switch
296 shouldBe("numberSwitch('\0')", '"default"');
297 shouldBe("numberSwitch('A')", '"default"');
298 shouldBe("numberSwitch('a')", '"default"');
299 shouldBe("numberSwitch('1')", '"default"');
300 shouldBe("numberSwitch('-1')", '"default"');
301 shouldBe("numberSwitch('B')", '"default"');
302 shouldBe("numberSwitch('\uffff')", '"default"');
303 shouldBe("numberSwitch('some string')", '"default"');
304 shouldBe("numberSwitch({valueOf: function(){return 0}})", '"default"');
305 shouldBe("numberSwitch('s')", '"default"');
306 shouldBe("numberSwitch(0)", '0');
307 shouldBe("numberSwitch(-0)", '0');
308 shouldBe("numberSwitch(1)", '1');
309 shouldBe("numberSwitch(1.1)", '"default"');
310 shouldBe("numberSwitch(-1)", '-1');
311 shouldBe("numberSwitch(-1000000000)", '"default"');
312 shouldBe("numberSwitch(1000000000)", '"default"');
313 shouldBe("numberSwitch({})", '"default"');
315 // Sparse number switch
316 shouldBe("sparseNumberSwitch('\0')", '"default"');
317 shouldBe("sparseNumberSwitch('A')", '"default"');
318 shouldBe("sparseNumberSwitch('a')", '"default"');
319 shouldBe("sparseNumberSwitch('1')", '"default"');
320 shouldBe("sparseNumberSwitch('-1')", '"default"');
321 shouldBe("sparseNumberSwitch('B')", '"default"');
322 shouldBe("sparseNumberSwitch('\uffff')", '"default"');
323 shouldBe("sparseNumberSwitch('some string')", '"default"');
324 shouldBe("sparseNumberSwitch({valueOf: function(){return 0}})", '"default"');
325 shouldBe("sparseNumberSwitch('s')", '"default"');
326 shouldBe("sparseNumberSwitch(0)", '0');
327 shouldBe("sparseNumberSwitch(-0)", '0');
328 shouldBe("sparseNumberSwitch(1)", '1');
329 shouldBe("sparseNumberSwitch(1.1)", '"default"');
330 shouldBe("sparseNumberSwitch(-1)", '-1');
331 shouldBe("sparseNumberSwitch(-1000000000)", '-1000000000');
332 shouldBe("sparseNumberSwitch(1000000000)", '1000000000');
333 shouldBe("sparseNumberSwitch({})", '"default"');
335 // General switch
336 shouldBe("generalSwitch('\0')", '"\0"');
337 shouldBe("generalSwitch('A')", '"A"');
338 shouldBe("generalSwitch('a')", '"a"');
339 shouldBe("generalSwitch('1')", '"1"');
340 shouldBe("generalSwitch('-1')", '"-1"');
341 shouldBe("generalSwitch('B')", '"B"');
342 shouldBe("generalSwitch('\uffff')", '"\uffff"');
343 shouldBe("generalSwitch('some string')", '"some string"');
344 shouldBe("generalSwitch({valueOf: function(){return 0}})", '"default"');
345 shouldBe("generalSwitch('s')", '"default"');
346 shouldBe("generalSwitch(0)", '0');
347 shouldBe("generalSwitch(-0)", '0');
348 shouldBe("generalSwitch(1)", '1');
349 shouldBe("generalSwitch(1.1)", '"default"');
350 shouldBe("generalSwitch(-1)", '-1');
351 shouldBe("generalSwitch(-1000000000)", '-1000000000');
352 shouldBe("generalSwitch(1000000000)", '1000000000');
353 shouldBe("generalSwitch({})", '"default"');