| 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 Int32test; | |
| 6 import 'package:fixnum/fixnum.dart'; | |
| 7 import 'package:unittest/unittest.dart'; | |
| 8 | |
| 9 void main() { | |
| 10 group("isX tests", () { | |
| 11 test("isEven", () { | |
| 12 expect((-Int32.ONE).isEven, false); | |
| 13 expect(Int32.ZERO.isEven, true); | |
| 14 expect(Int32.ONE.isEven, false); | |
| 15 expect(Int32.TWO.isEven, true); | |
| 16 }); | |
| 17 test("isMaxValue", () { | |
| 18 expect(Int32.MIN_VALUE.isMaxValue, false); | |
| 19 expect(Int32.ZERO.isMaxValue, false); | |
| 20 expect(Int32.MAX_VALUE.isMaxValue, true); | |
| 21 }); | |
| 22 test("isMinValue", () { | |
| 23 expect(Int32.MIN_VALUE.isMinValue, true); | |
| 24 expect(Int32.ZERO.isMinValue, false); | |
| 25 expect(Int32.MAX_VALUE.isMinValue, false); | |
| 26 }); | |
| 27 test("isNegative", () { | |
| 28 expect(Int32.MIN_VALUE.isNegative, true); | |
| 29 expect(Int32.ZERO.isNegative, false); | |
| 30 expect(Int32.ONE.isNegative, false); | |
| 31 }); | |
| 32 test("isOdd", () { | |
| 33 expect((-Int32.ONE).isOdd, true); | |
| 34 expect(Int32.ZERO.isOdd, false); | |
| 35 expect(Int32.ONE.isOdd, true); | |
| 36 expect(Int32.TWO.isOdd, false); | |
| 37 }); | |
| 38 test("isZero", () { | |
| 39 expect(Int32.MIN_VALUE.isZero, false); | |
| 40 expect(Int32.ZERO.isZero, true); | |
| 41 expect(Int32.MAX_VALUE.isZero, false); | |
| 42 }); | |
| 43 test("bitLength", () { | |
| 44 expect(new Int32(-2).bitLength, 1); | |
| 45 expect((-Int32.ONE).bitLength, 0); | |
| 46 expect(Int32.ZERO.bitLength, 0); | |
| 47 expect(Int32.ONE.bitLength, 1); | |
| 48 expect(new Int32(2).bitLength, 2); | |
| 49 expect(Int32.MAX_VALUE.bitLength, 31); | |
| 50 expect(Int32.MIN_VALUE.bitLength, 31); | |
| 51 }); | |
| 52 }); | |
| 53 | |
| 54 group("arithmetic operators", () { | |
| 55 Int32 n1 = new Int32(1234); | |
| 56 Int32 n2 = new Int32(9876); | |
| 57 Int32 n3 = new Int32(-1234); | |
| 58 Int32 n4 = new Int32(-9876); | |
| 59 | |
| 60 test("+", () { | |
| 61 expect(n1 + n2, new Int32(11110)); | |
| 62 expect(n3 + n2, new Int32(8642)); | |
| 63 expect(n3 + n4, new Int32(-11110)); | |
| 64 expect(Int32.MAX_VALUE + 1, Int32.MIN_VALUE); | |
| 65 expect(() => new Int32(17) + null, throws); | |
| 66 }); | |
| 67 | |
| 68 test("-", () { | |
| 69 expect(n1 - n2, new Int32(-8642)); | |
| 70 expect(n3 - n2, new Int32(-11110)); | |
| 71 expect(n3 - n4, new Int32(8642)); | |
| 72 expect(Int32.MIN_VALUE - 1, Int32.MAX_VALUE); | |
| 73 expect(() => new Int32(17) - null, throws); | |
| 74 }); | |
| 75 | |
| 76 test("unary -", () { | |
| 77 expect(-n1, new Int32(-1234)); | |
| 78 expect(-Int32.ZERO, Int32.ZERO); | |
| 79 }); | |
| 80 | |
| 81 test("*", () { | |
| 82 expect(n1 * n2, new Int32(12186984)); | |
| 83 expect(n2 * n3, new Int32(-12186984)); | |
| 84 expect(n3 * n3, new Int32(1522756)); | |
| 85 expect(n3 * n2, new Int32(-12186984)); | |
| 86 expect(new Int32(0x12345678) * new Int32(0x22222222), | |
| 87 new Int32(-899716112)); | |
| 88 expect((new Int32(123456789) * new Int32(987654321)), | |
| 89 new Int32(-67153019)); | |
| 90 expect(new Int32(0x12345678) * new Int64(0x22222222), | |
| 91 new Int64.fromInts(0x026D60DC, 0xCA5F6BF0)); | |
| 92 expect((new Int32(123456789) * 987654321), | |
| 93 new Int32(-67153019)); | |
| 94 expect(() => new Int32(17) * null, throws); | |
| 95 }); | |
| 96 | |
| 97 test("~/", () { | |
| 98 expect(new Int32(829893893) ~/ new Int32(1919), new Int32(432461)); | |
| 99 expect(new Int32(0x12345678) ~/ new Int32(0x22), | |
| 100 new Int32(0x12345678 ~/ 0x22)); | |
| 101 expect(new Int32(829893893) ~/ new Int64(1919), new Int32(432461)); | |
| 102 expect(new Int32(0x12345678) ~/ new Int64(0x22), | |
| 103 new Int32(0x12345678 ~/ 0x22)); | |
| 104 expect(new Int32(829893893) ~/ 1919, new Int32(432461)); | |
| 105 expect(() => new Int32(17) ~/ Int32.ZERO, throws); | |
| 106 expect(() => new Int32(17) ~/ null, throws); | |
| 107 }); | |
| 108 | |
| 109 test("%", () { | |
| 110 expect(new Int32(0x12345678) % new Int32(0x22), | |
| 111 new Int32(0x12345678 % 0x22)); | |
| 112 expect(new Int32(0x12345678) % new Int64(0x22), | |
| 113 new Int32(0x12345678 % 0x22)); | |
| 114 expect(() => new Int32(17) % null, throws); | |
| 115 }); | |
| 116 | |
| 117 test("remainder", () { | |
| 118 expect(new Int32(0x12345678).remainder(new Int32(0x22)), | |
| 119 new Int32(0x12345678.remainder(0x22))); | |
| 120 expect(new Int32(0x12345678).remainder(new Int32(-0x22)), | |
| 121 new Int32(0x12345678.remainder(-0x22))); | |
| 122 expect(new Int32(-0x12345678).remainder(new Int32(-0x22)), | |
| 123 new Int32(-0x12345678.remainder(-0x22))); | |
| 124 expect(new Int32(-0x12345678).remainder(new Int32(0x22)), | |
| 125 new Int32(-0x12345678.remainder(0x22))); | |
| 126 expect(new Int32(0x12345678).remainder(new Int64(0x22)), | |
| 127 new Int32(0x12345678.remainder(0x22))); | |
| 128 expect(() => new Int32(17).remainder(null), throws); | |
| 129 }); | |
| 130 | |
| 131 test("clamp", () { | |
| 132 Int32 val = new Int32(17); | |
| 133 expect(val.clamp(20, 30), new Int32(20)); | |
| 134 expect(val.clamp(10, 20), new Int32(17)); | |
| 135 expect(val.clamp(10, 15), new Int32(15)); | |
| 136 | |
| 137 expect(val.clamp(new Int32(20), new Int32(30)), new Int32(20)); | |
| 138 expect(val.clamp(new Int32(10), new Int32(20)), new Int32(17)); | |
| 139 expect(val.clamp(new Int32(10), new Int32(15)), new Int32(15)); | |
| 140 | |
| 141 expect(val.clamp(new Int64(20), new Int64(30)), new Int32(20)); | |
| 142 expect(val.clamp(new Int64(10), new Int64(20)), new Int32(17)); | |
| 143 expect(val.clamp(new Int64(10), new Int64(15)), new Int32(15)); | |
| 144 expect(val.clamp(Int64.MIN_VALUE, new Int64(30)), new Int32(17)); | |
| 145 expect(val.clamp(new Int64(10), Int64.MAX_VALUE), new Int32(17)); | |
| 146 | |
| 147 expect(() => val.clamp(1, 'b'), throwsA(isArgumentError)); | |
| 148 expect(() => val.clamp('a', 1), throwsA(isArgumentError)); | |
| 149 }); | |
| 150 }); | |
| 151 | |
| 152 group("comparison operators", () { | |
| 153 test("<", () { | |
| 154 expect(new Int32(17) < new Int32(18), true); | |
| 155 expect(new Int32(17) < new Int32(17), false); | |
| 156 expect(new Int32(17) < new Int32(16), false); | |
| 157 expect(new Int32(17) < new Int64(18), true); | |
| 158 expect(new Int32(17) < new Int64(17), false); | |
| 159 expect(new Int32(17) < new Int64(16), false); | |
| 160 expect(Int32.MIN_VALUE < Int32.MAX_VALUE, true); | |
| 161 expect(Int32.MAX_VALUE < Int32.MIN_VALUE, false); | |
| 162 expect(() => new Int32(17) < null, throws); | |
| 163 }); | |
| 164 | |
| 165 test("<=", () { | |
| 166 expect(new Int32(17) <= new Int32(18), true); | |
| 167 expect(new Int32(17) <= new Int32(17), true); | |
| 168 expect(new Int32(17) <= new Int32(16), false); | |
| 169 expect(new Int32(17) <= new Int64(18), true); | |
| 170 expect(new Int32(17) <= new Int64(17), true); | |
| 171 expect(new Int32(17) <= new Int64(16), false); | |
| 172 expect(Int32.MIN_VALUE <= Int32.MAX_VALUE, true); | |
| 173 expect(Int32.MAX_VALUE <= Int32.MIN_VALUE, false); | |
| 174 expect(() => new Int32(17) <= null, throws); | |
| 175 }); | |
| 176 | |
| 177 test("==", () { | |
| 178 expect(new Int32(17) == new Int32(18), false); | |
| 179 expect(new Int32(17) == new Int32(17), true); | |
| 180 expect(new Int32(17) == new Int32(16), false); | |
| 181 expect(new Int32(17) == new Int64(18), false); | |
| 182 expect(new Int32(17) == new Int64(17), true); | |
| 183 expect(new Int32(17) == new Int64(16), false); | |
| 184 expect(Int32.MIN_VALUE == Int32.MAX_VALUE, false); | |
| 185 expect(new Int32(17) == new Object(), false); | |
| 186 expect(new Int32(17) == null, false); | |
| 187 }); | |
| 188 | |
| 189 test(">=", () { | |
| 190 expect(new Int32(17) >= new Int32(18), false); | |
| 191 expect(new Int32(17) >= new Int32(17), true); | |
| 192 expect(new Int32(17) >= new Int32(16), true); | |
| 193 expect(new Int32(17) >= new Int64(18), false); | |
| 194 expect(new Int32(17) >= new Int64(17), true); | |
| 195 expect(new Int32(17) >= new Int64(16), true); | |
| 196 expect(Int32.MIN_VALUE >= Int32.MAX_VALUE, false); | |
| 197 expect(Int32.MAX_VALUE >= Int32.MIN_VALUE, true); | |
| 198 expect(() => new Int32(17) >= null, throws); | |
| 199 }); | |
| 200 | |
| 201 test(">", () { | |
| 202 expect(new Int32(17) > new Int32(18), false); | |
| 203 expect(new Int32(17) > new Int32(17), false); | |
| 204 expect(new Int32(17) > new Int32(16), true); | |
| 205 expect(new Int32(17) > new Int64(18), false); | |
| 206 expect(new Int32(17) > new Int64(17), false); | |
| 207 expect(new Int32(17) > new Int64(16), true); | |
| 208 expect(Int32.MIN_VALUE > Int32.MAX_VALUE, false); | |
| 209 expect(Int32.MAX_VALUE > Int32.MIN_VALUE, true); | |
| 210 expect(() => new Int32(17) > null, throws); | |
| 211 }); | |
| 212 }); | |
| 213 | |
| 214 group("bitwise operators", () { | |
| 215 test("&", () { | |
| 216 expect(new Int32(0x12345678) & new Int32(0x22222222), | |
| 217 new Int32(0x12345678 & 0x22222222)); | |
| 218 expect(new Int32(0x12345678) & new Int64(0x22222222), | |
| 219 new Int64(0x12345678 & 0x22222222)); | |
| 220 expect(() => new Int32(17) & null, throwsArgumentError); | |
| 221 }); | |
| 222 | |
| 223 test("|", () { | |
| 224 expect(new Int32(0x12345678) | new Int32(0x22222222), | |
| 225 new Int32(0x12345678 | 0x22222222)); | |
| 226 expect(new Int32(0x12345678) | new Int64(0x22222222), | |
| 227 new Int64(0x12345678 | 0x22222222)); | |
| 228 expect(() => new Int32(17) | null, throws); | |
| 229 }); | |
| 230 | |
| 231 test("^", () { | |
| 232 expect(new Int32(0x12345678) ^ new Int32(0x22222222), | |
| 233 new Int32(0x12345678 ^ 0x22222222)); | |
| 234 expect(new Int32(0x12345678) ^ new Int64(0x22222222), | |
| 235 new Int64(0x12345678 ^ 0x22222222)); | |
| 236 expect(() => new Int32(17) ^ null, throws); | |
| 237 }); | |
| 238 | |
| 239 test("~", () { | |
| 240 expect(~(new Int32(0x12345678)), new Int32(~0x12345678)); | |
| 241 expect(-(new Int32(0x12345678)), new Int64(-0x12345678)); | |
| 242 }); | |
| 243 }); | |
| 244 | |
| 245 group("bitshift operators", () { | |
| 246 test("<<", () { | |
| 247 expect(new Int32(0x12345678) << 7, new Int32(0x12345678 << 7)); | |
| 248 expect(() => new Int32(17) << -1, throwsArgumentError); | |
| 249 expect(() => new Int32(17) << null, throws); | |
| 250 }); | |
| 251 | |
| 252 test(">>", () { | |
| 253 expect(new Int32(0x12345678) >> 7, new Int32(0x12345678 >> 7)); | |
| 254 expect(() => new Int32(17) >> -1, throwsArgumentError); | |
| 255 expect(() => new Int32(17) >> null, throws); | |
| 256 }); | |
| 257 | |
| 258 test("shiftRightUnsigned", () { | |
| 259 expect(new Int32(0x12345678).shiftRightUnsigned(7), | |
| 260 new Int32(0x12345678 >> 7)); | |
| 261 expect(() => (new Int32(17).shiftRightUnsigned(-1)), throwsArgumentError); | |
| 262 expect(() => (new Int32(17).shiftRightUnsigned(null)), throws); | |
| 263 }); | |
| 264 }); | |
| 265 | |
| 266 group("conversions", () { | |
| 267 test("toSigned", () { | |
| 268 expect(Int32.ONE.toSigned(2), Int32.ONE); | |
| 269 expect(Int32.ONE.toSigned(1), -Int32.ONE); | |
| 270 expect(Int32.MAX_VALUE.toSigned(32), Int32.MAX_VALUE); | |
| 271 expect(Int32.MIN_VALUE.toSigned(32), Int32.MIN_VALUE); | |
| 272 expect(Int32.MAX_VALUE.toSigned(31), -Int32.ONE); | |
| 273 expect(Int32.MIN_VALUE.toSigned(31), Int32.ZERO); | |
| 274 expect(() => Int32.ONE.toSigned(0), throwsRangeError); | |
| 275 expect(() => Int32.ONE.toSigned(33), throwsRangeError); | |
| 276 }); | |
| 277 test("toUnsigned", () { | |
| 278 expect(Int32.ONE.toUnsigned(1), Int32.ONE); | |
| 279 expect(Int32.ONE.toUnsigned(0), Int32.ZERO); | |
| 280 expect(Int32.MAX_VALUE.toUnsigned(32), Int32.MAX_VALUE); | |
| 281 expect(Int32.MIN_VALUE.toUnsigned(32), Int32.MIN_VALUE); | |
| 282 expect(Int32.MAX_VALUE.toUnsigned(31), Int32.MAX_VALUE); | |
| 283 expect(Int32.MIN_VALUE.toUnsigned(31), Int32.ZERO); | |
| 284 expect(() => Int32.ONE.toUnsigned(-1), throwsRangeError); | |
| 285 expect(() => Int32.ONE.toUnsigned(33), throwsRangeError); | |
| 286 }); | |
| 287 test("toDouble", () { | |
| 288 expect(new Int32(17).toDouble(), same(17.0)); | |
| 289 expect(new Int32(-17).toDouble(), same(-17.0)); | |
| 290 }); | |
| 291 test("toInt", () { | |
| 292 expect(new Int32(17).toInt(), 17); | |
| 293 expect(new Int32(-17).toInt(), -17); | |
| 294 }); | |
| 295 test("toInt32", () { | |
| 296 expect(new Int32(17).toInt32(), new Int32(17)); | |
| 297 expect(new Int32(-17).toInt32(), new Int32(-17)); | |
| 298 }); | |
| 299 test("toInt64", () { | |
| 300 expect(new Int32(17).toInt64(), new Int64(17)); | |
| 301 expect(new Int32(-17).toInt64(), new Int64(-17)); | |
| 302 }); | |
| 303 }); | |
| 304 | |
| 305 group("parse", () { | |
| 306 test("base 10", () { | |
| 307 checkInt(int x) { | |
| 308 expect(Int32.parseRadix('$x', 10), new Int32(x)); | |
| 309 } | |
| 310 checkInt(0); | |
| 311 checkInt(1); | |
| 312 checkInt(1000); | |
| 313 checkInt(12345678); | |
| 314 checkInt(2147483647); | |
| 315 checkInt(2147483648); | |
| 316 checkInt(4294967295); | |
| 317 checkInt(4294967296); | |
| 318 expect(() => Int32.parseRadix('xyzzy', -1), throwsArgumentError); | |
| 319 expect(() => Int32.parseRadix('plugh', 10), | |
| 320 throwsA(new isInstanceOf<FormatException>())); | |
| 321 }); | |
| 322 | |
| 323 test("parseRadix", () { | |
| 324 check(String s, int r, String x) { | |
| 325 expect(Int32.parseRadix(s, r).toString(), x); | |
| 326 } | |
| 327 check('deadbeef', 16, '-559038737'); | |
| 328 check('95', 12, '113'); | |
| 329 }); | |
| 330 }); | |
| 331 | |
| 332 group("string representation", () { | |
| 333 test("toString", () { | |
| 334 expect(new Int32(0).toString(), "0"); | |
| 335 expect(new Int32(1).toString(), "1"); | |
| 336 expect(new Int32(-1).toString(), "-1"); | |
| 337 expect(new Int32(1000).toString(), "1000"); | |
| 338 expect(new Int32(-1000).toString(), "-1000"); | |
| 339 expect(new Int32(123456789).toString(), "123456789"); | |
| 340 expect(new Int32(2147483647).toString(), "2147483647"); | |
| 341 expect(new Int32(2147483648).toString(), "-2147483648"); | |
| 342 expect(new Int32(2147483649).toString(), "-2147483647"); | |
| 343 expect(new Int32(2147483650).toString(), "-2147483646"); | |
| 344 expect(new Int32(-2147483648).toString(), "-2147483648"); | |
| 345 expect(new Int32(-2147483649).toString(), "2147483647"); | |
| 346 expect(new Int32(-2147483650).toString(), "2147483646"); | |
| 347 }); | |
| 348 }); | |
| 349 | |
| 350 group("toHexString", () { | |
| 351 test("returns hexadecimal string representation", () { | |
| 352 expect(new Int32(-1).toHexString(), "-1"); | |
| 353 expect((new Int32(-1) >> 8).toHexString(), "-1"); | |
| 354 expect((new Int32(-1) << 8).toHexString(), "-100"); | |
| 355 expect(new Int32(123456789).toHexString(), "75bcd15"); | |
| 356 expect(new Int32(-1).shiftRightUnsigned(8).toHexString(), "ffffff"); | |
| 357 }); | |
| 358 }); | |
| 359 | |
| 360 group("toRadixString", () { | |
| 361 test("returns base n string representation", () { | |
| 362 expect(new Int32(123456789).toRadixString(5), "223101104124"); | |
| 363 }); | |
| 364 }); | |
| 365 } | |
| OLD | NEW |