2012-07-20 20:51:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
2013-09-05 17:45:56 +02:00
|
|
|
#ifndef TEST_ACM_RANDOM_H_
|
|
|
|
#define TEST_ACM_RANDOM_H_
|
2012-07-20 20:51:06 +02:00
|
|
|
|
2013-04-05 04:00:31 +02:00
|
|
|
#include "third_party/googletest/src/include/gtest/gtest.h"
|
2012-07-20 20:51:06 +02:00
|
|
|
|
2012-07-24 19:19:44 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
|
|
|
|
2012-07-20 20:51:06 +02:00
|
|
|
namespace libvpx_test {
|
|
|
|
|
|
|
|
class ACMRandom {
|
|
|
|
public:
|
2013-04-05 04:00:31 +02:00
|
|
|
ACMRandom() : random_(DeterministicSeed()) {}
|
2012-06-28 20:43:58 +02:00
|
|
|
|
2013-04-05 04:00:31 +02:00
|
|
|
explicit ACMRandom(int seed) : random_(seed) {}
|
2012-07-20 20:51:06 +02:00
|
|
|
|
2016-07-26 07:50:48 +02:00
|
|
|
void Reset(int seed) { random_.Reseed(seed); }
|
2013-12-13 19:05:40 +01:00
|
|
|
uint16_t Rand16(void) {
|
|
|
|
const uint32_t value =
|
|
|
|
random_.Generate(testing::internal::Random::kMaxRange);
|
2014-12-02 00:10:00 +01:00
|
|
|
return (value >> 15) & 0xffff;
|
2013-12-13 19:05:40 +01:00
|
|
|
}
|
2012-07-20 20:51:06 +02:00
|
|
|
|
2016-04-15 20:35:56 +02:00
|
|
|
int16_t Rand9Signed(void) {
|
|
|
|
// Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
|
|
|
|
const uint32_t value = random_.Generate(512);
|
2016-05-27 19:33:56 +02:00
|
|
|
return static_cast<int16_t>(value) - 256;
|
2016-04-15 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:51:06 +02:00
|
|
|
uint8_t Rand8(void) {
|
2013-04-05 04:00:31 +02:00
|
|
|
const uint32_t value =
|
|
|
|
random_.Generate(testing::internal::Random::kMaxRange);
|
|
|
|
// There's a bit more entropy in the upper bits of this implementation.
|
2014-11-27 00:17:49 +01:00
|
|
|
return (value >> 23) & 0xff;
|
2012-07-20 20:51:06 +02:00
|
|
|
}
|
|
|
|
|
2013-04-18 22:05:38 +02:00
|
|
|
uint8_t Rand8Extremes(void) {
|
|
|
|
// Returns a random value near 0 or near 255, to better exercise
|
|
|
|
// saturation behavior.
|
|
|
|
const uint8_t r = Rand8();
|
|
|
|
return r < 128 ? r << 4 : r >> 4;
|
|
|
|
}
|
|
|
|
|
2016-07-26 07:50:48 +02:00
|
|
|
int PseudoUniform(int range) { return random_.Generate(range); }
|
2012-07-20 20:51:06 +02:00
|
|
|
|
2016-07-26 07:50:48 +02:00
|
|
|
int operator()(int n) { return PseudoUniform(n); }
|
2012-07-20 20:51:06 +02:00
|
|
|
|
2016-07-26 07:50:48 +02:00
|
|
|
static int DeterministicSeed(void) { return 0xbaba; }
|
2013-04-05 04:00:31 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
testing::internal::Random random_;
|
2012-07-20 20:51:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libvpx_test
|
|
|
|
|
2013-09-05 17:45:56 +02:00
|
|
|
#endif // TEST_ACM_RANDOM_H_
|