unordered/test/helpers/equivalent.hpp
Daniel James 6aa582a90e Merged revisions 43838-43894 via svnmerge from
https://svn.boost.org/svn/boost/branches/unordered/trunk

........
  r43840 | danieljames | 2008-03-24 17:25:07 +0000 (Mon, 24 Mar 2008) | 1 line
  
  Fix a g++ warning.
........
  r43844 | danieljames | 2008-03-24 17:56:28 +0000 (Mon, 24 Mar 2008) | 1 line
  
  It's a new-ish year.
........
  r43885 | danieljames | 2008-03-27 20:36:10 +0000 (Thu, 27 Mar 2008) | 1 line
  
  The release script doesn't need to copy images and css - because that's now done in the jamfiles. Also tweak the shell script a tad bit.
........
  r43890 | danieljames | 2008-03-27 23:01:40 +0000 (Thu, 27 Mar 2008) | 1 line
  
  Starting to add a docbook bibliography.
........
  r43894 | danieljames | 2008-03-27 23:24:18 +0000 (Thu, 27 Mar 2008) | 1 line
  
  Redeclare 'data' in iterator_base to help compilers which have trouble with accessing the nested typedef.
........


[SVN r43895]
2008-03-27 23:38:01 +00:00

95 lines
3.0 KiB
C++

// Copyright 2005-2008 Daniel James.
// 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)
#if !defined(BOOST_UNORDERED_TESTS_EQUIVALENT_HEADER)
#define BOOST_UNORDERED_TESTS_EQUIVALENT_HEADER
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <vector>
#include <algorithm>
#include "./metafunctions.hpp"
#include "./fwd.hpp"
namespace test
{
template <class T1, class T2>
bool equivalent_impl(T1 const& x, T2 const& y, base_type) {
return x == y;
}
template <class T>
bool equivalent_impl(boost::hash<T> const&, boost::hash<T> const&, derived_type) {
return true;
}
template <class T>
bool equivalent_impl(std::equal_to<T> const&, std::equal_to<T> const&, derived_type) {
return true;
}
template <class T1, class T2, class T3, class T4>
bool equivalent_impl(std::pair<T1, T2> const& x1,
std::pair<T3, T4> const& x2, derived_type) {
return equivalent_impl(x1.first, x2.first, derived) &&
equivalent_impl(x1.second, x2.second, derived);
}
struct equivalent_type {
template <class T1, class T2>
bool operator()(T1 const& x, T2 const& y) {
return equivalent_impl(x, y, derived);
}
};
namespace {
equivalent_type equivalent;
}
template <class Container>
class unordered_equivalence_tester
{
BOOST_DEDUCED_TYPENAME Container::size_type size_;
BOOST_DEDUCED_TYPENAME Container::hasher hasher_;
BOOST_DEDUCED_TYPENAME Container::key_equal key_equal_;
float max_load_factor_;
typedef BOOST_DEDUCED_TYPENAME non_const_value_type<Container>::type value_type;
std::vector<value_type> values_;
public:
unordered_equivalence_tester(Container const &x)
: size_(x.size()),
hasher_(x.hash_function()), key_equal_(x.key_eq()),
max_load_factor_(x.max_load_factor()),
values_()
{
// Can't initialise values_ straight from x because of Visual C++ 6
values_.reserve(x.size());
std::copy(x.begin(), x.end(), std::back_inserter(values_));
std::sort(values_.begin(), values_.end());
}
bool operator()(Container const& x) const
{
if(!((size_ == x.size()) &&
(test::equivalent(hasher_, x.hash_function())) &&
(test::equivalent(key_equal_, x.key_eq())) &&
(max_load_factor_ == x.max_load_factor()) &&
(values_.size() == x.size()))) return false;
std::vector<value_type> copy;
copy.reserve(x.size());
std::copy(x.begin(), x.end(), std::back_inserter(copy));
std::sort(copy.begin(), copy.end());
return(std::equal(values_.begin(), values_.end(), copy.begin()));
}
private:
unordered_equivalence_tester();
};
}
#endif