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