dart-sdk/tests/standalone/double_hash_distribution_test.dart
Robert Nystrom 35da19ac07 Migrate standalone_2 to null safety.
Change-Id: I0e97add738bc79314a210cf137525b7391467fc2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/152595
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2020-06-26 19:17:50 +00:00

48 lines
1.4 KiB
Dart

// Copyright (c) 2017, 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 that the distribution of hash codes for doubles is reasonable.
// VMOptions=--intrinsify
// VMOptions=--no_intrinsify
import 'package:expect/expect.dart';
main() {
Expect.isTrue(ratio(0, 1) >= 0.95);
Expect.isTrue(ratio(0, 100) >= 0.95);
Expect.isTrue(ratio(0, 0xffffff) >= 0.95);
Expect.isTrue(ratio(0xffffff) >= 0.95);
Expect.isTrue(ratio(0xffffffff) >= 0.95);
Expect.isTrue(ratio(0xffffffffffffff) >= 0.95);
Expect.isTrue(ratio(0, -1) >= 0.95);
Expect.isTrue(ratio(0, -100) >= 0.95);
Expect.isTrue(ratio(0, -0xffffff) >= 0.95);
Expect.isTrue(ratio(-0xffffff) >= 0.95);
Expect.isTrue(ratio(-0xffffffff) >= 0.95);
Expect.isTrue(ratio(-0xffffffffffffff) >= 0.95);
}
double ratio(num start, [num? end]) {
final n = 1000;
end ??= (start + 1) * 2;
// Collect the set of distinct doubles and the
// set of distinct hash codes.
final doubles = new Set<double>();
final codes = new Set<int>();
final step = (end.toDouble() - start.toDouble()) / n;
var current = start.toDouble();
for (int i = 0; i < n; i++) {
doubles.add(current);
codes.add(current.hashCode);
current += step;
}
// Return the ratio between distinct doubles and
// distinct hash codes.
return codes.length / doubles.length;
}