1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
28 // Flags: --allow-natives-syntax
32 // switch deliberately left empty
45 assertEquals("f1", f1(0), "default-switch.0");
46 assertEquals("f1", f1(1), "default-switch.1");
69 assertEquals("zero", f2(0), "0-1-switch.0");
70 assertEquals("one", f2(1), "0-1-switch.1");
71 assertEquals("default", f2(7), "0-1-switch.2");
72 assertEquals("default", f2(-1), "0-1-switch.-1");
73 assertEquals("default", f2(NaN), "0-1-switch.NaN");
74 assertEquals("default", f2(Math.pow(2,34)), "0-1-switch.largeNum");
75 assertEquals("default", f2("0"), "0-1-switch.string");
76 assertEquals("default", f2(false), "0-1-switch.bool");
77 assertEquals("default", f2(null), "0-1-switch.null");
78 assertEquals("default", f2(undefined), "0-1-switch.undef");
79 assertEquals("default", f2(new Number(2)), "0-1-switch.undef");
80 assertEquals("default", f2({valueOf: function(){return 2; }}), "0-1-switch.obj");
90 r = "value is c = " + c;
105 assertEquals("two", f3(2,0), "value-switch.2-0");
106 assertEquals("minus 5", f3(-5,0), "value-switch.-5-0");
107 assertEquals("nine", f3(9,0), "value-switch.9-0");
108 assertEquals("value is c = 0", f3(0,0), "value-switch.0-0");
109 assertEquals("value is c = 2", f3(2,2), "value-switch.2-2");
110 assertEquals("default", f3(7,0), "value-switch.7-0");
126 assertEquals(3, f4(0), "fallthrough-switch.0");
127 assertEquals(3, f4(1), "fallthrough-switch.1");
128 assertEquals(3, f4(2), "fallthrough-switch.2");
129 assertEquals(5, f4(3), "fallthrough-switch.3");
131 function f4_string(tag, x) {
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");
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");
152 assertEquals(3, f4_string(null, 3), "fallthrough-string-switch.6");
154 // Test for regression
155 function regress_string(value) {
157 switch (typeof value) {
166 assertEquals(1, regress_string('object'), 'regression-string');
170 case -2: return true;
171 case -1: return false;
173 case 2: return false;
178 assertTrue(f5(-2), "negcase.-2");
179 assertFalse(f5(-1), "negcase.-1");
180 assertTrue(f5(0), "negcase.-0");
181 assertEquals(42, f5(1), "negcase.1");
182 assertFalse(f5(2), "negcase.2");
185 // long enough case that code buffer grows during code-generation
187 for(var i = 0; i < N; i++) {
189 case 0: res += 0; break;
190 case 1: res += 1; break;
191 case 2: res += 2; break;
192 case 3: res += 3; break;
193 case 4: res += 4; break;
194 case 5: res += 5; break;
195 case 6: res += 6; break;
196 case 7: res += 7; break;
197 case 8: res += 8; break;
198 case 9: res += 9; break;
199 case 10: res += 10; break;
200 case 11: res += 11; break;
201 case 12: res += 12; break;
202 case 13: res += 13; break;
203 case 14: res += 14; break;
204 case 15: res += 15; break;
205 case 16: res += 16; break;
206 case 17: res += 17; break;
207 case 18: res += 18; break;
208 case 19: res += 19; break;
209 case 20: res += 20; break;
210 case 21: res += 21; break;
211 case 22: res += 22; break;
212 case 23: res += 23; break;
213 case 24: res += 24; break;
214 case 25: res += 25; break;
215 case 26: res += 26; break;
216 case 27: res += 27; break;
217 case 28: res += 28; break;
218 case 29: res += 29; break;
219 case 30: res += 30; break;
220 case 31: res += 31; break;
221 case 32: res += 32; break;
222 case 33: res += 33; break;
223 case 34: res += 34; break;
224 case 35: res += 35; break;
225 case 36: res += 36; break;
226 case 37: res += 37; break;
227 case 38: res += 38; break;
228 case 39: res += 39; break;
229 case 40: res += 40; break;
230 case 41: res += 41; break;
231 case 42: res += 42; break;
232 case 43: res += 43; break;
233 case 44: res += 44; break;
234 case 45: res += 45; break;
235 case 46: res += 46; break;
236 case 47: res += 47; break;
237 case 48: res += 48; break;
238 case 49: res += 49; break;
239 case 50: res += 50; break;
240 case 51: res += 51; break;
241 case 52: res += 52; break;
242 case 53: res += 53; break;
243 case 54: res += 54; break;
244 case 55: res += 55; break;
245 case 56: res += 56; break;
246 case 57: res += 57; break;
247 case 58: res += 58; break;
248 case 59: res += 59; break;
249 case 60: res += 60; break;
250 case 61: res += 61; break;
251 case 62: res += 62; break;
252 case 63: res += 63; break;
260 assertEquals(190, f6(20), "largeSwitch.20");
261 assertEquals(2016, f6(64), "largeSwitch.64");
262 assertEquals(4032, f6(128), "largeSwitch.128");
263 assertEquals(4222, f6(148), "largeSwitch.148");
269 case -0: return "-0";
270 case 1: case 2: case 3: case 4: // Dummy fillers.
273 case 0x3fffffff: return "MaxSmi";
278 case 0x3ffffffa: // Dummy fillers
281 case -0x40000000: return "MinSmi";
286 case -0x3ffffffb: // Dummy fillers
294 case 15: // Dummy fillers
300 assertEquals("default", f7(0.1), "0-1-switch.double-0.1");
301 assertEquals("0", f7(-0), "0-1-switch.double-neg0");
302 assertEquals("MaxSmi", f7((1<<30)-1), "0-1-switch.maxsmi");
303 assertEquals("MinSmi", f7(-(1<<30)), "0-1-switch.minsmi");
304 assertEquals("default", f7(1<<30), "0-1-switch.maxsmi++");
305 assertEquals("default", f7(-(1<<30)-1), "0-1-switch.minsmi--");
306 assertEquals("A", f7((170/16)-(170%16/16)), "0-1-switch.heapnum");
309 function makeVeryLong(length) {
310 var res = "(function () {\n" +
312 " for (var i = 0; i <= " + length + "; i++) {\n" +
314 for (var i = 0; i < length; i++) {
315 res += " case " + i + ": res += 2; break;\n";
317 res += " default: res += 1;\n" +
324 var verylong_size = 1000;
325 var verylong = makeVeryLong(verylong_size);
327 assertEquals(verylong_size * 2 + 1, verylong());
330 // Test suite below aims to cover all possible combinations of following:
332 // clauses | tags | type feedback | optimization
333 // =========================================================
334 // strings | symbol | all | on
335 // smis | string | target | off
336 // mixed | oddball | non-target |
339 // =========================================================
341 // Function-with-switch generator
344 string: ['abc', 'def', 'ghi', 'jkl'],
346 mixed: ['abc', 1, 'def', 2, 'ghi', 3, 'jkl', 4]
349 function switch_gen(clause_type, feedback, optimize) {
350 var values = clause_values[clause_type];
353 if (feedback === 'all') {
355 } else if (Array.isArray(feedback)) {
357 values.filter(function(v) {
358 return feedback.indexOf(v) === -1;
360 } else if (feedback !== undefined) {
367 if (optimize) %OptimizeFunctionOnNextCall(fn);
372 return opt(new Function(
374 '"' + (test_id++) + '";' +
376 values.map(function(value) {
377 return 'case ' + JSON.stringify(value) + ': return' +
378 JSON.stringify('ok-' + value);
384 function test_switch(clause_type, test_type, feedback, optimize) {
386 fn = switch_gen(clause_type, feedback, optimize);
388 if (Array.isArray(test_type)) {
389 pairs = test_type.map(function(v) {
395 } else if (test_type === 'symbols') {
396 pairs = clause_values.string.map(function(v) {
399 expected: clause_type !== 'smi' ? 'ok-' + v : undefined
402 } else if (test_type === 'strings') {
403 pairs = clause_values.string.map(function(v) {
405 value: ('%%' + v).slice(2),
406 expected: clause_type !== 'smi' ? 'ok-' + v : undefined
409 } else if (test_type === 'oddball') {
411 { value: null, expected: undefined },
412 { value: NaN, expected: undefined },
413 { value: undefined, expected: undefined }
415 } else if (test_type === 'smi') {
416 pairs = clause_values.smi.map(function(v) {
419 expected: clause_type !== 'string' ? 'ok-' + v : undefined
422 } else if (test_type === 'heapnum') {
423 pairs = clause_values.smi.map(function(v) {
425 value: ((v * 17)/16) - ((v*17)%16/16),
426 expected: clause_type !== 'string' ? 'ok-' + v : undefined
431 pairs.forEach(function(pair) {
432 assertEquals(fn(pair.value), pair.expected);
436 // test_switch(clause_type, test_type, feedback, optimize);
438 function test_switches(opt) {
439 var test_types = ['symbols', 'strings', 'oddball', 'smi', 'heapnum'];
441 function test(clause_type) {
442 var values = clause_values[clause_type];
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);
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);
461 test_switches(false);