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 // 16 17 #include "update_engine/update_manager/prng.h" 18 19 #include <vector> 20 21 #include <gtest/gtest.h> 22 23 using std::vector; 24 25 namespace chromeos_update_manager { 26 27 TEST(UmPRNGTest, ShouldBeDeterministic) { 28 PRNG a(42); 29 PRNG b(42); 30 31 for (int i = 0; i < 1000; ++i) { 32 EXPECT_EQ(a.Rand(), b.Rand()) << "Iteration i=" << i; 33 } 34 } 35 36 TEST(UmPRNGTest, SeedChangesGeneratedSequence) { 37 PRNG a(42); 38 PRNG b(5); 39 40 vector<uint32_t> values_a; 41 vector<uint32_t> values_b; 42 43 for (int i = 0; i < 100; ++i) { 44 values_a.push_back(a.Rand()); 45 values_b.push_back(b.Rand()); 46 } 47 EXPECT_NE(values_a, values_b); 48 } 49 50 TEST(UmPRNGTest, IsNotConstant) { 51 PRNG prng(5); 52 53 uint32_t initial_value = prng.Rand(); 54 bool prng_is_constant = true; 55 for (int i = 0; i < 100; ++i) { 56 if (prng.Rand() != initial_value) { 57 prng_is_constant = false; 58 break; 59 } 60 } 61 EXPECT_FALSE(prng_is_constant) << "After 100 iterations."; 62 } 63 64 TEST(UmPRNGTest, RandCoversRange) { 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 79 } // namespace chromeos_update_manager 80