Add foa-based node containers to node_handle_tests

This commit is contained in:
Christian Mazakas
2023-02-14 10:40:20 -08:00
parent eed1eeedeb
commit 4398ffe8c9
2 changed files with 193 additions and 34 deletions

View File

@ -143,6 +143,7 @@ build_foa scary_tests ;
build_foa init_type_insert_tests ;
build_foa max_load_tests ;
build_foa extract_tests ;
build_foa node_handle_tests ;
run unordered/hash_is_avalanching_test.cpp ;

View File

@ -1,12 +1,12 @@
// Copyright 2016 Daniel James.
// Copyright 2023 Christian Mazakas
// 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 "../helpers/postfix.hpp"
#include "../helpers/prefix.hpp"
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include "../helpers/unordered.hpp"
#include "../helpers/helpers.hpp"
#include "../helpers/metafunctions.hpp"
@ -17,15 +17,19 @@
#include <set>
#include <string>
UNORDERED_AUTO_TEST (example1) {
typedef boost::unordered_map<int, std::string>::insert_return_type
insert_return_type;
template <template <class Key, class T, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<Key const, T> > >
class Map>
static void example1()
{
typedef typename Map<int, std::string>::insert_return_type insert_return_type;
boost::unordered_map<int, std::string> src;
Map<int, std::string> src;
src.emplace(1, "one");
src.emplace(2, "two");
src.emplace(3, "buckle my shoe");
boost::unordered_map<int, std::string> dst;
Map<int, std::string> dst;
dst.emplace(3, "three");
dst.insert(src.extract(src.find(1)));
@ -42,12 +46,16 @@ UNORDERED_AUTO_TEST (example1) {
BOOST_TEST(r.node.mapped() == "buckle my shoe");
}
UNORDERED_AUTO_TEST (example2) {
boost::unordered_set<int> src;
template <template <class Key, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> >
class Set>
static void example2()
{
Set<int> src;
src.insert(1);
src.insert(3);
src.insert(5);
boost::unordered_set<int> dst;
Set<int> dst;
dst.insert(2);
dst.insert(4);
dst.insert(5);
@ -57,14 +65,18 @@ UNORDERED_AUTO_TEST (example2) {
// dst == {1, 2, 3, 4, 5}
}
UNORDERED_AUTO_TEST (example3) {
typedef boost::unordered_set<int>::iterator iterator;
template <template <class Key, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> >
class Set>
static void example3()
{
typedef typename Set<int>::iterator iterator;
boost::unordered_set<int> src;
Set<int> src;
src.insert(1);
src.insert(3);
src.insert(5);
boost::unordered_set<int> dst;
Set<int> dst;
dst.insert(2);
dst.insert(4);
dst.insert(5);
@ -88,16 +100,24 @@ UNORDERED_AUTO_TEST (example3) {
BOOST_TEST(it == dst2.end());
}
UNORDERED_AUTO_TEST (failed_insertion_with_hint) {
template <template <class Key, class T, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<Key const, T> > >
class Map,
template <class Key, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> >
class Set>
static void failed_insertion_with_hint()
{
{
boost::unordered_set<int> src;
boost::unordered_set<int> dst;
Set<int> src;
Set<int> dst;
src.emplace(10);
src.emplace(20);
dst.emplace(10);
dst.emplace(20);
boost::unordered_set<int>::node_type nh = src.extract(10);
typename Set<int>::node_type nh = src.extract(10);
BOOST_TEST(dst.insert(dst.find(10), boost::move(nh)) == dst.find(10));
BOOST_TEST(nh);
@ -116,14 +136,14 @@ UNORDERED_AUTO_TEST (failed_insertion_with_hint) {
}
{
boost::unordered_map<int, int> src;
boost::unordered_map<int, int> dst;
Map<int, int> src;
Map<int, int> dst;
src.emplace(10, 30);
src.emplace(20, 5);
dst.emplace(10, 20);
dst.emplace(20, 2);
boost::unordered_map<int, int>::node_type nh = src.extract(10);
typename Map<int, int>::node_type nh = src.extract(10);
BOOST_TEST(dst.insert(dst.find(10), boost::move(nh)) == dst.find(10));
BOOST_TEST(nh);
BOOST_TEST(!nh.empty());
@ -231,20 +251,38 @@ template <typename Container> void node_handle_tests_impl(Container& c)
BOOST_TEST(!n4);
}
UNORDERED_AUTO_TEST (node_handle_tests) {
boost::unordered_set<int> x1;
template <template <class Key, class T, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<Key const, T> > >
class Map,
template <class Key, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>, class Allocator = std::allocator<Key> >
class Set>
static void node_handle_tests()
{
Set<int> x1;
x1.emplace(100);
x1.emplace(140);
x1.emplace(-55);
node_handle_tests_impl(x1);
boost::unordered_map<int, std::string> x2;
Map<int, std::string> x2;
x2.emplace(10, "ten");
x2.emplace(-23, "twenty");
x2.emplace(-76, "thirty");
node_handle_tests_impl(x2);
}
#ifdef BOOST_UNORDERED_FOA_TESTS
template <class X> typename X::iterator insert_empty_node(X& x)
{
typedef typename X::node_type node_type;
return x.insert(node_type()).position;
}
#else
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator
insert_empty_node(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& c)
@ -288,8 +326,10 @@ insert_empty_node(boost::unordered_multiset<T, Hash, KeyEqual, Allocator>& c)
return c.insert(node_type());
}
#endif
template <typename Container1, typename Container2>
void insert_node_handle_unique(Container1& c1, Container2& c2)
static void insert_node_handle_unique(Container1& c1, Container2& c2)
{
typedef typename Container1::node_type node_type;
typedef typename Container1::value_type value_type;
@ -330,7 +370,7 @@ void insert_node_handle_unique(Container1& c1, Container2& c2)
}
template <typename Container1, typename Container2>
void insert_node_handle_unique2(Container1& c1, Container2& c2)
static void insert_node_handle_unique2(Container1& c1, Container2& c2)
{
typedef typename Container1::node_type node_type;
typedef typename Container1::value_type value_type;
@ -362,6 +402,130 @@ void insert_node_handle_unique2(Container1& c1, Container2& c2)
}
}
struct hash_thing
{
std::size_t operator()(int x) const
{
return static_cast<std::size_t>(x * 13 + 5);
}
};
#ifdef BOOST_UNORDERED_FOA_TESTS
UNORDERED_AUTO_TEST (examples) {
example1<boost::unordered_node_map>();
example2<boost::unordered_node_set>();
example3<boost::unordered_node_set>();
failed_insertion_with_hint<boost::unordered_node_map,
boost::unordered_node_set>();
node_handle_tests<boost::unordered_node_map, boost::unordered_node_set>();
}
UNORDERED_AUTO_TEST (insert_node_handle_unique_tests) {
{
boost::unordered_node_set<int> x1;
boost::unordered_node_set<int> x2;
x1.emplace(100);
x1.emplace(140);
x1.emplace(-55);
x2.emplace(140);
insert_node_handle_unique(x1, x2);
BOOST_TEST(x2.size() == 3);
}
{
boost::unordered_node_set<int> x1;
boost::unordered_node_set<int> x2;
x1.emplace(100);
x1.emplace(140);
x1.emplace(-55);
x2.emplace(140);
insert_node_handle_unique(x1, x2);
BOOST_TEST(x2.size() == 3);
}
{
boost::unordered_node_map<int, int, hash_thing> x1;
boost::unordered_node_map<int, int> x2;
x1.emplace(67, 50);
x1.emplace(23, 45);
x1.emplace(18, 19);
x2.emplace(23, 50);
x2.emplace(12, 49);
insert_node_handle_unique(x1, x2);
BOOST_TEST(x2.size() == 4);
}
{
boost::unordered_node_map<int, int, hash_thing> x1;
boost::unordered_node_map<int, int> x2;
x1.emplace(67, 50);
x1.emplace(23, 45);
x1.emplace(18, 19);
x2.emplace(23, 50);
x2.emplace(12, 49);
insert_node_handle_unique(x1, x2);
BOOST_TEST(x2.size() == 4);
}
}
UNORDERED_AUTO_TEST (insert_node_handle_unique_tests2) {
{
boost::unordered_node_set<int> x1;
boost::unordered_node_set<int> x2;
x1.emplace(100);
x1.emplace(140);
x1.emplace(-55);
x2.emplace(140);
insert_node_handle_unique2(x1, x2);
BOOST_TEST(x2.size() == 3);
}
{
boost::unordered_node_set<int> x1;
boost::unordered_node_set<int> x2;
x1.emplace(100);
x1.emplace(140);
x1.emplace(-55);
x2.emplace(140);
insert_node_handle_unique2(x1, x2);
BOOST_TEST(x2.size() == 3);
}
{
boost::unordered_node_map<int, int, hash_thing> x1;
boost::unordered_node_map<int, int> x2;
x1.emplace(67, 50);
x1.emplace(23, 45);
x1.emplace(18, 19);
x2.emplace(23, 50);
x2.emplace(12, 49);
insert_node_handle_unique2(x1, x2);
BOOST_TEST(x2.size() == 4);
}
{
boost::unordered_node_map<int, int, hash_thing> x1;
boost::unordered_node_map<int, int> x2;
x1.emplace(67, 50);
x1.emplace(23, 45);
x1.emplace(18, 19);
x2.emplace(23, 50);
x2.emplace(12, 49);
insert_node_handle_unique2(x1, x2);
BOOST_TEST(x2.size() == 4);
}
}
#else
UNORDERED_AUTO_TEST (examples) {
example1<boost::unordered_map>();
example2<boost::unordered_set>();
example3<boost::unordered_set>();
failed_insertion_with_hint<boost::unordered_map, boost::unordered_set>();
node_handle_tests<boost::unordered_map, boost::unordered_set>();
}
template <typename Container1, typename Container2>
void insert_node_handle_equiv(Container1& c1, Container2& c2)
{
@ -389,14 +553,6 @@ void insert_node_handle_equiv(Container1& c1, Container2& c2)
}
}
struct hash_thing
{
std::size_t operator()(int x) const
{
return static_cast<std::size_t>(x * 13 + 5);
}
};
UNORDERED_AUTO_TEST (insert_node_handle_unique_tests) {
{
boost::unordered_set<int> x1;
@ -547,4 +703,6 @@ UNORDERED_AUTO_TEST (insert_node_handle_unique_tests2) {
}
}
#endif
RUN_TESTS()