Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2018 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef ABSL_CONTAINER_BTREE_TEST_H_ |
| 16 | #define ABSL_CONTAINER_BTREE_TEST_H_ |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <cassert> |
| 20 | #include <random> |
| 21 | #include <string> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "absl/container/btree_map.h" |
| 26 | #include "absl/container/btree_set.h" |
| 27 | #include "absl/container/flat_hash_set.h" |
| 28 | #include "absl/time/time.h" |
| 29 | |
| 30 | namespace absl { |
| 31 | namespace container_internal { |
| 32 | |
| 33 | // Like remove_const but propagates the removal through std::pair. |
| 34 | template <typename T> |
| 35 | struct remove_pair_const { |
| 36 | using type = typename std::remove_const<T>::type; |
| 37 | }; |
| 38 | template <typename T, typename U> |
| 39 | struct remove_pair_const<std::pair<T, U> > { |
| 40 | using type = std::pair<typename remove_pair_const<T>::type, |
| 41 | typename remove_pair_const<U>::type>; |
| 42 | }; |
| 43 | |
| 44 | // Utility class to provide an accessor for a key given a value. The default |
| 45 | // behavior is to treat the value as a pair and return the first element. |
| 46 | template <typename K, typename V> |
| 47 | struct KeyOfValue { |
| 48 | struct type { |
| 49 | const K& operator()(const V& p) const { return p.first; } |
| 50 | }; |
| 51 | }; |
| 52 | |
| 53 | // Partial specialization of KeyOfValue class for when the key and value are |
| 54 | // the same type such as in set<> and btree_set<>. |
| 55 | template <typename K> |
| 56 | struct KeyOfValue<K, K> { |
| 57 | struct type { |
| 58 | const K& operator()(const K& k) const { return k; } |
| 59 | }; |
| 60 | }; |
| 61 | |
| 62 | inline char* GenerateDigits(char buf[16], unsigned val, unsigned maxval) { |
| 63 | assert(val <= maxval); |
| 64 | constexpr unsigned kBase = 64; // avoid integer division. |
| 65 | unsigned p = 15; |
| 66 | buf[p--] = 0; |
| 67 | while (maxval > 0) { |
| 68 | buf[p--] = ' ' + (val % kBase); |
| 69 | val /= kBase; |
| 70 | maxval /= kBase; |
| 71 | } |
| 72 | return buf + p + 1; |
| 73 | } |
| 74 | |
| 75 | template <typename K> |
| 76 | struct Generator { |
| 77 | int maxval; |
| 78 | explicit Generator(int m) : maxval(m) {} |
| 79 | K operator()(int i) const { |
| 80 | assert(i <= maxval); |
| 81 | return K(i); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | template <> |
| 86 | struct Generator<absl::Time> { |
| 87 | int maxval; |
| 88 | explicit Generator(int m) : maxval(m) {} |
| 89 | absl::Time operator()(int i) const { return absl::FromUnixMillis(i); } |
| 90 | }; |
| 91 | |
| 92 | template <> |
| 93 | struct Generator<std::string> { |
| 94 | int maxval; |
| 95 | explicit Generator(int m) : maxval(m) {} |
| 96 | std::string operator()(int i) const { |
| 97 | char buf[16]; |
| 98 | return GenerateDigits(buf, i, maxval); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | template <typename T, typename U> |
| 103 | struct Generator<std::pair<T, U> > { |
| 104 | Generator<typename remove_pair_const<T>::type> tgen; |
| 105 | Generator<typename remove_pair_const<U>::type> ugen; |
| 106 | |
| 107 | explicit Generator(int m) : tgen(m), ugen(m) {} |
| 108 | std::pair<T, U> operator()(int i) const { |
| 109 | return std::make_pair(tgen(i), ugen(i)); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | // Generate n values for our tests and benchmarks. Value range is [0, maxval]. |
| 114 | inline std::vector<int> GenerateNumbersWithSeed(int n, int maxval, int seed) { |
| 115 | // NOTE: Some tests rely on generated numbers not changing between test runs. |
| 116 | // We use std::minstd_rand0 because it is well-defined, but don't use |
| 117 | // std::uniform_int_distribution because platforms use different algorithms. |
| 118 | std::minstd_rand0 rng(seed); |
| 119 | |
| 120 | std::vector<int> values; |
| 121 | absl::flat_hash_set<int> unique_values; |
| 122 | if (values.size() < n) { |
| 123 | for (int i = values.size(); i < n; i++) { |
| 124 | int value; |
| 125 | do { |
| 126 | value = static_cast<int>(rng()) % (maxval + 1); |
| 127 | } while (!unique_values.insert(value).second); |
| 128 | |
| 129 | values.push_back(value); |
| 130 | } |
| 131 | } |
| 132 | return values; |
| 133 | } |
| 134 | |
| 135 | // Generates n values in the range [0, maxval]. |
| 136 | template <typename V> |
| 137 | std::vector<V> GenerateValuesWithSeed(int n, int maxval, int seed) { |
| 138 | const std::vector<int> nums = GenerateNumbersWithSeed(n, maxval, seed); |
| 139 | Generator<V> gen(maxval); |
| 140 | std::vector<V> vec; |
| 141 | |
| 142 | vec.reserve(n); |
| 143 | for (int i = 0; i < n; i++) { |
| 144 | vec.push_back(gen(nums[i])); |
| 145 | } |
| 146 | |
| 147 | return vec; |
| 148 | } |
| 149 | |
| 150 | } // namespace container_internal |
| 151 | } // namespace absl |
| 152 | |
| 153 | #endif // ABSL_CONTAINER_BTREE_TEST_H_ |