1 /* 2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef TEST_ACM_RANDOM_H_ 12 #define TEST_ACM_RANDOM_H_ 13 14 #include <assert.h> 15 16 #include <limits> 17 18 #include "third_party/googletest/src/include/gtest/gtest.h" 19 20 #include "vpx/vpx_integer.h" 21 22 namespace libvpx_test { 23 24 class ACMRandom { 25 public: ACMRandom()26 ACMRandom() : random_(DeterministicSeed()) {} 27 ACMRandom(int seed)28 explicit ACMRandom(int seed) : random_(seed) {} 29 Reset(int seed)30 void Reset(int seed) { random_.Reseed(seed); } Rand16(void)31 uint16_t Rand16(void) { 32 const uint32_t value = 33 random_.Generate(testing::internal::Random::kMaxRange); 34 return (value >> 15) & 0xffff; 35 } 36 Rand9Signed(void)37 int16_t Rand9Signed(void) { 38 // Use 9 bits: values between 255 (0x0FF) and -256 (0x100). 39 const uint32_t value = random_.Generate(512); 40 return static_cast<int16_t>(value) - 256; 41 } 42 Rand8(void)43 uint8_t Rand8(void) { 44 const uint32_t value = 45 random_.Generate(testing::internal::Random::kMaxRange); 46 // There's a bit more entropy in the upper bits of this implementation. 47 return (value >> 23) & 0xff; 48 } 49 Rand8Extremes(void)50 uint8_t Rand8Extremes(void) { 51 // Returns a random value near 0 or near 255, to better exercise 52 // saturation behavior. 53 const uint8_t r = Rand8(); 54 return r < 128 ? r << 4 : r >> 4; 55 } 56 RandRange(const uint32_t range)57 uint32_t RandRange(const uint32_t range) { 58 // testing::internal::Random::Generate provides values in the range 59 // testing::internal::Random::kMaxRange. 60 assert(range <= testing::internal::Random::kMaxRange); 61 return random_.Generate(range); 62 } 63 PseudoUniform(int range)64 int PseudoUniform(int range) { return random_.Generate(range); } 65 operator()66 int operator()(int n) { return PseudoUniform(n); } 67 DeterministicSeed(void)68 static int DeterministicSeed(void) { return 0xbaba; } 69 70 private: 71 testing::internal::Random random_; 72 }; 73 74 } // namespace libvpx_test 75 76 #endif // TEST_ACM_RANDOM_H_ 77