Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 // Testing Bigints. | |
| 5 | |
| 6 library bit_twiddling_test; | |
| 7 import "package:expect/expect.dart"; | |
| 8 | |
| 9 | |
| 10 bool haveBigints() { | |
| 11 return 100000000000000000000 + 1 != 100000000000000000000; | |
| 12 } | |
| 13 | |
| 14 testBitLength() { | |
| 15 check(int i, width) { | |
| 16 Expect.equals(width, i.bitLength, '$i.bitLength == $width'); | |
| 17 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.
| |
| 18 } | |
| 19 | |
| 20 check(0, 0); | |
| 21 check(1, 1); | |
| 22 check(2, 2); | |
| 23 check(3, 2); | |
| 24 check(4, 3); | |
| 25 check(5, 3); | |
| 26 check(6, 3); | |
| 27 check(7, 3); | |
| 28 check(8, 4); | |
| 29 check(127, 7); | |
| 30 check(128, 8); | |
| 31 check(129, 8); | |
| 32 check(2147483646, 31); | |
| 33 check(2147483647, 31); | |
| 34 check(2147483648, 32); | |
| 35 check(2147483649, 32); | |
| 36 check(4294967295, 32); | |
| 37 check(4294967296, 33); | |
| 38 check(0xffffffffff, 40); | |
| 39 check(0xfffffffffff, 44); | |
| 40 check(0xffffffffffff, 48); | |
| 41 check(0x1000000000000, 49); | |
| 42 check(0x1000000000001, 49); | |
| 43 check(0x1ffffffffffff, 49); | |
| 44 check(0x2000000000000, 50); | |
| 45 check(0x2000000000001, 50); | |
| 46 | |
| 47 if (haveBigints()) { | |
| 48 check(0xffffffffffffff, 56); | |
| 49 check(0xffffffffffffffff, 64); | |
| 50 check(0xffffffffffffffffff, 72); | |
| 51 check(0x1000000000000000000, 73); | |
| 52 check(0x1000000000000000001, 73); | |
| 53 | |
| 54 | |
| 55 check(0xfffffffffffffffffffffffffffffffffffffe, 152); | |
| 56 check(0xffffffffffffffffffffffffffffffffffffff, 152); | |
| 57 check(0x100000000000000000000000000000000000000, 153); | |
| 58 check(0x100000000000000000000000000000000000001, 153); | |
| 59 | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 testConversions() { | |
| 64 checkU(src, width, expected) { | |
| 65 Expect.equals(expected, src.toUnsigned(width)); | |
| 66 } | |
| 67 checkS(src, width, expected) { | |
| 68 Expect.equals(expected, src.toSigned(width), | |
| 69 '$src.toSigned($width) == $expected'); | |
| 70 } | |
| 71 checkU(1, 8, 1); | |
| 72 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.
| |
| 73 checkU(0xffffffff, 32, 0xffffffff); | |
| 74 | |
| 75 checkU( 1, 0, 0); | |
| 76 checkU( 0, 0, 0); | |
| 77 checkU(-1, 0, 0); | |
| 78 | |
| 79 checkS(0xff, 8, -1); | |
| 80 checkS(128, 8, -128); | |
| 81 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.
| |
| 82 | |
| 83 checkS(-1, 1, -1); | |
| 84 checkS( 0, 1, 0); | |
| 85 checkS( 1, 1, -1); | |
| 86 checkS( 2, 1, 0); | |
| 87 checkS( 3, 1, -1); | |
| 88 } | |
| 89 | |
| 90 main() { | |
| 91 testBitLength(); | |
| 92 testConversions(); | |
| 93 } | |
| OLD | NEW |