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
) {
18 return 'Should not hit second \'A\'';
32 return 'Did not reach default clause';
35 function sparseCharacterSwitch(scrutinee
) {
50 return 'Should not hit second \'A\'';
66 return 'Did not reach default clause';
75 function stringSwitch(scrutinee
) {
87 return 'Should not hit second \'A\'';
92 case '[object Object]':
93 return '[object Object]';
106 return 'Did not reach default clause';
112 function numberSwitch(scrutinee
) {
122 return 'Should not hit second 1';
131 return 'Did not reach default clause';
137 function sparseNumberSwitch(scrutinee
) {
147 return 'Should not hit second 1';
160 return 'Did not reach default clause';
166 function generalSwitch(scrutinee
) {
182 return 'Should not hit second 1';
198 return 'Should not hit second \'A\'';
203 case '[object Object]':
204 return '[object Object]';
206 return 'some string';
221 return 'Did not reach default clause';
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"');
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"');
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"');
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"');