1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef QUICHE_HTTP2_TEST_TOOLS_HTTP2_RANDOM_H_ 6 #define QUICHE_HTTP2_TEST_TOOLS_HTTP2_RANDOM_H_ 7 8 #include <cmath> 9 #include <cstdint> 10 #include <limits> 11 #include <random> 12 #include <string> 13 14 #include "absl/strings/string_view.h" 15 16 namespace http2 { 17 namespace test { 18 19 // The random number generator used for unit tests. Since the algorithm is 20 // deterministic and fixed, this can be used to reproduce flakes in the unit 21 // tests caused by specific random values. 22 class Http2Random { 23 public: 24 Http2Random(); 25 26 Http2Random(const Http2Random&) = delete; 27 Http2Random& operator=(const Http2Random&) = delete; 28 29 // Reproducible random number generation: by using the same key, the same 30 // sequence of results is obtained. 31 explicit Http2Random(absl::string_view key); 32 std::string Key() const; 33 34 void FillRandom(void* buffer, size_t buffer_size); 35 std::string RandString(int length); 36 37 // Returns a random 64-bit value. 38 uint64_t Rand64(); 39 40 // Return a uniformly distrubted random number in [0, n). Uniform(uint32_t n)41 uint32_t Uniform(uint32_t n) { return Rand64() % n; } 42 // Return a uniformly distrubted random number in [lo, hi). UniformInRange(uint64_t lo,uint64_t hi)43 uint64_t UniformInRange(uint64_t lo, uint64_t hi) { 44 return lo + Rand64() % (hi - lo); 45 } 46 // Return an integer of logarithmically random scale. Skewed(uint32_t max_log)47 uint32_t Skewed(uint32_t max_log) { 48 const uint32_t base = Rand32() % (max_log + 1); 49 const uint32_t mask = ((base < 32) ? (1u << base) : 0u) - 1u; 50 return Rand32() & mask; 51 } 52 // Return a random number in [0, max] range that skews low. RandomSizeSkewedLow(uint64_t max)53 uint64_t RandomSizeSkewedLow(uint64_t max) { 54 return std::round(max * std::pow(RandDouble(), 2)); 55 } 56 57 // Returns a random double between 0 and 1. 58 double RandDouble(); RandFloat()59 float RandFloat() { return RandDouble(); } 60 61 // Has 1/n chance of returning true. OneIn(int n)62 bool OneIn(int n) { return Uniform(n) == 0; } 63 Rand8()64 uint8_t Rand8() { return Rand64(); } Rand16()65 uint16_t Rand16() { return Rand64(); } Rand32()66 uint32_t Rand32() { return Rand64(); } 67 68 // Return a random string consisting of the characters from the specified 69 // alphabet. 70 std::string RandStringWithAlphabet(int length, absl::string_view alphabet); 71 72 // STL UniformRandomNumberGenerator implementation. 73 using result_type = uint64_t; min()74 static constexpr result_type min() { return 0; } max()75 static constexpr result_type max() { 76 return std::numeric_limits<result_type>::max(); 77 } operator()78 result_type operator()() { return Rand64(); } 79 80 private: 81 uint8_t key_[32]; 82 uint32_t counter_ = 0; 83 }; 84 85 } // namespace test 86 } // namespace http2 87 88 #endif // QUICHE_HTTP2_TEST_TOOLS_HTTP2_RANDOM_H_ 89