Add foa-based node containers to init_type_insert_tests

This commit is contained in:
Christian Mazakas
2023-02-14 10:22:12 -08:00
parent 99b8d0063b
commit 2a7dc9e40e

View File

@ -7,9 +7,16 @@
#error "This test is only for the FOA-style conatiners" #error "This test is only for the FOA-style conatiners"
#endif #endif
#include "../helpers/test.hpp"
#include "../helpers/unordered.hpp" #include "../helpers/unordered.hpp"
#include "../helpers/test.hpp" #include <boost/config.hpp>
#if defined(BOOST_LIBSTDCXX_VERSION)
#if BOOST_LIBSTDCXX_VERSION < 60000
#define BOOST_UNORDERED_NO_INIT_TYPE_TESTS
#endif
#endif
struct move_only struct move_only
{ {
@ -26,18 +33,49 @@ struct move_only
} }
}; };
namespace std{ namespace std {
template <> struct hash<move_only> template <> struct hash<move_only>
{
std::size_t operator()(move_only const& mo) const noexcept
{ {
return std::hash<int>()(mo.x_); std::size_t operator()(move_only const& mo) const noexcept
{
return std::hash<int>()(mo.x_);
}
};
} // namespace std
#ifndef BOOST_UNORDERED_NO_INIT_TYPE_TESTS
struct immovable
{
int x_ = -1;
immovable() = default;
immovable(int x) : x_{x} {}
immovable(immovable const&) = delete;
immovable(immovable&&) = delete;
friend bool operator==(immovable const& lhs, immovable const& rhs)
{
return lhs.x_ == rhs.x_;
} }
}; };
namespace std {
template <> struct hash<immovable>
{
std::size_t operator()(immovable const& im) const noexcept
{
return std::hash<int>()(im.x_);
}
};
} // namespace std } // namespace std
#endif
struct raii_tracker struct raii_tracker
{ {
static unsigned move_constructs; static unsigned move_constructs;
@ -68,15 +106,15 @@ struct raii_tracker
} }
}; };
namespace std{ namespace std {
template <> struct hash<raii_tracker> template <> struct hash<raii_tracker>
{
std::size_t operator()(raii_tracker const& rt) const noexcept
{ {
return std::hash<int>()(rt.x_); std::size_t operator()(raii_tracker const& rt) const noexcept
} {
}; return std::hash<int>()(rt.x_);
}
};
} // namespace std } // namespace std
@ -90,9 +128,7 @@ static void test_move_only()
boost::unordered_flat_map<move_only, int, std::hash<move_only> > map; boost::unordered_flat_map<move_only, int, std::hash<move_only> > map;
using init_type = decltype(map)::init_type; using init_type = decltype(map)::init_type;
static_assert( static_assert(std::is_same<std::pair<move_only, int>, init_type>::value, "");
std::is_same<decltype(std::make_pair(move_only(1), v)), init_type>::value,
"");
map.insert(std::make_pair(move_only(1), v)); map.insert(std::make_pair(move_only(1), v));
map.insert({move_only(2), v}); map.insert({move_only(2), v});
@ -235,10 +271,167 @@ static void test_insert_hint_tracking()
BOOST_TEST_EQ(raii_tracker::move_constructs, 7u + 2u * map.size()); BOOST_TEST_EQ(raii_tracker::move_constructs, 7u + 2u * map.size());
} }
static void test_immovable()
{
#ifndef BOOST_UNORDERED_NO_INIT_TYPE_TESTS
int const v = 128;
boost::unordered_node_map<immovable, int, std::hash<immovable> > map;
using init_type = decltype(map)::init_type;
static_assert(std::is_same<std::pair<immovable, int>, init_type>::value, "");
map.emplace(1, v);
map.emplace(2, v);
BOOST_TEST_EQ(map.size(), 2u);
map.rehash(1024);
BOOST_TEST_GE(map.bucket_count(), 1024u);
#endif
}
static void test_insert_node_tracking()
{
raii_tracker::reset_counts();
BOOST_TEST_EQ(raii_tracker::copy_constructs, 0u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 0u);
boost::unordered_node_map<raii_tracker, raii_tracker,
std::hash<raii_tracker> >
map;
{
std::pair<raii_tracker, raii_tracker> value{1, 2};
map.insert(value);
BOOST_TEST_EQ(raii_tracker::copy_constructs, 2u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 0u);
}
{
std::pair<raii_tracker, raii_tracker> value{2, 3};
map.insert(std::move(value));
BOOST_TEST_EQ(raii_tracker::copy_constructs, 2u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 2u);
}
{
std::pair<raii_tracker const, raii_tracker> value{3, 4};
map.insert(value);
BOOST_TEST_EQ(raii_tracker::copy_constructs, 4u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 2u);
}
{
std::pair<raii_tracker const, raii_tracker> value{4, 5};
map.insert(std::move(value));
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 3u);
}
{
map.insert(std::make_pair(5, 6));
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 5u);
}
{
map.insert({6, 7});
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 7u);
}
BOOST_TEST_EQ(map.size(), 6u);
map.rehash(1024);
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 7u);
}
static void test_insert_hint_node_tracking()
{
raii_tracker::reset_counts();
BOOST_TEST_EQ(raii_tracker::copy_constructs, 0u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 0u);
boost::unordered_node_map<raii_tracker, raii_tracker,
std::hash<raii_tracker> >
map;
{
std::pair<raii_tracker, raii_tracker> value{1, 2};
map.insert(map.begin(), value);
BOOST_TEST_EQ(raii_tracker::copy_constructs, 2u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 0u);
}
{
std::pair<raii_tracker, raii_tracker> value{2, 3};
map.insert(std::move(value));
BOOST_TEST_EQ(raii_tracker::copy_constructs, 2u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 2u);
}
{
std::pair<raii_tracker const, raii_tracker> value{3, 4};
map.insert(map.begin(), value);
BOOST_TEST_EQ(raii_tracker::copy_constructs, 4u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 2u);
}
{
std::pair<raii_tracker const, raii_tracker> value{4, 5};
map.insert(map.begin(), std::move(value));
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 3u);
}
{
map.insert(map.begin(), std::make_pair(5, 6));
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 5u);
}
{
map.insert(map.begin(), {6, 7});
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 7u);
}
BOOST_TEST_EQ(map.size(), 6u);
map.rehash(1024);
BOOST_TEST_EQ(raii_tracker::copy_constructs, 5u);
BOOST_TEST_EQ(raii_tracker::move_constructs, 7u);
}
int main() int main()
{ {
test_move_only(); test_move_only();
test_insert_tracking(); test_insert_tracking();
test_insert_hint_tracking(); test_insert_hint_tracking();
test_immovable();
test_insert_node_tracking();
test_insert_hint_node_tracking();
return boost::report_errors(); return boost::report_errors();
} }