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 "third_party/googletest/src/include/gtest/gtest.h" 15 16 #include "vpx/vpx_integer.h" 17 18 namespace libvpx_test { 19 20 class ACMRandom { 21 public: ACMRandom()22 ACMRandom() : random_(DeterministicSeed()) {} 23 ACMRandom(int seed)24 explicit ACMRandom(int seed) : random_(seed) {} 25 Reset(int seed)26 void Reset(int seed) { random_.Reseed(seed); } Rand16(void)27 uint16_t Rand16(void) { 28 const uint32_t value = 29 random_.Generate(testing::internal::Random::kMaxRange); 30 return (value >> 15) & 0xffff; 31 } 32 Rand9Signed(void)33 int16_t Rand9Signed(void) { 34 // Use 9 bits: values between 255 (0x0FF) and -256 (0x100). 35 const uint32_t value = random_.Generate(512); 36 return static_cast<int16_t>(value) - 256; 37 } 38 Rand8(void)39 uint8_t Rand8(void) { 40 const uint32_t value = 41 random_.Generate(testing::internal::Random::kMaxRange); 42 // There's a bit more entropy in the upper bits of this implementation. 43 return (value >> 23) & 0xff; 44 } 45 Rand8Extremes(void)46 uint8_t Rand8Extremes(void) { 47 // Returns a random value near 0 or near 255, to better exercise 48 // saturation behavior. 49 const uint8_t r = Rand8(); 50 return r < 128 ? r << 4 : r >> 4; 51 } 52 PseudoUniform(int range)53 int PseudoUniform(int range) { return random_.Generate(range); } 54 operator()55 int operator()(int n) { return PseudoUniform(n); } 56 DeterministicSeed(void)57 static int DeterministicSeed(void) { return 0xbaba; } 58 59 private: 60 testing::internal::Random random_; 61 }; 62 63 } // namespace libvpx_test 64 65 #endif // TEST_ACM_RANDOM_H_ 66