OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 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 // Repeat most the tests in smi-ops.js that use SMI_MIN and SMI_MAX, but |
| 29 // with SMI_MIN and SMI_MAX from the 64-bit platform, which represents all |
| 30 // signed 32-bit integer values as smis. |
| 31 |
| 32 const SMI_MAX = (1 << 30) - 1 + (1 << 30); // Create without overflowing. |
| 33 const SMI_MIN = -SMI_MAX - 1; // Create without overflowing. |
| 34 const ONE = 1; |
| 35 const ONE_HUNDRED = 100; |
| 36 |
| 37 const OBJ_42 = new (function() { |
| 38 this.valueOf = function() { return 42; }; |
| 39 })(); |
| 40 |
| 41 assertEquals(42, OBJ_42.valueOf()); |
| 42 |
| 43 |
| 44 function Add1(x) { |
| 45 return x + 1; |
| 46 } |
| 47 |
| 48 function Add100(x) { |
| 49 return x + 100; |
| 50 } |
| 51 |
| 52 function Add1Reversed(x) { |
| 53 return 1 + x; |
| 54 } |
| 55 |
| 56 function Add100Reversed(x) { |
| 57 return 100 + x; |
| 58 } |
| 59 |
| 60 |
| 61 assertEquals(1, Add1(0)); // fast case |
| 62 assertEquals(1, Add1Reversed(0)); // fast case |
| 63 assertEquals(SMI_MAX + ONE, Add1(SMI_MAX), "smimax + 1"); |
| 64 assertEquals(SMI_MAX + ONE, Add1Reversed(SMI_MAX), "1 + smimax"); |
| 65 assertEquals(42 + ONE, Add1(OBJ_42)); // non-smi |
| 66 assertEquals(42 + ONE, Add1Reversed(OBJ_42)); // non-smi |
| 67 |
| 68 assertEquals(100, Add100(0)); // fast case |
| 69 assertEquals(100, Add100Reversed(0)); // fast case |
| 70 assertEquals(SMI_MAX + ONE_HUNDRED, Add100(SMI_MAX), "smimax + 100"); |
| 71 assertEquals(SMI_MAX + ONE_HUNDRED, Add100Reversed(SMI_MAX), " 100 + smimax"); |
| 72 assertEquals(42 + ONE_HUNDRED, Add100(OBJ_42)); // non-smi |
| 73 assertEquals(42 + ONE_HUNDRED, Add100Reversed(OBJ_42)); // non-smi |
| 74 |
| 75 |
| 76 |
| 77 function Sub1(x) { |
| 78 return x - 1; |
| 79 } |
| 80 |
| 81 function Sub100(x) { |
| 82 return x - 100; |
| 83 } |
| 84 |
| 85 function Sub1Reversed(x) { |
| 86 return 1 - x; |
| 87 } |
| 88 |
| 89 function Sub100Reversed(x) { |
| 90 return 100 - x; |
| 91 } |
| 92 |
| 93 |
| 94 assertEquals(0, Sub1(1)); // fast case |
| 95 assertEquals(-1, Sub1Reversed(2)); // fast case |
| 96 assertEquals(SMI_MIN - ONE, Sub1(SMI_MIN)); // overflow |
| 97 assertEquals(ONE - SMI_MIN, Sub1Reversed(SMI_MIN)); // overflow |
| 98 assertEquals(42 - ONE, Sub1(OBJ_42)); // non-smi |
| 99 assertEquals(ONE - 42, Sub1Reversed(OBJ_42)); // non-smi |
| 100 |
| 101 assertEquals(0, Sub100(100)); // fast case |
| 102 assertEquals(1, Sub100Reversed(99)); // fast case |
| 103 assertEquals(SMI_MIN - ONE_HUNDRED, Sub100(SMI_MIN)); // overflow |
| 104 assertEquals(ONE_HUNDRED - SMI_MIN, Sub100Reversed(SMI_MIN)); // overflow |
| 105 assertEquals(42 - ONE_HUNDRED, Sub100(OBJ_42)); // non-smi |
| 106 assertEquals(ONE_HUNDRED - 42, Sub100Reversed(OBJ_42)); // non-smi |
| 107 |
| 108 |
| 109 function Shr1(x) { |
| 110 return x >>> 1; |
| 111 } |
| 112 |
| 113 function Shr100(x) { |
| 114 return x >>> 100; |
| 115 } |
| 116 |
| 117 function Shr1Reversed(x) { |
| 118 return 1 >>> x; |
| 119 } |
| 120 |
| 121 function Shr100Reversed(x) { |
| 122 return 100 >>> x; |
| 123 } |
| 124 |
| 125 function Sar1(x) { |
| 126 return x >> 1; |
| 127 } |
| 128 |
| 129 function Sar100(x) { |
| 130 return x >> 100; |
| 131 } |
| 132 |
| 133 function Sar1Reversed(x) { |
| 134 return 1 >> x; |
| 135 } |
| 136 |
| 137 function Sar100Reversed(x) { |
| 138 return 100 >> x; |
| 139 } |
| 140 |
| 141 |
| 142 assertEquals(0, Shr1(1)); |
| 143 assertEquals(0, Sar1(1)); |
| 144 assertEquals(0, Shr1Reversed(2)); |
| 145 assertEquals(0, Sar1Reversed(2)); |
| 146 assertEquals(1073741824, Shr1(SMI_MIN)); |
| 147 assertEquals(-1073741824, Sar1(SMI_MIN)); |
| 148 assertEquals(1, Shr1Reversed(SMI_MIN)); |
| 149 assertEquals(1, Sar1Reversed(SMI_MIN)); |
| 150 assertEquals(21, Shr1(OBJ_42)); |
| 151 assertEquals(21, Sar1(OBJ_42)); |
| 152 assertEquals(0, Shr1Reversed(OBJ_42)); |
| 153 assertEquals(0, Sar1Reversed(OBJ_42)); |
| 154 |
| 155 assertEquals(6, Shr100(100), "100 >>> 100"); |
| 156 assertEquals(6, Sar100(100), "100 >> 100"); |
| 157 assertEquals(12, Shr100Reversed(99)); |
| 158 assertEquals(12, Sar100Reversed(99)); |
| 159 assertEquals(134217728, Shr100(SMI_MIN)); |
| 160 assertEquals(-134217728, Sar100(SMI_MIN)); |
| 161 assertEquals(100, Shr100Reversed(SMI_MIN)); |
| 162 assertEquals(100, Sar100Reversed(SMI_MIN)); |
| 163 assertEquals(2, Shr100(OBJ_42)); |
| 164 assertEquals(2, Sar100(OBJ_42)); |
| 165 assertEquals(0, Shr100Reversed(OBJ_42)); |
| 166 assertEquals(0, Sar100Reversed(OBJ_42)); |
| 167 |
| 168 |
| 169 function Xor1(x) { |
| 170 return x ^ 1; |
| 171 } |
| 172 |
| 173 function Xor100(x) { |
| 174 return x ^ 100; |
| 175 } |
| 176 |
| 177 function Xor1Reversed(x) { |
| 178 return 1 ^ x; |
| 179 } |
| 180 |
| 181 function Xor100Reversed(x) { |
| 182 return 100 ^ x; |
| 183 } |
| 184 |
| 185 |
| 186 assertEquals(0, Xor1(1)); |
| 187 assertEquals(3, Xor1Reversed(2)); |
| 188 assertEquals(SMI_MIN + 1, Xor1(SMI_MIN)); |
| 189 assertEquals(SMI_MIN + 1, Xor1Reversed(SMI_MIN)); |
| 190 assertEquals(43, Xor1(OBJ_42)); |
| 191 assertEquals(43, Xor1Reversed(OBJ_42)); |
| 192 |
| 193 assertEquals(0, Xor100(100)); |
| 194 assertEquals(7, Xor100Reversed(99)); |
| 195 assertEquals(-2147483548, Xor100(SMI_MIN)); |
| 196 assertEquals(-2147483548, Xor100Reversed(SMI_MIN)); |
| 197 assertEquals(78, Xor100(OBJ_42)); |
| 198 assertEquals(78, Xor100Reversed(OBJ_42)); |
| 199 |
| 200 var x = 0x23; var y = 0x35; |
| 201 assertEquals(0x16, x ^ y); |
| 202 |
| 203 |
| 204 // Bitwise not. |
| 205 var v = 0; |
| 206 assertEquals(-1, ~v); |
| 207 v = SMI_MIN; |
| 208 assertEquals(0x7fffffff, ~v, "~smimin"); |
| 209 v = SMI_MAX; |
| 210 assertEquals(-0x80000000, ~v, "~smimax"); |
| 211 |
| 212 // Overflowing ++ and --. |
| 213 v = SMI_MAX; |
| 214 v++; |
| 215 assertEquals(0x80000000, v, "smimax++"); |
| 216 v = SMI_MIN; |
| 217 v--; |
| 218 assertEquals(-0x80000001, v, "smimin--"); |
| 219 |
| 220 // Check that comparisons of numbers separated by MIN_SMI work. |
| 221 assertFalse(SMI_MIN > 0); |
| 222 assertFalse(SMI_MIN + 1 > 1); |
| 223 assertFalse(SMI_MIN + 1 > 2); |
| 224 assertFalse(SMI_MIN + 2 > 1); |
| 225 assertFalse(0 < SMI_MIN); |
| 226 assertTrue(-1 < SMI_MAX); |
| 227 assertFalse(SMI_MAX < -1); |
OLD | NEW |