Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / unordered / test / helpers / random_values.hpp
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
9 #include "./generators.hpp"
10 #include "./list.hpp"
11 #include "./metafunctions.hpp"
12 #include <algorithm>
13 #include <boost/detail/select_type.hpp>
14
15 namespace test {
16 template <class X> struct unordered_generator_set
17 {
18     typedef BOOST_DEDUCED_TYPENAME X::value_type value_type;
19
20     random_generator type_;
21
22     unordered_generator_set(random_generator type) : type_(type) {}
23
24     template <class T> void fill(T& x, std::size_t len)
25     {
26         value_type* value_ptr = 0;
27         len += x.size();
28
29         for (std::size_t i = 0; i < len; ++i) {
30             value_type value = generate(value_ptr, type_);
31
32             std::size_t count =
33                 type_ == generate_collisions ? random_value(5) + 1 : 1;
34
35             for (std::size_t j = 0; j < count; ++j) {
36                 x.push_back(value);
37             }
38         }
39     }
40 };
41
42 template <class X> struct unordered_generator_map
43 {
44     typedef BOOST_DEDUCED_TYPENAME X::key_type key_type;
45     typedef BOOST_DEDUCED_TYPENAME X::mapped_type mapped_type;
46
47     random_generator type_;
48
49     unordered_generator_map(random_generator type) : type_(type) {}
50
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;
55
56         for (std::size_t i = 0; i < len; ++i) {
57             key_type key = generate(key_ptr, type_);
58
59             std::size_t count =
60                 type_ == generate_collisions ? random_value(5) + 1 : 1;
61
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_)));
65             }
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 {
81     typedef BOOST_DEDUCED_TYPENAME unordered_generator_base<X>::type base;
82
83     unordered_generator(random_generator const& type = default_generator)
84         : base(type)
85     {
86     }
87 };
88
89 template <class X>
90 struct random_values : public test::list<BOOST_DEDUCED_TYPENAME X::value_type>
91 {
92     random_values() {}
93
94     explicit random_values(std::size_t count,
95         test::random_generator const& generator = test::default_generator)
96     {
97         fill(count, generator);
98     }
99
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 };
107 }
108
109 #endif