Files

419 lines
12 KiB
C++
Raw Permalink Normal View History

2006-03-19 22:24:06 +00:00
// Copyright 2006-2009 Daniel James.
// Copyright 2022-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)
2006-03-19 22:24:06 +00:00
// This test creates the containers with members that meet their minimum
// requirements. Makes sure everything compiles and is defined correctly.
#include "../helpers/unordered.hpp"
2006-03-19 22:24:06 +00:00
2008-03-24 17:03:15 +00:00
#include "../helpers/test.hpp"
2006-03-19 22:24:06 +00:00
#include "../objects/minimal.hpp"
#include "./compile_tests.hpp"
// Explicit instantiation to catch compile-time errors
2022-10-11 15:20:37 -07:00
#ifdef BOOST_UNORDERED_FOA_TESTS
// emulates what was already done for previous tests but without leaking to
// the detail namespace
//
template <typename T, typename H, typename P, typename A>
class instantiate_flat_set
{
typedef boost::unordered_flat_set<T, H, P, A> container;
container x;
};
template class instantiate_flat_set<int, boost::hash<int>, std::equal_to<int>,
test::minimal::allocator<int> >;
template class instantiate_flat_set<test::minimal::assignable const,
test::minimal::hash<test::minimal::assignable>,
test::minimal::equal_to<test::minimal::assignable>,
test::minimal::allocator<int> >;
template <typename T, typename H, typename P, typename A>
class instantiate_node_set
{
typedef boost::unordered_node_set<T, H, P, A> container;
container x;
};
template class instantiate_node_set<int, boost::hash<int>, std::equal_to<int>,
test::minimal::allocator<int> >;
template class instantiate_node_set<test::minimal::assignable const,
test::minimal::hash<test::minimal::assignable>,
test::minimal::equal_to<test::minimal::assignable>,
test::minimal::allocator<int> >;
2022-10-11 15:20:37 -07:00
#else
2017-02-27 03:59:02 +00:00
#define INSTANTIATE(type) \
template class boost::unordered::detail::instantiate_##type
2017-02-27 03:59:02 +00:00
INSTANTIATE(set)<int, boost::hash<int>, std::equal_to<int>,
test::minimal::allocator<int> >;
2017-02-27 03:59:02 +00:00
INSTANTIATE(multiset)<int const, boost::hash<int>, std::equal_to<int>,
test::minimal::allocator<int> >;
2017-02-27 03:59:02 +00:00
INSTANTIATE(set)<test::minimal::assignable const,
test::minimal::hash<test::minimal::assignable>,
test::minimal::equal_to<test::minimal::assignable>,
test::minimal::allocator<int> >;
2017-02-27 03:59:02 +00:00
INSTANTIATE(multiset)<test::minimal::assignable,
test::minimal::hash<test::minimal::assignable>,
test::minimal::equal_to<test::minimal::assignable>,
test::minimal::allocator<int> >;
2022-10-11 15:20:37 -07:00
#endif
template <class X> static void type_traits_impl()
{
BOOST_STATIC_ASSERT(boost::is_same<int const&,
typename std::iterator_traits<typename X::iterator>::reference>::value);
}
UNORDERED_AUTO_TEST (type_traits) {
#ifdef BOOST_UNORDERED_FOA_TESTS
type_traits_impl<boost::unordered_flat_set<int> >();
type_traits_impl<boost::unordered_node_set<int> >();
#else
type_traits_impl<boost::unordered_set<int> >();
type_traits_impl<boost::unordered_multiset<int> >();
#endif
}
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void test0_impl()
{
test::minimal::constructor_param x;
test::minimal::assignable assignable(x);
2006-03-19 22:24:06 +00:00
Set<int> int_set;
Set<int, boost::hash<int>, std::equal_to<int>,
2022-10-11 15:20:37 -07:00
test::minimal::cxx11_allocator<int> >
int_set2;
Set<test::minimal::assignable, test::minimal::hash<test::minimal::assignable>,
2022-10-11 15:20:37 -07:00
test::minimal::equal_to<test::minimal::assignable>,
test::minimal::allocator<test::minimal::assignable> >
set;
2006-03-19 22:24:06 +00:00
container_test(int_set, 0);
container_test(int_set2, 0);
container_test(set, assignable);
}
2006-03-19 22:24:06 +00:00
UNORDERED_AUTO_TEST (test0) {
#ifdef BOOST_UNORDERED_FOA_TESTS
test0_impl<boost::unordered_flat_set>();
test0_impl<boost::unordered_node_set>();
#else
test0_impl<boost::unordered_set>();
test0_impl<boost::unordered_multiset>();
2022-10-11 15:20:37 -07:00
#endif
}
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void equality_tests_impl()
{
typedef test::minimal::copy_constructible_equality_comparable value_type;
Set<int> int_set;
2022-10-11 15:20:37 -07:00
Set<int, boost::hash<int>, std::equal_to<int>,
2022-10-11 15:20:37 -07:00
test::minimal::cxx11_allocator<int> >
int_set2;
Set<test::minimal::copy_constructible_equality_comparable,
2022-10-11 15:20:37 -07:00
test::minimal::hash<test::minimal::copy_constructible_equality_comparable>,
test::minimal::equal_to<
test::minimal::copy_constructible_equality_comparable>,
test::minimal::allocator<value_type> >
set;
equality_test(int_set);
equality_test(int_set2);
equality_test(set);
}
UNORDERED_AUTO_TEST (equality_tests) {
#ifdef BOOST_UNORDERED_FOA_TESTS
equality_tests_impl<boost::unordered_flat_set>();
equality_tests_impl<boost::unordered_node_set>();
#else
equality_tests_impl<boost::unordered_set>();
equality_tests_impl<boost::unordered_multiset>();
2022-10-11 15:20:37 -07:00
#endif
}
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void test1_unique_impl()
{
boost::hash<int> hash;
std::equal_to<int> equal_to;
int value = 0;
Set<int> set;
Set<int, boost::hash<int>, std::equal_to<int>,
2022-10-11 15:20:37 -07:00
test::minimal::cxx11_allocator<int> >
set2;
unordered_unique_test(set, value);
unordered_set_test(set, value);
unordered_copyable_test(set, value, value, hash, equal_to);
unordered_unique_test(set2, value);
unordered_set_test(set2, value);
unordered_copyable_test(set2, value, value, hash, equal_to);
}
2022-10-11 15:20:37 -07:00
#ifndef BOOST_UNORDERED_FOA_TESTS
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void test1_equivalent_impl()
{
boost::hash<int> hash;
std::equal_to<int> equal_to;
int value = 0;
Set<int> set;
2017-02-19 13:05:17 +00:00
Set<int, boost::hash<int>, std::equal_to<int>,
test::minimal::cxx11_allocator<int> >
set2;
unordered_equivalent_test(set, value);
unordered_set_test(set, value);
unordered_copyable_test(set, value, value, hash, equal_to);
unordered_equivalent_test(set2, value);
unordered_set_test(set2, value);
unordered_copyable_test(set2, value, value, hash, equal_to);
}
#endif
UNORDERED_AUTO_TEST (test1) {
#ifdef BOOST_UNORDERED_FOA_TESTS
test1_unique_impl<boost::unordered_flat_set>();
test1_unique_impl<boost::unordered_node_set>();
#else
test1_unique_impl<boost::unordered_set>();
test1_equivalent_impl<boost::unordered_multiset>();
2022-10-11 15:20:37 -07:00
#endif
}
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void test2_unique_impl()
{
test::minimal::constructor_param x;
2017-02-19 13:05:17 +00:00
test::minimal::assignable assignable(x);
test::minimal::copy_constructible copy_constructible(x);
test::minimal::hash<test::minimal::assignable> hash(x);
test::minimal::equal_to<test::minimal::assignable> equal_to(x);
Set<test::minimal::assignable, test::minimal::hash<test::minimal::assignable>,
2022-10-11 15:20:37 -07:00
test::minimal::equal_to<test::minimal::assignable>,
test::minimal::allocator<test::minimal::assignable> >
set;
unordered_unique_test(set, assignable);
unordered_set_test(set, assignable);
unordered_copyable_test(set, assignable, assignable, hash, equal_to);
unordered_set_member_test(set, assignable);
}
2022-10-11 15:20:37 -07:00
#ifndef BOOST_UNORDERED_FOA_TESTS
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void test2_equivalent_impl()
{
test::minimal::constructor_param x;
test::minimal::assignable assignable(x);
test::minimal::copy_constructible copy_constructible(x);
test::minimal::hash<test::minimal::assignable> hash(x);
test::minimal::equal_to<test::minimal::assignable> equal_to(x);
Set<test::minimal::assignable, test::minimal::hash<test::minimal::assignable>,
test::minimal::equal_to<test::minimal::assignable>,
test::minimal::allocator<test::minimal::assignable> >
set;
unordered_equivalent_test(set, assignable);
unordered_set_test(set, assignable);
unordered_copyable_test(set, assignable, assignable, hash, equal_to);
unordered_set_member_test(set, assignable);
}
#endif
UNORDERED_AUTO_TEST (test2) {
#ifdef BOOST_UNORDERED_FOA_TESTS
test2_unique_impl<boost::unordered_flat_set>();
test2_unique_impl<boost::unordered_node_set>();
#else
test2_unique_impl<boost::unordered_set>();
test2_equivalent_impl<boost::unordered_multiset>();
2022-10-11 15:20:37 -07:00
#endif
2011-08-16 22:17:11 +00:00
}
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void movable1_tests_impl()
{
test::minimal::constructor_param x;
2011-08-16 22:17:11 +00:00
test::minimal::movable1 movable1(x);
test::minimal::hash<test::minimal::movable1> hash(x);
test::minimal::equal_to<test::minimal::movable1> equal_to(x);
2011-08-16 22:17:11 +00:00
Set<test::minimal::movable1, test::minimal::hash<test::minimal::movable1>,
2022-10-11 15:20:37 -07:00
test::minimal::equal_to<test::minimal::movable1>,
test::minimal::allocator<test::minimal::movable1> >
set;
2011-08-16 22:17:11 +00:00
// TODO: find out why Daniel had this commented out and if we need it and the
// corresponding equivalent impl
//
// unordered_unique_test(set, movable1);
unordered_set_test(set, movable1);
unordered_movable_test(set, movable1, movable1, hash, equal_to);
}
2011-08-16 22:17:11 +00:00
UNORDERED_AUTO_TEST (movable1_tests) {
#ifdef BOOST_UNORDERED_FOA_TESTS
movable1_tests_impl<boost::unordered_flat_set>();
movable1_tests_impl<boost::unordered_node_set>();
#else
movable1_tests_impl<boost::unordered_set>();
movable1_tests_impl<boost::unordered_multiset>();
2022-10-11 15:20:37 -07:00
#endif
2011-08-16 22:17:11 +00:00
}
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void movable2_tests_impl()
{
test::minimal::constructor_param x;
2011-08-16 22:17:11 +00:00
test::minimal::movable2 movable2(x);
test::minimal::hash<test::minimal::movable2> hash(x);
test::minimal::equal_to<test::minimal::movable2> equal_to(x);
2011-08-16 22:17:11 +00:00
Set<test::minimal::movable2, test::minimal::hash<test::minimal::movable2>,
2022-10-11 15:20:37 -07:00
test::minimal::equal_to<test::minimal::movable2>,
test::minimal::allocator<test::minimal::movable2> >
set;
2011-08-16 22:17:11 +00:00
// unordered_unique_test(set, movable2);
unordered_set_test(set, movable2);
unordered_movable_test(set, movable2, movable2, hash, equal_to);
}
2011-08-16 22:17:11 +00:00
UNORDERED_AUTO_TEST (movable2_tests) {
2011-08-16 22:17:11 +00:00
#ifdef BOOST_UNORDERED_FOA_TESTS
movable2_tests_impl<boost::unordered_flat_set>();
movable2_tests_impl<boost::unordered_node_set>();
#else
movable2_tests_impl<boost::unordered_set>();
movable2_tests_impl<boost::unordered_multiset>();
2022-10-11 15:20:37 -07:00
#endif
}
template <template <class T, class H = boost::hash<T>,
class P = std::equal_to<T>, class A = std::allocator<T> >
class Set>
static void destructible_tests_impl()
{
test::minimal::constructor_param x;
test::minimal::destructible destructible(x);
test::minimal::hash<test::minimal::destructible> hash(x);
test::minimal::equal_to<test::minimal::destructible> equal_to(x);
Set<test::minimal::destructible,
2022-10-11 15:20:37 -07:00
test::minimal::hash<test::minimal::destructible>,
test::minimal::equal_to<test::minimal::destructible> >
set;
unordered_destructible_test(set);
}
UNORDERED_AUTO_TEST (destructible_tests) {
#ifdef BOOST_UNORDERED_FOA_TESTS
destructible_tests_impl<boost::unordered_flat_set>();
destructible_tests_impl<boost::unordered_node_set>();
#else
destructible_tests_impl<boost::unordered_set>();
destructible_tests_impl<boost::unordered_multiset>();
2022-10-11 15:20:37 -07:00
#endif
}
2017-04-20 22:59:00 +01:00
// Test for ambiguity when using key convertible from iterator
// See LWG2059
struct lwg2059_key
{
int value;
2017-04-20 22:59:00 +01:00
template <typename T> lwg2059_key(T v) : value(v) {}
2017-04-20 22:59:00 +01:00
};
std::size_t hash_value(lwg2059_key x)
{
return static_cast<std::size_t>(x.value);
2017-04-20 22:59:00 +01:00
}
bool operator==(lwg2059_key x, lwg2059_key y) { return x.value == y.value; }
2017-10-09 12:34:47 +01:00
UNORDERED_AUTO_TEST (lwg2059) {
2022-10-11 15:20:37 -07:00
#ifdef BOOST_UNORDERED_FOA_TESTS
{
boost::unordered_flat_set<lwg2059_key> x;
x.emplace(lwg2059_key(10));
x.erase(x.begin());
}
{
boost::unordered_node_set<lwg2059_key> x;
x.emplace(lwg2059_key(10));
x.erase(x.begin());
}
2022-10-11 15:20:37 -07:00
#else
{
boost::unordered_set<lwg2059_key> x;
x.emplace(lwg2059_key(10));
x.erase(x.begin());
}
2017-04-20 22:59:00 +01:00
{
boost::unordered_multiset<lwg2059_key> x;
x.emplace(lwg2059_key(10));
x.erase(x.begin());
}
2022-10-11 15:20:37 -07:00
#endif
2017-04-20 22:59:00 +01:00
}
2008-03-24 17:03:15 +00:00
RUN_TESTS()