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