2006-05-17 17:19:16 +00:00
|
|
|
|
2009-03-09 20:56:23 +00:00
|
|
|
// Copyright 2005-2009 Daniel James.
|
2006-07-01 22:31:26 +00:00
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
2006-05-17 17:19:16 +00:00
|
|
|
|
|
|
|
|
#if !defined(BOOST_UNORDERED_TEST_HELPERS_RANDOM_VALUES_HEADER)
|
|
|
|
|
#define BOOST_UNORDERED_TEST_HELPERS_RANDOM_VALUES_HEADER
|
|
|
|
|
|
2008-04-30 07:57:04 +00:00
|
|
|
#include "./list.hpp"
|
2006-05-17 17:19:16 +00:00
|
|
|
#include <algorithm>
|
2012-07-08 11:55:10 +00:00
|
|
|
#include <boost/detail/select_type.hpp>
|
2006-05-17 17:19:16 +00:00
|
|
|
#include "./generators.hpp"
|
2008-01-20 18:55:57 +00:00
|
|
|
#include "./metafunctions.hpp"
|
2006-05-17 17:19:16 +00:00
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
|
{
|
2008-01-20 18:55:57 +00:00
|
|
|
typedef enum {
|
|
|
|
|
default_generator,
|
|
|
|
|
generate_collisions
|
|
|
|
|
} random_generator;
|
|
|
|
|
|
|
|
|
|
template <class X>
|
|
|
|
|
struct unordered_generator_set
|
|
|
|
|
{
|
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME X::value_type value_type;
|
|
|
|
|
|
|
|
|
|
random_generator type_;
|
|
|
|
|
|
|
|
|
|
unordered_generator_set(random_generator type)
|
|
|
|
|
: type_(type) {}
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
void fill(T& x, std::size_t len) {
|
|
|
|
|
value_type* value_ptr = 0;
|
|
|
|
|
int* int_ptr = 0;
|
2012-09-05 23:33:22 +00:00
|
|
|
len += x.size();
|
2008-01-20 18:55:57 +00:00
|
|
|
|
2012-09-06 08:49:43 +00:00
|
|
|
for (std::size_t i = 0; i < len; ++i) {
|
2008-01-20 18:55:57 +00:00
|
|
|
value_type value = generate(value_ptr);
|
|
|
|
|
|
2012-09-05 23:33:22 +00:00
|
|
|
int count = type_ == generate_collisions ?
|
2012-09-06 08:49:43 +00:00
|
|
|
1 + (generate(int_ptr) % 5) : 1;
|
2012-09-05 23:33:22 +00:00
|
|
|
|
2014-01-26 22:57:14 +00:00
|
|
|
for(int j = 0; j < count; ++j) {
|
2008-01-20 18:55:57 +00:00
|
|
|
x.push_back(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class X>
|
|
|
|
|
struct unordered_generator_map
|
|
|
|
|
{
|
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME X::key_type key_type;
|
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME X::mapped_type mapped_type;
|
|
|
|
|
|
|
|
|
|
random_generator type_;
|
|
|
|
|
|
|
|
|
|
unordered_generator_map(random_generator type)
|
|
|
|
|
: type_(type) {}
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
void fill(T& x, std::size_t len) {
|
|
|
|
|
key_type* key_ptr = 0;
|
|
|
|
|
mapped_type* mapped_ptr = 0;
|
|
|
|
|
int* int_ptr = 0;
|
|
|
|
|
|
2012-09-06 08:49:43 +00:00
|
|
|
for (std::size_t i = 0; i < len; ++i) {
|
2008-01-20 18:55:57 +00:00
|
|
|
key_type key = generate(key_ptr);
|
|
|
|
|
|
2012-09-05 23:33:22 +00:00
|
|
|
int count = type_ == generate_collisions ?
|
2012-09-06 08:49:43 +00:00
|
|
|
1 + (generate(int_ptr) % 5) : 1;
|
2012-09-05 23:33:22 +00:00
|
|
|
|
2014-01-26 22:57:14 +00:00
|
|
|
for(int j = 0; j < count; ++j) {
|
2012-09-05 23:33:22 +00:00
|
|
|
x.push_back(std::pair<key_type const, mapped_type>(
|
|
|
|
|
key, generate(mapped_ptr)));
|
2008-01-20 18:55:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class X>
|
|
|
|
|
struct unordered_generator_base
|
2012-07-08 11:55:10 +00:00
|
|
|
: public boost::detail::if_true<
|
|
|
|
|
test::is_set<X>::value
|
|
|
|
|
>::BOOST_NESTED_TEMPLATE then<
|
2008-01-20 18:55:57 +00:00
|
|
|
test::unordered_generator_set<X>,
|
2012-07-08 11:55:10 +00:00
|
|
|
test::unordered_generator_map<X>
|
|
|
|
|
>
|
2008-01-20 18:55:57 +00:00
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class X>
|
|
|
|
|
struct unordered_generator : public unordered_generator_base<X>::type
|
|
|
|
|
{
|
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME unordered_generator_base<X>::type base;
|
|
|
|
|
|
|
|
|
|
unordered_generator(random_generator const& type = default_generator)
|
|
|
|
|
: base(type) {}
|
|
|
|
|
};
|
|
|
|
|
|
2006-05-17 17:19:16 +00:00
|
|
|
template <class X>
|
|
|
|
|
struct random_values
|
2008-04-30 07:57:04 +00:00
|
|
|
: public test::list<BOOST_DEDUCED_TYPENAME X::value_type>
|
2006-05-17 17:19:16 +00:00
|
|
|
{
|
2016-05-30 11:29:20 +01:00
|
|
|
random_values() {}
|
|
|
|
|
|
|
|
|
|
explicit random_values(int count, test::random_generator const& generator =
|
2008-01-23 23:39:59 +00:00
|
|
|
test::default_generator)
|
2016-05-30 11:29:20 +01:00
|
|
|
{
|
|
|
|
|
fill(count, generator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fill(int count, test::random_generator const& generator =
|
|
|
|
|
test::default_generator)
|
2008-01-20 18:55:57 +00:00
|
|
|
{
|
2012-09-05 23:33:22 +00:00
|
|
|
test::unordered_generator<X> gen(generator);
|
2008-01-20 18:55:57 +00:00
|
|
|
gen.fill(*this, count);
|
2006-05-17 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|