ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | const SMI_MAX = (1 << 30) - 1; |
| 29 | const SMI_MIN = -(1 << 30); |
| 30 | const ONE = 1; |
| 31 | const ONE_HUNDRED = 100; |
| 32 | |
| 33 | const OBJ_42 = new (function() { |
| 34 | this.valueOf = function() { return 42; }; |
| 35 | })(); |
| 36 | |
| 37 | assertEquals(42, OBJ_42.valueOf()); |
| 38 | |
| 39 | |
| 40 | function Add1(x) { |
| 41 | return x + 1; |
| 42 | } |
| 43 | |
| 44 | function Add100(x) { |
| 45 | return x + 100; |
| 46 | } |
| 47 | |
| 48 | function Add1Reversed(x) { |
| 49 | return 1 + x; |
| 50 | } |
| 51 | |
| 52 | function Add100Reversed(x) { |
| 53 | return 100 + x; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | assertEquals(1, Add1(0)); // fast case |
| 58 | assertEquals(1, Add1Reversed(0)); // fast case |
| 59 | assertEquals(SMI_MAX + ONE, Add1(SMI_MAX)); // overflow |
| 60 | assertEquals(SMI_MAX + ONE, Add1Reversed(SMI_MAX)); // overflow |
| 61 | assertEquals(42 + ONE, Add1(OBJ_42)); // non-smi |
| 62 | assertEquals(42 + ONE, Add1Reversed(OBJ_42)); // non-smi |
| 63 | |
| 64 | assertEquals(100, Add100(0)); // fast case |
| 65 | assertEquals(100, Add100Reversed(0)); // fast case |
| 66 | assertEquals(SMI_MAX + ONE_HUNDRED, Add100(SMI_MAX)); // overflow |
| 67 | assertEquals(SMI_MAX + ONE_HUNDRED, Add100Reversed(SMI_MAX)); // overflow |
| 68 | assertEquals(42 + ONE_HUNDRED, Add100(OBJ_42)); // non-smi |
| 69 | assertEquals(42 + ONE_HUNDRED, Add100Reversed(OBJ_42)); // non-smi |
| 70 | |
| 71 | |
| 72 | |
| 73 | function Sub1(x) { |
| 74 | return x - 1; |
| 75 | } |
| 76 | |
| 77 | function Sub100(x) { |
| 78 | return x - 100; |
| 79 | } |
| 80 | |
| 81 | function Sub1Reversed(x) { |
| 82 | return 1 - x; |
| 83 | } |
| 84 | |
| 85 | function Sub100Reversed(x) { |
| 86 | return 100 - x; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | assertEquals(0, Sub1(1)); // fast case |
| 91 | assertEquals(-1, Sub1Reversed(2)); // fast case |
| 92 | assertEquals(SMI_MIN - ONE, Sub1(SMI_MIN)); // overflow |
| 93 | assertEquals(ONE - SMI_MIN, Sub1Reversed(SMI_MIN)); // overflow |
| 94 | assertEquals(42 - ONE, Sub1(OBJ_42)); // non-smi |
| 95 | assertEquals(ONE - 42, Sub1Reversed(OBJ_42)); // non-smi |
| 96 | |
| 97 | assertEquals(0, Sub100(100)); // fast case |
| 98 | assertEquals(1, Sub100Reversed(99)); // fast case |
| 99 | assertEquals(SMI_MIN - ONE_HUNDRED, Sub100(SMI_MIN)); // overflow |
| 100 | assertEquals(ONE_HUNDRED - SMI_MIN, Sub100Reversed(SMI_MIN)); // overflow |
| 101 | assertEquals(42 - ONE_HUNDRED, Sub100(OBJ_42)); // non-smi |
| 102 | assertEquals(ONE_HUNDRED - 42, Sub100Reversed(OBJ_42)); // non-smi |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | function Shr1(x) { |
| 106 | return x >>> 1; |
| 107 | } |
| 108 | |
| 109 | function Shr100(x) { |
| 110 | return x >>> 100; |
| 111 | } |
| 112 | |
| 113 | function Shr1Reversed(x) { |
| 114 | return 1 >>> x; |
| 115 | } |
| 116 | |
| 117 | function Shr100Reversed(x) { |
| 118 | return 100 >>> x; |
| 119 | } |
| 120 | |
| 121 | function Sar1(x) { |
| 122 | return x >> 1; |
| 123 | } |
| 124 | |
| 125 | function Sar100(x) { |
| 126 | return x >> 100; |
| 127 | } |
| 128 | |
| 129 | function Sar1Reversed(x) { |
| 130 | return 1 >> x; |
| 131 | } |
| 132 | |
| 133 | function Sar100Reversed(x) { |
| 134 | return 100 >> x; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | assertEquals(0, Shr1(1)); |
| 139 | assertEquals(0, Sar1(1)); |
| 140 | assertEquals(0, Shr1Reversed(2)); |
| 141 | assertEquals(0, Sar1Reversed(2)); |
| 142 | assertEquals(1610612736, Shr1(SMI_MIN)); |
| 143 | assertEquals(-536870912, Sar1(SMI_MIN)); |
| 144 | assertEquals(1, Shr1Reversed(SMI_MIN)); |
| 145 | assertEquals(1, Sar1Reversed(SMI_MIN)); |
| 146 | assertEquals(21, Shr1(OBJ_42)); |
| 147 | assertEquals(21, Sar1(OBJ_42)); |
| 148 | assertEquals(0, Shr1Reversed(OBJ_42)); |
| 149 | assertEquals(0, Sar1Reversed(OBJ_42)); |
| 150 | |
| 151 | assertEquals(6, Shr100(100)); |
| 152 | assertEquals(6, Sar100(100)); |
| 153 | assertEquals(12, Shr100Reversed(99)); |
| 154 | assertEquals(12, Sar100Reversed(99)); |
| 155 | assertEquals(201326592, Shr100(SMI_MIN)); |
| 156 | assertEquals(-67108864, Sar100(SMI_MIN)); |
| 157 | assertEquals(100, Shr100Reversed(SMI_MIN)); |
| 158 | assertEquals(100, Sar100Reversed(SMI_MIN)); |
| 159 | assertEquals(2, Shr100(OBJ_42)); |
| 160 | assertEquals(2, Sar100(OBJ_42)); |
| 161 | assertEquals(0, Shr100Reversed(OBJ_42)); |
| 162 | assertEquals(0, Sar100Reversed(OBJ_42)); |
| 163 | |
| 164 | |
| 165 | function Xor1(x) { |
| 166 | return x ^ 1; |
| 167 | } |
| 168 | |
| 169 | function Xor100(x) { |
| 170 | return x ^ 100; |
| 171 | } |
| 172 | |
| 173 | function Xor1Reversed(x) { |
| 174 | return 1 ^ x; |
| 175 | } |
| 176 | |
| 177 | function Xor100Reversed(x) { |
| 178 | return 100 ^ x; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | assertEquals(0, Xor1(1)); |
| 183 | assertEquals(3, Xor1Reversed(2)); |
| 184 | assertEquals(SMI_MIN + 1, Xor1(SMI_MIN)); |
| 185 | assertEquals(SMI_MIN + 1, Xor1Reversed(SMI_MIN)); |
| 186 | assertEquals(43, Xor1(OBJ_42)); |
| 187 | assertEquals(43, Xor1Reversed(OBJ_42)); |
| 188 | |
| 189 | assertEquals(0, Xor100(100)); |
| 190 | assertEquals(7, Xor100Reversed(99)); |
| 191 | assertEquals(-1073741724, Xor100(SMI_MIN)); |
| 192 | assertEquals(-1073741724, Xor100Reversed(SMI_MIN)); |
| 193 | assertEquals(78, Xor100(OBJ_42)); |
| 194 | assertEquals(78, Xor100Reversed(OBJ_42)); |
| 195 | |
| 196 | var x = 0x23; var y = 0x35; |
| 197 | assertEquals(0x16, x ^ y); |