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 <list> |
21 | |
22 | #include <boost/random/mersenne_twister.hpp> |
23 | #include <boost/random/uniform_int.hpp> |
24 | |
25 | #include <folly/Benchmark.h> |
26 | #include <folly/FBString.h> |
27 | #include <folly/Random.h> |
28 | #include <folly/portability/GTest.h> |
29 | |
30 | namespace folly { |
31 | namespace test { |
32 | namespace detail { |
33 | |
34 | auto static const seed = randomNumberSeed(); |
35 | typedef boost::random::mt19937 RandomT; |
36 | extern RandomT rng; |
37 | |
38 | template <class Integral1, class Integral2> |
39 | Integral2 random(Integral1 low, Integral2 up) { |
40 | boost::uniform_int<> range(low, up); |
41 | return range(rng); |
42 | } |
43 | |
44 | template <class String> |
45 | void randomString(String* toFill, unsigned int maxSize = 1000) { |
46 | assert(toFill); |
47 | toFill->resize(random(0, maxSize)); |
48 | for (auto& c : *toFill) { |
49 | c = random('a', 'z'); |
50 | } |
51 | } |
52 | |
53 | template <class String, class Integral> |
54 | void Num2String(String& str, Integral /* n */) { |
55 | str.resize(10, '\0'); |
56 | sprintf(&str[0], "%ul" , 10); |
57 | str.resize(strlen(str.c_str())); |
58 | } |
59 | |
60 | std::list<char> RandomList(unsigned int maxSize); |
61 | |
62 | template <class T> |
63 | T randomObject(); |
64 | |
65 | template <> |
66 | int randomObject<int>(); |
67 | |
68 | template <> |
69 | std::string randomObject<std::string>(); |
70 | |
71 | template <> |
72 | folly::fbstring randomObject<folly::fbstring>(); |
73 | |
74 | #define CONCAT(A, B) CONCAT_HELPER(A, B) |
75 | #define CONCAT_HELPER(A, B) A##B |
76 | #define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR)) |
77 | #define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR)) |
78 | |
79 | } // namespace detail |
80 | } // namespace test |
81 | } // namespace folly |
82 | |