blob: 2a3f68951b3e5ef3edd96d9951caa70fe56d0cb2 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymoedfa1d42014-04-28 16:53:51 -070016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/prng.h"
Alex Deymoedfa1d42014-04-28 16:53:51 -070018
19#include <vector>
20
21#include <gtest/gtest.h>
22
23using std::vector;
24
Alex Deymo63784a52014-05-28 10:46:14 -070025namespace chromeos_update_manager {
Alex Deymoedfa1d42014-04-28 16:53:51 -070026
Alex Deymo63784a52014-05-28 10:46:14 -070027TEST(UmPRNGTest, ShouldBeDeterministic) {
Alex Deymoedfa1d42014-04-28 16:53:51 -070028 PRNG a(42);
29 PRNG b(42);
30
31 for (int i = 0; i < 1000; ++i) {
Gilad Arnolde1218812014-05-07 12:21:36 -070032 EXPECT_EQ(a.Rand(), b.Rand()) << "Iteration i=" << i;
Alex Deymoedfa1d42014-04-28 16:53:51 -070033 }
34}
35
Alex Deymo63784a52014-05-28 10:46:14 -070036TEST(UmPRNGTest, SeedChangesGeneratedSequence) {
Alex Deymoedfa1d42014-04-28 16:53:51 -070037 PRNG a(42);
38 PRNG b(5);
39
Gilad Arnolde1218812014-05-07 12:21:36 -070040 vector<uint32_t> values_a;
41 vector<uint32_t> values_b;
Alex Deymoedfa1d42014-04-28 16:53:51 -070042
43 for (int i = 0; i < 100; ++i) {
Gilad Arnolde1218812014-05-07 12:21:36 -070044 values_a.push_back(a.Rand());
45 values_b.push_back(b.Rand());
Alex Deymoedfa1d42014-04-28 16:53:51 -070046 }
47 EXPECT_NE(values_a, values_b);
48}
49
Alex Deymo63784a52014-05-28 10:46:14 -070050TEST(UmPRNGTest, IsNotConstant) {
Alex Deymoedfa1d42014-04-28 16:53:51 -070051 PRNG prng(5);
52
Gilad Arnolde1218812014-05-07 12:21:36 -070053 uint32_t initial_value = prng.Rand();
Alex Deymoedfa1d42014-04-28 16:53:51 -070054 bool prng_is_constant = true;
55 for (int i = 0; i < 100; ++i) {
Gilad Arnolde1218812014-05-07 12:21:36 -070056 if (prng.Rand() != initial_value) {
Alex Deymoedfa1d42014-04-28 16:53:51 -070057 prng_is_constant = false;
58 break;
59 }
60 }
61 EXPECT_FALSE(prng_is_constant) << "After 100 iterations.";
62}
63
Alex Deymo63784a52014-05-28 10:46:14 -070064TEST(UmPRNGTest, RandCoversRange) {
Gilad Arnolde1218812014-05-07 12:21:36 -070065 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 Deymo63784a52014-05-28 10:46:14 -070079} // namespace chromeos_update_manager