Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/policy_manager/prng.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using std::vector; |
| 12 | |
| 13 | namespace chromeos_policy_manager { |
| 14 | |
| 15 | TEST(PmPRNGTest, ShouldBeDeterministic) { |
| 16 | PRNG a(42); |
| 17 | PRNG b(42); |
| 18 | |
| 19 | for (int i = 0; i < 1000; ++i) { |
| 20 | EXPECT_EQ(a.rand(), b.rand()) << "Iteration i=" << i; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | TEST(PmPRNGTest, SeedChangesGeneratedSequence) { |
| 25 | PRNG a(42); |
| 26 | PRNG b(5); |
| 27 | |
| 28 | vector<int> values_a; |
| 29 | vector<int> values_b; |
| 30 | |
| 31 | for (int i = 0; i < 100; ++i) { |
| 32 | values_a.push_back(a.rand()); |
| 33 | values_b.push_back(b.rand()); |
| 34 | } |
| 35 | EXPECT_NE(values_a, values_b); |
| 36 | } |
| 37 | |
| 38 | TEST(PmPRNGTest, IsNotConstant) { |
| 39 | PRNG prng(5); |
| 40 | |
| 41 | int initial_value = prng.rand(); |
| 42 | bool prng_is_constant = true; |
| 43 | for (int i = 0; i < 100; ++i) { |
| 44 | if (prng.rand() != initial_value) { |
| 45 | prng_is_constant = false; |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | EXPECT_FALSE(prng_is_constant) << "After 100 iterations."; |
| 50 | } |
| 51 | |
| 52 | } // namespace chromeos_policy_manager |