Add equality_tests

This commit is contained in:
Christian Mazakas
2022-10-07 14:15:32 -07:00
parent e0bb258b39
commit e543818e3e
4 changed files with 104 additions and 10 deletions

View File

@@ -332,6 +332,11 @@ namespace boost {
return table_.find(key); return table_.find(key);
} }
bool contains(key_type const& key) const
{
return this->find(key) != this->end();
}
std::pair<iterator, iterator> equal_range(key_type const& key) std::pair<iterator, iterator> equal_range(key_type const& key)
{ {
auto pos = table_.find(key); auto pos = table_.find(key);
@@ -382,6 +387,34 @@ namespace boost {
key_equal key_eq() const { return table_.key_eq(); } key_equal key_eq() const { return table_.key_eq(); }
}; };
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
bool operator==(
unordered_flat_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
unordered_flat_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
{
if (&lhs == &rhs) {
return true;
}
return (lhs.size() == rhs.size()) && ([&] {
for (auto const& kvp : lhs) {
auto pos = rhs.find(kvp.first);
if (pos != rhs.end() && (pos->second != kvp.second)) {
return false;
}
}
return true;
})();
}
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
bool operator!=(
unordered_flat_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
unordered_flat_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
{
return !(lhs == rhs);
}
} // namespace unordered } // namespace unordered
} // namespace boost } // namespace boost

View File

@@ -279,6 +279,34 @@ namespace boost {
key_equal key_eq() const { return table_.key_eq(); } key_equal key_eq() const { return table_.key_eq(); }
}; };
template <class Key, class Hash, class KeyEqual, class Allocator>
bool operator==(
unordered_flat_set<Key, Hash, KeyEqual, Allocator> const& lhs,
unordered_flat_set<Key, Hash, KeyEqual, Allocator> const& rhs)
{
if (&lhs == &rhs) {
return true;
}
return (lhs.size() == rhs.size()) && ([&] {
for (auto const& key : lhs) {
auto pos = rhs.find(key);
if (pos != rhs.end() && (key != *pos)) {
return false;
}
}
return true;
})();
}
template <class Key, class Hash, class KeyEqual, class Allocator>
bool operator!=(
unordered_flat_set<Key, Hash, KeyEqual, Allocator> const& lhs,
unordered_flat_set<Key, Hash, KeyEqual, Allocator> const& rhs)
{
return !(lhs == rhs);
}
} // namespace unordered } // namespace unordered
} // namespace boost } // namespace boost

View File

@@ -114,3 +114,4 @@ build_foa find_tests ;
build_foa at_tests ; build_foa at_tests ;
build_foa load_factor_tests ; build_foa load_factor_tests ;
build_foa rehash_tests ; build_foa rehash_tests ;
build_foa equality_tests ;

View File

@@ -5,8 +5,14 @@
// clang-format off // clang-format off
#include "../helpers/prefix.hpp" #include "../helpers/prefix.hpp"
#ifdef BOOST_UNORDERED_FOA_TESTS
#include <boost/unordered_flat_set.hpp>
#include <boost/unordered_flat_map.hpp>
#include <boost/unordered/detail/implementation.hpp>
#else
#include <boost/unordered_set.hpp> #include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp> #include <boost/unordered_map.hpp>
#endif
#include "../helpers/postfix.hpp" #include "../helpers/postfix.hpp"
// clang-format on // clang-format on
@@ -30,14 +36,27 @@ namespace equality_tests {
} }
}; };
#define UNORDERED_EQUALITY_SET_TEST(seq1, op, seq2) \ #ifdef BOOST_UNORDERED_FOA_TESTS
using boost_unordered_set =
boost::unordered_flat_set<int, mod_compare, mod_compare>;
using boost_unordered_map =
boost::unordered_flat_map<int, int, mod_compare, mod_compare>;
#define UNORDERED_EQUALITY_MULTISET_TEST(seq1, op, seq2) \
{ \ { \
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_TEST(set1 op set2); \
} }
#define UNORDERED_EQUALITY_MULTIMAP_TEST(seq1, op, seq2) \
{ \
}
#else
typedef boost::unordered_set<int, mod_compare, mod_compare>
boost_unordered_set;
typedef boost::unordered_map<int, int, mod_compare, mod_compare>
boost_unordered_map;
#define UNORDERED_EQUALITY_MULTISET_TEST(seq1, op, seq2) \ #define UNORDERED_EQUALITY_MULTISET_TEST(seq1, op, seq2) \
{ \ { \
boost::unordered_multiset<int, mod_compare, mod_compare> set1, set2; \ boost::unordered_multiset<int, mod_compare, mod_compare> set1, set2; \
@@ -46,17 +65,26 @@ namespace equality_tests {
BOOST_TEST(set1 op set2); \ BOOST_TEST(set1 op set2); \
} }
#define UNORDERED_EQUALITY_MAP_TEST(seq1, op, seq2) \ #define UNORDERED_EQUALITY_MULTIMAP_TEST(seq1, op, seq2) \
{ \ { \
boost::unordered_map<int, int, mod_compare, mod_compare> map1, map2; \ 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, map1, seq1) \
BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2) \ BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2) \
BOOST_TEST(map1 op map2); \ BOOST_TEST(map1 op map2); \
} }
#endif
#define UNORDERED_EQUALITY_MULTIMAP_TEST(seq1, op, seq2) \ #define UNORDERED_EQUALITY_SET_TEST(seq1, op, seq2) \
{ \ { \
boost::unordered_multimap<int, int, mod_compare, mod_compare> map1, map2; \ boost_unordered_set set1, set2; \
BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set1, seq1) \
BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set2, seq2) \
BOOST_TEST(set1 op set2); \
}
#define UNORDERED_EQUALITY_MAP_TEST(seq1, op, seq2) \
{ \
boost_unordered_map map1, map2; \
BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map1, seq1) \ BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map1, seq1) \
BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2) \ BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2) \
BOOST_TEST(map1 op map2); \ BOOST_TEST(map1 op map2); \
@@ -67,7 +95,11 @@ namespace equality_tests {
map.insert(std::pair<int const, int> BOOST_PP_SEQ_TO_TUPLE(item)); map.insert(std::pair<int const, int> BOOST_PP_SEQ_TO_TUPLE(item));
UNORDERED_AUTO_TEST (equality_size_tests) { UNORDERED_AUTO_TEST (equality_size_tests) {
#ifdef BOOST_UNORDERED_FOA_TESTS
boost::unordered_flat_set<int> x1, x2;
#else
boost::unordered_set<int> x1, x2; boost::unordered_set<int> x1, x2;
#endif
BOOST_TEST(x1 == x2); BOOST_TEST(x1 == x2);
BOOST_TEST(!(x1 != x2)); BOOST_TEST(!(x1 != x2));
@@ -134,7 +166,7 @@ namespace equality_tests {
// different hash functions but the same equality predicate. // different hash functions but the same equality predicate.
UNORDERED_AUTO_TEST (equality_different_hash_test) { UNORDERED_AUTO_TEST (equality_different_hash_test) {
typedef boost::unordered_set<int, mod_compare, mod_compare> set; typedef boost_unordered_set set;
set set1(0, mod_compare(false), mod_compare(false)); set set1(0, mod_compare(false), mod_compare(false));
set set2(0, mod_compare(true), mod_compare(true)); set set2(0, mod_compare(true), mod_compare(true));
BOOST_TEST(set1 == set2); BOOST_TEST(set1 == set2);