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