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

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: 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
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) ~/ 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", () {
justinfagnani 2013/07/18 18:27:28 Ideally we want comparison tests that also cover c
justinfagnani 2013/07/18 18:58:56 oops, I meant this for the 64 bit tests
Chris Bracken 2013/07/18 19:06:07 Done in 64-bit tests.
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(() => new int32.fromInt(17) < null, throws);
97 new int32.fromInt(0x12345678) + new int32.fromInt(0x22222222)); 97 });
98 Expect.equals(new int64.fromInt(0x12345678 + 0x22222222), 98
99 new int32.fromInt(0x12345678) + new int64.fromInt(0x22222222)); 99 test("<=", () {
100 Expect.equals(new int32.fromInt(0x12345678 - 0x22222222), 100 expect(new int32.fromInt(17) <= new int32.fromInt(18), true);
101 new int32.fromInt(0x12345678) - new int32.fromInt(0x22222222)); 101 expect(new int32.fromInt(17) <= new int32.fromInt(17), true);
102 Expect.equals(new int64.fromInt(0x12345678 - 0x22222222), 102 expect(new int32.fromInt(17) <= new int32.fromInt(16), false);
103 new int32.fromInt(0x12345678) - new int64.fromInt(0x22222222)); 103 expect(() => new int32.fromInt(17) <= null, throws);
104 Expect.equals(new int32.fromInt(-899716112), 104 });
105 new int32.fromInt(0x12345678) * new int32.fromInt(0x22222222)); 105
106 Expect.equals(new int64.fromInts(0x026D60DC, 0xCA5F6BF0), 106 test("==", () {
107 new int32.fromInt(0x12345678) * new int64.fromInt(0x22222222)); 107 expect(new int32.fromInt(17) == new int32.fromInt(18), false);
108 Expect.equals(new int32.fromInt(0x12345678 % 0x22), 108 expect(new int32.fromInt(17) == new int32.fromInt(17), true);
109 new int32.fromInt(0x12345678) % new int32.fromInt(0x22)); 109 expect(new int32.fromInt(17) == new int32.fromInt(16), false);
110 Expect.equals(new int32.fromInt(0x12345678 % 0x22), 110 expect(new int32.fromInt(17) == null, false);
111 new int32.fromInt(0x12345678) % new int64.fromInt(0x22)); 111 });
112 Expect.equals(new int32.fromInt(0x12345678.remainder(0x22)), 112
113 new int32.fromInt(0x12345678).remainder(new int32.fromInt(0x22))); 113 test(">=", () {
114 Expect.equals(new int32.fromInt(0x12345678.remainder(-0x22)), 114 expect(new int32.fromInt(17) >= new int32.fromInt(18), false);
115 new int32.fromInt(0x12345678).remainder(new int32.fromInt(-0x22))); 115 expect(new int32.fromInt(17) >= new int32.fromInt(17), true);
116 Expect.equals(new int32.fromInt(-0x12345678.remainder(-0x22)), 116 expect(new int32.fromInt(17) >= new int32.fromInt(16), true);
117 new int32.fromInt(-0x12345678).remainder(new int32.fromInt(-0x22))); 117 expect(() => new int32.fromInt(17) >= null, throws);
118 Expect.equals(new int32.fromInt(-0x12345678.remainder(0x22)), 118 });
119 new int32.fromInt(-0x12345678).remainder(new int32.fromInt(0x22))); 119
120 Expect.equals(new int32.fromInt(0x12345678.remainder(0x22)), 120 test(">", () {
121 new int32.fromInt(0x12345678).remainder(new int64.fromInt(0x22))); 121 expect(new int32.fromInt(17) > new int32.fromInt(18), false);
122 Expect.equals(new int32.fromInt(0x12345678 ~/ 0x22), 122 expect(new int32.fromInt(17) > new int32.fromInt(17), false);
123 new int32.fromInt(0x12345678) ~/ new int32.fromInt(0x22)); 123 expect(new int32.fromInt(17) > new int32.fromInt(16), 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
128 new int32.fromInt(0x12345678) >> 7); 128 group("bitwise operators", () {
129 Expect.equals(new int32.fromInt(0x12345678 << 7), 129 test("&", () {
130 new int32.fromInt(0x12345678) << 7); 130 expect(new int32.fromInt(0x12345678) & new int32.fromInt(0x22222222),
131 Expect.equals(new int32.fromInt(0x12345678 >> 7), 131 new int32.fromInt(0x12345678 & 0x22222222));
132 new int32.fromInt(0x12345678).shiftRightUnsigned(7)); 132 expect(new int32.fromInt(0x12345678) & new int64.fromInt(0x22222222),
133 133 new int64.fromInt(0x12345678 & 0x22222222));
134 try { 134 expect(() => (new int32.fromInt(17) & null), throwsArgumentError);
135 new int32.fromInt(17) < null; 135 });
136 Expect.fail("x < null should throw ArgumentError"); 136
137 } on ArgumentError catch (e) { 137 test("|", () {
138 } 138 expect(new int32.fromInt(0x12345678) | new int32.fromInt(0x22222222),
139 139 new int32.fromInt(0x12345678 | 0x22222222));
140 try { 140 expect(new int32.fromInt(0x12345678) | new int64.fromInt(0x22222222),
141 new int32.fromInt(17) <= null; 141 new int64.fromInt(0x12345678 | 0x22222222));
142 Expect.fail("x <= null should throw ArgumentError"); 142 expect(() => (new int32.fromInt(17) | null), throws);
143 } on ArgumentError catch (e) { 143 });
144 } 144
145 145 test("^", () {
146 try { 146 expect(new int32.fromInt(0x12345678) ^ new int32.fromInt(0x22222222),
147 new int32.fromInt(17) > null; 147 new int32.fromInt(0x12345678 ^ 0x22222222));
148 Expect.fail("x > null should throw ArgumentError"); 148 expect(new int32.fromInt(0x12345678) ^ new int64.fromInt(0x22222222),
149 } on ArgumentError catch (e) { 149 new int64.fromInt(0x12345678 ^ 0x22222222));
150 } 150 expect(() => (new int32.fromInt(17) ^ null), throws);
151 151 });
152 try { 152
153 new int32.fromInt(17) < null; 153 test("~", () {
154 Expect.fail("x >= null should throw ArgumentError"); 154 expect(~(new int32.fromInt(0x12345678)), new int32.fromInt(~0x12345678));
155 } on ArgumentError catch (e) { 155 expect(-(new int32.fromInt(0x12345678)), new int64.fromInt(-0x12345678));
156 } 156 });
157 157 });
158 Expect.isFalse(new int32.fromInt(17) == null); 158
159 group("bitshift operators", () {
160 test("<<", () {
161 expect(new int32.fromInt(0x12345678) << 7,
162 new int32.fromInt(0x12345678 << 7));
163 expect(() => (new int32.fromInt(17) << -1), throwsArgumentError);
164 expect(() => (new int32.fromInt(17) << null), throws);
165 });
166
167 test(">>", () {
168 expect(new int32.fromInt(0x12345678) >> 7,
169 new int32.fromInt(0x12345678 >> 7));
170 expect(() => (new int32.fromInt(17) >> -1), throwsArgumentError);
171 expect(() => (new int32.fromInt(17) >> null), throws);
172 });
173
174 test("shiftRightUnsigned", () {
175 expect(new int32.fromInt(0x12345678).shiftRightUnsigned(7),
176 new int32.fromInt(0x12345678 >> 7));
177 expect(() => (new int32.fromInt(17).shiftRightUnsigned(-1)),
178 throwsArgumentError);
179 expect(() => (new int32.fromInt(17).shiftRightUnsigned(null)), throws);
180 });
181 });
182
183 group("type conversions", () {
184 expect(new int32.fromInt(17).toInt(), 17);
185 expect(new int32.fromInt(-17).toInt(), -17);
186 expect(new int32.fromInt(17).toInt32(), new int32.fromInt(17));
187 expect(new int32.fromInt(-17).toInt32(), new int32.fromInt(-17));
188 expect(new int32.fromInt(17).toInt64(), new int64.fromInt(17));
189 expect(new int32.fromInt(-17).toInt64(), new int64.fromInt(-17));
190 });
191
192 group("string representation", () {
193 test("toString", () {
194 expect(new int32.fromInt(0).toString(), "0");
195 expect(new int32.fromInt(1).toString(), "1");
196 expect(new int32.fromInt(-1).toString(), "-1");
197 expect(new int32.fromInt(1000).toString(), "1000");
198 expect(new int32.fromInt(-1000).toString(), "-1000");
199 expect(new int32.fromInt(123456789).toString(), "123456789");
200 expect(new int32.fromInt(2147483647).toString(), "2147483647");
201 expect(new int32.fromInt(2147483648).toString(), "-2147483648");
202 expect(new int32.fromInt(2147483649).toString(), "-2147483647");
203 expect(new int32.fromInt(2147483650).toString(), "-2147483646");
204 expect(new int32.fromInt(-2147483648).toString(), "-2147483648");
205 expect(new int32.fromInt(-2147483649).toString(), "2147483647");
206 expect(new int32.fromInt(-2147483650).toString(), "2147483646");
207 });
208 });
209
210 group("toHexString", () {
211 test("returns hexadecimal string representation", () {
212 expect(new int32.fromInt(-1).toHexString(), "-1");
213 expect((new int32.fromInt(-1) >> 8).toHexString(), "-1");
214 expect((new int32.fromInt(-1) << 8).toHexString(), "-100");
215 expect(new int32.fromInt(123456789).toHexString(), "75bcd15");
216 expect(new int32.fromInt(-1).shiftRightUnsigned(8).toHexString(),
217 "ffffff");
218 });
219 });
220
221 group("toRadixString", () {
222 test("returns base n string representation", () {
223 expect(new int32.fromInt(123456789).toRadixString(5), "223101104124");
224 });
225 });
159 } 226 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698