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