robin_set_tests.cpp
1 /** 2 * MIT License 3 * 4 * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 #define BOOST_TEST_DYN_LINK 25 26 27 #include <boost/test/unit_test.hpp> 28 #include <boost/mpl/list.hpp> 29 #include <cstddef> 30 #include <cstdint> 31 #include <functional> 32 #include <memory> 33 #include <string> 34 #include <tuple> 35 #include <utility> 36 37 #include <tsl/robin_set.h> 38 #include "utils.h" 39 40 41 BOOST_AUTO_TEST_SUITE(test_robin_set) 42 43 using test_types = boost::mpl::list<tsl::robin_set<std::int64_t>, 44 tsl::robin_set<std::string>, 45 tsl::robin_set<self_reference_member_test>, 46 tsl::robin_set<move_only_test>, 47 tsl::robin_pg_set<self_reference_member_test>, 48 tsl::robin_set<move_only_test, 49 std::hash<move_only_test>, 50 std::equal_to<move_only_test>, 51 std::allocator<move_only_test>, 52 true, 53 tsl::rh::prime_growth_policy>, 54 tsl::robin_set<self_reference_member_test, 55 std::hash<self_reference_member_test>, 56 std::equal_to<self_reference_member_test>, 57 std::allocator<self_reference_member_test>, 58 true, 59 tsl::rh::mod_growth_policy<>>, 60 tsl::robin_set<move_only_test, 61 std::hash<move_only_test>, 62 std::equal_to<move_only_test>, 63 std::allocator<move_only_test>, 64 false, 65 tsl::rh::mod_growth_policy<>> 66 >; 67 68 69 70 BOOST_AUTO_TEST_CASE_TEMPLATE(test_insert, HSet, test_types) { 71 // insert x values, insert them again, check values 72 using key_t = typename HSet::key_type; 73 74 const std::size_t nb_values = 1000; 75 HSet set; 76 typename HSet::iterator it; 77 bool inserted; 78 79 for(std::size_t i = 0; i < nb_values; i++) { 80 std::tie(it, inserted) = set.insert(utils::get_key<key_t>(i)); 81 82 BOOST_CHECK_EQUAL(*it, utils::get_key<key_t>(i)); 83 BOOST_CHECK(inserted); 84 } 85 BOOST_CHECK_EQUAL(set.size(), nb_values); 86 87 for(std::size_t i = 0; i < nb_values; i++) { 88 std::tie(it, inserted) = set.insert(utils::get_key<key_t>(i)); 89 90 BOOST_CHECK_EQUAL(*it, utils::get_key<key_t>(i)); 91 BOOST_CHECK(!inserted); 92 } 93 94 for(std::size_t i = 0; i < nb_values; i++) { 95 it = set.find(utils::get_key<key_t>(i)); 96 97 BOOST_CHECK_EQUAL(*it, utils::get_key<key_t>(i)); 98 } 99 } 100 101 BOOST_AUTO_TEST_CASE(test_compare) { 102 const tsl::robin_set<std::string> set1 = {"a", "e", "d", "c", "b"}; 103 const tsl::robin_set<std::string> set1_copy = {"e", "c", "b", "a", "d"}; 104 const tsl::robin_set<std::string> set2 = {"e", "c", "b", "a", "d", "f"}; 105 const tsl::robin_set<std::string> set3 = {"e", "c", "b", "a"}; 106 const tsl::robin_set<std::string> set4 = {"a", "e", "d", "c", "z"}; 107 108 BOOST_CHECK(set1 == set1_copy); 109 BOOST_CHECK(set1_copy == set1); 110 111 BOOST_CHECK(set1 != set2); 112 BOOST_CHECK(set2 != set1); 113 114 BOOST_CHECK(set1 != set3); 115 BOOST_CHECK(set3 != set1); 116 117 BOOST_CHECK(set1 != set4); 118 BOOST_CHECK(set4 != set1); 119 120 BOOST_CHECK(set2 != set3); 121 BOOST_CHECK(set3 != set2); 122 123 BOOST_CHECK(set2 != set4); 124 BOOST_CHECK(set4 != set2); 125 126 BOOST_CHECK(set3 != set4); 127 BOOST_CHECK(set4 != set3); 128 } 129 130 BOOST_AUTO_TEST_CASE(test_insert_pointer) { 131 // Test added mainly to be sure that the code compiles with MSVC due to a bug in the compiler. 132 // See robin_hash::insert_value_impl for details. 133 std::string value; 134 std::string* value_ptr = &value; 135 136 tsl::robin_set<std::string*> set; 137 set.insert(value_ptr); 138 set.emplace(value_ptr); 139 140 BOOST_CHECK_EQUAL(set.size(), 1); 141 BOOST_CHECK_EQUAL(**set.begin(), value); 142 } 143 144 BOOST_AUTO_TEST_SUITE_END()