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 #include <boost/test/unit_test.hpp>
27 #include <boost/mpl/list.hpp>
28 #include <cstddef>
29 #include <limits>
30 #include <ratio>
31 #include <stdexcept>
32
33 #include <tsl/robin_growth_policy.h>
34 #include "utils.h"
35
36
37 BOOST_AUTO_TEST_SUITE(test_policy)
38
39 using test_types = boost::mpl::list<tsl::rh::power_of_two_growth_policy<2>,
40 tsl::rh::power_of_two_growth_policy<4>,
41 tsl::rh::prime_growth_policy,
42 tsl::rh::mod_growth_policy<>,
43 tsl::rh::mod_growth_policy<std::ratio<7,2>>>;
44
45
BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy,Policy,test_types)46 BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy, Policy, test_types) {
47 // Call next_bucket_count() on the policy until we reach its max_bucket_count()
48 std::size_t bucket_count = 0;
49 Policy policy(bucket_count);
50
51 BOOST_CHECK_EQUAL(policy.bucket_for_hash(0), 0);
52 BOOST_CHECK_EQUAL(bucket_count, 0);
53
54 #ifndef TSL_RH_NO_EXCEPTIONS
55 bool exception_thrown = false;
56 try {
57 while(true) {
58 const std::size_t previous_bucket_count = bucket_count;
59
60 bucket_count = policy.next_bucket_count();
61 policy = Policy(bucket_count);
62
63 BOOST_CHECK_EQUAL(policy.bucket_for_hash(0), 0);
64 BOOST_CHECK(bucket_count > previous_bucket_count);
65 }
66 }
67 catch(const std::length_error& ) {
68 exception_thrown = true;
69 }
70
71 BOOST_CHECK(exception_thrown);
72 #endif
73 }
74
BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_min_bucket_count,Policy,test_types)75 BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_min_bucket_count, Policy, test_types) {
76 // Check policy when a bucket_count of 0 is asked.
77 std::size_t bucket_count = 0;
78 Policy policy(bucket_count);
79
80 BOOST_CHECK_EQUAL(policy.bucket_for_hash(0), 0);
81 }
82
BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_max_bucket_count,Policy,test_types)83 BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_max_bucket_count, Policy, test_types) {
84 // Test a bucket_count equals to the max_bucket_count limit and above
85 std::size_t bucket_count = 0;
86 Policy policy(bucket_count);
87
88
89 bucket_count = policy.max_bucket_count();
90 Policy policy2(bucket_count);
91
92
93 bucket_count = std::numeric_limits<std::size_t>::max();
94 TSL_RH_CHECK_THROW((Policy(bucket_count)), std::length_error);
95
96
97 bucket_count = policy.max_bucket_count() + 1;
98 TSL_RH_CHECK_THROW((Policy(bucket_count)), std::length_error);
99 }
100
101
102 BOOST_AUTO_TEST_SUITE_END()
103