OLD | NEW |
---|---|
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --allow-natives-syntax | |
29 | |
28 function f0() { | 30 function f0() { |
29 switch (0) { | 31 switch (0) { |
30 // switch deliberately left empty | 32 // switch deliberately left empty |
31 } | 33 } |
32 } | 34 } |
33 | 35 |
34 f0(); // no errors | 36 f0(); // no errors |
35 | 37 |
36 function f1(x) { | 38 function f1(x) { |
37 switch (x) { | 39 switch (x) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 } | 121 } |
120 return x; | 122 return x; |
121 } | 123 } |
122 | 124 |
123 | 125 |
124 assertEquals(3, f4(0), "fallthrough-switch.0"); | 126 assertEquals(3, f4(0), "fallthrough-switch.0"); |
125 assertEquals(3, f4(1), "fallthrough-switch.1"); | 127 assertEquals(3, f4(1), "fallthrough-switch.1"); |
126 assertEquals(3, f4(2), "fallthrough-switch.2"); | 128 assertEquals(3, f4(2), "fallthrough-switch.2"); |
127 assertEquals(5, f4(3), "fallthrough-switch.3"); | 129 assertEquals(5, f4(3), "fallthrough-switch.3"); |
128 | 130 |
131 function f4_string(tag, x) { | |
132 switch(tag) { | |
133 case 'zero': | |
134 x++; | |
135 case 'two': | |
136 x++; | |
137 } | |
138 return x; | |
139 } | |
140 | |
141 // Symbols | |
142 assertEquals(2, f4_string('zero', 0), "fallthrough-string-switch.0"); | |
143 assertEquals(1, f4_string('one', 1), "fallthrough-string-switch.1"); | |
144 assertEquals(3, f4_string('two', 2), "fallthrough-string-switch.2"); | |
145 | |
146 // Strings | |
147 assertEquals(2, f4_string('_zero'.slice(1), 0), "fallthrough-string-switch.3"); | |
148 assertEquals(1, f4_string('_one'.slice(1), 1), "fallthrough-string-switch.4"); | |
149 assertEquals(3, f4_string('_two'.slice(1), 2), "fallthrough-string-switch.5"); | |
150 | |
151 // Oddball | |
152 assertEquals(3, f4_string(null, 3), "fallthrough-string-switch.6"); | |
153 | |
154 // Test for regression | |
155 function regress_string(value) { | |
156 var json = 1; | |
157 switch (typeof value) { | |
158 case 'object': | |
159 break; | |
160 | |
161 default: | |
162 | |
163 } | |
164 return json; | |
165 }; | |
166 assertEquals(1, regress_string('object'), 'regression-string'); | |
129 | 167 |
130 function f5(x) { | 168 function f5(x) { |
131 switch(x) { | 169 switch(x) { |
132 case -2: return true; | 170 case -2: return true; |
133 case -1: return false; | 171 case -1: return false; |
134 case 0: return true; | 172 case 0: return true; |
135 case 2: return false; | 173 case 2: return false; |
136 default: return 42; | 174 default: return 42; |
137 } | 175 } |
138 } | 176 } |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 " }\n" + | 318 " }\n" + |
281 " }\n" + | 319 " }\n" + |
282 " return res;\n" + | 320 " return res;\n" + |
283 "})"; | 321 "})"; |
284 return eval(res); | 322 return eval(res); |
285 } | 323 } |
286 var verylong_size = 1000; | 324 var verylong_size = 1000; |
287 var verylong = makeVeryLong(verylong_size); | 325 var verylong = makeVeryLong(verylong_size); |
288 | 326 |
289 assertEquals(verylong_size * 2 + 1, verylong()); | 327 assertEquals(verylong_size * 2 + 1, verylong()); |
328 | |
329 // | |
330 // Test suite below aims to cover all possible combinations of following: | |
331 // | |
332 // clauses | tags | type feedback | optimization | |
333 // ========================================================= | |
334 // strings | symbol | all | on | |
335 // smis | string | target | off | |
336 // mixed | oddball | non-target | | |
337 // | smis | none | | |
338 // | heapnum | | | |
339 // ========================================================= | |
340 | |
fschneider
2011/11/17 10:05:32
Global unique id for each generated test case need
| |
341 // Function-with-switch generator | |
342 var clause_values = { | |
343 string: ['abc', 'def', 'ghi', 'jkl'], | |
344 smi: [1, 2, 3, 4], | |
345 mixed: ['abc', 1, 'def', 2, 'ghi', 3, 'jkl', 4] | |
346 }; | |
347 | |
348 function switch_gen(clause_type, feedback, optimize) { | |
349 var values = clause_values[clause_type]; | |
350 | |
351 function opt(fn) { | |
352 if (feedback === 'all') { | |
353 values.forEach(fn); | |
354 } else if (Array.isArray(feedback)) { | |
355 // Non-target | |
356 values.filter(function(v) { | |
357 return feedback.indexOf(v) === -1; | |
358 }).forEach(fn); | |
359 } else if (feedback !== undefined) { | |
360 // Target | |
361 fn(feedback); | |
362 } else { | |
363 // None | |
364 } | |
365 | |
366 if (optimize) %OptimizeFunctionOnNextCall(fn); | |
367 | |
368 return fn; | |
369 }; | |
370 | |
371 return opt(new Function( | |
372 'tag', | |
373 'switch(tag) {' + | |
fschneider
2011/11/17 10:05:32
Here we need some kind of unique id so that the co
| |
374 values.map(function(value) { | |
375 return 'case ' + JSON.stringify(value) + ': return' + | |
376 JSON.stringify('ok-' + value); | |
377 }).join(';') + | |
378 '}' | |
379 )); | |
380 }; | |
381 | |
382 function test_switch(clause_type, test_type, feedback, optimize) { | |
383 var pairs = [], | |
384 fn = switch_gen(clause_type, feedback, optimize); | |
385 | |
386 if (Array.isArray(test_type)) { | |
387 pairs = test_type.map(function(v) { | |
388 return { | |
389 value: v, | |
390 expected: 'ok-' + v | |
391 }; | |
392 }); | |
393 } else if (test_type === 'symbols') { | |
394 pairs = clause_values.string.map(function(v) { | |
395 return { | |
396 value: v, | |
397 expected: clause_type !== 'smi' ? 'ok-' + v : undefined | |
398 }; | |
399 }); | |
400 } else if (test_type === 'strings') { | |
401 pairs = clause_values.string.map(function(v) { | |
402 return { | |
403 value: ('%%' + v).slice(2), | |
404 expected: clause_type !== 'smi' ? 'ok-' + v : undefined | |
405 }; | |
406 }); | |
407 } else if (test_type === 'oddball') { | |
408 pairs = [ | |
409 { value: null, expected: undefined }, | |
410 { value: NaN, expected: undefined }, | |
411 { value: undefined, expected: undefined } | |
412 ]; | |
413 } else if (test_type === 'smi') { | |
414 pairs = clause_values.smi.map(function(v) { | |
415 return { | |
416 value: v, | |
417 expected: clause_type !== 'string' ? 'ok-' + v : undefined | |
418 }; | |
419 }); | |
420 } else if (test_type === 'heapnum') { | |
421 pairs = clause_values.smi.map(function(v) { | |
422 return { | |
423 value: ((v * 17)/16) - ((v*17)%16/16), | |
424 expected: clause_type !== 'string' ? 'ok-' + v : undefined | |
425 }; | |
426 }); | |
427 } | |
428 | |
429 pairs.forEach(function(pair) { | |
430 assertEquals(fn(pair.value), pair.expected); | |
431 }); | |
432 }; | |
433 | |
434 // test_switch(clause_type, test_type, feedback, optimize); | |
435 | |
436 function test_switches(opt) { | |
437 var test_types = ['symbols', 'strings', 'oddball', 'smi', 'heapnum']; | |
438 | |
439 function test(clause_type) { | |
440 var values = clause_values[clause_type]; | |
441 | |
442 test_types.forEach(function(test_type) { | |
443 test_switch(clause_type, test_type, 'all', opt); | |
444 test_switch(clause_type, test_type, 'none', opt); | |
445 | |
446 // Targeting specific clause feedback | |
447 values.forEach(function(value) { | |
448 test_switch(clause_type, test_type, [value], value, opt); | |
449 test_switch(clause_type, test_type, value, value, opt); | |
450 }); | |
451 }); | |
452 }; | |
453 | |
454 test('string'); | |
455 test('smi'); | |
456 test('mixed'); | |
457 }; | |
458 | |
459 test_switches(false); | |
460 test_switches(true); | |
OLD | NEW |