forked from boostorg/unordered
hopefully better cross-platform support. Merged revisions 44778-44835,44837-44918 via svnmerge from https://svn.boost.org/svn/boost/branches/unordered/trunk ........ r44778 | danieljames | 2008-04-26 17:15:44 +0100 (Sat, 26 Apr 2008) | 2 lines Remove a trailing comma. ........ r44779 | danieljames | 2008-04-26 17:23:51 +0100 (Sat, 26 Apr 2008) | 1 line Merge in support for equality operators. ........ r44780 | danieljames | 2008-04-26 17:28:44 +0100 (Sat, 26 Apr 2008) | 1 line Use my own list container to avoid working around STL container bugs. ........ r44833 | danieljames | 2008-04-28 08:03:43 +0100 (Mon, 28 Apr 2008) | 1 line Better equality tests. ........ r44834 | danieljames | 2008-04-28 08:04:03 +0100 (Mon, 28 Apr 2008) | 1 line Remove a superfluous check. ........ r44835 | danieljames | 2008-04-28 08:04:21 +0100 (Mon, 28 Apr 2008) | 1 line Add equality reference documentation. ........ r44916 | danieljames | 2008-04-30 08:16:52 +0100 (Wed, 30 Apr 2008) | 1 line New version of list.hpp ........ r44917 | danieljames | 2008-04-30 08:18:31 +0100 (Wed, 30 Apr 2008) | 1 line Support compilers without ADL in the compile tests. ........ r44918 | danieljames | 2008-04-30 08:25:20 +0100 (Wed, 30 Apr 2008) | 7 lines Change the typedef of buffered functions as it was confusing MSVC 6.5 get_allocator wasn't compiling when the allocator workaround is used because it couldn't cast from the wrapped allocator to an allocator of another type. So use value_alloc_ when it's available (it's only unavailable on compilers with C++0x support, which don't require the workaround). ........ [SVN r44919]
144 lines
4.0 KiB
C++
144 lines
4.0 KiB
C++
|
|
// Copyright 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)
|
|
|
|
#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 operator()(int x, int y) const
|
|
{
|
|
return x % 1000 == y % 1000;
|
|
}
|
|
|
|
int operator()(int x) const
|
|
{
|
|
return x % 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)));
|
|
}
|
|
|
|
}
|
|
|
|
RUN_TESTS()
|