1#ifndef BENCHMARK_GENERATE_INPUT_H
2#define BENCHMARK_GENERATE_INPUT_H
3
4#include <algorithm>
5#include <climits>
6#include <cstddef>
7#include <random>
8#include <string>
9#include <vector>
10
11static const char Letters[] = {
12 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
13 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
14 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
15static const std::size_t LettersSize = sizeof(Letters);
16
17inline std::default_random_engine& getRandomEngine() {
18 static std::default_random_engine RandEngine(std::random_device{}());
19 return RandEngine;
20}
21
22inline char getRandomChar() {
23 std::uniform_int_distribution<> LettersDist(0, LettersSize - 1);
24 return Letters[LettersDist(getRandomEngine())];
25}
26
27template <class IntT>
28inline IntT getRandomInteger(IntT Min, IntT Max) {
29 std::uniform_int_distribution<unsigned long long> dist(Min, Max);
30 return static_cast<IntT>(dist(getRandomEngine()));
31}
32
33inline std::string getRandomString(std::size_t Len) {
34 std::string str(Len, 0);
35 std::generate_n(first: str.begin(), n: Len, gen: &getRandomChar);
36 return str;
37}
38
39template <class IntT>
40inline std::vector<IntT> getDuplicateIntegerInputs(size_t N) {
41 std::vector<IntT> inputs(N, static_cast<IntT>(-1));
42 return inputs;
43}
44
45template <class IntT>
46inline std::vector<IntT> getSortedIntegerInputs(size_t N) {
47 std::vector<IntT> inputs;
48 for (size_t i = 0; i < N; i += 1)
49 inputs.push_back(i);
50 return inputs;
51}
52
53template <class IntT>
54std::vector<IntT> getSortedLargeIntegerInputs(size_t N) {
55 std::vector<IntT> inputs;
56 for (size_t i = 0; i < N; ++i) {
57 inputs.push_back(i + N);
58 }
59 return inputs;
60}
61
62template <class IntT>
63std::vector<IntT> getSortedTopBitsIntegerInputs(size_t N) {
64 std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N);
65 for (auto& E : inputs)
66 E <<= ((sizeof(IntT) / 2) * CHAR_BIT);
67 return inputs;
68}
69
70template <class IntT>
71inline std::vector<IntT> getReverseSortedIntegerInputs(size_t N) {
72 std::vector<IntT> inputs;
73 std::size_t i = N;
74 while (i > 0) {
75 --i;
76 inputs.push_back(i);
77 }
78 return inputs;
79}
80
81template <class IntT>
82std::vector<IntT> getPipeOrganIntegerInputs(size_t N) {
83 std::vector<IntT> v;
84 v.reserve(N);
85 for (size_t i = 0; i < N / 2; ++i)
86 v.push_back(i);
87 for (size_t i = N / 2; i < N; ++i)
88 v.push_back(N - i);
89 return v;
90}
91
92template <class IntT>
93std::vector<IntT> getRandomIntegerInputs(size_t N) {
94 std::vector<IntT> inputs;
95 for (size_t i = 0; i < N; ++i) {
96 inputs.push_back(getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()));
97 }
98 return inputs;
99}
100
101inline std::vector<std::string> getDuplicateStringInputs(size_t N) {
102 std::vector<std::string> inputs(N, getRandomString(Len: 1024));
103 return inputs;
104}
105
106inline std::vector<std::string> getRandomStringInputs(size_t N) {
107 std::vector<std::string> inputs;
108 for (size_t i = 0; i < N; ++i) {
109 inputs.push_back(x: getRandomString(Len: 1024));
110 }
111 return inputs;
112}
113
114inline std::vector<std::string> getPrefixedRandomStringInputs(size_t N) {
115 std::vector<std::string> inputs;
116 constexpr int kSuffixLength = 32;
117 const std::string prefix = getRandomString(Len: 1024 - kSuffixLength);
118 for (size_t i = 0; i < N; ++i) {
119 inputs.push_back(x: prefix + getRandomString(Len: kSuffixLength));
120 }
121 return inputs;
122}
123
124inline std::vector<std::string> getSortedStringInputs(size_t N) {
125 std::vector<std::string> inputs = getRandomStringInputs(N);
126 std::sort(first: inputs.begin(), last: inputs.end());
127 return inputs;
128}
129
130inline std::vector<std::string> getReverseSortedStringInputs(size_t N) {
131 std::vector<std::string> inputs = getSortedStringInputs(N);
132 std::reverse(first: inputs.begin(), last: inputs.end());
133 return inputs;
134}
135
136inline std::vector<const char*> getRandomCStringInputs(size_t N) {
137 static std::vector<std::string> inputs = getRandomStringInputs(N);
138 std::vector<const char*> cinputs;
139 for (auto const& str : inputs)
140 cinputs.push_back(x: str.c_str());
141 return cinputs;
142}
143
144#endif // BENCHMARK_GENERATE_INPUT_H
145

source code of libcxx/benchmarks/GenerateInput.h