1 /* 2 * Copyright (C) 2017 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 <limits> 18 #include "gtest/gtest.h" 19 20 #include "include/random_generator.h" 21 22 template <typename TestType> 23 class RandomGeneratorTest : public testing::Test { 24 public: 25 wifi_offload_test::RandomGenerator random_gen_; 26 27 void GeneratedNumbersAreSmallerOrEqualTypeMaxValue() { 28 uint64_t rand_val = random_gen_.get<TestType>(); 29 uint64_t max_val = std::numeric_limits<TestType>::max(); 30 31 EXPECT_TRUE(rand_val <= max_val); 32 } 33 34 void AllRandomGeneratorsGenerateTheSameSequence() { 35 constexpr size_t num_values = 10; 36 TestType rand_values[num_values]; 37 38 for (size_t i = 0; i < num_values; i++) { 39 rand_values[i] = random_gen_.get<TestType>(); 40 } 41 42 wifi_offload_test::RandomGenerator another_random_gen_; 43 for (size_t i = 0; i < num_values; i++) { 44 ASSERT_EQ(rand_values[i], another_random_gen_.get<TestType>()); 45 } 46 } 47 48 void AfterResetGeneratesTheSameSequence() { 49 constexpr size_t num_values = 10; 50 TestType rand_values[num_values]; 51 52 for (size_t i = 0; i < num_values; i++) { 53 rand_values[i] = random_gen_.get<TestType>(); 54 } 55 56 random_gen_.Reset(); 57 for (size_t i = 0; i < num_values; i++) { 58 ASSERT_EQ(rand_values[i], random_gen_.get<TestType>()); 59 } 60 } 61 62 void GeneratesDifferentNumbersIn8Bytes() { 63 constexpr size_t num_values = 10; 64 uint64_t rand_values[num_values]; 65 66 constexpr size_t repeats_to_fill_8_bytes = 67 sizeof(uint64_t) / sizeof(TestType); 68 constexpr size_t shift_size = 69 (sizeof(TestType) * 8) % (sizeof(uint64_t) * 8); 70 71 for (size_t i = 0; i < num_values; i++) { 72 rand_values[i] = 0; 73 for (size_t j = 0; j < repeats_to_fill_8_bytes; j++) { 74 rand_values[i] <<= shift_size; 75 rand_values[i] |= random_gen_.get<TestType>(); 76 } 77 } 78 79 // The probability of choosing equal random numbers out of 2 ^ 64 values 80 // is "extremely" small. Smaller than the change of memory failure. 81 for (size_t i = 0; i < num_values - 1; i++) { 82 for (size_t j = i + 1; j < num_values; j++) { 83 ASSERT_NE(rand_values[i], rand_values[j]); 84 } 85 } 86 } 87 }; 88 89 typedef testing::Types<uint8_t, uint16_t, uint32_t, uint64_t> Implementations; 90 91 TYPED_TEST_CASE(RandomGeneratorTest, Implementations); 92 93 TYPED_TEST(RandomGeneratorTest, GeneratedNumbersAreSmallerOrEqualTypeMaxValue) { 94 this->GeneratedNumbersAreSmallerOrEqualTypeMaxValue(); 95 } 96 97 TYPED_TEST(RandomGeneratorTest, AllRandomGeneratorsGenerateTheSameSequence) { 98 this->AllRandomGeneratorsGenerateTheSameSequence(); 99 } 100 101 TYPED_TEST(RandomGeneratorTest, AfterResetGeneratesTheSameSequence) { 102 this->AfterResetGeneratesTheSameSequence(); 103 } 104 105 TYPED_TEST(RandomGeneratorTest, GeneratesDifferentNumbersIn8Bytes) { 106 this->GeneratesDifferentNumbersIn8Bytes(); 107 } 108