Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #import('../../../fixnum/fixnum.dart'); | |
| 6 | |
| 7 class Int64Test { | |
|
Mads Ager (google)
2012/05/15 07:37:39
No need for the class. We could just use top-level
| |
| 8 | |
| 9 static testMain() { | |
| 10 testAdditive(); | |
| 11 testBitOps(); | |
| 12 testComparisons(); | |
| 13 testConversions(); | |
| 14 testDiv(); | |
| 15 testFactorial(); | |
| 16 testMinMax(); | |
| 17 testMod(); | |
| 18 testMultiplicative(); | |
| 19 testNegate(); | |
| 20 testShift(); | |
| 21 testToHexString(); | |
| 22 testToString(); | |
| 23 } | |
| 24 | |
| 25 static void testAdditive() { | |
| 26 { | |
| 27 int64 n1 = new int64.fromInt(1234); | |
| 28 int64 n2 = new int64.fromInt(9876); | |
| 29 Expect.equals(new int64.fromInt(11110), n1 + n2); | |
| 30 Expect.equals(new int64.fromInt(-8642), n1 - n2); | |
| 31 } | |
| 32 | |
| 33 { | |
| 34 int64 n1 = new int64.fromInt(-1234); | |
| 35 int64 n2 = new int64.fromInt(9876); | |
| 36 Expect.equals(new int64.fromInt(8642), n1 + n2); | |
| 37 Expect.equals(new int64.fromInt(-11110), n1 - n2); | |
| 38 } | |
| 39 | |
| 40 { | |
| 41 int64 n1 = new int64.fromInt(-1234); | |
| 42 int64 n2 = new int64.fromInt(-9876); | |
| 43 Expect.equals(new int64.fromInt(-11110), n1 + n2); | |
| 44 Expect.equals(new int64.fromInt(8642), n1 - n2); | |
| 45 } | |
| 46 | |
| 47 { | |
| 48 int64 n1 = new int64.fromInts(0x12345678, 0xabcdabcd); | |
| 49 int64 n2 = new int64.fromInts(0x77773333, 0x22224444); | |
| 50 Expect.equals(new int64.fromInts(0x89ab89ab, 0xcdeff011), n1 + n2); | |
| 51 Expect.equals(new int64.fromInts(0x9abd2345, 0x89ab6789), n1 - n2); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 static void testBitOps() { | |
| 56 { | |
| 57 int64 n1 = new int64.fromInt(1234); | |
| 58 int64 n2 = new int64.fromInt(9876); | |
| 59 | |
| 60 Expect.equals(new int64.fromInt(1168), n1 & n2); | |
| 61 Expect.equals(new int64.fromInt(9942), n1 | n2); | |
| 62 Expect.equals(new int64.fromInt(8774), n1 ^ n2); | |
| 63 Expect.equals(new int64.fromInt(-1235), ~n1); | |
| 64 Expect.equals(new int64.fromInt(-9877), ~n2); | |
| 65 } | |
| 66 | |
| 67 { | |
| 68 int64 n1 = new int64.fromInt(-1234); | |
| 69 int64 n2 = new int64.fromInt(9876); | |
| 70 Expect.equals(new int64.fromInt(8708), n1 & n2); | |
| 71 Expect.equals(new int64.fromInt(-66), n1 | n2); | |
| 72 Expect.equals(new int64.fromInt(-8774), n1 ^ n2); | |
| 73 Expect.equals(new int64.fromInt(1233), ~n1); | |
| 74 Expect.equals(new int64.fromInt(-9877), ~n2); | |
| 75 } | |
| 76 | |
| 77 { | |
| 78 int64 n1 = new int64.fromInt(0x1234) << 32; | |
| 79 int64 n2 = new int64.fromInt(0x9876) << 32; | |
| 80 Expect.equals(new int64.fromInt(0x1034) << 32, n1 & n2); | |
| 81 Expect.equals(new int64.fromInt(0x9a76) << 32, n1 | n2); | |
| 82 Expect.equals(new int64.fromInt(0x8a42) << 32, n1 ^ n2); | |
| 83 Expect.equals(new int64.fromInts(0xffffedcb, 0xffffffff), ~n1); | |
| 84 Expect.equals(new int64.fromInts(0xffff6789, 0xffffffff), ~n2); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 static void testComparisons() { | |
| 89 Expect.isTrue(new int64.fromInt(10) < new int64.fromInt(11)); | |
| 90 Expect.isTrue(new int64.fromInt(10) <= new int64.fromInt(11)); | |
| 91 Expect.isTrue(!(new int64.fromInt(10) == new int64.fromInt(11))); | |
| 92 Expect.isTrue(!(new int64.fromInt(10) >= new int64.fromInt(11))); | |
| 93 Expect.isTrue(!(new int64.fromInt(10) > new int64.fromInt(11))); | |
| 94 | |
| 95 Expect.isTrue(!(new int64.fromInt(10) < new int64.fromInt(10))); | |
| 96 Expect.isTrue(new int64.fromInt(10) <= new int64.fromInt(10)); | |
| 97 Expect.isTrue(new int64.fromInt(10) == new int64.fromInt(10)); | |
| 98 Expect.isTrue(new int64.fromInt(10) >= new int64.fromInt(10)); | |
| 99 Expect.isTrue(!(new int64.fromInt(10) > new int64.fromInt(10))); | |
| 100 | |
| 101 Expect.isTrue(!(new int64.fromInt(12) < new int64.fromInt(11))); | |
| 102 Expect.isTrue(!(new int64.fromInt(12) <= new int64.fromInt(11))); | |
| 103 Expect.isTrue(!(new int64.fromInt(12) == new int64.fromInt(11))); | |
| 104 Expect.isTrue(new int64.fromInt(12) >= new int64.fromInt(11)); | |
| 105 Expect.isTrue(new int64.fromInt(12) > new int64.fromInt(11)); | |
| 106 | |
| 107 Expect.isTrue(new int64.fromInt(-10) > new int64.fromInt(-11)); | |
| 108 Expect.isTrue(new int64.fromInt(10) > new int64.fromInt(-11)); | |
| 109 Expect.isTrue(!(new int64.fromInt(-10) > new int64.fromInt(11))); | |
| 110 Expect.isTrue(new int64.fromInt(-10) >= new int64.fromInt(-11)); | |
| 111 Expect.isTrue(new int64.fromInt(-10) >= new int64.fromInt(-10)); | |
| 112 Expect.isTrue(!(new int64.fromInt(-10) < new int64.fromInt(-11))); | |
| 113 Expect.isTrue(!(new int64.fromInt(-10) <= new int64.fromInt(-11))); | |
| 114 Expect.isTrue(new int64.fromInt(-10) <= new int64.fromInt(-10)); | |
| 115 Expect.isTrue(new int64.fromInt(-10) == new int64.fromInt(-10)); | |
| 116 Expect.isTrue(!(new int64.fromInt(-10) != new int64.fromInt(-10))); | |
| 117 | |
| 118 // the following three comparisons cannot be implemented by | |
| 119 // subtracting the arguments, because the subtraction causes an overflow | |
| 120 int64 largeNeg = new int64.fromInts(0x82341234, 0x0); | |
| 121 int64 largePos = new int64.fromInts(0x12341234, 0x0); | |
| 122 Expect.isTrue(largeNeg < largePos); | |
| 123 | |
| 124 Expect.isTrue(int64.MIN_VALUE < new int64.fromInt(0)); | |
| 125 Expect.isTrue(new int64.fromInt(0) > int64.MIN_VALUE); | |
| 126 | |
| 127 int64 largePosPlusOne = largePos + new int64.fromInt(1); | |
| 128 | |
| 129 Expect.isTrue(largePos < largePosPlusOne); | |
| 130 Expect.isTrue(largePos <= largePosPlusOne); | |
| 131 Expect.isTrue(!(largePos == largePosPlusOne)); | |
| 132 Expect.isTrue(!(largePos >= largePosPlusOne)); | |
| 133 Expect.isTrue(!(largePos > largePosPlusOne)); | |
| 134 | |
| 135 Expect.isTrue(!(largePos < largePos)); | |
| 136 Expect.isTrue(largePos <= largePos); | |
| 137 Expect.isTrue(largePos == largePos); | |
| 138 Expect.isTrue(largePos >= largePos); | |
| 139 Expect.isTrue(!(largePos > largePos)); | |
| 140 | |
| 141 Expect.isTrue(!(largePosPlusOne < largePos)); | |
| 142 Expect.isTrue(!(largePosPlusOne <= largePos)); | |
| 143 Expect.isTrue(!(largePosPlusOne == largePos)); | |
| 144 Expect.isTrue(largePosPlusOne >= largePos); | |
| 145 Expect.isTrue(largePosPlusOne > largePos); | |
| 146 | |
| 147 try { | |
| 148 new int64.fromInt(17) < null; | |
| 149 Expect.fail("x < null should throw NullPointerException"); | |
| 150 } catch (NullPointerException e) { | |
| 151 } | |
| 152 | |
| 153 try { | |
| 154 new int64.fromInt(17) <= null; | |
| 155 Expect.fail("x <= null should throw NullPointerException"); | |
| 156 } catch (NullPointerException e) { | |
| 157 } | |
| 158 | |
| 159 try { | |
| 160 new int64.fromInt(17) > null; | |
| 161 Expect.fail("x > null should throw NullPointerException"); | |
| 162 } catch (NullPointerException e) { | |
| 163 } | |
| 164 | |
| 165 try { | |
| 166 new int64.fromInt(17) < null; | |
| 167 Expect.fail("x >= null should throw NullPointerException"); | |
| 168 } catch (NullPointerException e) { | |
| 169 } | |
| 170 | |
| 171 Expect.isFalse(new int64.fromInt(17) == null); | |
| 172 } | |
| 173 | |
| 174 static void testConversions() { | |
| 175 Expect.equals(0, new int64.fromInt(0).toInt()); | |
| 176 Expect.equals(100, new int64.fromInt(100).toInt()); | |
| 177 Expect.equals(-100, new int64.fromInt(-100).toInt()); | |
| 178 Expect.equals(2147483647, new int64.fromInt(2147483647).toInt()); | |
| 179 Expect.equals(2147483648, new int64.fromInt(2147483648).toInt()); | |
| 180 Expect.equals(-2147483647, new int64.fromInt(-2147483647).toInt()); | |
| 181 Expect.equals(-2147483648, new int64.fromInt(-2147483648).toInt()); | |
| 182 Expect.equals(4503599627370495, new int64.fromInt(4503599627370495).toInt()) ; | |
|
Mads Ager (google)
2012/05/15 07:37:39
Long lines.
| |
| 183 Expect.equals(4503599627370496, new int64.fromInt(4503599627370496).toInt()) ; | |
| 184 Expect.equals(-4503599627370495, | |
| 185 new int64.fromInt(-4503599627370495).toInt()); | |
| 186 Expect.equals(-4503599627370496, | |
| 187 new int64.fromInt(-4503599627370496).toInt()); | |
| 188 | |
| 189 Expect.equals(new int32.fromInt(0), new int64.fromInt(0).toInt32()); | |
| 190 Expect.equals(new int32.fromInt(1), new int64.fromInt(1).toInt32()); | |
| 191 Expect.equals(new int32.fromInt(-1), new int64.fromInt(-1).toInt32()); | |
| 192 Expect.equals(new int32.fromInt(2147483647), | |
| 193 new int64.fromInt(2147483647).toInt32()); | |
| 194 Expect.equals(new int32.fromInt(-2147483648), | |
| 195 new int64.fromInt(2147483648).toInt32()); | |
| 196 Expect.equals(new int32.fromInt(-2147483647), | |
| 197 new int64.fromInt(2147483649).toInt32()); | |
| 198 Expect.equals(new int32.fromInt(-2147483646), | |
| 199 new int64.fromInt(2147483650).toInt32()); | |
| 200 | |
| 201 Expect.equals(new int32.fromInt(-2147483648), | |
| 202 new int64.fromInt(-2147483648).toInt32()); | |
| 203 Expect.equals(new int32.fromInt(2147483647), | |
| 204 new int64.fromInt(-2147483649).toInt32()); | |
| 205 Expect.equals(new int32.fromInt(2147483646), | |
| 206 new int64.fromInt(-2147483650).toInt32()); | |
| 207 Expect.equals(new int32.fromInt(2147483645), | |
| 208 new int64.fromInt(-2147483651).toInt32()); | |
| 209 } | |
| 210 | |
| 211 static void testDiv() { | |
| 212 int64 deadBeef = new int64.fromInts(0xDEADBEEF, 0xDEADBEEF); | |
| 213 int64 ten = new int64.fromInt(10); | |
| 214 Expect.equals(new int64.fromInts(0xfcaaf97e, 0x63115fe5), deadBeef ~/ ten); | |
| 215 Expect.equals(int64.ZERO, int64.ONE ~/ int64.TWO); | |
| 216 Expect.equals(new int64.fromInts(0x3fffffff, 0xffffffff), | |
| 217 int64.MAX_VALUE ~/ int64.TWO); | |
| 218 | |
| 219 Expect.equals(int64.ZERO, int64.ZERO ~/ new int64.fromInt(1000)); | |
| 220 Expect.equals(int64.ONE, int64.MIN_VALUE ~/ int64.MIN_VALUE); | |
| 221 Expect.equals(int64.ZERO, new int64.fromInt(1000) ~/ int64.MIN_VALUE); | |
| 222 Expect.equals("-1125899906842624", | |
| 223 (int64.MIN_VALUE ~/ new int64.fromInt(8192)).toString()); | |
| 224 Expect.equals("-1125762484664320", | |
| 225 (int64.MIN_VALUE ~/ new int64.fromInt(8193)).toString()); | |
| 226 Expect.equals(int64.ZERO, | |
| 227 new int64.fromInt(-1000) ~/ new int64.fromInt(8192)); | |
| 228 Expect.equals(int64.ZERO, | |
| 229 new int64.fromInt(-1000) ~/ new int64.fromInt(8193)); | |
| 230 Expect.equals(new int64.fromInt(-122070), | |
| 231 new int64.fromInt(-1000000000) ~/ new int64.fromInt(8192)); | |
| 232 Expect.equals(new int64.fromInt(-122055), | |
| 233 new int64.fromInt(-1000000000) ~/ new int64.fromInt(8193)); | |
| 234 Expect.equals(new int64.fromInt(122070), | |
| 235 new int64.fromInt(1000000000) ~/ new int64.fromInt(8192)); | |
| 236 Expect.equals(new int64.fromInt(122055), | |
| 237 new int64.fromInt(1000000000) ~/ new int64.fromInt(8193)); | |
| 238 | |
| 239 Expect.equals(new int64.fromInts(0x1fffff, 0xffffffff), | |
| 240 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x00000400)); | |
| 241 Expect.equals(new int64.fromInts(0x1fff, 0xffffffff), | |
| 242 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x00040000)); | |
| 243 Expect.equals(new int64.fromInts(0x1f, 0xffffffff), | |
| 244 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x04000000)); | |
| 245 Expect.equals(new int64.fromInt(536870911), | |
| 246 int64.MAX_VALUE ~/ new int64.fromInts(0x00000004, 0x00000000)); | |
| 247 Expect.equals(new int64.fromInt(2097151), | |
| 248 int64.MAX_VALUE ~/ new int64.fromInts(0x00000400, 0x00000000)); | |
| 249 Expect.equals(new int64.fromInt(8191), | |
| 250 int64.MAX_VALUE ~/ new int64.fromInts(0x00040000, 0x00000000)); | |
| 251 Expect.equals(new int64.fromInt(31), | |
| 252 int64.MAX_VALUE ~/ new int64.fromInts(0x04000000, 0x00000000)); | |
| 253 | |
| 254 Expect.equals(new int64.fromInts(0x2AAAAA, 0xAAAAAAAA), | |
| 255 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x00000300)); | |
| 256 Expect.equals(new int64.fromInts(0x2, 0xAAAAAAAA), | |
| 257 int64.MAX_VALUE ~/ new int64.fromInts(0x00000000, 0x30000000)); | |
| 258 Expect.equals(new int64.fromInt(0x2AA), | |
| 259 int64.MAX_VALUE ~/ new int64.fromInts(0x00300000, 0x00000000)); | |
| 260 | |
| 261 Expect.equals(new int64.fromInts(0x708, 0x002E9501), | |
| 262 int64.MAX_VALUE ~/ new int64.fromInt(0x123456)); | |
| 263 Expect.equals(new int64.fromInt(0x3BDA9), | |
| 264 int64.MAX_VALUE % new int64.fromInt(0x123456)); | |
| 265 } | |
| 266 | |
| 267 static void testFactorial() { | |
| 268 | |
| 269 int64 _fact(int64 n) { | |
| 270 if (n.isZero()) { | |
| 271 return new int64.fromInt(1); | |
| 272 } else { | |
| 273 return n * _fact(n - new int64.fromInt(1)); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 int64 fact18 = _fact(new int64.fromInt(18)); | |
| 278 int64 fact17 = _fact(new int64.fromInt(17)); | |
| 279 Expect.equals(new int64.fromInt(18), fact18 ~/ fact17); | |
| 280 } | |
| 281 | |
| 282 static void testMinMax() { | |
| 283 Expect.equals(int64.MIN_VALUE, new int64.fromInt(1) << 63); | |
| 284 Expect.equals(int64.MAX_VALUE, -(int64.MIN_VALUE + new int64.fromInt(1))); | |
| 285 } | |
| 286 | |
| 287 // Define % as Euclidean mod, with positive result for all arguments | |
| 288 static void testMod() { | |
| 289 Expect.equals(new int64.fromInt(0), int64.ZERO % new int64.fromInt(1000)); | |
| 290 Expect.equals(new int64.fromInt(0), int64.MIN_VALUE % int64.MIN_VALUE); | |
| 291 Expect.equals(new int64.fromInt(1000), | |
| 292 new int64.fromInt(1000) % int64.MIN_VALUE); | |
| 293 Expect.equals(new int64.fromInt(0), | |
| 294 int64.MIN_VALUE % new int64.fromInt(8192)); | |
| 295 Expect.equals(new int64.fromInt(6145), | |
| 296 int64.MIN_VALUE % new int64.fromInt(8193)); | |
| 297 Expect.equals(new int64.fromInt(7192), | |
| 298 new int64.fromInt(-1000) % new int64.fromInt(8192)); | |
| 299 Expect.equals(new int64.fromInt(7193), | |
| 300 new int64.fromInt(-1000) % new int64.fromInt(8193)); | |
| 301 Expect.equals(new int64.fromInt(5632), | |
| 302 new int64.fromInt(-1000000000) % new int64.fromInt(8192)); | |
| 303 Expect.equals(new int64.fromInt(4808), | |
| 304 new int64.fromInt(-1000000000) % new int64.fromInt(8193)); | |
| 305 Expect.equals(new int64.fromInt(2560), | |
| 306 new int64.fromInt(1000000000) % new int64.fromInt(8192)); | |
| 307 Expect.equals(new int64.fromInt(3385), | |
| 308 new int64.fromInt(1000000000) % new int64.fromInt(8193)); | |
| 309 | |
| 310 Expect.equals(new int64.fromInts(0x0, 0x3ff), | |
| 311 int64.MAX_VALUE % new int64.fromInts(0x00000000, 0x00000400)); | |
| 312 Expect.equals(new int64.fromInts(0x0, 0x3ffff), | |
| 313 int64.MAX_VALUE % new int64.fromInts(0x00000000, 0x00040000)); | |
| 314 Expect.equals(new int64.fromInts(0x0, 0x3ffffff), | |
| 315 int64.MAX_VALUE % new int64.fromInts(0x00000000, 0x04000000)); | |
| 316 Expect.equals(new int64.fromInts(0x3, 0xffffffff), | |
| 317 int64.MAX_VALUE % new int64.fromInts(0x00000004, 0x00000000)); | |
| 318 Expect.equals(new int64.fromInts(0x3ff, 0xffffffff), | |
| 319 int64.MAX_VALUE % new int64.fromInts(0x00000400, 0x00000000)); | |
| 320 Expect.equals(new int64.fromInts(0x3ffff, 0xffffffff), | |
| 321 int64.MAX_VALUE % new int64.fromInts(0x00040000, 0x00000000)); | |
| 322 Expect.equals(new int64.fromInts(0x3ffffff, 0xffffffff), | |
| 323 int64.MAX_VALUE % new int64.fromInts(0x04000000, 0x00000000)); | |
| 324 | |
| 325 Expect.equals(new int64.fromInt(0x12345678.remainder(0x22)), | |
| 326 new int64.fromInt(0x12345678).remainder(new int64.fromInt(0x22))); | |
| 327 Expect.equals(new int64.fromInt(0x12345678.remainder(-0x22)), | |
| 328 new int64.fromInt(0x12345678).remainder(new int64.fromInt(-0x22))); | |
| 329 Expect.equals(new int64.fromInt(-0x12345678.remainder(-0x22)), | |
| 330 new int64.fromInt(-0x12345678).remainder(new int64.fromInt(-0x22))); | |
| 331 Expect.equals(new int64.fromInt(-0x12345678.remainder(0x22)), | |
| 332 new int64.fromInt(-0x12345678).remainder(new int64.fromInt(0x22))); | |
| 333 Expect.equals(new int64.fromInt(0x12345678.remainder(0x22)), | |
| 334 new int32.fromInt(0x12345678).remainder(new int64.fromInt(0x22))); | |
| 335 } | |
| 336 | |
| 337 static void testMultiplicative() { | |
| 338 Expect.equals(new int64.fromInt(3333), | |
| 339 new int64.fromInt(1111) * new int64.fromInt(3)); | |
| 340 Expect.equals(new int64.fromInt(-3333), | |
| 341 new int64.fromInt(1111) * new int64.fromInt(-3)); | |
| 342 Expect.equals(new int64.fromInt(-3333), | |
| 343 new int64.fromInt(-1111) * new int64.fromInt(3)); | |
| 344 Expect.equals(new int64.fromInt(3333), | |
| 345 new int64.fromInt(-1111) * new int64.fromInt(-3)); | |
| 346 Expect.equals(new int64.fromInt(0), | |
| 347 new int64.fromInt(100) * new int64.fromInt(0)); | |
| 348 | |
| 349 Expect.equals(new int64.fromInts(0x7ff63f7c, 0x1df4d840), | |
| 350 new int64.fromInts(0x12345678, 0x12345678) * | |
| 351 new int64.fromInts(0x1234, 0x12345678)); | |
| 352 Expect.equals(new int64.fromInts(0x7ff63f7c, 0x1df4d840), | |
| 353 new int64.fromInts(0xf2345678, 0x12345678) * | |
| 354 new int64.fromInts(0x1234, 0x12345678)); | |
| 355 Expect.equals(new int64.fromInts(0x297e3f7c, 0x1df4d840), | |
| 356 new int64.fromInts(0xf2345678, 0x12345678) * | |
| 357 new int64.fromInts(0xffff1234, 0x12345678)); | |
| 358 | |
| 359 Expect.equals(new int64.fromInt(0), int64.MIN_VALUE * new int64.fromInt(2)); | |
| 360 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE * new int64.fromInt(1)); | |
| 361 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE * new int64.fromInt(-1)); | |
| 362 | |
| 363 Expect.equals(new int64.fromInt(1), new int64.fromInt(5) ~/ | |
| 364 new int64.fromInt(5)); | |
| 365 Expect.equals(new int64.fromInt(333), new int64.fromInt(1000) ~/ | |
| 366 new int64.fromInt(3)); | |
| 367 Expect.equals(new int64.fromInt(-333), new int64.fromInt(1000) ~/ | |
| 368 new int64.fromInt(-3)); | |
| 369 Expect.equals(new int64.fromInt(-333), new int64.fromInt(-1000) ~/ | |
| 370 new int64.fromInt(3)); | |
| 371 Expect.equals(new int64.fromInt(333), new int64.fromInt(-1000) ~/ | |
| 372 new int64.fromInt(-3)); | |
| 373 Expect.equals(new int64.fromInt(0), new int64.fromInt(3) ~/ | |
| 374 new int64.fromInt(1000)); | |
| 375 Expect.equals(new int64.fromInts(0x1003d0, 0xe84f5ae8), new int64.fromInts( | |
| 376 0x12345678, 0x12345678) ~/ new int64.fromInts(0x0, 0x123)); | |
| 377 Expect.equals(new int64.fromInts(0x0, 0x10003), new int64.fromInts( | |
| 378 0x12345678, 0x12345678) ~/ new int64.fromInts(0x1234, 0x12345678)); | |
| 379 Expect.equals(new int64.fromInts(0xffffffff, 0xffff3dfe), | |
| 380 new int64.fromInts(0xf2345678, 0x12345678) ~/ | |
| 381 new int64.fromInts(0x1234, 0x12345678)); | |
| 382 Expect.equals(new int64.fromInts(0x0, 0xeda), new int64.fromInts(0xf2345678, | |
| 383 0x12345678) ~/ new int64.fromInts(0xffff1234, 0x12345678)); | |
| 384 | |
| 385 try { | |
| 386 new int64.fromInt(1) ~/ new int64.fromInt(0); | |
| 387 Expect.fail("Expected an IntegerDivisionByZeroException"); | |
| 388 } catch (IntegerDivisionByZeroException e) { | |
| 389 } | |
| 390 | |
| 391 Expect.equals(new int64.fromInts(0xc0000000, 0x00000000), | |
| 392 int64.MIN_VALUE ~/ new int64.fromInt(2)); | |
| 393 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE ~/ | |
| 394 new int64.fromInt(1)); | |
| 395 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE ~/ | |
| 396 new int64.fromInt(-1)); | |
| 397 } | |
| 398 | |
| 399 static void testNegate() { | |
| 400 Expect.equals(new int64.fromInt(-1), -new int64.fromInt(1)); | |
| 401 Expect.equals(new int64.fromInt(1), -new int64.fromInt(-1)); | |
| 402 Expect.equals(int64.MIN_VALUE, -int64.MIN_VALUE); | |
| 403 } | |
| 404 | |
| 405 static void testShift() { | |
| 406 Expect.equals(new int64.fromInts(0xd048d115, 0x9d159c00), | |
| 407 new int64.fromInts(0x12341234, 0x45674567) << 10); | |
| 408 Expect.equals(new int64.fromInts(0x48d04, 0x8d1159d1), | |
| 409 new int64.fromInts(0x12341234, 0x45674567) >> 10); | |
| 410 Expect.equals(new int64.fromInts(0x48d04, 0x8d1159d1), | |
| 411 new int64.fromInts(0x12341234, 0x45674567).shiftRightUnsigned(10)); | |
| 412 Expect.equals(new int64.fromInts(0xd048d115, 0x9d159c00), | |
| 413 new int64.fromInts(0x92341234, 0x45674567) << 10); | |
| 414 Expect.equals(new int64.fromInts(0xffe48d04, 0x8d1159d1), | |
| 415 new int64.fromInts(0x92341234, 0x45674567) >> 10); | |
| 416 Expect.equals(new int64.fromInt(67108863), | |
| 417 new int64.fromInts(0xFFFFFFF, 0xFFFFFFFF) >> 34); | |
| 418 Expect.equals(new int64.fromInts(0x248d04, 0x8d1159d1), | |
| 419 new int64.fromInts(0x92341234, 0x45674567).shiftRightUnsigned(10)); | |
| 420 | |
| 421 Expect.equals(new int64.fromInt(-1), new int64.fromInt(-1) >> 10); | |
| 422 Expect.equals(new int64.fromInt(-1), new int64.fromInt(-1) >> 63); | |
| 423 | |
| 424 Expect.equals(new int64.fromInt(-1 << 5), new int64.fromInt(-1) << 5); | |
| 425 Expect.equals(new int64.fromInt(-1), new int64.fromInt(-1) << 0); | |
| 426 Expect.equals(-new int64.fromInts(0x40000000, 0x00000000), | |
| 427 (new int64.fromInt(1) << 63) >> 1); | |
| 428 Expect.equals(new int64.fromInt(0), (new int64.fromInt(-1) << 32) << 32); | |
| 429 Expect.equals(int64.MIN_VALUE, int64.MIN_VALUE << 0); | |
| 430 Expect.equals(new int64.fromInt(0), int64.MIN_VALUE << 1); | |
| 431 Expect.equals(new int64.fromInts(0xfffffffc, 0x00000000), | |
| 432 (-new int64.fromInts(8, 0)) >> 1); | |
| 433 Expect.equals(new int64.fromInts(0x7ffffffc, 0x0), | |
| 434 (-new int64.fromInts(8, 0)).shiftRightUnsigned(1)); | |
| 435 | |
| 436 Expect.equals(new int64.fromInts(0x00723456, 0x789abcde), | |
| 437 new int64.fromInts(0x72345678, 0x9abcdef0) >> 8); | |
| 438 Expect.equals(new int64.fromInts(0x00007234, 0x56789abc), | |
| 439 new int64.fromInts(0x72345678, 0x9abcdef0) >> 16); | |
| 440 Expect.equals(new int64.fromInts(0x00000072, 0x3456789a), | |
| 441 new int64.fromInts(0x72345678, 0x9abcdef0) >> 24); | |
| 442 Expect.equals(new int64.fromInts(0x00000007, 0x23456789), | |
| 443 new int64.fromInts(0x72345678, 0x9abcdef0) >> 28); | |
| 444 Expect.equals(new int64.fromInts(0x00000000, 0x72345678), | |
| 445 new int64.fromInts(0x72345678, 0x9abcdef0) >> 32); | |
| 446 Expect.equals(new int64.fromInts(0x00000000, 0x07234567), | |
| 447 new int64.fromInts(0x72345678, 0x9abcdef0) >> 36); | |
| 448 Expect.equals(new int64.fromInts(0x00000000, 0x00723456), | |
| 449 new int64.fromInts(0x72345678, 0x9abcdef0) >> 40); | |
| 450 Expect.equals(new int64.fromInts(0x00000000, 0x00072345), | |
| 451 new int64.fromInts(0x72345678, 0x9abcde00) >> 44); | |
| 452 Expect.equals(new int64.fromInts(0x00000000, 0x00007234), | |
| 453 new int64.fromInts(0x72345678, 0x9abcdef0) >> 48); | |
| 454 | |
| 455 Expect.equals(new int64.fromInts(0x00723456, 0x789abcde), | |
| 456 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(8)); | |
| 457 Expect.equals(new int64.fromInts(0x00007234, 0x56789abc), | |
| 458 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(16)); | |
| 459 Expect.equals(new int64.fromInts(0x00000072, 0x3456789a), | |
| 460 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(24)); | |
| 461 Expect.equals(new int64.fromInts(0x00000007, 0x23456789), | |
| 462 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(28)); | |
| 463 Expect.equals(new int64.fromInts(0x00000000, 0x72345678), | |
| 464 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(32)); | |
| 465 Expect.equals(new int64.fromInts(0x00000000, 0x07234567), | |
| 466 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(36)); | |
| 467 Expect.equals(new int64.fromInts(0x00000000, 0x00723456), | |
| 468 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(40)); | |
| 469 Expect.equals(new int64.fromInts(0x00000000, 0x00072345), | |
| 470 new int64.fromInts(0x72345678, 0x9abcde00).shiftRightUnsigned(44)); | |
| 471 Expect.equals(new int64.fromInts(0x00000000, 0x00007234), | |
| 472 new int64.fromInts(0x72345678, 0x9abcdef0).shiftRightUnsigned(48)); | |
| 473 | |
| 474 Expect.equals(new int64.fromInts(0xff923456, 0x789abcde), | |
| 475 new int64.fromInts(0x92345678, 0x9abcdef0) >> 8); | |
| 476 Expect.equals(new int64.fromInts(0xffff9234, 0x56789abc), | |
| 477 new int64.fromInts(0x92345678, 0x9abcdef0) >> 16); | |
| 478 Expect.equals(new int64.fromInts(0xffffff92, 0x3456789a), | |
| 479 new int64.fromInts(0x92345678, 0x9abcdef0) >> 24); | |
| 480 Expect.equals(new int64.fromInts(0xfffffff9, 0x23456789), | |
| 481 new int64.fromInts(0x92345678, 0x9abcdef0) >> 28); | |
| 482 Expect.equals(new int64.fromInts(0xffffffff, 0x92345678), | |
| 483 new int64.fromInts(0x92345678, 0x9abcdef0) >> 32); | |
| 484 Expect.equals(new int64.fromInts(0xffffffff, 0xf9234567), | |
| 485 new int64.fromInts(0x92345678, 0x9abcdef0) >> 36); | |
| 486 Expect.equals(new int64.fromInts(0xffffffff, 0xff923456), | |
| 487 new int64.fromInts(0x92345678, 0x9abcdef0) >> 40); | |
| 488 Expect.equals(new int64.fromInts(0xffffffff, 0xfff92345), | |
| 489 new int64.fromInts(0x92345678, 0x9abcdef0) >> 44); | |
| 490 Expect.equals(new int64.fromInts(0xffffffff, 0xffff9234), | |
| 491 new int64.fromInts(0x92345678, 0x9abcdef0) >> 48); | |
| 492 | |
| 493 Expect.equals(new int64.fromInts(0x00923456, 0x789abcde), | |
| 494 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(8)); | |
| 495 Expect.equals(new int64.fromInts(0x00009234, 0x56789abc), | |
| 496 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(16)); | |
| 497 Expect.equals(new int64.fromInts(0x00000092, 0x3456789a), | |
| 498 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(24)); | |
| 499 Expect.equals(new int64.fromInts(0x00000009, 0x23456789), | |
| 500 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(28)); | |
| 501 Expect.equals(new int64.fromInts(0x00000000, 0x92345678), | |
| 502 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(32)); | |
| 503 Expect.equals(new int64.fromInts(0x00000000, 0x09234567), | |
| 504 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(36)); | |
| 505 Expect.equals(new int64.fromInts(0x00000000, 0x00923456), | |
| 506 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(40)); | |
| 507 Expect.equals(new int64.fromInts(0x00000000, 0x00092345), | |
| 508 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(44)); | |
| 509 Expect.equals(new int64.fromInts(0x00000000, 0x00009234), | |
| 510 new int64.fromInts(0x92345678, 0x9abcdef0).shiftRightUnsigned(48)); | |
| 511 | |
| 512 try { | |
| 513 new int64.fromInt(17) >> -1; | |
| 514 Expect.fail("x >> -1 should throw IllegalArgumentException"); | |
| 515 } catch (IllegalArgumentException e) { | |
| 516 } | |
| 517 | |
| 518 try { | |
| 519 new int64.fromInt(17) << -1; | |
| 520 Expect.fail("x >> -1 should throw IllegalArgumentException"); | |
| 521 } catch (IllegalArgumentException e) { | |
| 522 } | |
| 523 | |
| 524 try { | |
| 525 new int64.fromInt(17).shiftRightUnsigned(-1); | |
| 526 Expect.fail("x >> -1 should throw IllegalArgumentException"); | |
| 527 } catch (IllegalArgumentException e) { | |
| 528 } | |
| 529 | |
| 530 } | |
| 531 | |
| 532 static void testToHexString() { | |
| 533 int64 deadbeef12341234 = new int64.fromInts(0xDEADBEEF, 0x12341234); | |
| 534 Expect.equals("0", int64.ZERO.toHexString()); | |
| 535 Expect.equals("DEADBEEF12341234", deadbeef12341234.toHexString()); | |
| 536 } | |
| 537 | |
| 538 static void testToString() { | |
| 539 Expect.equals("0", new int64.fromInt(0).toString()); | |
| 540 Expect.equals("1", new int64.fromInt(1).toString()); | |
| 541 Expect.equals("-1", new int64.fromInt(-1).toString()); | |
| 542 Expect.equals("-10", new int64.fromInt(-10).toString()); | |
| 543 Expect.equals("-9223372036854775808", int64.MIN_VALUE.toString()); | |
| 544 Expect.equals("9223372036854775807", int64.MAX_VALUE.toString()); | |
| 545 | |
| 546 int top = 922337201; | |
| 547 int bottom = 967490662; | |
| 548 int64 fullnum = (new int64.fromInt(1000000000) * new int64.fromInt(top)) + | |
| 549 new int64.fromInt(bottom); | |
| 550 | |
| 551 Expect.equals("922337201967490662", fullnum.toString()); | |
| 552 Expect.equals("-922337201967490662", (-fullnum).toString()); | |
| 553 | |
| 554 Expect.equals("17678A7DEF01234", | |
| 555 new int64.fromInts(0x17678A7, 0xDEF01234).toHexString()); | |
| 556 | |
| 557 Expect.equals("123456789", new int64.fromInt(123456789).toString()); | |
| 558 Expect.equals("75BCD15", new int64.fromInt(123456789).toHexString()); | |
| 559 Expect.equals("223101104124", new int64.fromInt(123456789).toRadixString(5)) ; | |
| 560 | |
| 561 Expect.equals( | |
| 562 "-1000000000000000000000000000000000000000000000000000000000000000", | |
| 563 int64.MIN_VALUE.toRadixString(2)); | |
| 564 Expect.equals("-2021110011022210012102010021220101220222", | |
| 565 int64.MIN_VALUE.toRadixString(3)); | |
| 566 Expect.equals("-20000000000000000000000000000000", | |
| 567 int64.MIN_VALUE.toRadixString(4)); | |
| 568 Expect.equals("-1104332401304422434310311213", | |
| 569 int64.MIN_VALUE.toRadixString(5)); | |
| 570 Expect.equals("-1540241003031030222122212", int64.MIN_VALUE.toRadixString(6) ); | |
| 571 Expect.equals("-22341010611245052052301", int64.MIN_VALUE.toRadixString(7)); | |
| 572 Expect.equals("-1000000000000000000000", int64.MIN_VALUE.toRadixString(8)); | |
| 573 Expect.equals("-67404283172107811828", int64.MIN_VALUE.toRadixString(9)); | |
| 574 Expect.equals("-9223372036854775808", int64.MIN_VALUE.toRadixString(10)); | |
| 575 Expect.equals("-1728002635214590698", int64.MIN_VALUE.toRadixString(11)); | |
| 576 Expect.equals("-41A792678515120368", int64.MIN_VALUE.toRadixString(12)); | |
| 577 Expect.equals("-10B269549075433C38", int64.MIN_VALUE.toRadixString(13)); | |
| 578 Expect.equals("-4340724C6C71DC7A8", int64.MIN_VALUE.toRadixString(14)); | |
| 579 Expect.equals("-160E2AD3246366808", int64.MIN_VALUE.toRadixString(15)); | |
| 580 Expect.equals("-8000000000000000", int64.MIN_VALUE.toRadixString(16)); | |
| 581 | |
| 582 Expect.equals( | |
| 583 "111111111111111111111111111111111111111111111111111111111111111", | |
| 584 int64.MAX_VALUE.toRadixString(2)); | |
| 585 Expect.equals("2021110011022210012102010021220101220221", | |
| 586 int64.MAX_VALUE.toRadixString(3)); | |
| 587 Expect.equals("13333333333333333333333333333333", | |
| 588 int64.MAX_VALUE.toRadixString(4)); | |
| 589 Expect.equals("1104332401304422434310311212", | |
| 590 int64.MAX_VALUE.toRadixString(5)); | |
| 591 Expect.equals("1540241003031030222122211", int64.MAX_VALUE.toRadixString(6)) ; | |
| 592 Expect.equals("22341010611245052052300", int64.MAX_VALUE.toRadixString(7)); | |
| 593 Expect.equals("777777777777777777777", int64.MAX_VALUE.toRadixString(8)); | |
| 594 Expect.equals("67404283172107811827", int64.MAX_VALUE.toRadixString(9)); | |
| 595 Expect.equals("9223372036854775807", int64.MAX_VALUE.toRadixString(10)); | |
| 596 Expect.equals("1728002635214590697", int64.MAX_VALUE.toRadixString(11)); | |
| 597 Expect.equals("41A792678515120367", int64.MAX_VALUE.toRadixString(12)); | |
| 598 Expect.equals("10B269549075433C37", int64.MAX_VALUE.toRadixString(13)); | |
| 599 Expect.equals("4340724C6C71DC7A7", int64.MAX_VALUE.toRadixString(14)); | |
| 600 Expect.equals("160E2AD3246366807", int64.MAX_VALUE.toRadixString(15)); | |
| 601 Expect.equals("7FFFFFFFFFFFFFFF", int64.MAX_VALUE.toRadixString(16)); | |
| 602 } | |
| 603 } | |
| 604 | |
| 605 main() { | |
| 606 Int64Test.testMain(); | |
| 607 } | |
| 608 | |
| OLD | NEW |