mirror of
https://github.com/dart-lang/sdk
synced 2024-09-18 21:41:19 +00:00
ce4b15c0e9
- bit length - truncate to signed fixed width integer - truncate to unsigned fixed width integer This change is motivated by a program that spends 20-30% of its time here: // TODO(5828): Replace this with a bit-length method on int when available. int n_bitLength = this.n.toRadixString(2).length; See Also https://code.google.com/p/dart/issues/detail?id=5828#c3 https://code.google.com/p/dart/issues/detail?id=6486#c2 https://code.google.com/p/dart/issues/detail?id=5798#c5 https://code.google.com/p/dart/issues/detail?id=12008 R=lrn@google.com, srdjan@google.com Review URL: https://codereview.chromium.org//23645003 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@27269 260f80e4-7a28-3924-810f-c04153c831b5
62 lines
1.9 KiB
Dart
62 lines
1.9 KiB
Dart
// 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";
|
|
|
|
// See bit_twiddling_test.dart first. This file contains only the tests that
|
|
// need Bigint or would fail in dart2js compatibility mode.
|
|
|
|
testBitLength() {
|
|
check(int i, width) {
|
|
Expect.equals(width, i.bitLength, '$i.bitLength == $width');
|
|
// (~i) written as (-i-1) to avoid issues with limited range of dart2js ops.
|
|
Expect.equals(width, (-i-1).bitLength, '(~$i).bitLength == $width');
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
testToUnsigned() {
|
|
checkU(src, width, expected) {
|
|
Expect.equals(expected, src.toUnsigned(width));
|
|
}
|
|
|
|
checkU(0x100000100000000000001, 2, 1);
|
|
checkU(0x100000200000000000001, 60, 0x200000000000001);
|
|
checkU(0x100000200000000000001, 59, 0x200000000000001);
|
|
checkU(0x100000200000000000001, 58, 0x200000000000001);
|
|
checkU(0x100000200000000000001, 57, 1);
|
|
}
|
|
|
|
testToSigned() {
|
|
checkS(src, width, expected) {
|
|
Expect.equals(expected, src.toSigned(width),
|
|
'$src.toSigned($width) == $expected');
|
|
}
|
|
|
|
checkS(0x100000100000000000001, 2, 1);
|
|
checkS(0x100000200000000000001, 60, 0x200000000000001);
|
|
checkS(0x100000200000000000001, 59, 0x200000000000001);
|
|
checkS(0x100000200000000000001, 58, -0x200000000000000 + 1);
|
|
checkS(0x100000200000000000001, 57, 1);
|
|
}
|
|
|
|
main() {
|
|
testBitLength();
|
|
testToUnsigned();
|
|
testToSigned();
|
|
}
|