| 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 int32test; | 5 library int32test; |
| 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 Expect.equals("0", new int32.fromInt(0).toString()); | 10 group("arithmetic operators", () { |
| 11 Expect.equals("1", new int32.fromInt(1).toString()); | 11 int32 n1 = new int32.fromInt(1234); |
| 12 Expect.equals("-1", new int32.fromInt(-1).toString()); | 12 int32 n2 = new int32.fromInt(9876); |
| 13 Expect.equals("1000", new int32.fromInt(1000).toString()); | 13 int32 n3 = new int32.fromInt(-1234); |
| 14 Expect.equals("-1000", new int32.fromInt(-1000).toString()); | 14 int32 n4 = new int32.fromInt(-9876); |
| 15 Expect.equals("2147483647", new int32.fromInt(2147483647).toString()); | 15 int32 n5 = new int32.fromInt(0x12345678); |
| 16 Expect.equals("-2147483648", new int32.fromInt(2147483648).toString()); | 16 int32 n6 = new int32.fromInt(0x22222222); |
| 17 Expect.equals("-2147483647", new int32.fromInt(2147483649).toString()); | 17 |
| 18 Expect.equals("-2147483646", new int32.fromInt(2147483650).toString()); | 18 test("+", () { |
| 19 Expect.equals("-2147483648", new int32.fromInt(-2147483648).toString()); | 19 expect(n1 + n2, new int32.fromInt(11110)); |
| 20 Expect.equals("2147483647", new int32.fromInt(-2147483649).toString()); | 20 expect(n3 + n2, new int32.fromInt(8642)); |
| 21 Expect.equals("2147483646", new int32.fromInt(-2147483650).toString()); | 21 expect(n3 + n4, new int32.fromInt(-11110)); |
| 22 | 22 expect(int32.MAX_VALUE + 1, int32.MIN_VALUE); |
| 23 Expect.equals("-1", new int32.fromInt(-1).toHexString()); | 23 expect(() => (new int32.fromInt(17) + null), throws); |
| 24 Expect.equals("-1", (new int32.fromInt(-1) >> 8).toHexString()); | 24 }); |
| 25 Expect.equals("-100", (new int32.fromInt(-1) << 8).toHexString()); | 25 |
| 26 Expect.equals("ffffff", | 26 test("-", () { |
| 27 new int32.fromInt(-1).shiftRightUnsigned(8).toHexString()); | 27 expect(n1 - n2, new int32.fromInt(-8642)); |
| 28 | 28 expect(n3 - n2, new int32.fromInt(-11110)); |
| 29 Expect.equals("123456789", new int32.fromInt(123456789).toString()); | 29 expect(n3 - n4, new int32.fromInt(8642)); |
| 30 Expect.equals("75bcd15", new int32.fromInt(123456789).toHexString()); | 30 expect(int32.MIN_VALUE - 1, int32.MAX_VALUE); |
| 31 Expect.equals("223101104124", new int32.fromInt(123456789).toRadixString(5)); | 31 expect(() => (new int32.fromInt(17) - null), throws); |
| 32 | 32 }); |
| 33 try { | 33 |
| 34 new int32.fromInt(17) >> -1; | 34 test("unary -", () { |
| 35 Expect.fail("x >> -1 should throw ArgumentError"); | 35 expect(-n1, new int32.fromInt(-1234)); |
| 36 } on ArgumentError catch (e) { | 36 expect(-int32.ZERO, int32.ZERO); |
| 37 } | 37 }); |
| 38 | 38 |
| 39 try { | 39 test("*", () { |
| 40 new int32.fromInt(17) << -1; | 40 expect(n1 * n2, new int32.fromInt(12186984)); |
| 41 Expect.fail("x >> -1 should throw ArgumentError"); | 41 expect(n2 * n3, new int32.fromInt(-12186984)); |
| 42 } on ArgumentError catch (e) { | 42 expect(n3 * n3, new int32.fromInt(1522756)); |
| 43 } | 43 expect(n3 * n2, new int32.fromInt(-12186984)); |
| 44 | 44 expect(new int32.fromInt(0x12345678) * new int32.fromInt(0x22222222), |
| 45 try { | 45 new int32.fromInt(-899716112)); |
| 46 new int32.fromInt(17).shiftRightUnsigned(-1); | 46 expect((new int32.fromInt(123456789) * new int32.fromInt(987654321)), |
| 47 Expect.fail("x >> -1 should throw ArgumentError"); | 47 new int32.fromInt(-67153019)); |
| 48 } on ArgumentError catch (e) { | 48 expect(new int32.fromInt(0x12345678) * new int64.fromInt(0x22222222), |
| 49 } | 49 new int64.fromInts(0x026D60DC, 0xCA5F6BF0)); |
| 50 | 50 expect((new int32.fromInt(123456789) * 987654321), |
| 51 // wraparound | 51 new int32.fromInt(-67153019)); |
| 52 Expect.equals("-67153019", (new int32.fromInt(123456789) * | 52 expect(() => (new int32.fromInt(17) * null), throws); |
| 53 new int32.fromInt(987654321)).toString()); | 53 }); |
| 54 Expect.equals("121932631112635269", (new int64.fromInt(123456789) * | 54 |
| 55 new int32.fromInt(987654321)).toString()); | 55 test("~/", () { |
| 56 Expect.equals("121932631112635269", (new int32.fromInt(123456789) * | 56 expect(new int32.fromInt(829893893) ~/ new int32.fromInt(1919), |
| 57 new int64.fromInt(987654321)).toString()); | 57 new int32.fromInt(432461)); |
| 58 Expect.equals("121932631112635269", (new int64.fromInt(123456789) * | 58 expect(new int32.fromInt(0x12345678) ~/ new int32.fromInt(0x22), |
| 59 new int64.fromInt(987654321)).toString()); | 59 new int32.fromInt(0x12345678 ~/ 0x22)); |
| 60 | 60 expect(new int32.fromInt(829893893) ~/ new int64.fromInt(1919), |
| 61 Expect.equals("432461", | 61 new int32.fromInt(432461)); |
| 62 (new int32.fromInt(829893893) ~/ new int32.fromInt(1919)).toString()); | 62 expect(new int32.fromInt(0x12345678) ~/ new int64.fromInt(0x22), |
| 63 Expect.equals("432461", | 63 new int32.fromInt(0x12345678 ~/ 0x22)); |
| 64 (new int32.fromInt(829893893) ~/ new int64.fromInt(1919)).toString()); | 64 expect(new int32.fromInt(829893893) ~/ 1919, new int32.fromInt(432461)); |
| 65 Expect.equals("432461", | 65 expect(() => (new int32.fromInt(17) ~/ null), throws); |
| 66 (new int64.fromInt(829893893) ~/ new int32.fromInt(1919)).toString()); | 66 }); |
| 67 Expect.equals("432461", | 67 |
| 68 (new int64.fromInt(829893893) ~/ new int64.fromInt(1919)).toString()); | 68 test("%", () { |
| 69 Expect.equals("432461", | 69 expect(new int32.fromInt(0x12345678) % new int32.fromInt(0x22), |
| 70 (new int32.fromInt(829893893) ~/ 1919).toString()); | 70 new int32.fromInt(0x12345678 % 0x22)); |
| 71 Expect.equals("432461", | 71 expect(new int32.fromInt(0x12345678) % new int64.fromInt(0x22), |
| 72 (new int64.fromInt(829893893) ~/ 1919).toString()); | 72 new int32.fromInt(0x12345678 % 0x22)); |
| 73 | 73 expect(() => (new int32.fromInt(17) % null), throws); |
| 74 Expect.isTrue(new int32.fromInt(12345) == 12345); | 74 }); |
| 75 Expect.isTrue(new int32.fromInt(12345) == new int32.fromInt(12345)); | 75 |
| 76 Expect.isTrue(new int64.fromInt(12345) == new int32.fromInt(12345)); | 76 test("remainder", () { |
| 77 | 77 expect(new int32.fromInt(0x12345678).remainder(new int32.fromInt(0x22)), |
| 78 Expect.equals(new int32.fromInt(~0x12345678), | 78 new int32.fromInt(0x12345678.remainder(0x22))); |
| 79 ~(new int32.fromInt(0x12345678))); | 79 expect(new int32.fromInt(0x12345678).remainder(new int32.fromInt(-0x22)), |
| 80 Expect.equals(new int64.fromInt(-0x12345678), | 80 new int32.fromInt(0x12345678.remainder(-0x22))); |
| 81 -(new int32.fromInt(0x12345678))); | 81 expect(new int32.fromInt(-0x12345678).remainder(new int32.fromInt(-0x22)), |
| 82 | 82 new int32.fromInt(-0x12345678.remainder(-0x22))); |
| 83 Expect.equals(new int32.fromInt(0x12345678 & 0x22222222), | 83 expect(new int32.fromInt(-0x12345678).remainder(new int32.fromInt(0x22)), |
| 84 new int32.fromInt(0x12345678) & new int32.fromInt(0x22222222)); | 84 new int32.fromInt(-0x12345678.remainder(0x22))); |
| 85 Expect.equals(new int64.fromInt(0x12345678 & 0x22222222), | 85 expect(new int32.fromInt(0x12345678).remainder(new int64.fromInt(0x22)), |
| 86 new int32.fromInt(0x12345678) & new int64.fromInt(0x22222222)); | 86 new int32.fromInt(0x12345678.remainder(0x22))); |
| 87 Expect.equals(new int32.fromInt(0x12345678 | 0x22222222), | 87 expect(() => (new int32.fromInt(17).remainder(null)), throws); |
| 88 new int32.fromInt(0x12345678) | new int32.fromInt(0x22222222)); | 88 }); |
| 89 Expect.equals(new int64.fromInt(0x12345678 | 0x22222222), | 89 }); |
| 90 new int32.fromInt(0x12345678) | new int64.fromInt(0x22222222)); | 90 |
| 91 Expect.equals(new int32.fromInt(0x12345678 ^ 0x22222222), | 91 group("comparison operators", () { |
| 92 new int32.fromInt(0x12345678) ^ new int32.fromInt(0x22222222)); | 92 test("<", () { |
| 93 Expect.equals(new int64.fromInt(0x12345678 ^ 0x22222222), | 93 expect(new int32.fromInt(17) < new int32.fromInt(18), true); |
| 94 new int32.fromInt(0x12345678) ^ new int64.fromInt(0x22222222)); | 94 expect(new int32.fromInt(17) < new int32.fromInt(17), false); |
| 95 | 95 expect(new int32.fromInt(17) < new int32.fromInt(16), false); |
| 96 Expect.equals(new int32.fromInt(0x12345678 + 0x22222222), | 96 expect(int32.MIN_VALUE < int32.MAX_VALUE, true); |
| 97 new int32.fromInt(0x12345678) + new int32.fromInt(0x22222222)); | 97 expect(int32.MAX_VALUE < int32.MIN_VALUE, false); |
| 98 Expect.equals(new int64.fromInt(0x12345678 + 0x22222222), | 98 expect(() => new int32.fromInt(17) < null, throws); |
| 99 new int32.fromInt(0x12345678) + new int64.fromInt(0x22222222)); | 99 }); |
| 100 Expect.equals(new int32.fromInt(0x12345678 - 0x22222222), | 100 |
| 101 new int32.fromInt(0x12345678) - new int32.fromInt(0x22222222)); | 101 test("<=", () { |
| 102 Expect.equals(new int64.fromInt(0x12345678 - 0x22222222), | 102 expect(new int32.fromInt(17) <= new int32.fromInt(18), true); |
| 103 new int32.fromInt(0x12345678) - new int64.fromInt(0x22222222)); | 103 expect(new int32.fromInt(17) <= new int32.fromInt(17), true); |
| 104 Expect.equals(new int32.fromInt(-899716112), | 104 expect(new int32.fromInt(17) <= new int32.fromInt(16), false); |
| 105 new int32.fromInt(0x12345678) * new int32.fromInt(0x22222222)); | 105 expect(int32.MIN_VALUE <= int32.MAX_VALUE, true); |
| 106 Expect.equals(new int64.fromInts(0x026D60DC, 0xCA5F6BF0), | 106 expect(int32.MAX_VALUE <= int32.MIN_VALUE, false); |
| 107 new int32.fromInt(0x12345678) * new int64.fromInt(0x22222222)); | 107 expect(() => new int32.fromInt(17) <= null, throws); |
| 108 Expect.equals(new int32.fromInt(0x12345678 % 0x22), | 108 }); |
| 109 new int32.fromInt(0x12345678) % new int32.fromInt(0x22)); | 109 |
| 110 Expect.equals(new int32.fromInt(0x12345678 % 0x22), | 110 test("==", () { |
| 111 new int32.fromInt(0x12345678) % new int64.fromInt(0x22)); | 111 expect(new int32.fromInt(17) == new int32.fromInt(18), false); |
| 112 Expect.equals(new int32.fromInt(0x12345678.remainder(0x22)), | 112 expect(new int32.fromInt(17) == new int32.fromInt(17), true); |
| 113 new int32.fromInt(0x12345678).remainder(new int32.fromInt(0x22))); | 113 expect(new int32.fromInt(17) == new int32.fromInt(16), false); |
| 114 Expect.equals(new int32.fromInt(0x12345678.remainder(-0x22)), | 114 expect(int32.MIN_VALUE == int32.MAX_VALUE, false); |
| 115 new int32.fromInt(0x12345678).remainder(new int32.fromInt(-0x22))); | 115 expect(new int32.fromInt(17) == null, false); |
| 116 Expect.equals(new int32.fromInt(-0x12345678.remainder(-0x22)), | 116 }); |
| 117 new int32.fromInt(-0x12345678).remainder(new int32.fromInt(-0x22))); | 117 |
| 118 Expect.equals(new int32.fromInt(-0x12345678.remainder(0x22)), | 118 test(">=", () { |
| 119 new int32.fromInt(-0x12345678).remainder(new int32.fromInt(0x22))); | 119 expect(new int32.fromInt(17) >= new int32.fromInt(18), false); |
| 120 Expect.equals(new int32.fromInt(0x12345678.remainder(0x22)), | 120 expect(new int32.fromInt(17) >= new int32.fromInt(17), true); |
| 121 new int32.fromInt(0x12345678).remainder(new int64.fromInt(0x22))); | 121 expect(new int32.fromInt(17) >= new int32.fromInt(16), true); |
| 122 Expect.equals(new int32.fromInt(0x12345678 ~/ 0x22), | 122 expect(int32.MIN_VALUE >= int32.MAX_VALUE, false); |
| 123 new int32.fromInt(0x12345678) ~/ new int32.fromInt(0x22)); | 123 expect(int32.MAX_VALUE >= int32.MIN_VALUE, true); |
| 124 Expect.equals(new int32.fromInt(0x12345678 ~/ 0x22), | 124 expect(() => new int32.fromInt(17) >= null, throws); |
| 125 new int32.fromInt(0x12345678) ~/ new int64.fromInt(0x22)); | 125 }); |
| 126 | 126 |
| 127 Expect.equals(new int32.fromInt(0x12345678 >> 7), | 127 test(">", () { |
| 128 new int32.fromInt(0x12345678) >> 7); | 128 expect(new int32.fromInt(17) > new int32.fromInt(18), false); |
| 129 Expect.equals(new int32.fromInt(0x12345678 << 7), | 129 expect(new int32.fromInt(17) > new int32.fromInt(17), false); |
| 130 new int32.fromInt(0x12345678) << 7); | 130 expect(new int32.fromInt(17) > new int32.fromInt(16), true); |
| 131 Expect.equals(new int32.fromInt(0x12345678 >> 7), | 131 expect(int32.MIN_VALUE > int32.MAX_VALUE, false); |
| 132 new int32.fromInt(0x12345678).shiftRightUnsigned(7)); | 132 expect(int32.MAX_VALUE > int32.MIN_VALUE, true); |
| 133 | 133 expect(() => new int32.fromInt(17) > null, throws); |
| 134 try { | 134 }); |
| 135 new int32.fromInt(17) < null; | 135 }); |
| 136 Expect.fail("x < null should throw ArgumentError"); | 136 |
| 137 } on ArgumentError catch (e) { | 137 group("bitwise operators", () { |
| 138 } | 138 test("&", () { |
| 139 | 139 expect(new int32.fromInt(0x12345678) & new int32.fromInt(0x22222222), |
| 140 try { | 140 new int32.fromInt(0x12345678 & 0x22222222)); |
| 141 new int32.fromInt(17) <= null; | 141 expect(new int32.fromInt(0x12345678) & new int64.fromInt(0x22222222), |
| 142 Expect.fail("x <= null should throw ArgumentError"); | 142 new int64.fromInt(0x12345678 & 0x22222222)); |
| 143 } on ArgumentError catch (e) { | 143 expect(() => (new int32.fromInt(17) & null), throwsArgumentError); |
| 144 } | 144 }); |
| 145 | 145 |
| 146 try { | 146 test("|", () { |
| 147 new int32.fromInt(17) > null; | 147 expect(new int32.fromInt(0x12345678) | new int32.fromInt(0x22222222), |
| 148 Expect.fail("x > null should throw ArgumentError"); | 148 new int32.fromInt(0x12345678 | 0x22222222)); |
| 149 } on ArgumentError catch (e) { | 149 expect(new int32.fromInt(0x12345678) | new int64.fromInt(0x22222222), |
| 150 } | 150 new int64.fromInt(0x12345678 | 0x22222222)); |
| 151 | 151 expect(() => (new int32.fromInt(17) | null), throws); |
| 152 try { | 152 }); |
| 153 new int32.fromInt(17) < null; | 153 |
| 154 Expect.fail("x >= null should throw ArgumentError"); | 154 test("^", () { |
| 155 } on ArgumentError catch (e) { | 155 expect(new int32.fromInt(0x12345678) ^ new int32.fromInt(0x22222222), |
| 156 } | 156 new int32.fromInt(0x12345678 ^ 0x22222222)); |
| 157 | 157 expect(new int32.fromInt(0x12345678) ^ new int64.fromInt(0x22222222), |
| 158 Expect.isFalse(new int32.fromInt(17) == null); | 158 new int64.fromInt(0x12345678 ^ 0x22222222)); |
| 159 expect(() => (new int32.fromInt(17) ^ null), throws); |
| 160 }); |
| 161 |
| 162 test("~", () { |
| 163 expect(~(new int32.fromInt(0x12345678)), new int32.fromInt(~0x12345678)); |
| 164 expect(-(new int32.fromInt(0x12345678)), new int64.fromInt(-0x12345678)); |
| 165 }); |
| 166 }); |
| 167 |
| 168 group("bitshift operators", () { |
| 169 test("<<", () { |
| 170 expect(new int32.fromInt(0x12345678) << 7, |
| 171 new int32.fromInt(0x12345678 << 7)); |
| 172 expect(() => (new int32.fromInt(17) << -1), throwsArgumentError); |
| 173 expect(() => (new int32.fromInt(17) << null), throws); |
| 174 }); |
| 175 |
| 176 test(">>", () { |
| 177 expect(new int32.fromInt(0x12345678) >> 7, |
| 178 new int32.fromInt(0x12345678 >> 7)); |
| 179 expect(() => (new int32.fromInt(17) >> -1), throwsArgumentError); |
| 180 expect(() => (new int32.fromInt(17) >> null), throws); |
| 181 }); |
| 182 |
| 183 test("shiftRightUnsigned", () { |
| 184 expect(new int32.fromInt(0x12345678).shiftRightUnsigned(7), |
| 185 new int32.fromInt(0x12345678 >> 7)); |
| 186 expect(() => (new int32.fromInt(17).shiftRightUnsigned(-1)), |
| 187 throwsArgumentError); |
| 188 expect(() => (new int32.fromInt(17).shiftRightUnsigned(null)), throws); |
| 189 }); |
| 190 }); |
| 191 |
| 192 group("type conversions", () { |
| 193 expect(new int32.fromInt(17).toInt(), 17); |
| 194 expect(new int32.fromInt(-17).toInt(), -17); |
| 195 expect(new int32.fromInt(17).toInt32(), new int32.fromInt(17)); |
| 196 expect(new int32.fromInt(-17).toInt32(), new int32.fromInt(-17)); |
| 197 expect(new int32.fromInt(17).toInt64(), new int64.fromInt(17)); |
| 198 expect(new int32.fromInt(-17).toInt64(), new int64.fromInt(-17)); |
| 199 }); |
| 200 |
| 201 group("string representation", () { |
| 202 test("toString", () { |
| 203 expect(new int32.fromInt(0).toString(), "0"); |
| 204 expect(new int32.fromInt(1).toString(), "1"); |
| 205 expect(new int32.fromInt(-1).toString(), "-1"); |
| 206 expect(new int32.fromInt(1000).toString(), "1000"); |
| 207 expect(new int32.fromInt(-1000).toString(), "-1000"); |
| 208 expect(new int32.fromInt(123456789).toString(), "123456789"); |
| 209 expect(new int32.fromInt(2147483647).toString(), "2147483647"); |
| 210 expect(new int32.fromInt(2147483648).toString(), "-2147483648"); |
| 211 expect(new int32.fromInt(2147483649).toString(), "-2147483647"); |
| 212 expect(new int32.fromInt(2147483650).toString(), "-2147483646"); |
| 213 expect(new int32.fromInt(-2147483648).toString(), "-2147483648"); |
| 214 expect(new int32.fromInt(-2147483649).toString(), "2147483647"); |
| 215 expect(new int32.fromInt(-2147483650).toString(), "2147483646"); |
| 216 }); |
| 217 }); |
| 218 |
| 219 group("toHexString", () { |
| 220 test("returns hexadecimal string representation", () { |
| 221 expect(new int32.fromInt(-1).toHexString(), "-1"); |
| 222 expect((new int32.fromInt(-1) >> 8).toHexString(), "-1"); |
| 223 expect((new int32.fromInt(-1) << 8).toHexString(), "-100"); |
| 224 expect(new int32.fromInt(123456789).toHexString(), "75bcd15"); |
| 225 expect(new int32.fromInt(-1).shiftRightUnsigned(8).toHexString(), |
| 226 "ffffff"); |
| 227 }); |
| 228 }); |
| 229 |
| 230 group("toRadixString", () { |
| 231 test("returns base n string representation", () { |
| 232 expect(new int32.fromInt(123456789).toRadixString(5), "223101104124"); |
| 233 }); |
| 234 }); |
| 159 } | 235 } |
| OLD | NEW |