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 |
| 341 var test_id = 0, |
| 342 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-with-switch generator |
| 349 function switch_gen(clause_type, feedback, optimize) { |
| 350 var values = clause_values[clause_type]; |
| 351 |
| 352 function opt(fn) { |
| 353 if (feedback === 'all') { |
| 354 values.forEach(fn); |
| 355 } else if (Array.isArray(feedback)) { |
| 356 // Non-target |
| 357 values.filter(function(v) { |
| 358 return feedback.indexOf(v) === -1; |
| 359 }).forEach(fn); |
| 360 } else if (feedback !== undefined) { |
| 361 // Target |
| 362 fn(feedback); |
| 363 } else { |
| 364 // None |
| 365 } |
| 366 |
| 367 if (optimize) %OptimizeFunctionOnNextCall(fn); |
| 368 |
| 369 return fn; |
| 370 }; |
| 371 |
| 372 return opt(new Function( |
| 373 'tag', |
| 374 '"' + (test_id++) + '";' + |
| 375 'switch(tag) {' + |
| 376 values.map(function(value) { |
| 377 return 'case ' + JSON.stringify(value) + ': return' + |
| 378 JSON.stringify('ok-' + value); |
| 379 }).join(';') + |
| 380 '}' |
| 381 )); |
| 382 }; |
| 383 |
| 384 function test_switch(clause_type, test_type, feedback, optimize) { |
| 385 var pairs = [], |
| 386 fn = switch_gen(clause_type, feedback, optimize); |
| 387 |
| 388 if (Array.isArray(test_type)) { |
| 389 pairs = test_type.map(function(v) { |
| 390 return { |
| 391 value: v, |
| 392 expected: 'ok-' + v |
| 393 }; |
| 394 }); |
| 395 } else if (test_type === 'symbols') { |
| 396 pairs = clause_values.string.map(function(v) { |
| 397 return { |
| 398 value: v, |
| 399 expected: clause_type !== 'smi' ? 'ok-' + v : undefined |
| 400 }; |
| 401 }); |
| 402 } else if (test_type === 'strings') { |
| 403 pairs = clause_values.string.map(function(v) { |
| 404 return { |
| 405 value: ('%%' + v).slice(2), |
| 406 expected: clause_type !== 'smi' ? 'ok-' + v : undefined |
| 407 }; |
| 408 }); |
| 409 } else if (test_type === 'oddball') { |
| 410 pairs = [ |
| 411 { value: null, expected: undefined }, |
| 412 { value: NaN, expected: undefined }, |
| 413 { value: undefined, expected: undefined } |
| 414 ]; |
| 415 } else if (test_type === 'smi') { |
| 416 pairs = clause_values.smi.map(function(v) { |
| 417 return { |
| 418 value: v, |
| 419 expected: clause_type !== 'string' ? 'ok-' + v : undefined |
| 420 }; |
| 421 }); |
| 422 } else if (test_type === 'heapnum') { |
| 423 pairs = clause_values.smi.map(function(v) { |
| 424 return { |
| 425 value: ((v * 17)/16) - ((v*17)%16/16), |
| 426 expected: clause_type !== 'string' ? 'ok-' + v : undefined |
| 427 }; |
| 428 }); |
| 429 } |
| 430 |
| 431 pairs.forEach(function(pair) { |
| 432 assertEquals(fn(pair.value), pair.expected); |
| 433 }); |
| 434 }; |
| 435 |
| 436 // test_switch(clause_type, test_type, feedback, optimize); |
| 437 |
| 438 function test_switches(opt) { |
| 439 var test_types = ['symbols', 'strings', 'oddball', 'smi', 'heapnum']; |
| 440 |
| 441 function test(clause_type) { |
| 442 var values = clause_values[clause_type]; |
| 443 |
| 444 test_types.forEach(function(test_type) { |
| 445 test_switch(clause_type, test_type, 'all', opt); |
| 446 test_switch(clause_type, test_type, 'none', opt); |
| 447 |
| 448 // Targeting specific clause feedback |
| 449 values.forEach(function(value) { |
| 450 test_switch(clause_type, test_type, [value], value, opt); |
| 451 test_switch(clause_type, test_type, value, value, opt); |
| 452 }); |
| 453 }); |
| 454 }; |
| 455 |
| 456 test('string'); |
| 457 test('smi'); |
| 458 test('mixed'); |
| 459 }; |
| 460 |
| 461 test_switches(false); |
| 462 test_switches(true); |
OLD | NEW |