robin-map/tests/utils.h
Go to the documentation of this file.
1 
24 #ifndef TSL_UTILS_H
25 #define TSL_UTILS_H
26 
27 #include <tsl/robin_hash.h>
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <functional>
32 #include <memory>
33 #include <ostream>
34 #include <string>
35 #include <utility>
36 
37 #ifdef TSL_RH_NO_EXCEPTIONS
38 #define TSL_RH_CHECK_THROW(S, E)
39 #define TSL_RH_CHECK_THROW_EITHER(S, E1, E2)
40 #else
41 #define TSL_RH_CHECK_THROW(S, E) BOOST_CHECK_THROW(S, E)
42 #define TSL_RH_CHECK_THROW_EITHER(S, E1, E2) \
43  do { \
44  try { \
45  S; \
46  BOOST_CHECK(false); \
47  } catch (const E1&) { \
48  } catch (const E2&) { \
49  } \
50  } while (0)
51 #endif
52 
53 template <typename T>
55  public:
56  std::size_t operator()(const T& value) const {
57  return static_cast<std::size_t>(value);
58  }
59 };
60 
61 template <unsigned int MOD>
62 class mod_hash {
63  public:
64  template <typename T>
65  std::size_t operator()(const T& value) const {
66  return std::hash<T>()(value) % MOD;
67  }
68 };
69 
71  public:
73  : m_value(std::to_string(-1)), m_value_ptr(&m_value) {}
74 
75  explicit self_reference_member_test(std::int64_t value)
76  : m_value(std::to_string(value)), m_value_ptr(&m_value) {}
77 
79  : m_value(*other.m_value_ptr), m_value_ptr(&m_value) {}
80 
82  : m_value(*other.m_value_ptr), m_value_ptr(&m_value) {}
83 
85  const self_reference_member_test& other) {
86  m_value = *other.m_value_ptr;
88 
89  return *this;
90  }
91 
93  m_value = *other.m_value_ptr;
95 
96  return *this;
97  }
98 
99  std::string value() const { return *m_value_ptr; }
100 
101  friend std::ostream& operator<<(std::ostream& stream,
103  stream << *value.m_value_ptr;
104 
105  return stream;
106  }
107 
108  friend bool operator==(const self_reference_member_test& lhs,
109  const self_reference_member_test& rhs) {
110  return *lhs.m_value_ptr == *rhs.m_value_ptr;
111  }
112 
113  friend bool operator!=(const self_reference_member_test& lhs,
114  const self_reference_member_test& rhs) {
115  return !(lhs == rhs);
116  }
117 
118  friend bool operator<(const self_reference_member_test& lhs,
119  const self_reference_member_test& rhs) {
120  return *lhs.m_value_ptr < *rhs.m_value_ptr;
121  }
122 
123  private:
126 };
127 
129  public:
130  explicit move_only_test(std::int64_t value)
131  : m_value(new std::string(std::to_string(value))) {}
132 
134  : m_value(new std::string(std::move(value))) {}
135 
136  move_only_test(const move_only_test&) = delete;
137  move_only_test(move_only_test&&) = default;
138  move_only_test& operator=(const move_only_test&) = delete;
140 
141  friend std::ostream& operator<<(std::ostream& stream,
142  const move_only_test& value) {
143  if (value.m_value == nullptr) {
144  stream << "null";
145  } else {
146  stream << *value.m_value;
147  }
148 
149  return stream;
150  }
151 
152  friend bool operator==(const move_only_test& lhs, const move_only_test& rhs) {
153  if (lhs.m_value == nullptr || rhs.m_value == nullptr) {
154  return lhs.m_value == nullptr && rhs.m_value == nullptr;
155  } else {
156  return *lhs.m_value == *rhs.m_value;
157  }
158  }
159 
160  friend bool operator!=(const move_only_test& lhs, const move_only_test& rhs) {
161  return !(lhs == rhs);
162  }
163 
164  friend bool operator<(const move_only_test& lhs, const move_only_test& rhs) {
165  if (lhs.m_value == nullptr && rhs.m_value == nullptr) {
166  return false;
167  } else if (lhs.m_value == nullptr) {
168  return true;
169  } else if (rhs.m_value == nullptr) {
170  return false;
171  } else {
172  return *lhs.m_value < *rhs.m_value;
173  }
174  }
175 
176  const std::string& value() const { return *m_value; }
177 
178  private:
179  std::unique_ptr<std::string> m_value;
180 };
181 
183  public:
184  explicit copy_only_test(std::int64_t value)
185  : m_value(std::to_string(value)) {}
186 
187  copy_only_test(const copy_only_test& other) : m_value(other.m_value) {}
188 
190  m_value = other.m_value;
191 
192  return *this;
193  }
194 
196 
197  friend std::ostream& operator<<(std::ostream& stream,
198  const copy_only_test& value) {
199  stream << value.m_value;
200 
201  return stream;
202  }
203 
204  friend bool operator==(const copy_only_test& lhs, const copy_only_test& rhs) {
205  return lhs.m_value == rhs.m_value;
206  }
207 
208  friend bool operator!=(const copy_only_test& lhs, const copy_only_test& rhs) {
209  return !(lhs == rhs);
210  }
211 
212  friend bool operator<(const copy_only_test& lhs, const copy_only_test& rhs) {
213  return lhs.m_value < rhs.m_value;
214  }
215 
216  std::string value() const { return m_value; }
217 
218  private:
220 };
221 
222 namespace std {
223 template <>
225  std::size_t operator()(const self_reference_member_test& val) const {
226  return std::hash<std::string>()(val.value());
227  }
228 };
229 
230 template <>
231 struct hash<move_only_test> {
232  std::size_t operator()(const move_only_test& val) const {
233  return std::hash<std::string>()(val.value());
234  }
235 };
236 
237 template <>
238 struct hash<copy_only_test> {
239  std::size_t operator()(const copy_only_test& val) const {
240  return std::hash<std::string>()(val.value());
241  }
242 };
243 } // namespace std
244 
245 class utils {
246  public:
247  template <typename T>
248  static T get_key(std::size_t counter);
249 
250  template <typename T>
251  static T get_value(std::size_t counter);
252 
253  template <typename HMap>
254  static HMap get_filled_hash_map(std::size_t nb_elements);
255 };
256 
257 template <>
258 inline std::int32_t utils::get_key<std::int32_t>(std::size_t counter) {
259  return tsl::detail_robin_hash::numeric_cast<std::int32_t>(counter);
260 }
261 
262 template <>
263 inline std::int64_t utils::get_key<std::int64_t>(std::size_t counter) {
264  return tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter);
265 }
266 
267 template <>
268 inline self_reference_member_test utils::get_key<self_reference_member_test>(
269  std::size_t counter) {
271  tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter));
272 }
273 
274 template <>
275 inline std::string utils::get_key<std::string>(std::size_t counter) {
276  return "Key " + std::to_string(counter);
277 }
278 
279 template <>
280 inline move_only_test utils::get_key<move_only_test>(std::size_t counter) {
281  return move_only_test(
282  tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter));
283 }
284 
285 template <>
286 inline copy_only_test utils::get_key<copy_only_test>(std::size_t counter) {
287  return copy_only_test(
288  tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter));
289 }
290 
291 template <>
292 inline std::int32_t utils::get_value<std::int32_t>(std::size_t counter) {
293  return tsl::detail_robin_hash::numeric_cast<std::int32_t>(counter * 2);
294 }
295 
296 template <>
297 inline std::int64_t utils::get_value<std::int64_t>(std::size_t counter) {
298  return tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter * 2);
299 }
300 
301 template <>
302 inline self_reference_member_test utils::get_value<self_reference_member_test>(
303  std::size_t counter) {
305  tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter * 2));
306 }
307 
308 template <>
309 inline std::string utils::get_value<std::string>(std::size_t counter) {
310  return "Value " + std::to_string(counter);
311 }
312 
313 template <>
314 inline move_only_test utils::get_value<move_only_test>(std::size_t counter) {
315  return move_only_test(
316  tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter * 2));
317 }
318 
319 template <>
320 inline copy_only_test utils::get_value<copy_only_test>(std::size_t counter) {
321  return copy_only_test(
322  tsl::detail_robin_hash::numeric_cast<std::int64_t>(counter * 2));
323 }
324 
325 template <typename HMap>
326 inline HMap utils::get_filled_hash_map(std::size_t nb_elements) {
327  using key_t = typename HMap::key_type;
328  using value_t = typename HMap::mapped_type;
329 
330  HMap map;
331  map.reserve(nb_elements);
332 
333  for (std::size_t i = 0; i < nb_elements; i++) {
334  map.insert({utils::get_key<key_t>(i), utils::get_value<value_t>(i)});
335  }
336 
337  return map;
338 }
339 
340 template <class T>
342 
343 template <class T1, class T2>
344 struct is_pair<std::pair<T1, T2>> : std::true_type {};
345 
349 class serializer {
350  public:
351  serializer() { m_ostream.exceptions(m_ostream.badbit | m_ostream.failbit); }
352 
353  template <class T>
354  void operator()(const T& val) {
355  serialize_impl(val);
356  }
357 
358  std::string str() const { return m_ostream.str(); }
359 
360  private:
361  template <typename T, typename U>
362  void serialize_impl(const std::pair<T, U>& val) {
363  serialize_impl(val.first);
364  serialize_impl(val.second);
365  }
366 
367  void serialize_impl(const std::string& val) {
369  tsl::detail_robin_hash::numeric_cast<std::uint64_t>(val.size()));
370  m_ostream.write(val.data(), val.size());
371  }
372 
373  void serialize_impl(const move_only_test& val) {
374  serialize_impl(val.value());
375  }
376 
377  template <class T, typename std::enable_if<
378  std::is_arithmetic<T>::value>::type* = nullptr>
379  void serialize_impl(const T& val) {
380  m_ostream.write(reinterpret_cast<const char*>(&val), sizeof(val));
381  }
382 
383  private:
384  std::stringstream m_ostream;
385 };
386 
388  public:
389  explicit deserializer(const std::string& init_str = "")
390  : m_istream(init_str) {
391  m_istream.exceptions(m_istream.badbit | m_istream.failbit |
392  m_istream.eofbit);
393  }
394 
395  template <class T>
397  return deserialize_impl<T>();
398  }
399 
400  private:
401  template <class T,
402  typename std::enable_if<is_pair<T>::value>::type* = nullptr>
404  auto first = deserialize_impl<typename T::first_type>();
405  return std::make_pair(std::move(first),
406  deserialize_impl<typename T::second_type>());
407  }
408 
409  template <class T, typename std::enable_if<
410  std::is_same<std::string, T>::value>::type* = nullptr>
412  const std::size_t str_size =
413  tsl::detail_robin_hash::numeric_cast<std::size_t>(
414  deserialize_impl<std::uint64_t>());
415 
416  // TODO std::string::data() return a const pointer pre-C++17. Avoid the
417  // inefficient double allocation.
418  std::vector<char> chars(str_size);
419  m_istream.read(chars.data(), str_size);
420 
421  return std::string(chars.data(), chars.size());
422  }
423 
424  template <class T, typename std::enable_if<std::is_same<
425  move_only_test, T>::value>::type* = nullptr>
427  return move_only_test(deserialize_impl<std::string>());
428  }
429 
430  template <class T, typename std::enable_if<
431  std::is_arithmetic<T>::value>::type* = nullptr>
433  T val;
434  m_istream.read(reinterpret_cast<char*>(&val), sizeof(val));
435 
436  return val;
437  }
438 
439  private:
440  std::stringstream m_istream;
441 };
442 
443 #endif
std::hash< move_only_test >::operator()
std::size_t operator()(const move_only_test &val) const
Definition: robin-map/tests/utils.h:232
self_reference_member_test::operator=
self_reference_member_test & operator=(self_reference_member_test &&other)
Definition: robin-map/tests/utils.h:92
copy_only_test::copy_only_test
copy_only_test(const copy_only_test &other)
Definition: robin-map/tests/utils.h:187
self_reference_member_test::m_value_ptr
std::string * m_value_ptr
Definition: robin-map/tests/utils.h:125
move_only_test::operator!=
friend bool operator!=(const move_only_test &lhs, const move_only_test &rhs)
Definition: robin-map/tests/utils.h:160
deserializer::deserializer
deserializer(const std::string &init_str="")
Definition: robin-map/tests/utils.h:389
serializer::serialize_impl
void serialize_impl(const T &val)
Definition: robin-map/tests/utils.h:379
self_reference_member_test::operator=
self_reference_member_test & operator=(const self_reference_member_test &other)
Definition: robin-map/tests/utils.h:84
copy_only_test::value
std::string value() const
Definition: robin-map/tests/utils.h:216
utils::get_key
static T get_key(std::size_t counter)
self_reference_member_test::self_reference_member_test
self_reference_member_test(self_reference_member_test &&other)
Definition: robin-map/tests/utils.h:81
move_only_test::move_only_test
move_only_test(std::int64_t value)
Definition: robin-map/tests/utils.h:130
self_reference_member_test
Definition: robin-map/tests/utils.h:70
testing::internal::false_type
bool_constant< false > false_type
Definition: gtest.h:2724
testing::internal::true_type
bool_constant< true > true_type
Definition: gtest.h:2725
utils
Definition: robin-map/tests/utils.h:245
serializer
Definition: robin-map/tests/utils.h:349
self_reference_member_test::self_reference_member_test
self_reference_member_test(std::int64_t value)
Definition: robin-map/tests/utils.h:75
self_reference_member_test::operator<<
friend std::ostream & operator<<(std::ostream &stream, const self_reference_member_test &value)
Definition: robin-map/tests/utils.h:101
move_only_test::value
const std::string & value() const
Definition: robin-map/tests/utils.h:176
deserializer::deserialize_impl
move_only_test deserialize_impl()
Definition: robin-map/tests/utils.h:426
copy_only_test::operator<
friend bool operator<(const copy_only_test &lhs, const copy_only_test &rhs)
Definition: robin-map/tests/utils.h:212
serializer::m_ostream
std::stringstream m_ostream
Definition: robin-map/tests/utils.h:384
std::hash< self_reference_member_test >::operator()
std::size_t operator()(const self_reference_member_test &val) const
Definition: robin-map/tests/utils.h:225
copy_only_test::copy_only_test
copy_only_test(std::int64_t value)
Definition: robin-map/tests/utils.h:184
identity_hash
Definition: robin-map/tests/utils.h:54
testing::internal::string
::std::string string
Definition: gtest.h:1979
move_only_test
Definition: robin-map/tests/utils.h:128
copy_only_test::operator=
copy_only_test & operator=(const copy_only_test &other)
Definition: robin-map/tests/utils.h:189
deserializer::m_istream
std::stringstream m_istream
Definition: robin-map/tests/utils.h:440
self_reference_member_test::operator!=
friend bool operator!=(const self_reference_member_test &lhs, const self_reference_member_test &rhs)
Definition: robin-map/tests/utils.h:113
self_reference_member_test::operator<
friend bool operator<(const self_reference_member_test &lhs, const self_reference_member_test &rhs)
Definition: robin-map/tests/utils.h:118
robin_hash.h
utils::get_filled_hash_map
static HMap get_filled_hash_map(std::size_t nb_elements)
Definition: robin-map/tests/utils.h:326
self_reference_member_test::self_reference_member_test
self_reference_member_test(const self_reference_member_test &other)
Definition: robin-map/tests/utils.h:78
is_pair
Definition: robin-map/tests/utils.h:341
mod_hash::operator()
std::size_t operator()(const T &value) const
Definition: robin-map/tests/utils.h:65
serializer::serialize_impl
void serialize_impl(const std::pair< T, U > &val)
Definition: robin-map/tests/utils.h:362
self_reference_member_test::operator==
friend bool operator==(const self_reference_member_test &lhs, const self_reference_member_test &rhs)
Definition: robin-map/tests/utils.h:108
identity_hash::operator()
std::size_t operator()(const T &value) const
Definition: robin-map/tests/utils.h:56
move_only_test::operator==
friend bool operator==(const move_only_test &lhs, const move_only_test &rhs)
Definition: robin-map/tests/utils.h:152
self_reference_member_test::m_value
std::string m_value
Definition: robin-map/tests/utils.h:124
deserializer::deserialize_impl
T deserialize_impl()
Definition: robin-map/tests/utils.h:403
move_only_test::operator<<
friend std::ostream & operator<<(std::ostream &stream, const move_only_test &value)
Definition: robin-map/tests/utils.h:141
serializer::serialize_impl
void serialize_impl(const std::string &val)
Definition: robin-map/tests/utils.h:367
move_only_test::m_value
std::unique_ptr< std::string > m_value
Definition: robin-map/tests/utils.h:179
serializer::operator()
void operator()(const T &val)
Definition: robin-map/tests/utils.h:354
std
self_reference_member_test::value
std::string value() const
Definition: robin-map/tests/utils.h:99
copy_only_test
Definition: robin-map/tests/utils.h:182
copy_only_test::operator!=
friend bool operator!=(const copy_only_test &lhs, const copy_only_test &rhs)
Definition: robin-map/tests/utils.h:208
mod_hash
Definition: robin-map/tests/utils.h:62
serializer::serializer
serializer()
Definition: robin-map/tests/utils.h:351
move_only_test::move_only_test
move_only_test(std::string value)
Definition: robin-map/tests/utils.h:133
serializer::serialize_impl
void serialize_impl(const move_only_test &val)
Definition: robin-map/tests/utils.h:373
move_only_test::operator=
move_only_test & operator=(const move_only_test &)=delete
deserializer::operator()
T operator()()
Definition: robin-map/tests/utils.h:396
self_reference_member_test::self_reference_member_test
self_reference_member_test()
Definition: robin-map/tests/utils.h:72
serializer::str
std::string str() const
Definition: robin-map/tests/utils.h:358
deserializer
Definition: robin-map/tests/utils.h:387
utils::get_value
static T get_value(std::size_t counter)
move_only_test::operator<
friend bool operator<(const move_only_test &lhs, const move_only_test &rhs)
Definition: robin-map/tests/utils.h:164
copy_only_test::~copy_only_test
~copy_only_test()
Definition: robin-map/tests/utils.h:195
std::hash< copy_only_test >::operator()
std::size_t operator()(const copy_only_test &val) const
Definition: robin-map/tests/utils.h:239
copy_only_test::operator==
friend bool operator==(const copy_only_test &lhs, const copy_only_test &rhs)
Definition: robin-map/tests/utils.h:204
copy_only_test::operator<<
friend std::ostream & operator<<(std::ostream &stream, const copy_only_test &value)
Definition: robin-map/tests/utils.h:197
copy_only_test::m_value
std::string m_value
Definition: robin-map/tests/utils.h:219


mp2p_icp
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Sun Jun 23 2024 02:47:44