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 4// met: 5// 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. 15// 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. 27 28function f0() { 29 switch (0) { 30 // switch deliberately left empty 31 } 32} 33 34f0(); // no errors 35 36function f1(x) { 37 switch (x) { 38 default: return "f1"; 39 } 40 return "foo"; 41} 42 43assertEquals("f1", f1(0), "default-switch.0"); 44assertEquals("f1", f1(1), "default-switch.1"); 45 46function f2(x) { 47 var r; 48 switch (x) { 49 case 0: 50 r = "zero"; 51 break; 52 case 1: 53 r = "one"; 54 break; 55 case 2: 56 r = "two"; 57 break; 58 case 3: 59 r = "three"; 60 break; 61 default: 62 r = "default"; 63 } 64 return r; 65} 66 67assertEquals("zero", f2(0), "0-1-switch.0"); 68assertEquals("one", f2(1), "0-1-switch.1"); 69assertEquals("default", f2(7), "0-1-switch.2"); 70assertEquals("default", f2(-1), "0-1-switch.-1"); 71assertEquals("default", f2(NaN), "0-1-switch.NaN"); 72assertEquals("default", f2(Math.pow(2,34)), "0-1-switch.largeNum"); 73assertEquals("default", f2("0"), "0-1-switch.string"); 74assertEquals("default", f2(false), "0-1-switch.bool"); 75assertEquals("default", f2(null), "0-1-switch.null"); 76assertEquals("default", f2(undefined), "0-1-switch.undef"); 77assertEquals("default", f2(new Number(2)), "0-1-switch.undef"); 78assertEquals("default", f2({valueOf: function(){return 2; }}), "0-1-switch.obj"); 79 80 81function f3(x, c) { 82 var r = 0; 83 switch (x) { 84 default: 85 r = "default"; 86 break; 87 case c: 88 r = "value is c = " + c; 89 break; 90 case 2: 91 r = "two"; 92 break; 93 case -5: 94 r = "minus 5"; 95 break; 96 case 9: 97 r = "nine"; 98 break; 99 } 100 return r; 101} 102 103assertEquals("two", f3(2,0), "value-switch.2-0"); 104assertEquals("minus 5", f3(-5,0), "value-switch.-5-0"); 105assertEquals("nine", f3(9,0), "value-switch.9-0"); 106assertEquals("value is c = 0", f3(0,0), "value-switch.0-0"); 107assertEquals("value is c = 2", f3(2,2), "value-switch.2-2"); 108assertEquals("default", f3(7,0), "value-switch.7-0"); 109 110 111function f4(x) { 112 switch(x) { 113 case 0: 114 x++; 115 default: 116 x++; 117 case 2: 118 x++; 119 } 120 return x; 121} 122 123 124assertEquals(3, f4(0), "fallthrough-switch.0"); 125assertEquals(3, f4(1), "fallthrough-switch.1"); 126assertEquals(3, f4(2), "fallthrough-switch.2"); 127assertEquals(5, f4(3), "fallthrough-switch.3"); 128 129 130function f5(x) { 131 switch(x) { 132 case -2: return true; 133 case -1: return false; 134 case 0: return true; 135 case 2: return false; 136 default: return 42; 137 } 138} 139 140assertTrue(f5(-2), "negcase.-2"); 141assertFalse(f5(-1), "negcase.-1"); 142assertTrue(f5(0), "negcase.-0"); 143assertEquals(42, f5(1), "negcase.1"); 144assertFalse(f5(2), "negcase.2"); 145 146function f6(N) { 147 // long enough case that code buffer grows during code-generation 148 var res = 0; 149 for(var i = 0; i < N; i++) { 150 switch(i & 0x3f) { 151 case 0: res += 0; break; 152 case 1: res += 1; break; 153 case 2: res += 2; break; 154 case 3: res += 3; break; 155 case 4: res += 4; break; 156 case 5: res += 5; break; 157 case 6: res += 6; break; 158 case 7: res += 7; break; 159 case 8: res += 8; break; 160 case 9: res += 9; break; 161 case 10: res += 10; break; 162 case 11: res += 11; break; 163 case 12: res += 12; break; 164 case 13: res += 13; break; 165 case 14: res += 14; break; 166 case 15: res += 15; break; 167 case 16: res += 16; break; 168 case 17: res += 17; break; 169 case 18: res += 18; break; 170 case 19: res += 19; break; 171 case 20: res += 20; break; 172 case 21: res += 21; break; 173 case 22: res += 22; break; 174 case 23: res += 23; break; 175 case 24: res += 24; break; 176 case 25: res += 25; break; 177 case 26: res += 26; break; 178 case 27: res += 27; break; 179 case 28: res += 28; break; 180 case 29: res += 29; break; 181 case 30: res += 30; break; 182 case 31: res += 31; break; 183 case 32: res += 32; break; 184 case 33: res += 33; break; 185 case 34: res += 34; break; 186 case 35: res += 35; break; 187 case 36: res += 36; break; 188 case 37: res += 37; break; 189 case 38: res += 38; break; 190 case 39: res += 39; break; 191 case 40: res += 40; break; 192 case 41: res += 41; break; 193 case 42: res += 42; break; 194 case 43: res += 43; break; 195 case 44: res += 44; break; 196 case 45: res += 45; break; 197 case 46: res += 46; break; 198 case 47: res += 47; break; 199 case 48: res += 48; break; 200 case 49: res += 49; break; 201 case 50: res += 50; break; 202 case 51: res += 51; break; 203 case 52: res += 52; break; 204 case 53: res += 53; break; 205 case 54: res += 54; break; 206 case 55: res += 55; break; 207 case 56: res += 56; break; 208 case 57: res += 57; break; 209 case 58: res += 58; break; 210 case 59: res += 59; break; 211 case 60: res += 60; break; 212 case 61: res += 61; break; 213 case 62: res += 62; break; 214 case 63: res += 63; break; 215 case 64: break; 216 default: break; 217 } 218 } 219 return res; 220} 221 222assertEquals(190, f6(20), "largeSwitch.20"); 223assertEquals(2016, f6(64), "largeSwitch.64"); 224assertEquals(4032, f6(128), "largeSwitch.128"); 225assertEquals(4222, f6(148), "largeSwitch.148"); 226 227 228function f7(value) { 229 switch (value) { 230 case 0: return "0"; 231 case -0: return "-0"; 232 case 1: case 2: case 3: case 4: // Dummy fillers. 233 } 234 switch (value) { 235 case 0x3fffffff: return "MaxSmi"; 236 case 0x3ffffffe: 237 case 0x3ffffffd: 238 case 0x3ffffffc: 239 case 0x3ffffffb: 240 case 0x3ffffffa: // Dummy fillers 241 } 242 switch (value) { 243 case -0x40000000: return "MinSmi"; 244 case -0x3fffffff: 245 case -0x3ffffffe: 246 case -0x3ffffffd: 247 case -0x3ffffffc: 248 case -0x3ffffffb: // Dummy fillers 249 } 250 switch (value) { 251 case 10: return "A"; 252 case 11: 253 case 12: 254 case 13: 255 case 14: 256 case 15: // Dummy fillers 257 } 258 return "default"; 259} 260 261 262assertEquals("default", f7(0.1), "0-1-switch.double-0.1"); 263assertEquals("0", f7(-0), "0-1-switch.double-neg0"); 264assertEquals("MaxSmi", f7((1<<30)-1), "0-1-switch.maxsmi"); 265assertEquals("MinSmi", f7(-(1<<30)), "0-1-switch.minsmi"); 266assertEquals("default", f7(1<<30), "0-1-switch.maxsmi++"); 267assertEquals("default", f7(-(1<<30)-1), "0-1-switch.minsmi--"); 268assertEquals("A", f7((170/16)-(170%16/16)), "0-1-switch.heapnum"); 269 270 271function makeVeryLong(length) { 272 var res = "(function () {\n" + 273 " var res = 0;\n" + 274 " for (var i = 0; i <= " + length + "; i++) {\n" + 275 " switch(i) {\n"; 276 for (var i = 0; i < length; i++) { 277 res += " case " + i + ": res += 2; break;\n"; 278 } 279 res += " default: res += 1;\n" + 280 " }\n" + 281 " }\n" + 282 " return res;\n" + 283 "})"; 284 return eval(res); 285} 286var verylong_size = 1000; 287var verylong = makeVeryLong(verylong_size); 288 289assertEquals(verylong_size * 2 + 1, verylong()); 290