]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
2 | // Copyright 2006-2009 Daniel James. | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
b32b8144 | 6 | // clang-format off |
7c673cae FG |
7 | #include "../helpers/prefix.hpp" |
8 | #include <boost/unordered_set.hpp> | |
9 | #include <boost/unordered_map.hpp> | |
10 | #include "../helpers/postfix.hpp" | |
b32b8144 | 11 | // clang-format on |
7c673cae FG |
12 | |
13 | #include "../helpers/test.hpp" | |
14 | #include <boost/limits.hpp> | |
15 | #include "../helpers/random_values.hpp" | |
16 | ||
17 | #if defined(BOOST_MSVC) | |
18 | #pragma warning(push) | |
b32b8144 | 19 | #pragma warning(disable : 4127) // conditional expression is constant |
7c673cae FG |
20 | #endif |
21 | ||
b32b8144 | 22 | namespace load_factor_tests { |
7c673cae | 23 | |
b32b8144 | 24 | test::seed_t initialize_seed(783656); |
7c673cae | 25 | |
b32b8144 FG |
26 | template <class X> void set_load_factor_tests(X*) |
27 | { | |
7c673cae FG |
28 | X x; |
29 | ||
30 | BOOST_TEST(x.max_load_factor() == 1.0); | |
31 | BOOST_TEST(x.load_factor() == 0); | |
32 | ||
33 | // A valid implementation could fail these tests, but I think they're | |
34 | // reasonable. | |
b32b8144 FG |
35 | x.max_load_factor(2.0); |
36 | BOOST_TEST(x.max_load_factor() == 2.0); | |
37 | x.max_load_factor(0.5); | |
38 | BOOST_TEST(x.max_load_factor() == 0.5); | |
39 | } | |
40 | ||
41 | template <class X> | |
42 | void insert_test(X*, float mlf, test::random_generator generator) | |
43 | { | |
7c673cae FG |
44 | X x; |
45 | x.max_load_factor(mlf); | |
46 | float b = x.max_load_factor(); | |
47 | ||
48 | test::random_values<X> values(1000, generator); | |
49 | ||
b32b8144 FG |
50 | for (BOOST_DEDUCED_TYPENAME test::random_values<X>::const_iterator |
51 | it = values.begin(), | |
52 | end = values.end(); | |
53 | it != end; ++it) { | |
54 | BOOST_DEDUCED_TYPENAME X::size_type old_size = x.size(), | |
55 | old_bucket_count = x.bucket_count(); | |
56 | x.insert(*it); | |
57 | if (static_cast<double>(old_size + 1) <= | |
58 | b * static_cast<double>(old_bucket_count)) | |
59 | BOOST_TEST(x.bucket_count() == old_bucket_count); | |
7c673cae | 60 | } |
b32b8144 | 61 | } |
7c673cae | 62 | |
b32b8144 FG |
63 | template <class X> |
64 | void load_factor_insert_tests(X* ptr, test::random_generator generator) | |
65 | { | |
7c673cae FG |
66 | insert_test(ptr, 1.0f, generator); |
67 | insert_test(ptr, 0.1f, generator); | |
68 | insert_test(ptr, 100.0f, generator); | |
69 | ||
b32b8144 | 70 | insert_test(ptr, (std::numeric_limits<float>::min)(), generator); |
7c673cae | 71 | |
b32b8144 FG |
72 | if (std::numeric_limits<float>::has_infinity) |
73 | insert_test(ptr, std::numeric_limits<float>::infinity(), generator); | |
74 | } | |
7c673cae | 75 | |
b32b8144 FG |
76 | boost::unordered_set<int>* int_set_ptr; |
77 | boost::unordered_multiset<int>* int_multiset_ptr; | |
78 | boost::unordered_map<int, int>* int_map_ptr; | |
79 | boost::unordered_multimap<int, int>* int_multimap_ptr; | |
7c673cae | 80 | |
b32b8144 FG |
81 | using test::default_generator; |
82 | using test::generate_collisions; | |
83 | using test::limited_range; | |
7c673cae | 84 | |
b32b8144 FG |
85 | UNORDERED_TEST(set_load_factor_tests, |
86 | ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr))) | |
7c673cae | 87 | |
b32b8144 FG |
88 | UNORDERED_TEST(load_factor_insert_tests, |
89 | ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr))( | |
90 | (default_generator)(generate_collisions)(limited_range))) | |
7c673cae FG |
91 | } |
92 | ||
93 | RUN_TESTS() | |
94 | ||
95 | #if defined(BOOST_MSVC) | |
96 | #pragma warning(pop) | |
b32b8144 | 97 | #pragma warning(disable : 4127) // conditional expression is constant |
7c673cae | 98 | #endif |