| OLD | NEW |
| (Empty) |
| 1 // Copyright 2004-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 | |
| 17 #include <cstring> | |
| 18 #include "omaha/base/tr_rand.h" | |
| 19 #include "omaha/testing/unit_test.h" | |
| 20 | |
| 21 namespace omaha { | |
| 22 | |
| 23 TEST(TRRandTest, TRRand) { | |
| 24 int min_period = +99999; | |
| 25 int max_period = -99999; | |
| 26 | |
| 27 int min_period_at = -99999; | |
| 28 int max_period_at = -99999; | |
| 29 | |
| 30 byte hits[65536] = {0}; | |
| 31 memset(hits, 0, sizeof(hits)); | |
| 32 | |
| 33 // Compute minimum and maximum period by considering all possible seed values. | |
| 34 for (int seed = 0; seed < 65536; ++seed) { | |
| 35 // See if value is part of some known sequence we've traversed. | |
| 36 // If multiple values map to same next-val, this check could cause us to | |
| 37 // report a min_period that's too short. But a long min_period still | |
| 38 // indicates success. | |
| 39 if (hits[seed]) { continue; } | |
| 40 | |
| 41 // Compute length of period starting at this seed. | |
| 42 tr_srand(seed); | |
| 43 int i = seed; | |
| 44 int period = 0; | |
| 45 do { | |
| 46 ++hits[i]; | |
| 47 ++period; | |
| 48 i = tr_rand(); | |
| 49 ASSERT_GE(i, 0); | |
| 50 } while (hits[i] == 0); | |
| 51 | |
| 52 // Update stats. | |
| 53 if (period < min_period) { | |
| 54 min_period = period; | |
| 55 min_period_at = seed; | |
| 56 } | |
| 57 if (period > max_period) { | |
| 58 max_period = period; | |
| 59 max_period_at = seed; | |
| 60 } | |
| 61 } | |
| 62 ASSERT_GE(min_period, (0xFFFF / 2)); | |
| 63 } | |
| 64 | |
| 65 } // namespace omaha | |
| 66 | |
| OLD | NEW |