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

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

Issue 24388004: Added bitLength, clamp(), toDouble(), toSigned(), toUnsigned(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use string literals in place of doubles where precision is exceeded Created 7 years, 3 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/lib/src/intx.dart ('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:fixnum/fixnum.dart'; 6 import 'package:fixnum/fixnum.dart';
7 import 'package:unittest/unittest.dart'; 7 import 'package:unittest/unittest.dart';
8 8
9 void main() { 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
10 group("arithmetic operators", () { 54 group("arithmetic operators", () {
11 Int32 n1 = new Int32(1234); 55 Int32 n1 = new Int32(1234);
12 Int32 n2 = new Int32(9876); 56 Int32 n2 = new Int32(9876);
13 Int32 n3 = new Int32(-1234); 57 Int32 n3 = new Int32(-1234);
14 Int32 n4 = new Int32(-9876); 58 Int32 n4 = new Int32(-9876);
15 Int32 n5 = new Int32(0x12345678); 59 Int32 n5 = new Int32(0x12345678);
16 Int32 n6 = new Int32(0x22222222); 60 Int32 n6 = new Int32(0x22222222);
17 61
18 test("+", () { 62 test("+", () {
19 expect(n1 + n2, new Int32(11110)); 63 expect(n1 + n2, new Int32(11110));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 expect(new Int32(0x12345678).remainder(new Int32(-0x22)), 122 expect(new Int32(0x12345678).remainder(new Int32(-0x22)),
79 new Int32(0x12345678.remainder(-0x22))); 123 new Int32(0x12345678.remainder(-0x22)));
80 expect(new Int32(-0x12345678).remainder(new Int32(-0x22)), 124 expect(new Int32(-0x12345678).remainder(new Int32(-0x22)),
81 new Int32(-0x12345678.remainder(-0x22))); 125 new Int32(-0x12345678.remainder(-0x22)));
82 expect(new Int32(-0x12345678).remainder(new Int32(0x22)), 126 expect(new Int32(-0x12345678).remainder(new Int32(0x22)),
83 new Int32(-0x12345678.remainder(0x22))); 127 new Int32(-0x12345678.remainder(0x22)));
84 expect(new Int32(0x12345678).remainder(new Int64(0x22)), 128 expect(new Int32(0x12345678).remainder(new Int64(0x22)),
85 new Int32(0x12345678.remainder(0x22))); 129 new Int32(0x12345678.remainder(0x22)));
86 expect(() => new Int32(17).remainder(null), throws); 130 expect(() => new Int32(17).remainder(null), throws);
87 }); 131 });
132
133 test("clamp", () {
134 Int32 val = new Int32(17);
135 expect(val.clamp(20, 30), new Int32(20));
136 expect(val.clamp(10, 20), new Int32(17));
137 expect(val.clamp(10, 15), new Int32(15));
138
139 expect(val.clamp(new Int32(20), new Int32(30)), new Int32(20));
140 expect(val.clamp(new Int32(10), new Int32(20)), new Int32(17));
141 expect(val.clamp(new Int32(10), new Int32(15)), new Int32(15));
142
143 expect(val.clamp(new Int64(20), new Int64(30)), new Int32(20));
144 expect(val.clamp(new Int64(10), new Int64(20)), new Int32(17));
145 expect(val.clamp(new Int64(10), new Int64(15)), new Int32(15));
146 expect(val.clamp(Int64.MIN_VALUE, new Int64(30)), new Int32(17));
147 expect(val.clamp(new Int64(10), Int64.MAX_VALUE), new Int32(17));
148
149 expect(() => val.clamp(1, 'b'), throwsA(isArgumentError));
150 expect(() => val.clamp('a', 1), throwsA(isArgumentError));
151 });
88 }); 152 });
89 153
90 group("comparison operators", () { 154 group("comparison operators", () {
91 test("<", () { 155 test("<", () {
92 expect(new Int32(17) < new Int32(18), true); 156 expect(new Int32(17) < new Int32(18), true);
93 expect(new Int32(17) < new Int32(17), false); 157 expect(new Int32(17) < new Int32(17), false);
94 expect(new Int32(17) < new Int32(16), false); 158 expect(new Int32(17) < new Int32(16), false);
95 expect(new Int32(17) < new Int64(18), true); 159 expect(new Int32(17) < new Int64(18), true);
96 expect(new Int32(17) < new Int64(17), false); 160 expect(new Int32(17) < new Int64(17), false);
97 expect(new Int32(17) < new Int64(16), false); 161 expect(new Int32(17) < new Int64(16), false);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 }); 258 });
195 259
196 test("shiftRightUnsigned", () { 260 test("shiftRightUnsigned", () {
197 expect(new Int32(0x12345678).shiftRightUnsigned(7), 261 expect(new Int32(0x12345678).shiftRightUnsigned(7),
198 new Int32(0x12345678 >> 7)); 262 new Int32(0x12345678 >> 7));
199 expect(() => (new Int32(17).shiftRightUnsigned(-1)), throwsArgumentError); 263 expect(() => (new Int32(17).shiftRightUnsigned(-1)), throwsArgumentError);
200 expect(() => (new Int32(17).shiftRightUnsigned(null)), throws); 264 expect(() => (new Int32(17).shiftRightUnsigned(null)), throws);
201 }); 265 });
202 }); 266 });
203 267
204 group("type conversions", () { 268 group("conversions", () {
205 expect(new Int32(17).toInt(), 17); 269 test("toSigned", () {
206 expect(new Int32(-17).toInt(), -17); 270 expect(Int32.ONE.toSigned(2), Int32.ONE);
207 expect(new Int32(17).toInt32(), new Int32(17)); 271 expect(Int32.ONE.toSigned(1), -Int32.ONE);
208 expect(new Int32(-17).toInt32(), new Int32(-17)); 272 expect(Int32.MAX_VALUE.toSigned(32), Int32.MAX_VALUE);
209 expect(new Int32(17).toInt64(), new Int64(17)); 273 expect(Int32.MIN_VALUE.toSigned(32), Int32.MIN_VALUE);
210 expect(new Int32(-17).toInt64(), new Int64(-17)); 274 expect(Int32.MAX_VALUE.toSigned(31), -Int32.ONE);
275 expect(Int32.MIN_VALUE.toSigned(31), Int32.ZERO);
276 expect(() => Int32.ONE.toSigned(0), throws);
277 expect(() => Int32.ONE.toSigned(33), throws);
278 });
279 test("toUnsigned", () {
280 expect(Int32.ONE.toUnsigned(1), Int32.ONE);
281 expect(Int32.ONE.toUnsigned(0), Int32.ZERO);
282 expect(Int32.MAX_VALUE.toUnsigned(32), Int32.MAX_VALUE);
283 expect(Int32.MIN_VALUE.toUnsigned(32), Int32.MIN_VALUE);
284 expect(Int32.MAX_VALUE.toUnsigned(31), Int32.MAX_VALUE);
285 expect(Int32.MIN_VALUE.toUnsigned(31), Int32.ZERO);
286 expect(() => Int32.ONE.toUnsigned(-1), throws);
287 expect(() => Int32.ONE.toUnsigned(33), throws);
288 });
289 test("toDouble", () {
290 expect(new Int32(17).toDouble(), same(17.0));
291 expect(new Int32(-17).toDouble(), same(-17.0));
292 });
293 test("toInt", () {
294 expect(new Int32(17).toInt(), 17);
295 expect(new Int32(-17).toInt(), -17);
296 });
297 test("toInt32", () {
298 expect(new Int32(17).toInt32(), new Int32(17));
299 expect(new Int32(-17).toInt32(), new Int32(-17));
300 });
301 test("toInt64", () {
302 expect(new Int32(17).toInt64(), new Int64(17));
303 expect(new Int32(-17).toInt64(), new Int64(-17));
304 });
211 }); 305 });
212 306
213 group("string representation", () { 307 group("string representation", () {
214 test("toString", () { 308 test("toString", () {
215 expect(new Int32(0).toString(), "0"); 309 expect(new Int32(0).toString(), "0");
216 expect(new Int32(1).toString(), "1"); 310 expect(new Int32(1).toString(), "1");
217 expect(new Int32(-1).toString(), "-1"); 311 expect(new Int32(-1).toString(), "-1");
218 expect(new Int32(1000).toString(), "1000"); 312 expect(new Int32(1000).toString(), "1000");
219 expect(new Int32(-1000).toString(), "-1000"); 313 expect(new Int32(-1000).toString(), "-1000");
220 expect(new Int32(123456789).toString(), "123456789"); 314 expect(new Int32(123456789).toString(), "123456789");
(...skipping 16 matching lines...) Expand all
237 expect(new Int32(-1).shiftRightUnsigned(8).toHexString(), "ffffff"); 331 expect(new Int32(-1).shiftRightUnsigned(8).toHexString(), "ffffff");
238 }); 332 });
239 }); 333 });
240 334
241 group("toRadixString", () { 335 group("toRadixString", () {
242 test("returns base n string representation", () { 336 test("returns base n string representation", () {
243 expect(new Int32(123456789).toRadixString(5), "223101104124"); 337 expect(new Int32(123456789).toRadixString(5), "223101104124");
244 }); 338 });
245 }); 339 });
246 } 340 }
OLDNEW
« no previous file with comments | « pkg/fixnum/lib/src/intx.dart ('k') | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698