Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: pkg/fixnum/test/int_32_test.dart

Issue 19669002: Migrate fixnum tests to unittest. Fix int32 rollover bug. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Get divide-by-zero tests passing when js-compiled. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/fixnum/pubspec.yaml ('k') | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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) ~/ int32.ZERO, throws);
66 (new int64.fromInt(829893893) ~/ new int32.fromInt(1919)).toString()); 66 expect(() => new int32.fromInt(17) ~/ null, throws);
67 Expect.equals("432461", 67 });
68 (new int64.fromInt(829893893) ~/ new int64.fromInt(1919)).toString()); 68
69 Expect.equals("432461", 69 test("%", () {
70 (new int32.fromInt(829893893) ~/ 1919).toString()); 70 expect(new int32.fromInt(0x12345678) % new int32.fromInt(0x22),
71 Expect.equals("432461", 71 new int32.fromInt(0x12345678 % 0x22));
72 (new int64.fromInt(829893893) ~/ 1919).toString()); 72 expect(new int32.fromInt(0x12345678) % new int64.fromInt(0x22),
73 73 new int32.fromInt(0x12345678 % 0x22));
74 Expect.isTrue(new int32.fromInt(12345) == 12345); 74 expect(() => new int32.fromInt(17) % null, throws);
75 Expect.isTrue(new int32.fromInt(12345) == new int32.fromInt(12345)); 75 });
76 Expect.isTrue(new int64.fromInt(12345) == new int32.fromInt(12345)); 76
77 77 test("remainder", () {
78 Expect.equals(new int32.fromInt(~0x12345678), 78 expect(new int32.fromInt(0x12345678).remainder(new int32.fromInt(0x22)),
79 ~(new int32.fromInt(0x12345678))); 79 new int32.fromInt(0x12345678.remainder(0x22)));
80 Expect.equals(new int64.fromInt(-0x12345678), 80 expect(new int32.fromInt(0x12345678).remainder(new int32.fromInt(-0x22)),
81 -(new int32.fromInt(0x12345678))); 81 new int32.fromInt(0x12345678.remainder(-0x22)));
82 82 expect(new int32.fromInt(-0x12345678).remainder(new int32.fromInt(-0x22)),
83 Expect.equals(new int32.fromInt(0x12345678 & 0x22222222), 83 new int32.fromInt(-0x12345678.remainder(-0x22)));
84 new int32.fromInt(0x12345678) & new int32.fromInt(0x22222222)); 84 expect(new int32.fromInt(-0x12345678).remainder(new int32.fromInt(0x22)),
85 Expect.equals(new int64.fromInt(0x12345678 & 0x22222222), 85 new int32.fromInt(-0x12345678.remainder(0x22)));
86 new int32.fromInt(0x12345678) & new int64.fromInt(0x22222222)); 86 expect(new int32.fromInt(0x12345678).remainder(new int64.fromInt(0x22)),
87 Expect.equals(new int32.fromInt(0x12345678 | 0x22222222), 87 new int32.fromInt(0x12345678.remainder(0x22)));
88 new int32.fromInt(0x12345678) | new int32.fromInt(0x22222222)); 88 expect(() => new int32.fromInt(17).remainder(null), throws);
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
92 new int32.fromInt(0x12345678) ^ new int32.fromInt(0x22222222)); 92 group("comparison operators", () {
93 Expect.equals(new int64.fromInt(0x12345678 ^ 0x22222222), 93 test("<", () {
94 new int32.fromInt(0x12345678) ^ new int64.fromInt(0x22222222)); 94 expect(new int32.fromInt(17) < new int32.fromInt(18), true);
95 95 expect(new int32.fromInt(17) < new int32.fromInt(17), false);
96 Expect.equals(new int32.fromInt(0x12345678 + 0x22222222), 96 expect(new int32.fromInt(17) < new int32.fromInt(16), false);
97 new int32.fromInt(0x12345678) + new int32.fromInt(0x22222222)); 97 expect(int32.MIN_VALUE < int32.MAX_VALUE, true);
98 Expect.equals(new int64.fromInt(0x12345678 + 0x22222222), 98 expect(int32.MAX_VALUE < int32.MIN_VALUE, false);
99 new int32.fromInt(0x12345678) + new int64.fromInt(0x22222222)); 99 expect(() => new int32.fromInt(17) < null, throws);
100 Expect.equals(new int32.fromInt(0x12345678 - 0x22222222), 100 });
101 new int32.fromInt(0x12345678) - new int32.fromInt(0x22222222)); 101
102 Expect.equals(new int64.fromInt(0x12345678 - 0x22222222), 102 test("<=", () {
103 new int32.fromInt(0x12345678) - new int64.fromInt(0x22222222)); 103 expect(new int32.fromInt(17) <= new int32.fromInt(18), true);
104 Expect.equals(new int32.fromInt(-899716112), 104 expect(new int32.fromInt(17) <= new int32.fromInt(17), true);
105 new int32.fromInt(0x12345678) * new int32.fromInt(0x22222222)); 105 expect(new int32.fromInt(17) <= new int32.fromInt(16), false);
106 Expect.equals(new int64.fromInts(0x026D60DC, 0xCA5F6BF0), 106 expect(int32.MIN_VALUE <= int32.MAX_VALUE, true);
107 new int32.fromInt(0x12345678) * new int64.fromInt(0x22222222)); 107 expect(int32.MAX_VALUE <= int32.MIN_VALUE, false);
108 Expect.equals(new int32.fromInt(0x12345678 % 0x22), 108 expect(() => new int32.fromInt(17) <= null, throws);
109 new int32.fromInt(0x12345678) % new int32.fromInt(0x22)); 109 });
110 Expect.equals(new int32.fromInt(0x12345678 % 0x22), 110
111 new int32.fromInt(0x12345678) % new int64.fromInt(0x22)); 111 test("==", () {
112 Expect.equals(new int32.fromInt(0x12345678.remainder(0x22)), 112 expect(new int32.fromInt(17) == new int32.fromInt(18), false);
113 new int32.fromInt(0x12345678).remainder(new int32.fromInt(0x22))); 113 expect(new int32.fromInt(17) == new int32.fromInt(17), true);
114 Expect.equals(new int32.fromInt(0x12345678.remainder(-0x22)), 114 expect(new int32.fromInt(17) == new int32.fromInt(16), false);
115 new int32.fromInt(0x12345678).remainder(new int32.fromInt(-0x22))); 115 expect(int32.MIN_VALUE == int32.MAX_VALUE, false);
116 Expect.equals(new int32.fromInt(-0x12345678.remainder(-0x22)), 116 expect(new int32.fromInt(17) == null, false);
117 new int32.fromInt(-0x12345678).remainder(new int32.fromInt(-0x22))); 117 });
118 Expect.equals(new int32.fromInt(-0x12345678.remainder(0x22)), 118
119 new int32.fromInt(-0x12345678).remainder(new int32.fromInt(0x22))); 119 test(">=", () {
120 Expect.equals(new int32.fromInt(0x12345678.remainder(0x22)), 120 expect(new int32.fromInt(17) >= new int32.fromInt(18), false);
121 new int32.fromInt(0x12345678).remainder(new int64.fromInt(0x22))); 121 expect(new int32.fromInt(17) >= new int32.fromInt(17), true);
122 Expect.equals(new int32.fromInt(0x12345678 ~/ 0x22), 122 expect(new int32.fromInt(17) >= new int32.fromInt(16), true);
123 new int32.fromInt(0x12345678) ~/ new int32.fromInt(0x22)); 123 expect(int32.MIN_VALUE >= int32.MAX_VALUE, false);
124 Expect.equals(new int32.fromInt(0x12345678 ~/ 0x22), 124 expect(int32.MAX_VALUE >= int32.MIN_VALUE, true);
125 new int32.fromInt(0x12345678) ~/ new int64.fromInt(0x22)); 125 expect(() => new int32.fromInt(17) >= null, throws);
126 126 });
127 Expect.equals(new int32.fromInt(0x12345678 >> 7), 127
128 new int32.fromInt(0x12345678) >> 7); 128 test(">", () {
129 Expect.equals(new int32.fromInt(0x12345678 << 7), 129 expect(new int32.fromInt(17) > new int32.fromInt(18), false);
130 new int32.fromInt(0x12345678) << 7); 130 expect(new int32.fromInt(17) > new int32.fromInt(17), false);
131 Expect.equals(new int32.fromInt(0x12345678 >> 7), 131 expect(new int32.fromInt(17) > new int32.fromInt(16), true);
132 new int32.fromInt(0x12345678).shiftRightUnsigned(7)); 132 expect(int32.MIN_VALUE > int32.MAX_VALUE, false);
133 133 expect(int32.MAX_VALUE > int32.MIN_VALUE, true);
134 try { 134 expect(() => new int32.fromInt(17) > null, throws);
135 new int32.fromInt(17) < null; 135 });
136 Expect.fail("x < null should throw ArgumentError"); 136 });
137 } on ArgumentError catch (e) { 137
138 } 138 group("bitwise operators", () {
139 139 test("&", () {
140 try { 140 expect(new int32.fromInt(0x12345678) & new int32.fromInt(0x22222222),
141 new int32.fromInt(17) <= null; 141 new int32.fromInt(0x12345678 & 0x22222222));
142 Expect.fail("x <= null should throw ArgumentError"); 142 expect(new int32.fromInt(0x12345678) & new int64.fromInt(0x22222222),
143 } on ArgumentError catch (e) { 143 new int64.fromInt(0x12345678 & 0x22222222));
144 } 144 expect(() => new int32.fromInt(17) & null, throwsArgumentError);
145 145 });
146 try { 146
147 new int32.fromInt(17) > null; 147 test("|", () {
148 Expect.fail("x > null should throw ArgumentError"); 148 expect(new int32.fromInt(0x12345678) | new int32.fromInt(0x22222222),
149 } on ArgumentError catch (e) { 149 new int32.fromInt(0x12345678 | 0x22222222));
150 } 150 expect(new int32.fromInt(0x12345678) | new int64.fromInt(0x22222222),
151 151 new int64.fromInt(0x12345678 | 0x22222222));
152 try { 152 expect(() => new int32.fromInt(17) | null, throws);
153 new int32.fromInt(17) < null; 153 });
154 Expect.fail("x >= null should throw ArgumentError"); 154
155 } on ArgumentError catch (e) { 155 test("^", () {
156 } 156 expect(new int32.fromInt(0x12345678) ^ new int32.fromInt(0x22222222),
157 157 new int32.fromInt(0x12345678 ^ 0x22222222));
158 Expect.isFalse(new int32.fromInt(17) == null); 158 expect(new int32.fromInt(0x12345678) ^ new int64.fromInt(0x22222222),
159 new int64.fromInt(0x12345678 ^ 0x22222222));
160 expect(() => new int32.fromInt(17) ^ null, throws);
161 });
162
163 test("~", () {
164 expect(~(new int32.fromInt(0x12345678)), new int32.fromInt(~0x12345678));
165 expect(-(new int32.fromInt(0x12345678)), new int64.fromInt(-0x12345678));
166 });
167 });
168
169 group("bitshift operators", () {
170 test("<<", () {
171 expect(new int32.fromInt(0x12345678) << 7,
172 new int32.fromInt(0x12345678 << 7));
173 expect(() => new int32.fromInt(17) << -1, throwsArgumentError);
174 expect(() => new int32.fromInt(17) << null, throws);
175 });
176
177 test(">>", () {
178 expect(new int32.fromInt(0x12345678) >> 7,
179 new int32.fromInt(0x12345678 >> 7));
180 expect(() => new int32.fromInt(17) >> -1, throwsArgumentError);
181 expect(() => new int32.fromInt(17) >> null, throws);
182 });
183
184 test("shiftRightUnsigned", () {
185 expect(new int32.fromInt(0x12345678).shiftRightUnsigned(7),
186 new int32.fromInt(0x12345678 >> 7));
187 expect(() => (new int32.fromInt(17).shiftRightUnsigned(-1)),
188 throwsArgumentError);
189 expect(() => (new int32.fromInt(17).shiftRightUnsigned(null)), throws);
190 });
191 });
192
193 group("type conversions", () {
194 expect(new int32.fromInt(17).toInt(), 17);
195 expect(new int32.fromInt(-17).toInt(), -17);
196 expect(new int32.fromInt(17).toInt32(), new int32.fromInt(17));
197 expect(new int32.fromInt(-17).toInt32(), new int32.fromInt(-17));
198 expect(new int32.fromInt(17).toInt64(), new int64.fromInt(17));
199 expect(new int32.fromInt(-17).toInt64(), new int64.fromInt(-17));
200 });
201
202 group("string representation", () {
203 test("toString", () {
204 expect(new int32.fromInt(0).toString(), "0");
205 expect(new int32.fromInt(1).toString(), "1");
206 expect(new int32.fromInt(-1).toString(), "-1");
207 expect(new int32.fromInt(1000).toString(), "1000");
208 expect(new int32.fromInt(-1000).toString(), "-1000");
209 expect(new int32.fromInt(123456789).toString(), "123456789");
210 expect(new int32.fromInt(2147483647).toString(), "2147483647");
211 expect(new int32.fromInt(2147483648).toString(), "-2147483648");
212 expect(new int32.fromInt(2147483649).toString(), "-2147483647");
213 expect(new int32.fromInt(2147483650).toString(), "-2147483646");
214 expect(new int32.fromInt(-2147483648).toString(), "-2147483648");
215 expect(new int32.fromInt(-2147483649).toString(), "2147483647");
216 expect(new int32.fromInt(-2147483650).toString(), "2147483646");
217 });
218 });
219
220 group("toHexString", () {
221 test("returns hexadecimal string representation", () {
222 expect(new int32.fromInt(-1).toHexString(), "-1");
223 expect((new int32.fromInt(-1) >> 8).toHexString(), "-1");
224 expect((new int32.fromInt(-1) << 8).toHexString(), "-100");
225 expect(new int32.fromInt(123456789).toHexString(), "75bcd15");
226 expect(new int32.fromInt(-1).shiftRightUnsigned(8).toHexString(),
227 "ffffff");
228 });
229 });
230
231 group("toRadixString", () {
232 test("returns base n string representation", () {
233 expect(new int32.fromInt(123456789).toRadixString(5), "223101104124");
234 });
235 });
159 } 236 }
OLDNEW
« no previous file with comments | « pkg/fixnum/pubspec.yaml ('k') | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698