Index: tests/corelib/bit_twiddling_test.dart |
diff --git a/tests/corelib/bit_twiddling_test.dart b/tests/corelib/bit_twiddling_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7cf7e4cf786c80e57bc37ef5f231cb2cdbd8510c |
--- /dev/null |
+++ b/tests/corelib/bit_twiddling_test.dart |
@@ -0,0 +1,93 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+// Testing Bigints. |
+ |
+library bit_twiddling_test; |
+import "package:expect/expect.dart"; |
+ |
+ |
+bool haveBigints() { |
+ return 100000000000000000000 + 1 != 100000000000000000000; |
+} |
+ |
+testBitLength() { |
+ check(int i, width) { |
+ Expect.equals(width, i.bitLength, '$i.bitLength == $width'); |
+ Expect.equals(width, (-i-1).bitLength, '(~$i).bitLength == $width'); |
Lasse Reichstein Nielsen
2013/09/06 12:40:11
Why -i-1 instead of ~i here? Is it dart2js conside
sra1
2013/09/06 22:11:57
Yes, added comment.
|
+ } |
+ |
+ check(0, 0); |
+ check(1, 1); |
+ check(2, 2); |
+ check(3, 2); |
+ check(4, 3); |
+ check(5, 3); |
+ check(6, 3); |
+ check(7, 3); |
+ check(8, 4); |
+ check(127, 7); |
+ check(128, 8); |
+ check(129, 8); |
+ check(2147483646, 31); |
+ check(2147483647, 31); |
+ check(2147483648, 32); |
+ check(2147483649, 32); |
+ check(4294967295, 32); |
+ check(4294967296, 33); |
+ check(0xffffffffff, 40); |
+ check(0xfffffffffff, 44); |
+ check(0xffffffffffff, 48); |
+ check(0x1000000000000, 49); |
+ check(0x1000000000001, 49); |
+ check(0x1ffffffffffff, 49); |
+ check(0x2000000000000, 50); |
+ check(0x2000000000001, 50); |
+ |
+ if (haveBigints()) { |
+ check(0xffffffffffffff, 56); |
+ check(0xffffffffffffffff, 64); |
+ check(0xffffffffffffffffff, 72); |
+ check(0x1000000000000000000, 73); |
+ check(0x1000000000000000001, 73); |
+ |
+ |
+ check(0xfffffffffffffffffffffffffffffffffffffe, 152); |
+ check(0xffffffffffffffffffffffffffffffffffffff, 152); |
+ check(0x100000000000000000000000000000000000000, 153); |
+ check(0x100000000000000000000000000000000000001, 153); |
+ |
+ } |
+} |
+ |
+testConversions() { |
+ checkU(src, width, expected) { |
+ Expect.equals(expected, src.toUnsigned(width)); |
+ } |
+ checkS(src, width, expected) { |
+ Expect.equals(expected, src.toSigned(width), |
+ '$src.toSigned($width) == $expected'); |
+ } |
+ checkU(1, 8, 1); |
+ checkU(0xff, 8, 0xff); |
Lasse Reichstein Nielsen
2013/09/06 12:40:11
There is no truncating test, and not unsigned->sig
sra1
2013/09/06 22:11:57
Done.
|
+ checkU(0xffffffff, 32, 0xffffffff); |
+ |
+ checkU( 1, 0, 0); |
+ checkU( 0, 0, 0); |
+ checkU(-1, 0, 0); |
+ |
+ checkS(0xff, 8, -1); |
+ checkS(128, 8, -128); |
+ checkS(0x80000000, 32, -2147483648); |
Lasse Reichstein Nielsen
2013/09/06 12:40:11
Similar truncating tests for signed:
checkS(-1,
sra1
2013/09/06 22:11:57
Done.
|
+ |
+ checkS(-1, 1, -1); |
+ checkS( 0, 1, 0); |
+ checkS( 1, 1, -1); |
+ checkS( 2, 1, 0); |
+ checkS( 3, 1, -1); |
+} |
+ |
+main() { |
+ testBitLength(); |
+ testConversions(); |
+} |