blob: 9f046a1c300084d3cb375a6d73463229dfda5657 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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>
bnc47904002019-08-16 11:49:48 -070012#include <string>
QUICHE teamfd50a402018-12-07 22:54:05 -050013
bnc74646d12019-12-13 09:21:19 -080014#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050015
16namespace http2 {
17namespace 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.
22class 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.
bnc74646d12019-12-13 09:21:19 -080031 explicit Http2Random(quiche::QuicheStringPiece key);
bnc47904002019-08-16 11:49:48 -070032 std::string Key() const;
QUICHE teamfd50a402018-12-07 22:54:05 -050033
34 void FillRandom(void* buffer, size_t buffer_size);
bnc47904002019-08-16 11:49:48 -070035 std::string RandString(int length);
QUICHE teamfd50a402018-12-07 22:54:05 -050036
37 // Returns a random 64-bit value.
38 uint64_t Rand64();
39
40 // Return a uniformly distrubted random number in [0, n).
41 uint32_t Uniform(uint32_t n) { return Rand64() % n; }
42 // Return a uniformly distrubted random number in [lo, 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.
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.
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();
59 float RandFloat() { return RandDouble(); }
60
61 // Has 1/n chance of returning true.
62 bool OneIn(int n) { return Uniform(n) == 0; }
63
64 uint8_t Rand8() { return Rand64(); }
65 uint16_t Rand16() { return Rand64(); }
66 uint32_t Rand32() { return Rand64(); }
67
68 // Return a random string consisting of the characters from the specified
69 // alphabet.
bnc74646d12019-12-13 09:21:19 -080070 std::string RandStringWithAlphabet(int length,
71 quiche::QuicheStringPiece alphabet);
QUICHE teamfd50a402018-12-07 22:54:05 -050072
73 // STL UniformRandomNumberGenerator implementation.
74 using result_type = uint64_t;
75 static constexpr result_type min() { return 0; }
76 static constexpr result_type max() {
77 return std::numeric_limits<result_type>::max();
78 }
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_