]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
2 | // Copyright 2005-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 | ||
6 | #if !defined(BOOST_UNORDERED_TEST_HELPERS_RANDOM_VALUES_HEADER) | |
7 | #define BOOST_UNORDERED_TEST_HELPERS_RANDOM_VALUES_HEADER | |
8 | ||
b32b8144 | 9 | #include "./generators.hpp" |
7c673cae | 10 | #include "./list.hpp" |
b32b8144 | 11 | #include "./metafunctions.hpp" |
7c673cae FG |
12 | #include <algorithm> |
13 | #include <boost/detail/select_type.hpp> | |
7c673cae | 14 | |
b32b8144 FG |
15 | namespace test { |
16 | template <class X> struct unordered_generator_set | |
17 | { | |
11fdf7f2 | 18 | typedef typename X::value_type value_type; |
7c673cae | 19 | |
b32b8144 | 20 | random_generator type_; |
7c673cae | 21 | |
b32b8144 | 22 | unordered_generator_set(random_generator type) : type_(type) {} |
7c673cae | 23 | |
b32b8144 FG |
24 | template <class T> void fill(T& x, std::size_t len) |
25 | { | |
26 | value_type* value_ptr = 0; | |
27 | len += x.size(); | |
7c673cae | 28 | |
b32b8144 FG |
29 | for (std::size_t i = 0; i < len; ++i) { |
30 | value_type value = generate(value_ptr, type_); | |
7c673cae | 31 | |
b32b8144 FG |
32 | std::size_t count = |
33 | type_ == generate_collisions ? random_value(5) + 1 : 1; | |
7c673cae | 34 | |
b32b8144 FG |
35 | for (std::size_t j = 0; j < count; ++j) { |
36 | x.push_back(value); | |
7c673cae | 37 | } |
b32b8144 FG |
38 | } |
39 | } | |
40 | }; | |
7c673cae | 41 | |
b32b8144 FG |
42 | template <class X> struct unordered_generator_map |
43 | { | |
11fdf7f2 TL |
44 | typedef typename X::key_type key_type; |
45 | typedef typename X::mapped_type mapped_type; | |
7c673cae | 46 | |
b32b8144 | 47 | random_generator type_; |
7c673cae | 48 | |
b32b8144 | 49 | unordered_generator_map(random_generator type) : type_(type) {} |
7c673cae | 50 | |
b32b8144 FG |
51 | template <class T> void fill(T& x, std::size_t len) |
52 | { | |
53 | key_type* key_ptr = 0; | |
54 | mapped_type* mapped_ptr = 0; | |
7c673cae | 55 | |
b32b8144 FG |
56 | for (std::size_t i = 0; i < len; ++i) { |
57 | key_type key = generate(key_ptr, type_); | |
7c673cae | 58 | |
b32b8144 FG |
59 | std::size_t count = |
60 | type_ == generate_collisions ? random_value(5) + 1 : 1; | |
7c673cae | 61 | |
b32b8144 FG |
62 | for (std::size_t j = 0; j < count; ++j) { |
63 | x.push_back(std::pair<key_type const, mapped_type>( | |
64 | key, generate(mapped_ptr, type_))); | |
7c673cae | 65 | } |
b32b8144 FG |
66 | } |
67 | } | |
68 | }; | |
69 | ||
70 | template <class X> | |
71 | struct unordered_generator_base | |
72 | : public boost::detail::if_true<test::is_set<X>::value>:: | |
73 | BOOST_NESTED_TEMPLATE then<test::unordered_generator_set<X>, | |
74 | test::unordered_generator_map<X> > | |
75 | { | |
76 | }; | |
77 | ||
78 | template <class X> | |
79 | struct unordered_generator : public unordered_generator_base<X>::type | |
80 | { | |
11fdf7f2 | 81 | typedef typename unordered_generator_base<X>::type base; |
b32b8144 FG |
82 | |
83 | unordered_generator(random_generator const& type = default_generator) | |
84 | : base(type) | |
7c673cae | 85 | { |
b32b8144 FG |
86 | } |
87 | }; | |
7c673cae | 88 | |
b32b8144 | 89 | template <class X> |
11fdf7f2 | 90 | struct random_values : public test::list<typename X::value_type> |
b32b8144 FG |
91 | { |
92 | random_values() {} | |
7c673cae | 93 | |
b32b8144 FG |
94 | explicit random_values(std::size_t count, |
95 | test::random_generator const& generator = test::default_generator) | |
7c673cae | 96 | { |
b32b8144 FG |
97 | fill(count, generator); |
98 | } | |
7c673cae | 99 | |
b32b8144 FG |
100 | void fill(std::size_t count, |
101 | test::random_generator const& generator = test::default_generator) | |
102 | { | |
103 | test::unordered_generator<X> gen(generator); | |
104 | gen.fill(*this, count); | |
105 | } | |
106 | }; | |
7c673cae FG |
107 | } |
108 | ||
109 | #endif |