Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2044)

Unified Diff: test/codegen/lib/math/random_secure_test.dart

Issue 1967773005: update dart:math (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/codegen/lib/math/random_big_test.dart ('k') | test/codegen/lib/math/random_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/codegen/lib/math/random_secure_test.dart
diff --git a/test/codegen/lib/math/random_secure_test.dart b/test/codegen/lib/math/random_secure_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..33a563ef45f9a4aedc59755eb174fbedb0ebd694
--- /dev/null
+++ b/test/codegen/lib/math/random_secure_test.dart
@@ -0,0 +1,62 @@
+// Copyright (c) 2015, 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 secure random generator does not systematically generates
+// duplicates. Note that this test is flaky by definition, since duplicates
+// can occur. They should be extremely rare, though.
+
+// Library tag to allow Dartium to run the test.
+library random_secure;
+
+import "package:expect/expect.dart";
+import 'dart:math';
+
+main() {
+ var results;
+ var rng0;
+ var rng1;
+ var checkInt = (max) {
+ var intVal0 = rng0.nextInt(max);
+ var intVal1 = rng1.nextInt(max);
+ if (max > (1 << 28)) {
+ Expect.isFalse(results.contains(intVal0));
+ results.add(intVal0);
+ Expect.isFalse(results.contains(intVal1));
+ results.add(intVal1);
+ }
+ };
+ results = [];
+ rng0 = new Random.secure();
+ for(var i = 0; i <= 32; i++) {
+ rng1 = new Random.secure();
+ checkInt(pow(2, 32));
+ checkInt(pow(2, 32 - i));
+ checkInt(1000000000);
+ }
+ var checkDouble = () {
+ var doubleVal0 = rng0.nextDouble();
+ var doubleVal1 = rng1.nextDouble();
+ Expect.isFalse(results.contains(doubleVal0));
+ results.add(doubleVal0);
+ Expect.isFalse(results.contains(doubleVal1));
+ results.add(doubleVal1);
+ };
+ results = [];
+ rng0 = new Random.secure();
+ for(var i = 0; i < 32; i++) {
+ rng1 = new Random.secure();
+ checkDouble();
+ }
+ var cnt0 = 0;
+ var cnt1 = 0;
+ rng0 = new Random.secure();
+ for(var i = 0; i < 32; i++) {
+ rng1 = new Random.secure();
+ cnt0 += rng0.nextBool() ? 1 : 0;
+ cnt1 += rng1.nextBool() ? 1 : 0;
+ }
+ Expect.isTrue((cnt0 > 0) && (cnt0 < 32));
+ Expect.isTrue((cnt1 > 0) && (cnt1 < 32));
+}
+
« no previous file with comments | « test/codegen/lib/math/random_big_test.dart ('k') | test/codegen/lib/math/random_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698