OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BLINK_TEST_RANDOM_H_ | 5 #ifndef MEDIA_BLINK_TEST_RANDOM_H_ |
6 #define MEDIA_BLINK_TEST_RANDOM_H_ | 6 #define MEDIA_BLINK_TEST_RANDOM_H_ |
7 | 7 |
8 // Vastly simplified ACM random class meant to only be used for testing. | 8 // Vastly simplified ACM random class meant to only be used for testing. |
9 // This class is meant to generate predictable sequences of pseudorandom | 9 // This class is meant to generate predictable sequences of pseudorandom |
10 // numbers, unlike the classes in base/rand_util.h which are meant to generate | 10 // numbers, unlike the classes in base/rand_util.h which are meant to generate |
11 // unpredictable sequences. | 11 // unpredictable sequences. |
12 // See | 12 // See |
13 // https://code.google.com/p/szl/source/browse/trunk/src/utilities/acmrandom.h | 13 // https://code.google.com/p/szl/source/browse/trunk/src/utilities/acmrandom.h |
14 // for more information. | 14 // for more information. |
15 | 15 |
16 namespace media { | 16 namespace media { |
17 | 17 |
18 class TestRandom { | 18 class TestRandom { |
19 public: | 19 public: |
20 explicit TestRandom(uint32_t seed) { | 20 explicit TestRandom(uint32_t seed) { |
21 seed_ = seed & 0x7fffffff; // make this a non-negative number | 21 seed_ = seed & 0x7fffffff; // make this a non-negative number |
22 if (seed_ == 0 || seed_ == M) { | 22 if (seed_ == 0 || seed_ == M) { |
23 seed_ = 1; | 23 seed_ = 1; |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 int32_t Rand() { | 27 int32_t Rand() { |
28 static const uint64 A = 16807; // bits 14, 8, 7, 5, 2, 1, 0 | 28 static const uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0 |
29 seed_ = static_cast<int32_t>((seed_ * A) % M); | 29 seed_ = static_cast<int32_t>((seed_ * A) % M); |
30 CHECK_GT(seed_, 0); | 30 CHECK_GT(seed_, 0); |
31 return seed_; | 31 return seed_; |
32 } | 32 } |
33 | 33 |
34 private: | 34 private: |
35 static const uint64 M = 2147483647L; // 2^32-1 | 35 static const uint64_t M = 2147483647L; // 2^32-1 |
36 int32_t seed_; | 36 int32_t seed_; |
37 }; | 37 }; |
38 | 38 |
39 } // namespace media | 39 } // namespace media |
40 | 40 |
41 #endif // MEDIA_BLINK_TEST_RANDOM_H_ | 41 #endif // MEDIA_BLINK_TEST_RANDOM_H_ |
OLD | NEW |