• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var stdlib = this;
6var foreign = {};
7var heap = new ArrayBuffer(64 * 1024);
8
9
10var switch1 = (function(stdlib, foreign, heap) {
11  "use asm";
12  function switch1(i) {
13    i = i|0;
14    switch (i) {
15      case 0: return 1;
16      case 1: return 2;
17      default: return i|0;
18    }
19  }
20  return { switch1: switch1 };
21})(stdlib, foreign, heap).switch1;
22
23assertEquals(1, switch1(0));
24assertEquals(2, switch1(1));
25for (var i = -2147483648; i < 2147483648; i += 3999773) {
26  assertEquals(i, switch1(i));
27}
28
29
30var switch2 = (function(stdlib, foreign, heap) {
31  "use asm";
32  function switch2(i) {
33    i = i|0;
34    var j = 0;
35    switch (i) {
36      case 0: j = 1; break;
37      case 1: j = 2; break;
38      case 2: j = 3; break;
39      default: j = i|0; break;
40    }
41    return j|0;
42  }
43  return { switch2: switch2 };
44})(stdlib, foreign, heap).switch2;
45
46assertEquals(1, switch2(0));
47assertEquals(2, switch2(1));
48assertEquals(3, switch2(2));
49for (var i = -2147483648; i < 2147483648; i += 3999773) {
50  assertEquals(i, switch2(i));
51}
52
53
54var switch3 = (function(stdlib, foreign, heap) {
55  "use asm";
56  function switch3(i) {
57    i = i|0;
58    var j = 0;
59    switch (i) {
60      case 0:
61      case 1: j = 1; break;
62      case 2:
63      case 3: j = 2; break;
64      case 4:
65      case 5: j = 3; break;
66      default: j = 0; break;
67    }
68    return j|0;
69  }
70  return { switch3: switch3 };
71})(stdlib, foreign, heap).switch3;
72
73assertEquals(1, switch3(0));
74assertEquals(1, switch3(1));
75assertEquals(2, switch3(2));
76assertEquals(2, switch3(3));
77assertEquals(3, switch3(4));
78assertEquals(3, switch3(5));
79for (var i = -2147483648; i < 2147483648; i += 3999773) {
80  assertEquals(0, switch3(i));
81}
82
83
84var switch4 = (function(stdlib, foreign, heap) {
85  "use asm";
86  function switch4(i) {
87    i = i|0;
88    switch (i) {
89      case -1:
90      case 1:
91        return 0;
92
93      case -2:
94      case 2:
95        return 1;
96
97      case -3:
98      case 3:
99        return 2;
100
101      case -8:
102      case 8:
103        return 3;
104
105      default:
106        return 4;
107    }
108  }
109  return { switch4: switch4 };
110})(stdlib, foreign, heap).switch4;
111
112assertEquals(4, switch4(0));
113assertEquals(0, switch4(-1));
114assertEquals(0, switch4(1));
115assertEquals(1, switch4(-2));
116assertEquals(1, switch4(2));
117assertEquals(3, switch4(-8));
118assertEquals(3, switch4(8));
119assertEquals(4, switch4(-123456789));
120assertEquals(4, switch4(123456789));
121