switch.js 7.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Copyright 2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function f0() {
  switch (0) {
lrn@chromium.org's avatar
lrn@chromium.org committed
30
    // switch deliberately left empty
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  }
}

f0();  // no errors

function f1(x) {
  switch (x) {
    default:      return "f1";
  }
  return "foo";
}

assertEquals("f1", f1(0), "default-switch.0");
assertEquals("f1", f1(1), "default-switch.1");

function f2(x) {
  var r;
  switch (x) {
    case 0:
      r = "zero";
      break;
    case 1:
      r = "one";
      break;
    case 2:
      r = "two";
57
      break;
58 59 60 61 62 63 64 65 66 67 68 69 70 71
    case 3:
      r = "three";
      break;
    default:
      r = "default";
  }
  return r;
}

assertEquals("zero", f2(0), "0-1-switch.0");
assertEquals("one", f2(1), "0-1-switch.1");
assertEquals("default", f2(7), "0-1-switch.2");
assertEquals("default", f2(-1), "0-1-switch.-1");
assertEquals("default", f2(NaN), "0-1-switch.NaN");
72 73 74 75 76 77 78
assertEquals("default", f2(Math.pow(2,34)), "0-1-switch.largeNum");
assertEquals("default", f2("0"), "0-1-switch.string");
assertEquals("default", f2(false), "0-1-switch.bool");
assertEquals("default", f2(null), "0-1-switch.null");
assertEquals("default", f2(undefined), "0-1-switch.undef");
assertEquals("default", f2(new Number(2)), "0-1-switch.undef");
assertEquals("default", f2({valueOf: function(){return 2; }}), "0-1-switch.obj");
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123


function f3(x, c) {
  var r = 0;
  switch (x) {
    default:
      r = "default";
      break;
    case  c:
      r = "value is c = " + c;
      break;
    case  2:
      r = "two";
      break;
    case -5:
      r = "minus 5";
      break;
    case  9:
      r = "nine";
      break;
  }
  return r;
}

assertEquals("two", f3(2,0), "value-switch.2-0");
assertEquals("minus 5", f3(-5,0), "value-switch.-5-0");
assertEquals("nine", f3(9,0), "value-switch.9-0");
assertEquals("value is c = 0", f3(0,0), "value-switch.0-0");
assertEquals("value is c = 2", f3(2,2), "value-switch.2-2");
assertEquals("default", f3(7,0), "value-switch.7-0");


function f4(x) {
  switch(x) {
    case 0:
      x++;
    default:
      x++;
    case 2:
      x++;
  }
  return x;
}


124 125 126 127
assertEquals(3, f4(0), "fallthrough-switch.0");
assertEquals(3, f4(1), "fallthrough-switch.1");
assertEquals(3, f4(2), "fallthrough-switch.2");
assertEquals(5, f4(3), "fallthrough-switch.3");
128 129 130 131 132 133 134 135 136 137 138 139


function f5(x) {
  switch(x) {
     case -2: return true;
     case -1: return false;
     case 0: return true;
     case 2: return false;
     default: return 42;
  }
}

140 141 142 143 144
assertTrue(f5(-2), "negcase.-2");
assertFalse(f5(-1), "negcase.-1");
assertTrue(f5(0), "negcase.-0");
assertEquals(42, f5(1), "negcase.1");
assertFalse(f5(2), "negcase.2");
145 146

function f6(N) {
147
  // long enough case that code buffer grows during code-generation
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  var res = 0;
  for(var i = 0; i < N; i++) {
    switch(i & 0x3f) {
    case 0: res += 0; break;
    case 1: res += 1; break;
    case 2: res += 2; break;
    case 3: res += 3; break;
    case 4: res += 4; break;
    case 5: res += 5; break;
    case 6: res += 6; break;
    case 7: res += 7; break;
    case 8: res += 8; break;
    case 9: res += 9; break;
    case 10: res += 10; break;
    case 11: res += 11; break;
    case 12: res += 12; break;
    case 13: res += 13; break;
    case 14: res += 14; break;
    case 15: res += 15; break;
    case 16: res += 16; break;
    case 17: res += 17; break;
    case 18: res += 18; break;
    case 19: res += 19; break;
    case 20: res += 20; break;
    case 21: res += 21; break;
    case 22: res += 22; break;
    case 23: res += 23; break;
    case 24: res += 24; break;
    case 25: res += 25; break;
    case 26: res += 26; break;
    case 27: res += 27; break;
    case 28: res += 28; break;
    case 29: res += 29; break;
    case 30: res += 30; break;
    case 31: res += 31; break;
    case 32: res += 32; break;
    case 33: res += 33; break;
    case 34: res += 34; break;
    case 35: res += 35; break;
    case 36: res += 36; break;
    case 37: res += 37; break;
    case 38: res += 38; break;
    case 39: res += 39; break;
    case 40: res += 40; break;
    case 41: res += 41; break;
    case 42: res += 42; break;
    case 43: res += 43; break;
    case 44: res += 44; break;
    case 45: res += 45; break;
    case 46: res += 46; break;
    case 47: res += 47; break;
    case 48: res += 48; break;
    case 49: res += 49; break;
    case 50: res += 50; break;
    case 51: res += 51; break;
    case 52: res += 52; break;
    case 53: res += 53; break;
    case 54: res += 54; break;
    case 55: res += 55; break;
    case 56: res += 56; break;
    case 57: res += 57; break;
    case 58: res += 58; break;
    case 59: res += 59; break;
    case 60: res += 60; break;
    case 61: res += 61; break;
    case 62: res += 62; break;
    case 63: res += 63; break;
    case 64: break;
    default: break;
    }
  }
  return res;
}

assertEquals(190, f6(20), "largeSwitch.20");
assertEquals(2016, f6(64), "largeSwitch.64");
assertEquals(4032, f6(128), "largeSwitch.128");
225 226
assertEquals(4222, f6(148), "largeSwitch.148");

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

function f7(value) {
  switch (value) {
  case 0: return "0";
  case -0: return "-0";
  case 1: case 2: case 3: case 4:  // Dummy fillers.
  }
  switch (value) {
  case 0x3fffffff: return "MaxSmi";
  case 0x3ffffffe:
  case 0x3ffffffd:
  case 0x3ffffffc:
  case 0x3ffffffb:
  case 0x3ffffffa:  // Dummy fillers
  }
  switch (value) {
  case -0x40000000: return "MinSmi";
  case -0x3fffffff:
  case -0x3ffffffe:
  case -0x3ffffffd:
  case -0x3ffffffc:
  case -0x3ffffffb:  // Dummy fillers
  }
  switch (value) {
  case 10: return "A";
  case 11:
  case 12:
  case 13:
255
  case 14:
256 257 258 259 260 261 262 263 264 265 266 267 268 269
  case 15:  // Dummy fillers
  }
  return "default";
}


assertEquals("default", f7(0.1), "0-1-switch.double-0.1");
assertEquals("0", f7(-0), "0-1-switch.double-neg0");
assertEquals("MaxSmi", f7((1<<30)-1), "0-1-switch.maxsmi");
assertEquals("MinSmi", f7(-(1<<30)), "0-1-switch.minsmi");
assertEquals("default", f7(1<<30), "0-1-switch.maxsmi++");
assertEquals("default", f7(-(1<<30)-1), "0-1-switch.minsmi--");
assertEquals("A", f7((170/16)-(170%16/16)), "0-1-switch.heapnum");

270 271 272

function makeVeryLong(length) {
  var res = "function() {\n" +
273
            "  var res = 0;\n" +
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
            "  for (var i = 0; i <= " + length + "; i++) {\n" +
            "    switch(i) {\n";
  for (var i = 0; i < length; i++) {
    res += "    case " + i + ": res += 2; break;\n";
  }
  res += "    default: res += 1;\n" +
         "    }\n" +
         "  }\n" +
         "  return res;\n" +
         "}";
  return eval(res);
}
var verylong_size = 1000;
var verylong = makeVeryLong(verylong_size);

289
assertEquals(verylong_size * 2 + 1, verylong());