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

Side by Side Diff: tests/lib/math/random_secure_test.dart

Issue 1398453004: Add a 'secure' constructor to Random in dart:math returning a cryptographically (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: address review comments Created 5 years, 2 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Test that the secure random generator does not systematically generates
6 // duplicates. Note that this test is flaky by definition, since duplicates
7 // can occur. They should be extremely rare, though.
8
9 // Library tag to allow Dartium to run the test.
10 library random_secure;
11
12 import "package:expect/expect.dart";
13 import 'dart:math';
14
15 main() {
16 var results;
17 var rng0;
18 var rng1;
19 var checkInt = (max) {
20 var intVal0 = rng0.nextInt(max);
21 var intVal1 = rng1.nextInt(max);
22 if (max > (1 << 28)) {
23 Expect.isFalse(results.contains(intVal0));
24 results.add(intVal0);
25 Expect.isFalse(results.contains(intVal1));
26 results.add(intVal1);
27 }
28 };
29 results = [];
30 rng0 = new Random.secure();
31 for(var i = 0; i <= 32; i++) {
32 rng1 = new Random.secure();
33 checkInt(1 << 32);
34 checkInt(1 << (32 - i));
Lasse Reichstein Nielsen 2015/10/15 09:32:33 Does it even make sense to call for i < (1<<28) -
regis 2015/10/15 17:12:02 It makes sure that we can generate random values w
35 checkInt(1000000000);
36 }
37 var checkDouble = () {
38 var doubleVal0 = rng0.nextDouble();
39 var doubleVal1 = rng1.nextDouble();
40 Expect.isFalse(results.contains(doubleVal0));
41 results.add(doubleVal0);
42 Expect.isFalse(results.contains(doubleVal1));
43 results.add(doubleVal1);
44 };
45 results = [];
46 rng0 = new Random.secure();
47 for(var i = 0; i < 32; i++) {
48 rng1 = new Random.secure();
Lasse Reichstein Nielsen 2015/10/15 09:32:33 You could just make checkDouble create the new rng
regis 2015/10/15 17:12:02 Sure. I prefer to keep the symmetry with other tes
49 checkDouble();
50 }
51 var cnt0 = 0;
52 var cnt1 = 0;
53 rng0 = new Random.secure();
54 for(var i = 0; i < 32; i++) {
55 rng1 = new Random.secure();
56 cnt0 += rng0.nextBool() ? 1 : 0;
57 cnt1 += rng1.nextBool() ? 1 : 0;
58 }
59 Expect.isTrue((cnt0 > 0) && (cnt0 < 32));
60 Expect.isTrue((cnt1 > 0) && (cnt1 < 32));
61 }
62
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698