Flesh out deduction_tests to include unordered_[multi]set, update to use BOOST_TEST_TRAIT_SAME

This commit is contained in:
Christian Mazakas
2022-11-07 10:26:30 -08:00
parent 867e60113b
commit 2949b37490

View File

@ -3,11 +3,14 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying // 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) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/unordered_map.hpp> #include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
struct hash_equals struct hash_equals
{ {
template <typename T> bool operator()(T const& x) const template <typename T> bool operator()(T const& x) const
@ -33,22 +36,15 @@ template <typename T> struct test_allocator
bool operator==(test_allocator const&) const { return true; } bool operator==(test_allocator const&) const { return true; }
bool operator!=(test_allocator const&) const { return false; } bool operator!=(test_allocator const&) const { return false; }
}; };
#endif
int main() template <template <class...> class UnorderedMap> void map_tests()
{ {
std::cout << "BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES: "
<< BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES << std::endl;
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
std::vector<std::pair<int, int> > x; std::vector<std::pair<int, int> > x;
x.push_back(std::make_pair(1, 3)); x.push_back(std::make_pair(1, 3));
x.push_back(std::make_pair(5, 10)); x.push_back(std::make_pair(5, 10));
test_allocator<std::pair<const int, int> > pair_allocator; test_allocator<std::pair<const int, int> > pair_allocator;
hash_equals f; hash_equals f;
// unordered_map
/* /*
template<class InputIterator, template<class InputIterator,
class Hash = hash<iter_key_t<InputIterator>>, class Hash = hash<iter_key_t<InputIterator>>,
@ -63,32 +59,28 @@ int main()
*/ */
{ {
boost::unordered_map m(x.begin(), x.end()); UnorderedMap m(x.begin(), x.end());
static_assert( BOOST_TEST_TRAIT_SAME(decltype(m), UnorderedMap<int, int>);
std::is_same<decltype(m), boost::unordered_map<int, int> >::value);
}
/* Ambiguous:
{
boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>>>::value);
} }
{ {
boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>(), UnorderedMap m(x.begin(), x.end(), 0, std::hash<int>());
std::equal_to<int>()); BOOST_TEST_TRAIT_SAME(decltype(m), UnorderedMap<int, int, std::hash<int> >);
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>, std::equal_to<int>>>::value);
} }
*/
{ {
boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>(), UnorderedMap m(
x.begin(), x.end(), 0, std::hash<int>(), std::equal_to<int>());
BOOST_TEST_TRAIT_SAME(
decltype(m), UnorderedMap<int, int, std::hash<int>, std::equal_to<int> >);
}
{
UnorderedMap m(x.begin(), x.end(), 0, std::hash<int>(),
std::equal_to<int>(), pair_allocator); std::equal_to<int>(), pair_allocator);
static_assert(std::is_same<decltype(m), BOOST_TEST_TRAIT_SAME(
boost::unordered_map<int, int, std::hash<int>, std::equal_to<int>, decltype(m), UnorderedMap<int, int, std::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value); test_allocator<std::pair<const int, int> > >);
} }
/* /*
@ -102,33 +94,51 @@ int main()
*/ */
{ {
boost::unordered_map m({std::pair<int const, int>(1, 2)}); UnorderedMap m({std::pair<int const, int>(1, 2)});
static_assert( BOOST_TEST_TRAIT_SAME(decltype(m), UnorderedMap<int, int>);
std::is_same<decltype(m), boost::unordered_map<int, int> >::value);
}
/* Ambiguous
{
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0,
std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>>>::value);
} }
{ {
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0, UnorderedMap m({std::pair<int, int>(1, 2)});
std::hash<int>(), std::equal_to<int>()); BOOST_TEST_TRAIT_SAME(decltype(m), UnorderedMap<int, int>);
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>, std::equal_to<int>>>::value);
} }
*/
{ {
boost::unordered_map m( UnorderedMap m({std::pair<int const, int>(1, 2)}, 0, std::hash<int>());
{std::pair<int const, int>(1, 2)}, 0, f, f, pair_allocator); BOOST_TEST_TRAIT_SAME(decltype(m), UnorderedMap<int, int, std::hash<int> >);
static_assert(std::is_same<decltype(m), }
boost::unordered_map<int, int, hash_equals, hash_equals,
test_allocator<std::pair<const int, int> > > >::value); {
UnorderedMap m({std::pair<int, int>(1, 2)}, 0, std::hash<int>());
BOOST_TEST_TRAIT_SAME(decltype(m), UnorderedMap<int, int, std::hash<int> >);
}
{
UnorderedMap m({std::pair<int const, int>(1, 2)}, 0, std::hash<int>(),
std::equal_to<int>());
BOOST_TEST_TRAIT_SAME(
decltype(m), UnorderedMap<int, int, std::hash<int>, std::equal_to<int> >);
}
{
UnorderedMap m(
{std::pair<int, int>(1, 2)}, 0, std::hash<int>(), std::equal_to<int>());
BOOST_TEST_TRAIT_SAME(
decltype(m), UnorderedMap<int, int, std::hash<int>, std::equal_to<int> >);
}
{
UnorderedMap m({std::pair<int const, int>(1, 2)}, 0, f, f, pair_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(m), UnorderedMap<int, int, hash_equals, hash_equals,
test_allocator<std::pair<const int, int> > >);
}
{
UnorderedMap m({std::pair<int, int>(1, 2)}, 0, f, f, pair_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(m), UnorderedMap<int, int, hash_equals, hash_equals,
test_allocator<std::pair<const int, int> > >);
} }
/* /*
@ -141,14 +151,12 @@ int main()
Allocator>; Allocator>;
*/ */
/* Ambiguous
{ {
boost::unordered_map m(x.begin(), x.end(), 0u, pair_allocator); UnorderedMap m(x.begin(), x.end(), 0u, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_map<int, int, BOOST_TEST_TRAIT_SAME(
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int, decltype(m), UnorderedMap<int, int, boost::hash<int>, std::equal_to<int>,
int>>>>::value); test_allocator<std::pair<const int, int> > >);
} }
*/
/* /*
template<class InputIterator, class Allocator> template<class InputIterator, class Allocator>
@ -159,14 +167,12 @@ int main()
Allocator>; Allocator>;
*/ */
/* No constructor:
{ {
boost::unordered_map m(x.begin(), x.end(), pair_allocator); UnorderedMap m(x.begin(), x.end(), pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_map<int, int, BOOST_TEST_TRAIT_SAME(
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int, decltype(m), UnorderedMap<int, int, boost::hash<int>, std::equal_to<int>,
int>>>>::value); test_allocator<std::pair<const int, int> > >);
} }
*/
/* /*
template<class InputIterator, class Hash, class Allocator> template<class InputIterator, class Hash, class Allocator>
@ -177,14 +183,12 @@ int main()
equal_to<iter_key_t<InputIterator>>, Allocator>; equal_to<iter_key_t<InputIterator>>, Allocator>;
*/ */
/* Ambiguous
{ {
boost::unordered_map m(x.begin(), x.end(), 0u, f, pair_allocator); UnorderedMap m(x.begin(), x.end(), 0u, f, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_map<int, int, BOOST_TEST_TRAIT_SAME(
hash_equals, std::equal_to<int>, test_allocator<std::pair<const int, decltype(m), UnorderedMap<int, int, hash_equals, std::equal_to<int>,
int>>>>::value); test_allocator<std::pair<const int, int> > >);
} }
*/
/* /*
template<class Key, class T, typename Allocator> template<class Key, class T, typename Allocator>
@ -194,14 +198,19 @@ int main()
-> unordered_map<Key, T, hash<Key>, equal_to<Key>, Allocator>; -> unordered_map<Key, T, hash<Key>, equal_to<Key>, Allocator>;
*/ */
/* Ambiguous
{ {
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0, pair_allocator); UnorderedMap m({std::pair<int const, int>(1, 2)}, 0, pair_allocator);
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int, BOOST_TEST_TRAIT_SAME(
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int, decltype(m), UnorderedMap<int, int, boost::hash<int>, std::equal_to<int>,
int>>>>::value); test_allocator<std::pair<const int, int> > >);
}
{
UnorderedMap m({std::pair<int, int>(1, 2)}, 0, pair_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(m), UnorderedMap<int, int, boost::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > >);
} }
*/
/* /*
template<class Key, class T, typename Allocator> template<class Key, class T, typename Allocator>
@ -210,10 +219,17 @@ int main()
*/ */
{ {
boost::unordered_map m({std::pair<int const, int>(1, 2)}, pair_allocator); UnorderedMap m({std::pair<int const, int>(1, 2)}, pair_allocator);
static_assert(std::is_same<decltype(m), BOOST_TEST_TRAIT_SAME(
boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>, decltype(m), UnorderedMap<int, int, boost::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value); test_allocator<std::pair<const int, int> > >);
}
{
UnorderedMap m({std::pair<int, int>(1, 2)}, pair_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(m), UnorderedMap<int, int, boost::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > >);
} }
/* /*
@ -224,129 +240,168 @@ int main()
-> unordered_map<Key, T, Hash, equal_to<Key>, Allocator>; -> unordered_map<Key, T, Hash, equal_to<Key>, Allocator>;
*/ */
/* Ambiguous
{ {
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0, f, UnorderedMap m({std::pair<int const, int>(1, 2)}, 0, f, pair_allocator);
pair_allocator); BOOST_TEST_TRAIT_SAME(
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int, decltype(m), UnorderedMap<int, int, hash_equals, std::equal_to<int>,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int, test_allocator<std::pair<const int, int> > >);
int>>>>::value);
}
*/
// unordered_multimap
{
boost::unordered_multimap m(x.begin(), x.end());
static_assert(
std::is_same<decltype(m), boost::unordered_multimap<int, int> >::value);
}
/* Ambiguous:
{
boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
std::hash<int>>>::value);
} }
{ {
boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>(), UnorderedMap m({std::pair<int, int>(1, 2)}, 0, f, pair_allocator);
std::equal_to<int>()); BOOST_TEST_TRAIT_SAME(
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int, decltype(m), UnorderedMap<int, int, hash_equals, std::equal_to<int>,
std::hash<int>, std::equal_to<int>>>::value); test_allocator<std::pair<const int, int> > >);
}
}
template <template <class...> class UnorderedSet> void set_tests()
{
std::vector<int> y;
y.push_back(1);
y.push_back(2);
hash_equals f;
test_allocator<int> int_allocator;
/* template<class InputIt,
class Hash = std::hash<typename
std::iterator_traits<InputIt>::value_type>, class Pred =
std::equal_to<typename std::iterator_traits<InputIt>::value_type>, class
Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
unordered_set(InputIt, InputIt,
typename see below ::size_type = see below,
Hash = Hash(), Pred = Pred(), Alloc = Alloc())
-> unordered_set<typename std::iterator_traits<InputIt>::value_type, Hash,
Pred, Alloc>; */
{
UnorderedSet s(y.begin(), y.end());
BOOST_TEST_TRAIT_SAME(decltype(s), UnorderedSet<int>);
}
{
UnorderedSet s(y.begin(), y.end(), 0, std::hash<int>());
BOOST_TEST_TRAIT_SAME(decltype(s), UnorderedSet<int, std::hash<int> >);
}
{
UnorderedSet s(
y.begin(), y.end(), 0, std::hash<int>(), std::equal_to<int>());
BOOST_TEST_TRAIT_SAME(
decltype(s), UnorderedSet<int, std::hash<int>, std::equal_to<int> >);
}
{
UnorderedSet s(y.begin(), y.end(), 0, std::hash<int>(),
std::equal_to<int>(), int_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(s), UnorderedSet<int, std::hash<int>, std::equal_to<int>,
test_allocator<int> >);
}
/* template<class T,
class Hash = std::hash<T>,
class Pred = std::equal_to<T>,
class Alloc = std::allocator<T>>
unordered_set(std::initializer_list<T>,
typename see below::size_type = see below,
Hash = Hash(), Pred = Pred(), Alloc = Alloc())
-> unordered_set<T, Hash, Pred, Alloc>; */
{
UnorderedSet s({1, 2});
BOOST_TEST_TRAIT_SAME(decltype(s), UnorderedSet<int>);
}
{
UnorderedSet s({1, 2}, 0, std::hash<int>());
BOOST_TEST_TRAIT_SAME(decltype(s), UnorderedSet<int, std::hash<int> >);
}
{
UnorderedSet s({1, 2}, 0, std::hash<int>(), std::equal_to<int>());
BOOST_TEST_TRAIT_SAME(
decltype(s), UnorderedSet<int, std::hash<int>, std::equal_to<int> >);
}
{
UnorderedSet s(
{1, 2}, 0, std::hash<int>(), std::equal_to<int>(), int_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(s), UnorderedSet<int, std::hash<int>, std::equal_to<int>,
test_allocator<int> >);
}
{
UnorderedSet s({1, 2}, 0, f, f, int_allocator);
BOOST_TEST_TRAIT_SAME(decltype(s),
UnorderedSet<int, hash_equals, hash_equals, test_allocator<int> >);
}
/* template<class InputIt, class Alloc>
unordered_set(InputIt, InputIt, typename see below::size_type, Alloc)
-> unordered_set<typename std::iterator_traits<InputIt>::value_type,
std::hash<typename std::iterator_traits<InputIt>::value_type>,
std::equal_to<typename
std::iterator_traits<InputIt>::value_type>, Alloc>; */
{
UnorderedSet s(y.begin(), y.end(), 0u, int_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(s), UnorderedSet<int, boost::hash<int>, std::equal_to<int>,
test_allocator<int> >);
}
/* template<class InputIt, class Hash, class Alloc>
unordered_set(InputIt, InputIt, typename see below::size_type, Hash, Alloc)
-> unordered_set<typename std::iterator_traits<InputIt>::value_type, Hash,
std::equal_to<typename
std::iterator_traits<InputIt>::value_type>, Allocator>; */
{
UnorderedSet s(y.begin(), y.end(), 0u, f, int_allocator);
BOOST_TEST_TRAIT_SAME(decltype(s),
UnorderedSet<int, hash_equals, std::equal_to<int>, test_allocator<int> >);
}
/* template<class T, class Allocator>
unordered_set(std::initializer_list<T>, typename see below::size_type,
Allocator)
-> unordered_set<T, std::hash<T>, std::equal_to<T>, Alloc>; */
{
UnorderedSet s({1, 2}, 0u, int_allocator);
BOOST_TEST_TRAIT_SAME(
decltype(s), UnorderedSet<int, boost::hash<int>, std::equal_to<int>,
test_allocator<int> >);
}
/* template<class T, class Hash, class Alloc>
unordered_set(std::initializer_list<T>, typename see below::size_type, Hash,
Alloc)
-> unordered_set<T, Hash, std::equal_to<T>, Alloc>; */
{
UnorderedSet s({1, 2}, 0u, f, int_allocator);
BOOST_TEST_TRAIT_SAME(decltype(s),
UnorderedSet<int, hash_equals, std::equal_to<int>, test_allocator<int> >);
} }
*/
{
boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>(),
std::equal_to<int>(), pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_multimap<int, int, std::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value);
}
{
boost::unordered_multimap m({std::pair<int const, int>(1, 2)});
static_assert(
std::is_same<decltype(m), boost::unordered_multimap<int, int> >::value);
}
/* Ambiguous
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
std::hash<int>>>::value);
}
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
std::hash<int>(), std::equal_to<int>());
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
std::hash<int>, std::equal_to<int>>>::value);
}
*/
{
boost::unordered_multimap m(
{std::pair<int const, int>(1, 2)}, 0, f, f, pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_multimap<int, int, hash_equals, hash_equals,
test_allocator<std::pair<const int, int> > > >::value);
}
/* Ambiguous
{
boost::unordered_multimap m(x.begin(), x.end(), 0u, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/* No constructor:
{
boost::unordered_multimap m(x.begin(), x.end(), pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/* Ambiguous
{
boost::unordered_multimap m(x.begin(), x.end(), 0u, f, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
hash_equals, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
pair_allocator);
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
{
boost::unordered_multimap m(
{std::pair<int const, int>(1, 2)}, pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value);
}
/* Ambiguous
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0, f,
pair_allocator);
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
} }
*/
#endif #endif
int main()
{
std::cout << "BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES: "
<< BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES << std::endl;
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
map_tests<boost::unordered_map>();
map_tests<boost::unordered_multimap>();
set_tests<boost::unordered_set>();
set_tests<boost::unordered_multiset>();
return boost::report_errors();
#endif
} }