robin_set_tests.cpp
Go to the documentation of this file.
1 
24 #include <tsl/robin_set.h>
25 
26 #include <boost/mpl/list.hpp>
27 #include <boost/test/unit_test.hpp>
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 #include <memory>
32 #include <string>
33 #include <tuple>
34 #include <utility>
35 
36 #include "utils.h"
37 
38 BOOST_AUTO_TEST_SUITE(test_robin_set)
39 
40 using test_types =
41  boost::mpl::list<tsl::robin_set<std::int64_t>, tsl::robin_set<std::string>,
46  std::equal_to<move_only_test>,
51  std::equal_to<self_reference_member_test>,
55  std::equal_to<move_only_test>,
58 
60  // insert x values, insert them again, check values
61  using key_t = typename HSet::key_type;
62 
63  const std::size_t nb_values = 1000;
64  HSet set;
65  typename HSet::iterator it;
66  bool inserted;
67 
68  for (std::size_t i = 0; i < nb_values; i++) {
69  std::tie(it, inserted) = set.insert(utils::get_key<key_t>(i));
70 
71  BOOST_CHECK_EQUAL(*it, utils::get_key<key_t>(i));
72  BOOST_CHECK(inserted);
73  }
74  BOOST_CHECK_EQUAL(set.size(), nb_values);
75 
76  for (std::size_t i = 0; i < nb_values; i++) {
77  std::tie(it, inserted) = set.insert(utils::get_key<key_t>(i));
78 
79  BOOST_CHECK_EQUAL(*it, utils::get_key<key_t>(i));
80  BOOST_CHECK(!inserted);
81  }
82 
83  for (std::size_t i = 0; i < nb_values; i++) {
84  it = set.find(utils::get_key<key_t>(i));
85 
86  BOOST_CHECK_EQUAL(*it, utils::get_key<key_t>(i));
87  }
88 }
89 
90 BOOST_AUTO_TEST_CASE(test_compare) {
91  const tsl::robin_set<std::string> set1 = {"a", "e", "d", "c", "b"};
92  const tsl::robin_set<std::string> set1_copy = {"e", "c", "b", "a", "d"};
93  const tsl::robin_set<std::string> set2 = {"e", "c", "b", "a", "d", "f"};
94  const tsl::robin_set<std::string> set3 = {"e", "c", "b", "a"};
95  const tsl::robin_set<std::string> set4 = {"a", "e", "d", "c", "z"};
96 
97  BOOST_CHECK(set1 == set1_copy);
98  BOOST_CHECK(set1_copy == set1);
99 
100  BOOST_CHECK(set1 != set2);
101  BOOST_CHECK(set2 != set1);
102 
103  BOOST_CHECK(set1 != set3);
104  BOOST_CHECK(set3 != set1);
105 
106  BOOST_CHECK(set1 != set4);
107  BOOST_CHECK(set4 != set1);
108 
109  BOOST_CHECK(set2 != set3);
110  BOOST_CHECK(set3 != set2);
111 
112  BOOST_CHECK(set2 != set4);
113  BOOST_CHECK(set4 != set2);
114 
115  BOOST_CHECK(set3 != set4);
116  BOOST_CHECK(set4 != set3);
117 }
118 
119 BOOST_AUTO_TEST_CASE(test_insert_pointer) {
120  // Test added mainly to be sure that the code compiles with MSVC due to a bug
121  // in the compiler. See robin_hash::insert_value_impl for details.
122  std::string value;
123  std::string* value_ptr = &value;
124 
126  set.insert(value_ptr);
127  set.emplace(value_ptr);
128 
129  BOOST_CHECK_EQUAL(set.size(), 1);
130  BOOST_CHECK_EQUAL(**set.begin(), value);
131 }
132 
136 BOOST_AUTO_TEST_CASE(test_serialize_deserialize) {
137  // insert x values; delete some values; serialize set; deserialize in new set;
138  // check equal. for deserialization, test it with and without hash
139  // compatibility.
140  const std::size_t nb_values = 1000;
141 
143  for (std::size_t i = 0; i < nb_values + 40; i++) {
144  set.insert(utils::get_key<move_only_test>(i));
145  }
146 
147  for (std::size_t i = nb_values; i < nb_values + 40; i++) {
148  set.erase(utils::get_key<move_only_test>(i));
149  }
150  BOOST_CHECK_EQUAL(set.size(), nb_values);
151 
152  serializer serial;
153  set.serialize(serial);
154 
155  deserializer dserial(serial.str());
156  auto set_deserialized = decltype(set)::deserialize(dserial, true);
157  BOOST_CHECK(set == set_deserialized);
158 
159  deserializer dserial2(serial.str());
160  set_deserialized = decltype(set)::deserialize(dserial2, false);
161  BOOST_CHECK(set_deserialized == set);
162 }
163 
164 BOOST_AUTO_TEST_CASE(test_erase_fast) {
165  using Set = tsl::robin_set<int>;
166  Set set;
167  set.emplace(4);
168  auto it = set.find(4);
169  BOOST_CHECK(it != set.end());
170  set.erase_fast(it);
171  BOOST_CHECK(set.size() == 0);
172 }
173 
174 BOOST_AUTO_TEST_SUITE_END()
tsl::rh::prime_growth_policy
Definition: robin_growth_policy.h:369
robin_set.h
deserialize
void deserialize(Stream &stream, boost::array< T, N > &t)
tsl::robin_set
Definition: robin_set.h:90
self_reference_member_test
Definition: robin-map/tests/utils.h:70
serializer
Definition: robin-map/tests/utils.h:349
std::hash< self_reference_member_test >
Definition: robin-map/tests/utils.h:224
std::allocator
testing::internal::string
::std::string string
Definition: gtest.h:1979
test_types
boost::mpl::list< tsl::rh::power_of_two_growth_policy< 2 >, tsl::rh::power_of_two_growth_policy< 4 >, tsl::rh::prime_growth_policy, tsl::rh::mod_growth_policy<>, tsl::rh::mod_growth_policy< std::ratio< 7, 2 > >> test_types
Definition: policy_tests.cpp:41
utils.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_compare)
Definition: robin_set_tests.cpp:90
set
ROSCPP_DECL void set(const std::string &key, bool b)
BOOST_AUTO_TEST_CASE_TEMPLATE
BOOST_AUTO_TEST_CASE_TEMPLATE(test_insert, HSet, test_types)
Definition: robin_set_tests.cpp:59
serializer::str
std::string str() const
Definition: robin-map/tests/utils.h:358
tsl::rh::mod_growth_policy
Definition: robin_growth_policy.h:190
deserializer
Definition: robin-map/tests/utils.h:387


mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:46:00