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