proxygen
FBVectorTestUtil.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //
18 // Author: andrei.alexandrescu@fb.com
19 
20 #include <boost/random/mersenne_twister.hpp>
21 
22 #include <folly/Benchmark.h>
23 #include <folly/FBString.h>
24 #include <folly/Random.h>
26 
27 namespace folly {
28 namespace test {
29 namespace detail {
30 
31 auto static const seed = randomNumberSeed();
32 typedef boost::random::mt19937 RandomT;
33 static RandomT rng(seed);
34 
35 template <class Integral1, class Integral2>
36 Integral2 random(Integral1 low, Integral2 up) {
37  boost::uniform_int<> range(low, up);
38  return range(rng);
39 }
40 
41 template <class String>
42 void randomString(String* toFill, unsigned int maxSize = 1000) {
43  assert(toFill);
44  toFill->resize(random(0, maxSize));
45  for (auto& c : *toFill) {
46  c = random('a', 'z');
47  }
48 }
49 
50 template <class String, class Integral>
51 void Num2String(String& str, Integral /* n */) {
52  str.resize(10, '\0');
53  sprintf(&str[0], "%ul", 10);
54  str.resize(strlen(str.c_str()));
55 }
56 
57 std::list<char> RandomList(unsigned int maxSize) {
58  std::list<char> lst(random(0u, maxSize));
59  std::list<char>::iterator i = lst.begin();
60  for (; i != lst.end(); ++i) {
61  *i = random('a', 'z');
62  }
63  return lst;
64 }
65 
66 template <class T>
67 T randomObject();
68 
69 template <>
71  return random(0, 1024);
72 }
73 
74 template <>
75 std::string randomObject<std::string>() {
76  std::string result;
77  randomString(&result);
78  return result;
79 }
80 
81 template <>
82 folly::fbstring randomObject<folly::fbstring>() {
83  folly::fbstring result;
84  randomString(&result);
85  return result;
86 }
87 
88 #define CONCAT(A, B) CONCAT_HELPER(A, B)
89 #define CONCAT_HELPER(A, B) A##B
90 #define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR))
91 #define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR))
92 
93 } // namespace detail
94 } // namespace test
95 } // namespace folly
boost::random::mt19937 RandomT
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
Integral2 random(Integral1 low, Integral2 up)
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
void Num2String(String &str, Integral)
static auto const seed
const char * string
Definition: Conv.cpp:212
char c
uint32_t randomNumberSeed()
Definition: Random.h:367
std::list< char > RandomList(unsigned int maxSize)
static RandomT rng(seed)
void randomString(String *toFill, unsigned int maxSize=1000)