diff --git a/CMakeLists.txt b/CMakeLists.txt index b532a98..dc1f879 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,8 @@ find_package(Boost 1.54.0 REQUIRED COMPONENTS unit_test_framework) set(TEST_EXECUTABLE "test_robin_map") add_executable("${TEST_EXECUTABLE}" "tests/main.cpp" - "tests/custom_allocator_tests.cpp" + "tests/custom_allocator_tests.cpp" + "tests/policy_tests.cpp" "tests/robin_map_tests.cpp" "tests/robin_set_tests.cpp") diff --git a/tests/custom_allocator_tests.cpp b/tests/custom_allocator_tests.cpp index 1285150..4c8758c 100644 --- a/tests/custom_allocator_tests.cpp +++ b/tests/custom_allocator_tests.cpp @@ -33,8 +33,8 @@ #include #include +#include #include "utils.h" -#include "tsl/robin_map.h" static std::size_t nb_custom_allocs = 0; diff --git a/tests/policy_tests.cpp b/tests/policy_tests.cpp new file mode 100644 index 0000000..930d558 --- /dev/null +++ b/tests/policy_tests.cpp @@ -0,0 +1,93 @@ +/** + * MIT License + * + * Copyright (c) 2017 Tessil + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#define BOOST_TEST_DYN_LINK + +#include +#include +#include +#include +#include +#include + +#include + + +BOOST_AUTO_TEST_SUITE(test_policy) + +using test_types = boost::mpl::list, + tsl::power_of_two_growth_policy_rh<4>, + tsl::prime_growth_policy_rh, + tsl::mod_growth_policy_rh<>, + tsl::mod_growth_policy_rh>>; + + +BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy, Policy, test_types) { + // Call next_bucket_count() on the policy until we reach its max_bucket_count() + bool exception_thrown = false; + + std::size_t bucket_count = 0; + Policy policy(bucket_count); + + try { + while(true) { + bucket_count = policy.next_bucket_count(); + policy = Policy(bucket_count); + } + } + catch(const std::length_error& ) { + exception_thrown = true; + BOOST_CHECK_EQUAL(bucket_count, policy.max_bucket_count()); + } + + BOOST_CHECK(exception_thrown); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_min_bucket_count, Policy, test_types) { + // Check polcy when a bucket_count of 0 is asked. + std::size_t bucket_count = 0; + Policy policy(bucket_count); + + BOOST_CHECK_EQUAL(policy.bucket_for_hash(0), 0); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_max_bucket_count, Policy, test_types) { + // Test a bucket_count equals to the max_bucket_count limit and above + std::size_t bucket_count = 0; + Policy policy(bucket_count); + + + bucket_count = policy.max_bucket_count(); + Policy policy2(bucket_count); + + + bucket_count = std::numeric_limits::max(); + BOOST_CHECK_THROW((Policy(bucket_count)), std::length_error); + + + bucket_count = policy.max_bucket_count() + 1; + BOOST_CHECK_THROW((Policy(bucket_count)), std::length_error); +} + + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/robin_map_tests.cpp b/tests/robin_map_tests.cpp index 0b27afb..692d3fd 100644 --- a/tests/robin_map_tests.cpp +++ b/tests/robin_map_tests.cpp @@ -38,8 +38,8 @@ #include #include +#include #include "utils.h" -#include "tsl/robin_map.h" BOOST_AUTO_TEST_SUITE(test_robin_map) diff --git a/tests/robin_set_tests.cpp b/tests/robin_set_tests.cpp index 1c610c7..7363a59 100644 --- a/tests/robin_set_tests.cpp +++ b/tests/robin_set_tests.cpp @@ -34,7 +34,7 @@ #include #include -#include "tsl/robin_set.h" +#include #include "utils.h" diff --git a/tsl/robin_growth_policy.h b/tsl/robin_growth_policy.h index 3038470..0fb7e6d 100644 --- a/tsl/robin_growth_policy.h +++ b/tsl/robin_growth_policy.h @@ -86,7 +86,8 @@ public: * Return the maximum number of buckets supported by the policy. */ std::size_t max_bucket_count() const { - return (std::numeric_limits::max() / GrowthFactor) + 1; + // Largest power of two. + return (std::numeric_limits::max() / 2) + 1; } private: