[CI][Github] Do not fail premerge job
[llvm-project.git] / libcxx / test / benchmarks / GenerateInput.h
blob67ce798071eaa4e367655563ebbb8ae1a9cdc183
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef BENCHMARK_GENERATE_INPUT_H
10 #define BENCHMARK_GENERATE_INPUT_H
12 #include <algorithm>
13 #include <climits>
14 #include <cstddef>
15 #include <random>
16 #include <string>
17 #include <vector>
19 static const char Letters[] = {
20 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
21 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
22 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
23 static const std::size_t LettersSize = sizeof(Letters);
25 inline std::default_random_engine& getRandomEngine() {
26 static std::default_random_engine RandEngine(std::random_device{}());
27 return RandEngine;
30 inline char getRandomChar() {
31 std::uniform_int_distribution<> LettersDist(0, LettersSize - 1);
32 return Letters[LettersDist(getRandomEngine())];
35 template <class IntT>
36 inline IntT getRandomInteger(IntT Min, IntT Max) {
37 std::uniform_int_distribution<unsigned long long> dist(Min, Max);
38 return static_cast<IntT>(dist(getRandomEngine()));
41 inline std::string getRandomString(std::size_t Len) {
42 std::string str(Len, 0);
43 std::generate_n(str.begin(), Len, &getRandomChar);
44 return str;
47 template <class IntT>
48 inline std::vector<IntT> getDuplicateIntegerInputs(std::size_t N) {
49 std::vector<IntT> inputs(N, static_cast<IntT>(-1));
50 return inputs;
53 template <class IntT>
54 inline std::vector<IntT> getSortedIntegerInputs(std::size_t N) {
55 std::vector<IntT> inputs;
56 inputs.reserve(N);
57 for (std::size_t i = 0; i < N; i += 1)
58 inputs.push_back(i);
59 return inputs;
62 template <class IntT>
63 std::vector<IntT> getSortedLargeIntegerInputs(std::size_t N) {
64 std::vector<IntT> inputs;
65 inputs.reserve(N);
66 for (std::size_t i = 0; i < N; ++i)
67 inputs.push_back(i + N);
68 return inputs;
71 template <class IntT>
72 std::vector<IntT> getSortedTopBitsIntegerInputs(std::size_t N) {
73 std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N);
74 for (auto& E : inputs)
75 E <<= ((sizeof(IntT) / 2) * CHAR_BIT);
76 return inputs;
79 template <class IntT>
80 inline std::vector<IntT> getReverseSortedIntegerInputs(std::size_t N) {
81 std::vector<IntT> inputs;
82 inputs.reserve(N);
83 std::size_t i = N;
84 while (i > 0) {
85 --i;
86 inputs.push_back(i);
88 return inputs;
91 template <class IntT>
92 std::vector<IntT> getPipeOrganIntegerInputs(std::size_t N) {
93 std::vector<IntT> v;
94 v.reserve(N);
95 for (std::size_t i = 0; i < N / 2; ++i)
96 v.push_back(i);
97 for (std::size_t i = N / 2; i < N; ++i)
98 v.push_back(N - i);
99 return v;
102 template <class IntT>
103 std::vector<IntT> getRandomIntegerInputs(std::size_t N) {
104 std::vector<IntT> inputs;
105 inputs.reserve(N);
106 for (std::size_t i = 0; i < N; ++i)
107 inputs.push_back(getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()));
108 return inputs;
111 inline std::vector<std::string> getRandomStringInputsWithLength(std::size_t N, std::size_t len) { // N-by-len
112 std::vector<std::string> inputs;
113 inputs.reserve(N);
114 for (std::size_t i = 0; i < N; ++i)
115 inputs.push_back(getRandomString(len));
116 return inputs;
119 inline std::vector<std::string> getDuplicateStringInputs(std::size_t N) {
120 std::vector<std::string> inputs(N, getRandomString(1024));
121 return inputs;
124 inline std::vector<std::string> getRandomStringInputs(std::size_t N) {
125 return getRandomStringInputsWithLength(N, 1024);
128 template <class IntT>
129 std::vector<std::vector<IntT>> getRandomIntegerInputsWithLength(std::size_t N, std::size_t len) { // N-by-len
130 std::vector<std::vector<IntT>> inputs;
131 inputs.reserve(N);
132 for (std::size_t i = 0; i < N; ++i)
133 inputs.push_back(getRandomIntegerInputs<IntT>(len));
134 return inputs;
137 inline std::vector<std::string> getPrefixedRandomStringInputs(std::size_t N) {
138 std::vector<std::string> inputs;
139 inputs.reserve(N);
140 constexpr int kSuffixLength = 32;
141 const std::string prefix = getRandomString(1024 - kSuffixLength);
142 for (std::size_t i = 0; i < N; ++i)
143 inputs.push_back(prefix + getRandomString(kSuffixLength));
144 return inputs;
147 inline std::vector<std::string> getSortedStringInputs(std::size_t N) {
148 std::vector<std::string> inputs = getRandomStringInputs(N);
149 std::sort(inputs.begin(), inputs.end());
150 return inputs;
153 inline std::vector<std::string> getReverseSortedStringInputs(std::size_t N) {
154 std::vector<std::string> inputs = getSortedStringInputs(N);
155 std::reverse(inputs.begin(), inputs.end());
156 return inputs;
159 inline std::vector<const char*> getRandomCStringInputs(std::size_t N) {
160 static std::vector<std::string> inputs = getRandomStringInputs(N);
161 std::vector<const char*> cinputs;
162 for (auto const& str : inputs)
163 cinputs.push_back(str.c_str());
164 return cinputs;
167 #endif // BENCHMARK_GENERATE_INPUT_H