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
> getDuplicateStringInputs(std::size_t N
) {
112 std::vector
<std::string
> inputs(N
, getRandomString(1024));
116 inline std::vector
<std::string
> getRandomStringInputs(std::size_t N
) {
117 std::vector
<std::string
> inputs
;
119 for (std::size_t i
= 0; i
< N
; ++i
)
120 inputs
.push_back(getRandomString(1024));
124 inline std::vector
<std::string
> getPrefixedRandomStringInputs(std::size_t N
) {
125 std::vector
<std::string
> inputs
;
127 constexpr int kSuffixLength
= 32;
128 const std::string prefix
= getRandomString(1024 - kSuffixLength
);
129 for (std::size_t i
= 0; i
< N
; ++i
)
130 inputs
.push_back(prefix
+ getRandomString(kSuffixLength
));
134 inline std::vector
<std::string
> getSortedStringInputs(std::size_t N
) {
135 std::vector
<std::string
> inputs
= getRandomStringInputs(N
);
136 std::sort(inputs
.begin(), inputs
.end());
140 inline std::vector
<std::string
> getReverseSortedStringInputs(std::size_t N
) {
141 std::vector
<std::string
> inputs
= getSortedStringInputs(N
);
142 std::reverse(inputs
.begin(), inputs
.end());
146 inline std::vector
<const char*> getRandomCStringInputs(std::size_t N
) {
147 static std::vector
<std::string
> inputs
= getRandomStringInputs(N
);
148 std::vector
<const char*> cinputs
;
149 for (auto const& str
: inputs
)
150 cinputs
.push_back(str
.c_str());
154 #endif // BENCHMARK_GENERATE_INPUT_H