1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef BENCHMARK_GENERATE_INPUT_H
10 #define BENCHMARK_GENERATE_INPUT_H
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
{}());
30 inline char getRandomChar() {
31 std::uniform_int_distribution
<> LettersDist(0, LettersSize
- 1);
32 return Letters
[LettersDist(getRandomEngine())];
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
);
48 inline std::vector
<IntT
> getDuplicateIntegerInputs(std::size_t N
) {
49 std::vector
<IntT
> inputs(N
, static_cast<IntT
>(-1));
54 inline std::vector
<IntT
> getSortedIntegerInputs(std::size_t N
) {
55 std::vector
<IntT
> inputs
;
57 for (std::size_t i
= 0; i
< N
; i
+= 1)
63 std::vector
<IntT
> getSortedLargeIntegerInputs(std::size_t N
) {
64 std::vector
<IntT
> inputs
;
66 for (std::size_t i
= 0; i
< N
; ++i
)
67 inputs
.push_back(i
+ N
);
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
);
80 inline std::vector
<IntT
> getReverseSortedIntegerInputs(std::size_t N
) {
81 std::vector
<IntT
> inputs
;
92 std::vector
<IntT
> getPipeOrganIntegerInputs(std::size_t N
) {
95 for (std::size_t i
= 0; i
< N
/ 2; ++i
)
97 for (std::size_t i
= N
/ 2; i
< N
; ++i
)
102 template <class IntT
>
103 std::vector
<IntT
> getRandomIntegerInputs(std::size_t N
) {
104 std::vector
<IntT
> inputs
;
106 for (std::size_t i
= 0; i
< N
; ++i
)
107 inputs
.push_back(getRandomInteger
<IntT
>(0, std::numeric_limits
<IntT
>::max()));
111 inline std::vector
<std::string
> getRandomStringInputsWithLength(std::size_t N
, std::size_t len
) { // N-by-len
112 std::vector
<std::string
> inputs
;
114 for (std::size_t i
= 0; i
< N
; ++i
)
115 inputs
.push_back(getRandomString(len
));
119 inline std::vector
<std::string
> getDuplicateStringInputs(std::size_t N
) {
120 std::vector
<std::string
> inputs(N
, getRandomString(1024));
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
;
132 for (std::size_t i
= 0; i
< N
; ++i
)
133 inputs
.push_back(getRandomIntegerInputs
<IntT
>(len
));
137 inline std::vector
<std::string
> getPrefixedRandomStringInputs(std::size_t N
) {
138 std::vector
<std::string
> inputs
;
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
));
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());
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());
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());
167 #endif // BENCHMARK_GENERATE_INPUT_H