forked from boostorg/unordered
Remove deprecated headers, move hash_fwd.hpp into hash subdirectory. And several minor internal changes. Mostly minor internal details. Merged revisions 51262-51263,51407-51409,51504-51505,51644-51646,51667 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r51262 | danieljames | 2009-02-15 19:32:04 +0000 (Sun, 15 Feb 2009) | 1 line Use the new 'boost:' links for the hash, unordered and quickbook documentation. ........ r51263 | danieljames | 2009-02-15 19:32:19 +0000 (Sun, 15 Feb 2009) | 2 lines Don't copy images for the standalone hash and unordered documentation, was only really required before the libraries were integrated into boost. ........ r51407 | danieljames | 2009-02-22 23:49:51 +0000 (Sun, 22 Feb 2009) | 1 line Fix the hash dirname. ........ r51408 | danieljames | 2009-02-22 23:50:04 +0000 (Sun, 22 Feb 2009) | 1 line Make copy_buckets and move_buckets member functions - so that calling them is a bit simpler. ........ r51409 | danieljames | 2009-02-22 23:50:20 +0000 (Sun, 22 Feb 2009) | 1 line Move some of the data structure classes out of hash table data. ........ r51504 | danieljames | 2009-03-01 14:15:09 +0000 (Sun, 01 Mar 2009) | 1 line Add missing return for operator=. ........ r51505 | danieljames | 2009-03-01 14:15:39 +0000 (Sun, 01 Mar 2009) | 3 lines Make the sort stable. Doesn't really matter, but it might as well be. ........ r51644 | danieljames | 2009-03-08 09:44:51 +0000 (Sun, 08 Mar 2009) | 1 line Detab. ........ r51645 | danieljames | 2009-03-08 09:45:11 +0000 (Sun, 08 Mar 2009) | 4 lines Move hash_fwd into the hash subdirectory. I should have done this in the last release. But now all of the hash implementation is in the hash subdirectory. ........ r51646 | danieljames | 2009-03-08 09:45:30 +0000 (Sun, 08 Mar 2009) | 3 lines Remove deprecated headers. Fixes #2412. ........ r51667 | danieljames | 2009-03-09 20:56:23 +0000 (Mon, 09 Mar 2009) | 1 line Update copyright dates in hash and unordered. ........ [SVN r51729]
167 lines
5.3 KiB
C++
167 lines
5.3 KiB
C++
|
|
// Copyright 2008-2009 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)
|
|
|
|
#include <boost/unordered_set.hpp>
|
|
#include <boost/unordered_map.hpp>
|
|
#include <boost/preprocessor/seq.hpp>
|
|
#include <list>
|
|
#include "../helpers/test.hpp"
|
|
|
|
namespace equality_tests
|
|
{
|
|
struct mod_compare
|
|
{
|
|
bool alt_hash_;
|
|
|
|
explicit mod_compare(bool alt_hash = false) : alt_hash_(alt_hash) {}
|
|
|
|
bool operator()(int x, int y) const
|
|
{
|
|
return x % 1000 == y % 1000;
|
|
}
|
|
|
|
int operator()(int x) const
|
|
{
|
|
return alt_hash_ ? x % 250 : (x + 5) % 250;
|
|
}
|
|
};
|
|
|
|
#define UNORDERED_EQUALITY_SET_TEST(seq1, op, seq2) \
|
|
do { \
|
|
boost::unordered_set<int, mod_compare, mod_compare> set1, set2; \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set1, seq1) \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set2, seq2) \
|
|
BOOST_CHECK(set1 op set2); \
|
|
} while(false)
|
|
|
|
#define UNORDERED_EQUALITY_MULTISET_TEST(seq1, op, seq2) \
|
|
do { \
|
|
boost::unordered_multiset<int, mod_compare, mod_compare> set1, set2; \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set1, seq1) \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set2, seq2) \
|
|
BOOST_CHECK(set1 op set2); \
|
|
} while(false)
|
|
|
|
#define UNORDERED_EQUALITY_MAP_TEST(seq1, op, seq2) \
|
|
do { \
|
|
boost::unordered_map<int, int, mod_compare, mod_compare> map1, map2; \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map1, seq1) \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2) \
|
|
BOOST_CHECK(map1 op map2); \
|
|
} while(false)
|
|
|
|
#define UNORDERED_EQUALITY_MULTIMAP_TEST(seq1, op, seq2) \
|
|
do { \
|
|
boost::unordered_multimap<int, int, mod_compare, mod_compare> map1, map2; \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map1, seq1) \
|
|
BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2) \
|
|
BOOST_CHECK(map1 op map2); \
|
|
} while(false)
|
|
|
|
#define UNORDERED_SET_INSERT(r, set, item) set.insert(item);
|
|
#define UNORDERED_MAP_INSERT(r, map, item) \
|
|
map.insert(std::pair<int const, int> BOOST_PP_SEQ_TO_TUPLE(item));
|
|
|
|
UNORDERED_AUTO_TEST(equality_size_tests)
|
|
{
|
|
boost::unordered_set<int> x1, x2;
|
|
BOOST_CHECK(x1 == x2);
|
|
BOOST_CHECK(!(x1 != x2));
|
|
|
|
x1.insert(1);
|
|
BOOST_CHECK(x1 != x2);
|
|
BOOST_CHECK(!(x1 == x2));
|
|
BOOST_CHECK(x2 != x1);
|
|
BOOST_CHECK(!(x2 == x1));
|
|
|
|
x2.insert(1);
|
|
BOOST_CHECK(x1 == x2);
|
|
BOOST_CHECK(!(x1 != x2));
|
|
|
|
x2.insert(2);
|
|
BOOST_CHECK(x1 != x2);
|
|
BOOST_CHECK(!(x1 == x2));
|
|
BOOST_CHECK(x2 != x1);
|
|
BOOST_CHECK(!(x2 == x1));
|
|
}
|
|
|
|
UNORDERED_AUTO_TEST(equality_key_value_tests)
|
|
{
|
|
UNORDERED_EQUALITY_MULTISET_TEST((1), !=, (2));
|
|
UNORDERED_EQUALITY_SET_TEST((2), ==, (2));
|
|
UNORDERED_EQUALITY_MAP_TEST(((1)(1))((2)(1)), !=, ((1)(1))((3)(1)));
|
|
}
|
|
|
|
UNORDERED_AUTO_TEST(equality_collision_test)
|
|
{
|
|
UNORDERED_EQUALITY_MULTISET_TEST(
|
|
(1), !=, (501));
|
|
UNORDERED_EQUALITY_MULTISET_TEST(
|
|
(1)(251), !=, (1)(501));
|
|
UNORDERED_EQUALITY_MULTIMAP_TEST(
|
|
((251)(1))((1)(1)), !=, ((501)(1))((1)(1)));
|
|
UNORDERED_EQUALITY_MULTISET_TEST(
|
|
(1)(501), ==, (1)(501));
|
|
UNORDERED_EQUALITY_SET_TEST(
|
|
(1)(501), ==, (501)(1));
|
|
}
|
|
|
|
UNORDERED_AUTO_TEST(equality_group_size_test)
|
|
{
|
|
UNORDERED_EQUALITY_MULTISET_TEST(
|
|
(10)(20)(20), !=, (10)(10)(20));
|
|
UNORDERED_EQUALITY_MULTIMAP_TEST(
|
|
((10)(1))((20)(1))((20)(1)), !=,
|
|
((10)(1))((20)(1))((10)(1)));
|
|
UNORDERED_EQUALITY_MULTIMAP_TEST(
|
|
((20)(1))((10)(1))((10)(1)), ==,
|
|
((10)(1))((20)(1))((10)(1)));
|
|
}
|
|
|
|
UNORDERED_AUTO_TEST(equality_map_value_test)
|
|
{
|
|
UNORDERED_EQUALITY_MAP_TEST(
|
|
((1)(1)), !=, ((1)(2)));
|
|
UNORDERED_EQUALITY_MAP_TEST(
|
|
((1)(1)), ==, ((1)(1)));
|
|
UNORDERED_EQUALITY_MULTIMAP_TEST(
|
|
((1)(1)), !=, ((1)(2)));
|
|
UNORDERED_EQUALITY_MULTIMAP_TEST(
|
|
((1)(1))((1)(1)), !=, ((1)(1))((1)(2)));
|
|
UNORDERED_EQUALITY_MULTIMAP_TEST(
|
|
((1)(2))((1)(1)), !=, ((1)(1))((1)(2)));
|
|
}
|
|
|
|
UNORDERED_AUTO_TEST(equality_predicate_test)
|
|
{
|
|
UNORDERED_EQUALITY_SET_TEST(
|
|
(1), ==, (1001));
|
|
UNORDERED_EQUALITY_MAP_TEST(
|
|
((1)(2))((1001)(1)), ==, ((1001)(2))((1)(1)));
|
|
}
|
|
|
|
// Test that equality still works when the two containers have
|
|
// different hash functions but the same equality predicate.
|
|
|
|
UNORDERED_AUTO_TEST(equality_different_hash_test)
|
|
{
|
|
typedef boost::unordered_set<int, mod_compare, mod_compare> set;
|
|
set set1(0, mod_compare(false), mod_compare(false));
|
|
set set2(0, mod_compare(true), mod_compare(true));
|
|
BOOST_CHECK(set1 == set2);
|
|
set1.insert(1); set2.insert(2);
|
|
BOOST_CHECK(set1 != set2);
|
|
set1.insert(2); set2.insert(1);
|
|
BOOST_CHECK(set1 == set2);
|
|
set1.insert(10); set2.insert(20);
|
|
BOOST_CHECK(set1 != set2);
|
|
set1.insert(20); set2.insert(10);
|
|
BOOST_CHECK(set1 == set2);
|
|
}
|
|
|
|
}
|
|
|
|
RUN_TESTS()
|