• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2009 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
28// Test operations that involve one or more constants.
29// The code generator now handles compile-time constants specially.
30// Test the code generated when operands are known at compile time
31
32// Flags: --legacy-const
33
34// Test count operations involving constants
35function test_count() {
36  var x = "foo";
37  var y = "3";
38
39  x += x++;  // ++ and -- apply ToNumber to their operand, even for postfix.
40  assertEquals(x, "fooNaN", "fooNaN test");
41  x = "luft";
42  x += ++x;
43  assertEquals(x, "luftNaN", "luftNaN test");
44
45  assertTrue(y++ === 3, "y++ === 3, where y = \"3\"");
46  y = 3;
47  assertEquals(y++, 3, "y++ == 3, where y = 3");
48  y = "7.1";
49  assertTrue(y++ === 7.1, "y++ === 7.1, where y = \"7.1\"");
50  var z = y = x = "9";
51  assertEquals( z++ + (++y) + x++, 28, "z++ + (++y) + x++ == 28");
52  z = y = x = 13;
53  assertEquals( z++ + (++y) + x++, 40, "z++ + (++y) + x++ == 40");
54  z = y = x = -5.5;
55  assertEquals( z++ + (++y) + x++, -15.5, "z++ + (++y) + x++ == -15.5");
56
57  assertEquals(y, -4.5);
58  z = y;
59  z++;
60  assertEquals(y, -4.5);
61  z = y;
62  y++;
63  assertEquals(z, -4.5);
64
65  y = 20;
66  z = y;
67  z++;
68  assertEquals(y, 20);
69  z = y;
70  y++;
71  assertEquals(z, 20);
72
73  const w = 30;
74  assertEquals(w++, 30);
75  assertEquals(++w, 31);
76  assertEquals(++w, 31);
77}
78
79test_count();
80
81// Test comparison operations that involve one or two constant smis.
82
83function test() {
84  var i = 5;
85  var j = 3;
86
87  assertTrue( j < i );
88  i = 5; j = 3;
89  assertTrue( j <= i );
90  i = 5; j = 3;
91  assertTrue( i > j );
92  i = 5; j = 3;
93  assertTrue( i >= j );
94  i = 5; j = 3;
95  assertTrue( i != j );
96  i = 5; j = 3;
97  assertTrue( i == i );
98  i = 5; j = 3;
99  assertFalse( i < j );
100  i = 5; j = 3;
101  assertFalse( i <= j );
102  i = 5; j = 3;
103  assertFalse( j > i );
104  i = 5; j = 3;
105  assertFalse(j >= i );
106  i = 5; j = 3;
107  assertFalse( j == i);
108  i = 5; j = 3;
109  assertFalse( i != i);
110
111  i = 10 * 10;
112  while ( i < 107 ) {
113    ++i;
114  }
115  j = 21;
116
117  assertTrue( j < i );
118  j = 21;
119  assertTrue( j <= i );
120  j = 21;
121  assertTrue( i > j );
122  j = 21;
123  assertTrue( i >= j );
124  j = 21;
125  assertTrue( i != j );
126  j = 21;
127  assertTrue( i == i );
128  j = 21;
129  assertFalse( i < j );
130  j = 21;
131  assertFalse( i <= j );
132  j = 21;
133  assertFalse( j > i );
134  j = 21;
135  assertFalse(j >= i );
136  j = 21;
137  assertFalse( j == i);
138  j = 21;
139  assertFalse( i != i);
140  j = 21;
141  assertTrue( j == j );
142  j = 21;
143  assertFalse( j != j );
144
145  assertTrue( 100 > 99 );
146  assertTrue( 101 >= 90 );
147  assertTrue( 11111 > -234 );
148  assertTrue( -888 <= -20 );
149
150  while ( 234 > 456 ) {
151    i = i + 1;
152  }
153
154  switch(3) {
155    case 5:
156      assertUnreachable();
157      break;
158    case 3:
159      j = 13;
160    default:
161      i = 2;
162    case 7:
163      j = 17;
164      break;
165    case 9:
166      j = 19;
167      assertUnreachable();
168      break;
169  }
170  assertEquals(17, j, "switch with constant value");
171}
172
173
174function TrueToString() {
175  return true.toString();
176}
177
178
179function FalseToString() {
180  return false.toString();
181}
182
183
184function BoolTest() {
185  assertEquals("true", TrueToString());
186  assertEquals("true", TrueToString());
187  assertEquals("true", TrueToString());
188  assertEquals("false", FalseToString());
189  assertEquals("false", FalseToString());
190  assertEquals("false", FalseToString());
191  Boolean.prototype.toString = function() { return "foo"; }
192  assertEquals("foo", TrueToString());
193  assertEquals("foo", FalseToString());
194}
195
196
197// Some tests of shifts that get into the corners in terms of coverage.
198// We generate different code for the case where the operand is a constant.
199function ShiftTest() {
200  var x = 123;
201  assertEquals(x, x >> 0);
202  assertEquals(x, x << 0);
203  assertEquals(x, x >>> 0);
204  assertEquals(61, x >> 1);
205  assertEquals(246, x << 1);
206  assertEquals(61, x >>> 1);
207  x = -123;
208  assertEquals(x, x >> 0);
209  assertEquals(x, x << 0);
210  assertEquals(0x10000 * 0x10000 + x, x >>> 0);
211  assertEquals(-62, x >> 1);
212  assertEquals(-246, x << 1);
213  assertEquals(0x10000 * 0x8000 - 62, x >>> 1);
214  // Answer is non-Smi so the subtraction is not folded in the code
215  // generator.
216  assertEquals(-0x40000001, -0x3fffffff - 2);
217
218  x = 123;
219  assertEquals(0, x & 0);
220
221  // Answer is non-smi and lhs of << is a temporary heap number that we can
222  // overwrite.
223  x = 123.0001;
224  assertEquals(1073741824, (x * x) << 30);
225  x = 123;
226  // Answer is non-smi and lhs of << is a temporary heap number that we think
227  // we can overwrite (but we can't because it's a Smi).
228  assertEquals(1073741824, (x * x) << 30);
229}
230
231
232test();
233BoolTest();
234ShiftTest();
235