mirror of
https://github.com/dart-lang/sdk
synced 2024-09-20 01:38:14 +00:00
284312b101
In JavaScript, and integral valued double is an 'int'. Change-Id: I79dedcb00f74dc699f85a21a2052239ed25b7974 Reviewed-on: https://dart-review.googlesource.com/41640 Reviewed-by: Sigmund Cherem <sigmund@google.com> Reviewed-by: Vijay Menon <vsm@google.com> Commit-Queue: Stephen Adams <sra@google.com>
41 lines
1.3 KiB
Dart
41 lines
1.3 KiB
Dart
// Copyright (c) 2018, 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.
|
|
|
|
// Test for JavaScript specific BigInt behaviour. Any JavaScript number (double)
|
|
// that is an integral value is a Dart 'int' value, so any BigInt that has a
|
|
// value that is exactly a double integral value should return `true` for
|
|
// [BigInt.isValidInt].
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
int intPow(int a, int p) {
|
|
int result = 1;
|
|
for (int i = 0; i < p; i++) result *= a;
|
|
return result;
|
|
}
|
|
|
|
int pow2_53 = intPow(2, 53);
|
|
|
|
test(int n1, int n2, int shift, [bool expectedIsValidInt = true]) {
|
|
var n = (new BigInt.from(n1) * new BigInt.from(n2)) << shift;
|
|
Expect.equals(expectedIsValidInt, n.isValidInt, '${n}.isValidInt');
|
|
if (n >= new BigInt.from(pow2_53)) {
|
|
var nplus1 = n + BigInt.one;
|
|
Expect.isFalse(nplus1.isValidInt, '${nplus1}.isValidInt');
|
|
}
|
|
}
|
|
|
|
main() {
|
|
test(13, 19, 1);
|
|
test(19997, 19993, 100);
|
|
test(19997, pow2_53 ~/ 19997, 0);
|
|
test(19997, pow2_53 ~/ 19997, 1);
|
|
test(19997, pow2_53 ~/ 19997, 100);
|
|
test(1, 1, 100);
|
|
test(1, 1, 10000, false);
|
|
|
|
// More than 53 bits in product,
|
|
test(pow2_53 ~/ 3, pow2_53 ~/ 5, 0, false);
|
|
}
|