| 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 <boost/type_traits/conditional.hpp> |
| 13 | #include <algorithm> |
| 14 | |
| 15 | namespace test { |
| 16 | template <class X> struct unordered_generator_set |
| 17 | { |
| 18 | typedef 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(max: 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 typename X::key_type key_type; |
| 45 | typedef 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(max: 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::conditional<test::is_set<X>::value, |
| 73 | test::unordered_generator_set<X>, test::unordered_generator_map<X> > |
| 74 | { |
| 75 | }; |
| 76 | |
| 77 | template <class X> |
| 78 | struct unordered_generator : public unordered_generator_base<X>::type |
| 79 | { |
| 80 | typedef typename unordered_generator_base<X>::type base; |
| 81 | |
| 82 | unordered_generator(random_generator const& type = default_generator) |
| 83 | : base(type) |
| 84 | { |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | template <class X> |
| 89 | struct random_values : public test::list<typename X::value_type> |
| 90 | { |
| 91 | random_values() {} |
| 92 | |
| 93 | explicit random_values(std::size_t count, |
| 94 | test::random_generator const& generator = test::default_generator) |
| 95 | { |
| 96 | fill(count, generator); |
| 97 | } |
| 98 | |
| 99 | void fill(std::size_t count, |
| 100 | test::random_generator const& generator = test::default_generator) |
| 101 | { |
| 102 | test::unordered_generator<X> gen(generator); |
| 103 | gen.fill(*this, count); |
| 104 | } |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | #endif |
| 109 | |