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: 22 ACMRandom() : random_(DeterministicSeed()) {} 23 24 explicit ACMRandom(int seed) : random_(seed) {} 25 26 void Reset(int seed) { 27 random_.Reseed(seed); 28 } 29 30 uint8_t Rand8(void) { 31 const uint32_t value = 32 random_.Generate(testing::internal::Random::kMaxRange); 33 // There's a bit more entropy in the upper bits of this implementation. 34 return (value >> 24) & 0xff; 35 } 36 37 uint8_t Rand8Extremes(void) { 38 // Returns a random value near 0 or near 255, to better exercise 39 // saturation behavior. 40 const uint8_t r = Rand8(); 41 return r < 128 ? r << 4 : r >> 4; 42 } 43 44 int PseudoUniform(int range) { 45 return random_.Generate(range); 46 } 47 48 int operator()(int n) { 49 return PseudoUniform(n); 50 } 51 52 static int DeterministicSeed(void) { 53 return 0xbaba; 54 } 55 56 private: 57 testing::internal::Random random_; 58 }; 59 60 } // namespace libvpx_test 61 62 #endif // TEST_ACM_RANDOM_H_ 63