Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/prng.h" |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 18 | |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | using std::vector; |
| 24 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 25 | namespace chromeos_update_manager { |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 26 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 27 | TEST(UmPRNGTest, ShouldBeDeterministic) { |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 28 | PRNG a(42); |
| 29 | PRNG b(42); |
| 30 | |
| 31 | for (int i = 0; i < 1000; ++i) { |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 32 | EXPECT_EQ(a.Rand(), b.Rand()) << "Iteration i=" << i; |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 36 | TEST(UmPRNGTest, SeedChangesGeneratedSequence) { |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 37 | PRNG a(42); |
| 38 | PRNG b(5); |
| 39 | |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 40 | vector<uint32_t> values_a; |
| 41 | vector<uint32_t> values_b; |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 42 | |
| 43 | for (int i = 0; i < 100; ++i) { |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 44 | values_a.push_back(a.Rand()); |
| 45 | values_b.push_back(b.Rand()); |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 46 | } |
| 47 | EXPECT_NE(values_a, values_b); |
| 48 | } |
| 49 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 50 | TEST(UmPRNGTest, IsNotConstant) { |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 51 | PRNG prng(5); |
| 52 | |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 53 | uint32_t initial_value = prng.Rand(); |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 54 | bool prng_is_constant = true; |
| 55 | for (int i = 0; i < 100; ++i) { |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 56 | if (prng.Rand() != initial_value) { |
Alex Deymo | edfa1d4 | 2014-04-28 16:53:51 -0700 | [diff] [blame] | 57 | prng_is_constant = false; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | EXPECT_FALSE(prng_is_constant) << "After 100 iterations."; |
| 62 | } |
| 63 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 64 | TEST(UmPRNGTest, RandCoversRange) { |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 65 | PRNG a(42); |
| 66 | int hits[11] = { 0 }; |
| 67 | |
| 68 | for (int i = 0; i < 1000; i++) { |
| 69 | int r = a.RandMinMax(0, 10); |
| 70 | ASSERT_LE(0, r); |
| 71 | ASSERT_GE(10, r); |
| 72 | hits[r]++; |
| 73 | } |
| 74 | |
| 75 | for (auto& hit : hits) |
| 76 | EXPECT_LT(0, hit); |
| 77 | } |
| 78 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 79 | } // namespace chromeos_update_manager |