dart-sdk/tests/standalone_2/double_hash_distribution_test.dart
Ryan Macnak 442e0e8192 Test double.hashCode with and without intrinsics.
Change-Id: I34c317d3f11bef061cb9f2f6ad1123e948ef78f2
Reviewed-on: https://dart-review.googlesource.com/16683
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Reviewed-by: Kasper Lund <kasperl@google.com>
2017-10-26 23:59:32 +00:00

49 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;
}