Unnamed: 0
int64
0
0
repo_id
stringlengths
5
186
file_path
stringlengths
15
223
content
stringlengths
1
32.8M
0
repos/unordered/test
repos/unordered/test/unordered/rehash_tests.cpp
// 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) #include "../helpers/unordered.hpp" #include "../helpers/metafunctions.hpp" #include "../helpers/random_values.hpp" #include "../helpers/test.hpp" #include "../helpers/tracker.hpp" #include "../objects/test.hpp" namespace rehash_tests { test::seed_t initialize_seed(2974); static int count_allocations; template <class T> struct monotonic_allocator { typedef T value_type; monotonic_allocator() {} monotonic_allocator(monotonic_allocator const&) {} template <class U> monotonic_allocator(monotonic_allocator<U> const&) {} friend bool operator==( monotonic_allocator const&, monotonic_allocator const&) { return true; } friend bool operator!=( monotonic_allocator const&, monotonic_allocator const&) { return false; } T* allocate(std::size_t n) { ++count_allocations; return static_cast<T*>(::operator new(sizeof(T) * n)); } void deallocate(T* p, std::size_t) { ::operator delete(p); } }; void reset_counts() { count_allocations = 0; } template <class X> bool postcondition(X const& x, typename X::size_type n) { return static_cast<double>(x.bucket_count()) >= static_cast<double>(x.size()) / x.max_load_factor() && x.bucket_count() >= n; } template <class X> void rehash_empty_test1(X*) { X x; x.rehash(10000); BOOST_TEST(postcondition(x, 10000)); x.rehash(0); BOOST_TEST(postcondition(x, 0)); x.rehash(10000000); BOOST_TEST(postcondition(x, 10000000)); } template <class X> void rehash_empty_test2(X*, test::random_generator generator) { test::random_values<X> v(1000, generator); test::ordered<X> tracker; X x; x.rehash(10000); BOOST_TEST(postcondition(x, 10000)); tracker.insert_range(v.begin(), v.end()); x.insert(v.begin(), v.end()); tracker.compare(x); BOOST_TEST(postcondition(x, 10000)); x.rehash(10000000); tracker.compare(x); BOOST_TEST(postcondition(x, 10000000)); } template <class X> void rehash_empty_test3(X*, test::random_generator generator) { test::random_values<X> v(1000, generator); test::ordered<X> tracker; X x; x.rehash(0); BOOST_TEST(postcondition(x, 0)); tracker.insert_range(v.begin(), v.end()); x.insert(v.begin(), v.end()); tracker.compare(x); BOOST_TEST(postcondition(x, 0)); } template <class X> void rehash_empty_tracking(X*, test::random_generator) { // valid for all load factors float const max_load_factors[] = { 0.5f, 1.0f, 1e6f, std::numeric_limits<float>::infinity()}; std::size_t const max_load_factors_len = sizeof(max_load_factors) / sizeof(*max_load_factors); for (std::size_t i = 0; i < max_load_factors_len; ++i) { X x; BOOST_TEST_EQ(x.size(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); x.max_load_factor(max_load_factors[i]); { BOOST_TEST_EQ(x.bucket_count(), 0u); x.rehash(0); BOOST_TEST_EQ(x.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); } { BOOST_TEST_EQ(x.bucket_count(), 0u); x.rehash(1000); BOOST_TEST_GE(x.bucket_count(), 1000u); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); x.rehash(0); BOOST_TEST_EQ(x.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); } { BOOST_TEST_EQ(x.bucket_count(), 0u); x.rehash(1000); BOOST_TEST_GE(x.bucket_count(), 1000u); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); x.rehash(10); BOOST_TEST_GE(x.bucket_count(), 10u); BOOST_TEST_LT(x.bucket_count(), 1000u); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); } { BOOST_TEST_GT(x.bucket_count(), 0u); BOOST_TEST_LT(x.bucket_count(), 1000u); x.rehash(1000); BOOST_TEST_GE(x.bucket_count(), 1000u); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); x.rehash(0); BOOST_TEST_EQ(x.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); } } for (std::size_t i = 0; i < max_load_factors_len; ++i) { typedef typename X::size_type size_type; X x; BOOST_TEST_EQ(x.size(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); #ifdef BOOST_UNORDERED_FOA_TESTS float const mlf = boost::unordered::detail::foa::mlf; x.max_load_factor(max_load_factors[i]); #else float const mlf = max_load_factors[i]; x.max_load_factor(mlf); #endif { BOOST_TEST_EQ(x.bucket_count(), 0u); x.reserve(0); BOOST_TEST_EQ(x.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); } { BOOST_TEST_EQ(x.bucket_count(), 0u); x.reserve(1000); BOOST_TEST_GE( x.bucket_count(), static_cast<size_type>(std::ceil(1000 / mlf))); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); x.reserve(0); BOOST_TEST_EQ(x.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); } { BOOST_TEST_EQ(x.bucket_count(), 0u); x.reserve(1000); BOOST_TEST_GE( x.bucket_count(), static_cast<size_type>(std::ceil(1000 / mlf))); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); x.reserve(10); BOOST_TEST_GE( x.bucket_count(), static_cast<size_type>(std::ceil(10 / mlf))); BOOST_TEST_LT(x.bucket_count(), 1000u); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); } { BOOST_TEST_GT(x.bucket_count(), 0u); BOOST_TEST_LT(x.bucket_count(), 1000u); x.reserve(1000); BOOST_TEST_GE( x.bucket_count(), static_cast<size_type>(std::ceil(1000 / mlf))); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); x.reserve(0); BOOST_TEST_EQ(x.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, 0u); } } } template <class X> void rehash_nonempty_tracking(X*, test::random_generator generator) { test::random_values<X> const v(1000, generator); typedef typename X::size_type size_type; float const max_load_factors[] = {0.5f, 1.0f, 1e2f}; size_type const max_load_factors_len = sizeof(max_load_factors) / sizeof(*max_load_factors); for (size_type i = 0; i < max_load_factors_len; ++i) { float const mlf = max_load_factors[i]; X x(v.begin(), v.end()); BOOST_TEST_GT(x.size(), 0u); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); x.max_load_factor(mlf); size_type bucket_count = x.bucket_count(); { BOOST_TEST_GT(x.bucket_count(), 0u); x.rehash(0); BOOST_TEST_GE(x.bucket_count(), static_cast<size_type>( std::floor(static_cast<float>(x.size()) / x.max_load_factor()))); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); bucket_count = x.bucket_count(); } { BOOST_TEST_GT(bucket_count, 0u); x.rehash(2 * x.bucket_count()); BOOST_TEST_GT(x.bucket_count(), bucket_count); bucket_count = x.bucket_count(); } { float const old_mlf = x.max_load_factor(); BOOST_TEST_GT(bucket_count, 0u); x.rehash(bucket_count / 4); BOOST_TEST_LT(x.bucket_count(), bucket_count); x.max_load_factor(std::numeric_limits<float>::infinity()); x.rehash(0); BOOST_TEST_GT(x.bucket_count(), 0u); x.max_load_factor(old_mlf); } { std::size_t const max_load = static_cast<std::size_t>(static_cast<double>(x.max_load_factor()) * static_cast<double>(x.bucket_count())); while (x.size() < max_load) { test::random_values<X> const t(max_load, generator); typename test::random_values<X>::const_iterator pos = t.begin(); typename test::random_values<X>::const_iterator end = t.end(); for (; pos != end; ++pos) { x.insert(*pos); if (x.size() == max_load) { break; } } } while (x.size() > max_load) { x.erase(x.begin()); } BOOST_TEST_EQ(x.size(), max_load); bucket_count = x.bucket_count(); x.rehash(x.bucket_count()); BOOST_TEST_EQ(x.bucket_count(), bucket_count); } } for (size_type i = 0; i < max_load_factors_len; ++i) { X x(v.begin(), v.end()); BOOST_TEST_GT(x.size(), 0u); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); float const mlf = max_load_factors[i]; x.max_load_factor(mlf); size_type bucket_count = x.bucket_count(); { BOOST_TEST_GT(x.bucket_count(), 0u); x.reserve(0); BOOST_TEST_GE(x.bucket_count(), static_cast<size_type>( std::floor(static_cast<float>(x.size()) / x.max_load_factor()))); BOOST_TEST_GT(test::detail::tracker.count_allocations, 0u); bucket_count = x.bucket_count(); } { BOOST_TEST_GT(x.bucket_count(), 0u); x.reserve( 2 * (static_cast<size_type>( std::floor(static_cast<float>(x.size()) / x.max_load_factor()) + std::floor(static_cast<float>(x.size()) * x.max_load_factor())))); BOOST_TEST_GT(x.bucket_count(), bucket_count); bucket_count = x.bucket_count(); BOOST_TEST_GT(bucket_count, 1u); } { float const old_mlf = x.max_load_factor(); BOOST_TEST_GT(bucket_count, 4u); x.reserve(bucket_count / 4); BOOST_TEST_LT(x.bucket_count(), bucket_count); x.max_load_factor(std::numeric_limits<float>::infinity()); x.reserve(0); BOOST_TEST_GT(x.bucket_count(), 0u); x.max_load_factor(old_mlf); } { std::size_t const max_load = static_cast<std::size_t>(static_cast<double>(x.max_load_factor()) * static_cast<double>(x.bucket_count())); while (x.size() < max_load) { test::random_values<X> const t(max_load, generator); typename test::random_values<X>::const_iterator pos = t.begin(); typename test::random_values<X>::const_iterator end = t.end(); for (; pos != end; ++pos) { x.insert(*pos); if (x.size() == max_load) { break; } } } while (x.size() > max_load) { x.erase(x.begin()); } BOOST_TEST_EQ(x.size(), max_load); bucket_count = x.bucket_count(); x.reserve(x.size()); BOOST_TEST_EQ(x.bucket_count(), bucket_count); } } } template <class X> void rehash_stability(X*, test::random_generator generator) { reset_counts(); typedef typename X::size_type size_type; size_type bucket_count = 100; X x(bucket_count); size_type num_elems = x.bucket_count() - 1; test::random_values<X> v(num_elems, generator); test::ordered<X> tracker; tracker.insert_range(v.begin(), v.end()); typename test::random_values<X>::iterator pos = v.begin(); for (size_type i = 0; i < num_elems; ++i) { x.insert(*pos); ++pos; } int const old_count = count_allocations; x.rehash(0); BOOST_TEST_EQ(count_allocations, old_count); tracker.compare(x); } template <class X> void rehash_node_stability(X*, test::random_generator generator) { typedef typename X::value_type value_type; std::set<value_type const*> elements; test::random_values<X> v(1000, generator); test::ordered<X> tracker; tracker.insert_range(v.begin(), v.end()); X x(v.begin(), v.end()); typedef typename X::iterator iterator; for (iterator pos = x.begin(); pos != x.end(); ++pos) { elements.insert(std::addressof(*pos)); } x.rehash(2 * x.bucket_count()); for (iterator pos = x.begin(); pos != x.end(); ++pos) { if (!BOOST_TEST( elements.find(std::addressof(*pos)) != elements.end())) { break; } } tracker.compare(x); } template <class X> void rehash_test1(X*, test::random_generator generator) { test::random_values<X> v(1000, generator); test::ordered<X> tracker; tracker.insert_range(v.begin(), v.end()); X x(v.begin(), v.end()); x.rehash(0); BOOST_TEST(postcondition(x, 0)); tracker.compare(x); x.max_load_factor(0.25); x.rehash(0); BOOST_TEST(postcondition(x, 0)); tracker.compare(x); x.max_load_factor(50.0); x.rehash(0); BOOST_TEST(postcondition(x, 0)); tracker.compare(x); x.rehash(1000); BOOST_TEST(postcondition(x, 1000)); tracker.compare(x); } template <class X> void reserve_empty_test1(X*) { X x; x.reserve(10000); BOOST_TEST(x.bucket_count() >= 10000); x.reserve(0); x.reserve(10000000); BOOST_TEST(x.bucket_count() >= 10000000); } template <class X> void reserve_empty_test2(X*) { X x; x.max_load_factor(0.25); #ifdef BOOST_UNORDERED_FOA_TESTS x.reserve(10000); BOOST_TEST(x.bucket_count() >= 10000); x.reserve(0); x.reserve(10000000); BOOST_TEST(x.bucket_count() >= 10000000); #else x.reserve(10000); BOOST_TEST(x.bucket_count() >= 40000); x.reserve(0); x.reserve(10000000); BOOST_TEST(x.bucket_count() >= 40000000); #endif } template <class X> void reserve_test1(X*, test::random_generator generator) { for (int random_mlf = 0; random_mlf < 2; ++random_mlf) { for (std::size_t i = 1; i < 2000; i += i < 50 ? 1 : 13) { test::random_values<X> v(i, generator); test::ordered<X> tracker; tracker.insert_range(v.begin(), v.end()); X x; x.max_load_factor( random_mlf ? static_cast<float>(std::rand() % 1000) / 500.0f + 0.5f : 1.0f); x.reserve(test::has_unique_keys<X>::value ? i : v.size()); // Insert an element before the range insert, otherwise there are // no iterators to invalidate in the range insert, and it can // rehash. typename test::random_values<X>::iterator it = v.begin(); x.insert(*it); ++it; std::size_t bucket_count = x.bucket_count(); x.insert(it, v.end()); BOOST_TEST(bucket_count == x.bucket_count()); tracker.compare(x); } } } template <class X> void reserve_test2(X*, test::random_generator generator) { for (int random_mlf = 0; random_mlf < 2; ++random_mlf) { for (std::size_t i = 0; i < 2000; i += i < 50 ? 1 : 13) { test::random_values<X> v(i, generator); test::ordered<X> tracker; tracker.insert_range(v.begin(), v.end()); X x; x.max_load_factor( random_mlf ? static_cast<float>(std::rand() % 1000) / 500.0f + 0.5f : 1.0f); x.reserve(test::has_unique_keys<X>::value ? i : v.size()); std::size_t bucket_count = x.bucket_count(); for (typename test::random_values<X>::iterator it = v.begin(); it != v.end(); ++it) { x.insert(*it); } BOOST_TEST(bucket_count == x.bucket_count()); tracker.compare(x); } } } using test::default_generator; using test::generate_collisions; using test::limited_range; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_set<int>* int_set_ptr; boost::unordered_flat_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_map_ptr; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set_tracking; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_map_tracking; boost::unordered_flat_set<test::object, test::hash, test::equal_to, monotonic_allocator<test::object> >* test_set_monotonic; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, monotonic_allocator<std::pair<test::object const, test::object> > >* test_map_monotonic; boost::unordered_node_set<int>* int_node_set_ptr; boost::unordered_node_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_node_map_ptr; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_node_set_tracking; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_node_map_tracking; boost::unordered_node_set<test::object, test::hash, test::equal_to, monotonic_allocator<test::object> >* test_node_set_monotonic; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, monotonic_allocator<std::pair<test::object const, test::object> > >* test_node_map_monotonic; // clang-format off UNORDERED_TEST(rehash_empty_test1, ((int_set_ptr)(test_map_ptr) (int_node_set_ptr)(test_node_map_ptr))) UNORDERED_TEST(rehash_empty_test2, ((int_set_ptr)(test_map_ptr) (int_node_set_ptr)(test_node_map_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_empty_test3, ((int_set_ptr)(test_map_ptr) (int_node_set_ptr)(test_node_map_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_test1, ((int_set_ptr)(test_map_ptr) (int_node_set_ptr)(test_node_map_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(reserve_empty_test1, ((int_set_ptr)(test_map_ptr)(int_node_set_ptr)(test_node_map_ptr))) UNORDERED_TEST(reserve_empty_test2, ((int_set_ptr)(test_map_ptr)(int_node_set_ptr)(test_node_map_ptr))) UNORDERED_TEST(reserve_test1, ((int_set_ptr)(test_map_ptr)(int_node_set_ptr)(test_node_map_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(reserve_test2, ((int_set_ptr)(test_map_ptr)(int_node_set_ptr)(test_node_map_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_empty_tracking, ((test_set_tracking)(test_map_tracking) (test_node_set_tracking)(test_node_map_tracking))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_nonempty_tracking, ((test_set_tracking)(test_map_tracking) (test_node_set_tracking)(test_node_map_tracking))( (default_generator)(limited_range))) UNORDERED_TEST(rehash_stability, ((test_set_monotonic)(test_map_monotonic) (test_node_set_monotonic)(test_node_map_monotonic))( (default_generator)(limited_range))) UNORDERED_TEST(rehash_node_stability, ((int_node_set_ptr)(test_node_map_ptr) (test_node_set_tracking)(test_node_map_tracking))( (default_generator)(generate_collisions)(limited_range))) // clang-format on #else boost::unordered_set<int>* int_set_ptr; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_multiset_ptr; boost::unordered_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_map_ptr; boost::unordered_multimap<int, int>* int_multimap_ptr; boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set_tracking; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_multiset_tracking; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_map_tracking; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_multimap_tracking; boost::unordered_set<test::object, test::hash, test::equal_to, monotonic_allocator<test::object> >* test_set_monotonic; boost::unordered_multiset<test::object, test::hash, test::equal_to, monotonic_allocator<test::object> >* test_multiset_monotonic; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, monotonic_allocator<std::pair<test::object const, test::object> > >* test_map_monotonic; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, monotonic_allocator<std::pair<test::object const, test::object> > >* test_multimap_monotonic; // clang-format off UNORDERED_TEST(rehash_empty_test1, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))) UNORDERED_TEST(rehash_empty_test2, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_empty_test3, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_test1, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(reserve_empty_test1, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))) UNORDERED_TEST(reserve_empty_test2, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))) UNORDERED_TEST(reserve_test1, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(reserve_test2, ((int_set_ptr)(test_multiset_ptr)(test_map_ptr)(int_multimap_ptr))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_empty_tracking, ((test_set_tracking)(test_multiset_tracking)(test_map_tracking)(test_multimap_tracking))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_nonempty_tracking, ((test_set_tracking)(test_multiset_tracking)(test_map_tracking)(test_multimap_tracking))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(rehash_stability, ((test_set_monotonic)(test_multiset_monotonic)(test_map_monotonic)(test_multimap_monotonic))( (default_generator)(limited_range))) UNORDERED_TEST(rehash_node_stability, ((int_set_ptr)(test_map_ptr)(test_set_tracking)(test_map_tracking) (test_multiset_ptr)(int_multimap_ptr) (test_multiset_tracking)(test_multimap_tracking))( (default_generator)(generate_collisions)(limited_range))) // clang-format on #endif } // namespace rehash_tests RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/uses_allocator.cpp
// 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/test.hpp" #include <boost/unordered/detail/implementation.hpp> #include <boost/unordered/unordered_flat_map.hpp> #include <boost/unordered/unordered_flat_set.hpp> #include <boost/unordered/unordered_node_map.hpp> #include <boost/unordered/unordered_node_set.hpp> #include <boost/config.hpp> #include <boost/config/pragma_message.hpp> #include <boost/config/workaround.hpp> #if BOOST_CXX_VERSION <= 199711L || \ BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800) || \ (defined(BOOST_LIBSTDCXX_VERSION) && BOOST_CXX_VERSION > 201703L) || \ (defined(BOOST_MSVC_FULL_VER) && BOOST_MSVC_FULL_VER >= 192000000 && \ BOOST_MSVC_FULL_VER < 193000000) // automatically disable this test for C++03 builds so we can use the STL's // scoped_allocator_adaptor // we remove C++20 support for libstdc++ builds because of: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108952 // // msvc-14.2 w/ C++20 is similarly affected // BOOST_PRAGMA_MESSAGE("uses_allocator tests require C++11, scoped_allocator") int main() {} #else #include <memory> #include <scoped_allocator> #include <unordered_map> #include <vector> template <class T> struct allocator { typedef T value_type; int tag_ = -1; allocator() = default; allocator(int tag) : tag_{tag} {} allocator(allocator const&) = default; allocator(allocator&&) = default; template <class U> allocator(allocator<U> const& rhs) : tag_{rhs.tag_} {} BOOST_ATTRIBUTE_NODISCARD T* allocate(std::size_t n) { return static_cast<T*>(::operator new(n * sizeof(T))); } void deallocate(T* p, std::size_t) noexcept { ::operator delete(p); } allocator& operator=(allocator const& rhs) { tag_ = rhs.tag_; return *this; } allocator& operator=(allocator&& rhs) noexcept { tag_ = rhs.tag_; return *this; } bool operator==(allocator const&) const { return true; } bool operator!=(allocator const&) const { return false; } }; struct raii_tracker { static int count; static int copy_count; static int move_count; static int alloc_move_count; using allocator_type = allocator<int>; allocator_type a_; raii_tracker(allocator_type a) : a_(a) { ++count; } raii_tracker(int, allocator_type const& a) : a_(a) { ++count; } raii_tracker(raii_tracker const&) { ++copy_count; } raii_tracker(raii_tracker&&) noexcept { ++move_count; } raii_tracker(raii_tracker&&, allocator_type const& a) noexcept : a_(a) { ++alloc_move_count; } allocator_type get_allocator() const noexcept { return a_; } friend bool operator==(raii_tracker const&, raii_tracker const&) { return true; } }; int raii_tracker::count = 0; int raii_tracker::copy_count = 0; int raii_tracker::move_count = 0; int raii_tracker::alloc_move_count = 0; static void reset_counts() { raii_tracker::count = 0; raii_tracker::copy_count = 0; raii_tracker::move_count = 0; raii_tracker::alloc_move_count = 0; } std::size_t hash_value(raii_tracker const&) { return 0; } using map_allocator_type = std::scoped_allocator_adaptor< allocator<std::pair<raii_tracker const, raii_tracker> >, allocator<int> >; using set_allocator_type = std::scoped_allocator_adaptor<allocator<raii_tracker>, allocator<int> >; using map_type = boost::unordered_flat_map<raii_tracker, raii_tracker, boost::hash<raii_tracker>, std::equal_to<raii_tracker>, map_allocator_type>; using node_map_type = boost::unordered_node_map<raii_tracker, raii_tracker, boost::hash<raii_tracker>, std::equal_to<raii_tracker>, map_allocator_type>; using set_type = boost::unordered_flat_set<raii_tracker, boost::hash<raii_tracker>, std::equal_to<raii_tracker>, set_allocator_type>; using node_set_type = boost::unordered_node_set<raii_tracker, boost::hash<raii_tracker>, std::equal_to<raii_tracker>, set_allocator_type>; map_type* flat_map; node_map_type* node_map; set_type* flat_set; node_set_type* node_set; template <class X> static void map_uses_allocator_construction(X*) { reset_counts(); map_allocator_type alloc( allocator<std::pair<raii_tracker const, raii_tracker> >{12}, allocator<int>{34}); X map(1, alloc); map.emplace( std::piecewise_construct, std::make_tuple(1337), std::make_tuple(7331)); BOOST_TEST_EQ(raii_tracker::count, 2); BOOST_TEST_EQ(raii_tracker::move_count, 0); BOOST_TEST_EQ(raii_tracker::alloc_move_count, 2); BOOST_TEST_EQ(map.begin()->first.get_allocator().tag_, 34); BOOST_TEST_EQ(map.begin()->second.get_allocator().tag_, 34); } template <class X> static void set_uses_allocator_construction(X*) { reset_counts(); set_allocator_type alloc(allocator<raii_tracker>{12}, allocator<int>{34}); X set(1, alloc); set.emplace(); BOOST_TEST_EQ(raii_tracker::count, 1); BOOST_TEST_EQ(raii_tracker::move_count, 0); BOOST_TEST_EQ(raii_tracker::alloc_move_count, 1); BOOST_TEST_EQ(set.begin()->get_allocator().tag_, 34); } UNORDERED_TEST(map_uses_allocator_construction, ((flat_map)(node_map))) UNORDERED_TEST(set_uses_allocator_construction, ((flat_set)(node_set))) RUN_TESTS() #endif
0
repos/unordered/test
repos/unordered/test/unordered/fancy_pointer_noleak.cpp
// Copyright (C) 2023 Joaquin M Lopez Munoz // 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/unordered.hpp" #include "../helpers/random_values.hpp" #include "../objects/test.hpp" #include <boost/config.hpp> #include <boost/config/workaround.hpp> #if BOOST_WORKAROUND(BOOST_MSVC, < 1910) #pragma warning(disable:4714) /* marked as __forceinline not inlined */ #endif namespace { test::seed_t initialize_seed(1002310); static std::size_t counted_pointer_count = 0; template <typename T> class counted_pointer { public: counted_pointer(T* p_ = nullptr) : p{p_} { ++counted_pointer_count; } counted_pointer(counted_pointer const& x) : p{x.p} { ++counted_pointer_count; } ~counted_pointer() { --counted_pointer_count; } counted_pointer& operator=(counted_pointer const&) = default; counted_pointer& operator=(T* p_) { p = p_; return *this; } operator T*() const noexcept { return p; } template <typename Q = T> Q& operator*() const noexcept { return *p; } T* operator->() const noexcept { return p; } counted_pointer& operator++() noexcept { ++p; return *this; } counted_pointer operator++(int) noexcept { auto x = *this; ++p; return x; } counted_pointer& operator+=(std::ptrdiff_t n) noexcept { p += n; return *this; } counted_pointer& operator-=(std::ptrdiff_t n) noexcept { p -= n; return *this; } friend bool operator==(const counted_pointer& x, const counted_pointer& y) { return x.p == y.p; } friend bool operator!=(const counted_pointer& x, const counted_pointer& y) { return x.p != y.p; } friend bool operator<(const counted_pointer& x, const counted_pointer& y) { return x.p < y.p; } friend bool operator<=(const counted_pointer& x, const counted_pointer& y) { return x.p <= y.p; } friend bool operator>(const counted_pointer& x, const counted_pointer& y) { return x.p > y.p; } friend bool operator>=(const counted_pointer& x, const counted_pointer& y) { return x.p >= y.p; } template <typename Q = T> static counted_pointer pointer_to(Q& x) noexcept { return std::addressof(x); } private: T* p; }; template <class T> struct counted_pointer_allocator { using value_type = T; using pointer = counted_pointer<T>; counted_pointer_allocator() = default; template <class U> counted_pointer_allocator(const counted_pointer_allocator<U>&) noexcept {} template <class U> bool operator==(const counted_pointer_allocator<U>&) const noexcept { return true; } template <class U> bool operator!=(const counted_pointer_allocator<U>&) const noexcept { return false; } pointer allocate(std::size_t n) const { return std::allocator<T>().allocate(n); } void deallocate(pointer p, std::size_t n) const noexcept { std::allocator<T>().deallocate(p, n); } }; template <class T> void fancy_pointer_noleak_test(T*, test::random_generator const& generator) { // https://github.com/boostorg/unordered/issues/201 auto const pointer_count = counted_pointer_count; { test::random_values<T> v(1000, generator); T x(v.begin(), v.end()); (void)x.begin(); } BOOST_TEST_EQ(pointer_count, counted_pointer_count); } using test::default_generator; using test::generate_collisions; using test::limited_range; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_set<test::object, test::hash, test::equal_to, counted_pointer_allocator<test::object> >* test_flat_set; boost::unordered_node_set<test::object, test::hash, test::equal_to, counted_pointer_allocator<test::object> >* test_node_set; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, counted_pointer_allocator< std::pair<test::object const,test::object> > >* test_flat_map; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, counted_pointer_allocator< std::pair<test::object const,test::object> > >* test_node_map; UNORDERED_TEST(fancy_pointer_noleak_test, ((test_flat_set)(test_node_set)(test_flat_map)(test_node_map)) ((default_generator))) #else boost::unordered_set<test::object, test::hash, test::equal_to, counted_pointer_allocator<test::object> >* test_set; boost::unordered_multiset<test::object, test::hash, test::equal_to, counted_pointer_allocator<test::object> >* test_multiset; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, counted_pointer_allocator< std::pair<test::object const,test::object> > >* test_map; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, counted_pointer_allocator< std::pair<test::object const,test::object> > >* test_multimap; UNORDERED_TEST(fancy_pointer_noleak_test, ((test_set)(test_multiset)(test_map)(test_multimap)) ((default_generator))) #endif } // namespace RUN_TESTS_QUIET()
0
repos/unordered/test
repos/unordered/test/unordered/incomplete_test.cpp
// Copyright 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) #include "../helpers/unordered.hpp" #include <utility> namespace x { struct D { #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_map<D, D> x; boost::unordered_node_map<D, D> y; #else boost::unordered_map<D, D> x; #endif }; } // namespace x namespace incomplete_test { // Declare, but don't define some types. struct value; struct hash; struct equals; template <class T> struct allocator; // Declare some instances #ifdef BOOST_UNORDERED_FOA_TESTS typedef boost::unordered_flat_map<value, value, hash, equals, allocator<std::pair<value const, value> > > map; typedef boost::unordered_flat_set<value, hash, equals, allocator<value> > set; typedef boost::unordered_node_map<value, value, hash, equals, allocator<std::pair<value const, value> > > multimap; typedef boost::unordered_node_set<value, hash, equals, allocator<value> > multiset; #else typedef boost::unordered_map<value, value, hash, equals, allocator<std::pair<value const, value> > > map; typedef boost::unordered_multimap<value, value, hash, equals, allocator<std::pair<value const, value> > > multimap; typedef boost::unordered_set<value, hash, equals, allocator<value> > set; typedef boost::unordered_multiset<value, hash, equals, allocator<value> > multiset; #endif // Now define the types which are stored as members, as they are needed for // declaring struct members. struct hash { template <typename T> std::size_t operator()(T const&) const { return 0; } }; struct equals { template <typename T> bool operator()(T const&, T const&) const { return true; } }; // This is a dubious way to implement an allocator, but good enough // for this test. template <typename T> struct allocator : std::allocator<T> { allocator() {} template <typename T2> allocator(const allocator<T2>& other) : std::allocator<T>(other) { } template <typename T2> allocator(const std::allocator<T2>& other) : std::allocator<T>(other) { } }; // Declare some members of a structs. // // Incomplete hash, equals and allocator aren't here supported at the // moment. #ifdef BOOST_UNORDERED_FOA_TESTS struct struct1 { boost::unordered_flat_map<struct1, struct1, hash, equals, allocator<std::pair<struct1 const, struct1> > > x; }; struct struct2 { boost::unordered_node_map<struct2, struct2, hash, equals, allocator<std::pair<struct2 const, struct2> > > x; }; struct struct3 { boost::unordered_flat_set<struct3, hash, equals, allocator<struct3> > x; }; struct struct4 { boost::unordered_node_set<struct4, hash, equals, allocator<struct4> > x; }; #else struct struct1 { boost::unordered_map<struct1, struct1, hash, equals, allocator<std::pair<struct1 const, struct1> > > x; }; struct struct2 { boost::unordered_multimap<struct2, struct2, hash, equals, allocator<std::pair<struct2 const, struct2> > > x; }; struct struct3 { boost::unordered_set<struct3, hash, equals, allocator<struct3> > x; }; struct struct4 { boost::unordered_multiset<struct4, hash, equals, allocator<struct4> > x; }; #endif // Now define the value type. struct value { }; // Create some instances. incomplete_test::map m1; incomplete_test::multimap m2; incomplete_test::set s1; incomplete_test::multiset s2; incomplete_test::struct1 c1; incomplete_test::struct2 c2; incomplete_test::struct3 c3; incomplete_test::struct4 c4; // Now declare, but don't define, the operators required for comparing // elements. std::size_t hash_value(value const&); bool operator==(value const&, value const&); std::size_t hash_value(struct1 const&); std::size_t hash_value(struct2 const&); std::size_t hash_value(struct3 const&); std::size_t hash_value(struct4 const&); bool operator==(struct1 const&, struct1 const&); bool operator==(struct2 const&, struct2 const&); bool operator==(struct3 const&, struct3 const&); bool operator==(struct4 const&, struct4 const&); // And finally use these void use_types() { incomplete_test::value x; m1[x] = x; m2.insert(std::make_pair(x, x)); s1.insert(x); s2.insert(x); c1.x.insert(std::make_pair(c1, c1)); c2.x.insert(std::make_pair(c2, c2)); c3.x.insert(c3); c4.x.insert(c4); } // And finally define the operators required for comparing elements. std::size_t hash_value(value const&) { return 0; } bool operator==(value const&, value const&) { return true; } std::size_t hash_value(struct1 const&) { return 0; } std::size_t hash_value(struct2 const&) { return 0; } std::size_t hash_value(struct3 const&) { return 0; } std::size_t hash_value(struct4 const&) { return 0; } bool operator==(struct1 const&, struct1 const&) { return true; } bool operator==(struct2 const&, struct2 const&) { return true; } bool operator==(struct3 const&, struct3 const&) { return true; } bool operator==(struct4 const&, struct4 const&) { return true; } } // namespace incomplete_test int main() { // This could just be a compile test, but I like to be able to run these // things. It's probably irrational, but I find it reassuring. incomplete_test::use_types(); }
0
repos/unordered/test
repos/unordered/test/unordered/post_move_tests.cpp
// Copyright (C) 2022-2023 Christian Mazakas // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or move at http://www.boost.org/LICENSE_1_0.txt) #include "../helpers/equivalent.hpp" #include "../helpers/invariants.hpp" #include "../helpers/random_values.hpp" #include "../helpers/test.hpp" #include "../helpers/tracker.hpp" #include "../helpers/unordered.hpp" #include "../objects/cxx11_allocator.hpp" #include "../objects/test.hpp" #include <boost/core/ignore_unused.hpp> #include <iterator> #if defined(BOOST_MSVC) #pragma warning(disable : 4127) // conditional expression is constant #endif namespace move_tests { test::seed_t initialize_seed(98624); #define BOOST_UNORDERED_TEST_MOVING 1 template <class T> T empty(T*) { return T(); } template <class T> T create(test::random_values<T> const& v, test::object_count& count) { T x(v.begin(), v.end()); count = test::global_object_count; return x; } template <class T> T create(test::random_values<T> const& v, test::object_count& count, typename T::hasher hf, typename T::key_equal eq, typename T::allocator_type al, float mlf) { T x(0, hf, eq, al); x.max_load_factor(mlf); x.insert(v.begin(), v.end()); count = test::global_object_count; return x; } template <class T> T const& get_key(T const& t) { return t; } template <class K, class V> K const& get_key(std::pair<K const, V> const& kv) { return kv.first; } template <class T> T const& get_value(T const& t) { return t; } template <class K, class V> K const& get_value(std::pair<K const, V> const& kv) { return kv.second; } template <class T> static void insert_range(T& y, test::random_values<T> const& v) { y.insert(v.begin(), v.end()); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void insert_single(T& y, test::random_values<T> const& v) { y.insert(*v.begin()); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void insert_single_hint(T& y, test::random_values<T> const& v) { y.insert(y.end(), *v.begin()); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> struct insert_or_assign_invoker { void operator()(T&, test::random_values<T> const&) {} }; template <class Key, class T, class Hash, class KeyEqual, class Allocator> struct insert_or_assign_invoker< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > { void operator()(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& y, test::random_values< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > const& v) { typedef typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size_type; y.insert_or_assign(get_key(*v.begin()), get_value(*v.begin())); BOOST_TEST_EQ( y.size(), static_cast<size_type>(std::distance(y.begin(), y.end()))); } }; template <class T> static void insert_or_assign(T& y, test::random_values<T> const& v) { insert_or_assign_invoker<T>()(y, v); } template <class T> struct insert_or_assign_hint_invoker { void operator()(T&, test::random_values<T> const&) {} }; template <class Key, class T, class Hash, class KeyEqual, class Allocator> struct insert_or_assign_hint_invoker< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > { void operator()(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& y, test::random_values< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > const& v) { typedef typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size_type; y.insert_or_assign(y.end(), get_key(*v.begin()), get_value(*v.begin())); BOOST_TEST_EQ( y.size(), static_cast<size_type>(std::distance(y.begin(), y.end()))); } }; template <class T> static void insert_or_assign_hint(T& y, test::random_values<T> const& v) { insert_or_assign_hint_invoker<T>()(y, v); } template <class T> struct try_emplace_invoker { void operator()(T&, test::random_values<T> const&) {} }; template <class Key, class T, class Hash, class KeyEqual, class Allocator> struct try_emplace_invoker< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > { void operator()(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& y, test::random_values< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > const& v) { typedef typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size_type; y.try_emplace(get_key(*v.begin()), get_value(*v.begin())); BOOST_TEST_EQ( y.size(), static_cast<size_type>(std::distance(y.begin(), y.end()))); } }; template <class T> static void try_emplace(T& y, test::random_values<T> const& v) { try_emplace_invoker<T>()(y, v); } template <class T> struct try_emplace_hint_invoker { void operator()(T&, test::random_values<T> const&) {} }; template <class Key, class T, class Hash, class KeyEqual, class Allocator> struct try_emplace_hint_invoker< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > { void operator()(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& y, test::random_values< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > const& v) { typedef typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size_type; y.try_emplace(y.end(), get_key(*v.begin()), get_value(*v.begin())); BOOST_TEST_EQ( y.size(), static_cast<size_type>(std::distance(y.begin(), y.end()))); } }; template <class T> static void try_emplace_hint(T& y, test::random_values<T> const& v) { try_emplace_hint_invoker<T>()(y, v); } template <class T> struct at_invoker { void operator()(T&, test::random_values<T> const&) {} }; template <class Key, class T, class Hash, class KeyEqual, class Allocator> struct at_invoker<boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > { void operator()(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& y, test::random_values< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > const& v) { BOOST_TRY { y.at(get_key(*v.begin())); } BOOST_CATCH(...) {} BOOST_CATCH_END } }; template <class T> static void at(T& y, test::random_values<T> const& v) { at_invoker<T>()(y, v); } template <class T> struct index_operator_invoker { void operator()(T&, test::random_values<T> const&) {} }; template <class Key, class T, class Hash, class KeyEqual, class Allocator> struct index_operator_invoker< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > { void operator()(boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>& y, test::random_values< boost::unordered_map<Key, T, Hash, KeyEqual, Allocator> > const& v) { typedef typename boost::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size_type; y[get_key(*v.begin())] = get_value(*v.begin()); BOOST_TEST_EQ( y.size(), static_cast<size_type>(std::distance(y.begin(), y.end()))); } }; template <class T> static void index_operator(T& y, test::random_values<T> const& v) { index_operator_invoker<T>()(y, v); } template <class T> static void clear(T& y, test::random_values<T> const&) { y.clear(); BOOST_TEST(y.empty()); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void capacity(T& y, test::random_values<T> const&) { (void)y.empty(); (void)y.size(); (void)y.max_size(); (void)y.load_factor(); (void)y.max_load_factor(); (void)y.hash_function(); (void)y.key_eq(); (void)y.get_allocator(); } template <class T> static void iterators(T& y, test::random_values<T> const&) { BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void erase_range(T& y, test::random_values<T> const&) { y.erase(y.begin(), y.end()); BOOST_TEST(y.empty()); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void erase_key(T& y, test::random_values<T> const& v) { y.erase(get_key(*v.begin())); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void lookup(T& y, test::random_values<T> const& v) { (void)y.count(get_key(*v.begin())); (void)y.find(get_key(*v.begin())); (void)y.contains(get_key(*v.begin())); (void)y.equal_range(get_key(*v.begin())); } template <class T> static void reserve(T& y, test::random_values<T> const&) { y.reserve(1337); BOOST_TEST_GT(y.bucket_count(), 1337u); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void copy_assignment(T& y, test::random_values<T> const& v) { T x(v.begin(), v.end()); y = x; BOOST_TEST_EQ(y.size(), x.size()); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void move_assignment(T& y, test::random_values<T> const& v) { T x(v.begin(), v.end()); std::size_t const size = x.size(); y = std::move(x); BOOST_TEST_GE(y.size(), size); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void equal(T& y, test::random_values<T> const& v) { T x(v.begin(), v.end()); (void)(y == x); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } #ifndef BOOST_UNORDERED_FOA_TESTS template <class T> static void extract(T& y, test::random_values<T> const& v) { (void)y.extract(get_key(*v.begin())); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } #endif template <class T> static void merge(T& y, test::random_values<T> const& v) { T x(v.begin(), v.end()); if (y.get_allocator() == x.get_allocator()) { y.merge(x); } BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class X> bool pred(X const&) { return true; } template <class T> static void erase_with_pred(T& y, test::random_values<T> const&) { erase_if(y, pred<typename T::value_type>); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } template <class T> static void container_swap(T& y, test::random_values<T> const& v) { T x(v.begin(), v.end()); if (boost::allocator_propagate_on_container_swap< typename T::allocator_type>::type::value || x.get_allocator() == y.get_allocator()) { y.swap(x); } BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); } #ifndef BOOST_UNORDERED_FOA_TESTS template <class T> static void buckets(T& y, test::random_values<T> const& v) { (void)y.begin(0); (void)y.end(0); (void)y.bucket_count(); (void)y.max_bucket_count(); (void)y.bucket_size(0); (void)y.bucket(get_key(*v.begin())); } #endif template <class T> static void double_move_construct(T& y, test::random_values<T> const&) { T x = std::move(y); x.clear(); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); BOOST_TEST_EQ(x.size(), static_cast<typename T::size_type>(std::distance(x.begin(), x.end()))); } template <class T> static void double_move_assign(T& y, test::random_values<T> const&) { T x; x = std::move(y); x.clear(); BOOST_TEST_EQ(y.size(), static_cast<typename T::size_type>(std::distance(y.begin(), y.end()))); BOOST_TEST_EQ(x.size(), static_cast<typename T::size_type>(std::distance(x.begin(), x.end()))); } template <class T> static void post_move_tests(T* ptr, test::random_generator const& generator) { // clang-format off void (*fps[])(T&, test::random_values<T> const&) = { insert_range<T>, insert_single<T>, insert_single_hint<T>, insert_or_assign<T>, insert_or_assign_hint<T>, try_emplace<T>, try_emplace_hint<T>, at<T>, index_operator<T>, clear<T>, capacity<T>, iterators<T>, erase_range<T>, erase_key<T>, lookup<T>, reserve<T>, copy_assignment<T>, move_assignment<T>, equal<T>, #ifndef BOOST_UNORDERED_FOA_TESTS extract<T>, buckets<T>, #endif merge<T>, erase_with_pred<T>, container_swap<T>, double_move_construct<T>, double_move_assign<T> }; // clang-format on std::size_t const len = (sizeof(fps) / sizeof(*(fps))); for (std::size_t i = 0; i < len; ++i) { test::check_instances check_; test::random_values<T> const v(1000, generator); test::object_count count; T y(create(v, count)); unsigned num_allocs = test::detail::tracker.count_allocations; (void)num_allocs; T x(std::move(y)); BOOST_TEST(y.empty()); BOOST_TEST(y.begin() == y.end()); #ifdef BOOST_UNORDERED_FOA_TESTS { using allocator_type = typename T::allocator_type; using value_type = typename boost::allocator_value_type<allocator_type>::type; using pointer = typename boost::allocator_pointer<allocator_type>::type; if (std::is_same<pointer, value_type*>::value) { BOOST_TEST_EQ(y.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, num_allocs); } } #else BOOST_TEST_EQ(y.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, num_allocs); #endif fps[i](y, v); test::check_container(x, v); test::check_equivalent_keys(x); } for (std::size_t i = 0; i < len; ++i) { typename T::hasher hf(1); typename T::key_equal eq(1); typename T::allocator_type al1(1); typename T::allocator_type al2(2); test::check_instances check_; test::random_values<T> v(1000, generator); test::object_count count; T y(v.begin(), v.end(), 0, hf, eq, al1); T x(std::move(y), al2); #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST(y.empty()); BOOST_TEST(y.begin() == y.end()); #else BOOST_TEST_NOT(y.empty()); BOOST_TEST(y.begin() != y.end()); #endif fps[i](y, v); test::check_container(x, v); test::check_equivalent_keys(x); } for (std::size_t i = 0; i < len; ++i) { test::check_instances check_; test::random_values<T> v(1000, generator); test::object_count count; T y(create(v, count)); unsigned num_allocs = test::detail::tracker.count_allocations; (void)num_allocs; T x(empty(ptr)); x = std::move(y); BOOST_TEST(y.empty()); BOOST_TEST(y.begin() == y.end()); #ifdef BOOST_UNORDERED_FOA_TESTS { using allocator_type = typename T::allocator_type; using value_type = typename boost::allocator_value_type<allocator_type>::type; using pointer = typename boost::allocator_pointer<allocator_type>::type; if (std::is_same<pointer, value_type*>::value) { BOOST_TEST_EQ(y.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, num_allocs); } } #else BOOST_TEST_EQ(y.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, num_allocs); #endif fps[i](y, v); test::check_container(x, v); test::check_equivalent_keys(x); } for (std::size_t i = 0; i < len; ++i) { typename T::hasher hf(1); typename T::key_equal eq(1); typename T::allocator_type al1(1); typename T::allocator_type al2(2); test::check_instances check_; test::random_values<T> v(1000, generator); test::object_count count; T y(v.begin(), v.end(), 0, hf, eq, al1); unsigned num_allocs = test::detail::tracker.count_allocations; (void)num_allocs; T x(al2); x = std::move(y); bool b = boost::allocator_propagate_on_container_move_assignment< typename T::allocator_type>::type::value; if (b) { BOOST_TEST(y.empty()); BOOST_TEST(y.begin() == y.end()); BOOST_TEST_EQ(y.bucket_count(), 0u); BOOST_TEST_EQ(test::detail::tracker.count_allocations, num_allocs); } else { #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST(y.empty()); BOOST_TEST(y.begin() == y.end()); #else BOOST_TEST_NOT(y.empty()); BOOST_TEST(y.begin() != y.end()); #endif } fps[i](y, v); test::check_container(x, v); test::check_equivalent_keys(x); } } using test::default_generator; using test::generate_collisions; using test::limited_range; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, std::allocator<std::pair<test::object const, test::object> > >* test_map_std_alloc; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_set; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_map; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::propagate_move> >* test_set_prop_move; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::propagate_move> >* test_map_prop_move; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_propagate_move> >* test_set_no_prop_move; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::no_propagate_move> >* test_map_no_prop_move; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_node_set; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_node_map; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::propagate_move> >* test_node_set_prop_move; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::propagate_move> >* test_node_map_prop_move; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_propagate_move> >* test_node_set_no_prop_move; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::no_propagate_move> >* test_node_map_no_prop_move; // clang-format off UNORDERED_TEST(post_move_tests, ((test_set)(test_map)(test_set_prop_move)(test_map_prop_move) (test_set_no_prop_move)(test_map_no_prop_move) (test_node_set)(test_node_map) (test_node_set_prop_move)(test_node_map_prop_move) (test_node_set_no_prop_move)(test_node_map_no_prop_move))( (default_generator)(generate_collisions)(limited_range))) // clang-format on #else boost::unordered_map<test::object, test::object, test::hash, test::equal_to, std::allocator<std::pair<test::object const, test::object> > >* test_map_std_alloc; boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_set; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_multiset; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_map; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator2<std::pair<test::object const, test::object> > >* test_multimap; boost::unordered_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::propagate_move> >* test_set_prop_move; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::propagate_move> >* test_multiset_prop_move; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::propagate_move> >* test_map_prop_move; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::propagate_move> >* test_multimap_prop_move; boost::unordered_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_propagate_move> >* test_set_no_prop_move; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_propagate_move> >* test_multiset_no_prop_move; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::no_propagate_move> >* test_map_no_prop_move; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<std::pair<test::object const, test::object>, test::no_propagate_move> >* test_multimap_no_prop_move; // clang-format off UNORDERED_TEST(post_move_tests, ((test_set)(test_multiset)(test_map)(test_multimap)(test_set_prop_move)( test_multiset_prop_move)(test_map_prop_move)(test_multimap_prop_move)( test_set_no_prop_move)(test_multiset_no_prop_move)(test_map_no_prop_move)( test_multimap_no_prop_move))( (default_generator)(generate_collisions)(limited_range))) // clang-format on #endif } // namespace move_tests RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/fwd_map_test.cpp
// Copyright 2008-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) // clang-format off #include "../helpers/prefix.hpp" #ifdef BOOST_UNORDERED_FOA_TESTS #include <boost/unordered/unordered_flat_map_fwd.hpp> #include <boost/unordered/unordered_node_map_fwd.hpp> #include <boost/unordered/detail/implementation.hpp> #else #include <boost/unordered/unordered_map_fwd.hpp> #endif #include "../helpers/postfix.hpp" // clang-format on #ifdef BOOST_UNORDERED_FOA_TESTS template <typename T> void call_swap( boost::unordered_flat_map<T, T>& x, boost::unordered_flat_map<T, T>& y) { swap(x, y); } template <typename T> void call_swap( boost::unordered_node_map<T, T>& x, boost::unordered_node_map<T, T>& y) { swap(x, y); } template <typename T> bool call_equals( boost::unordered_flat_map<T, T>& x, boost::unordered_flat_map<T, T>& y) { return x == y; } template <typename T> bool call_equals( boost::unordered_node_map<T, T>& x, boost::unordered_node_map<T, T>& y) { return x == y; } template <typename T> bool call_not_equals( boost::unordered_flat_map<T, T>& x, boost::unordered_flat_map<T, T>& y) { return x != y; } template <typename T> bool call_not_equals( boost::unordered_node_map<T, T>& x, boost::unordered_node_map<T, T>& y) { return x != y; } #include <boost/unordered/unordered_flat_map.hpp> #include <boost/unordered/unordered_node_map.hpp> #else template <typename T> void call_swap(boost::unordered_map<T, T>& x, boost::unordered_map<T, T>& y) { swap(x, y); } template <typename T> bool call_equals(boost::unordered_map<T, T>& x, boost::unordered_map<T, T>& y) { return x == y; } template <typename T> bool call_not_equals( boost::unordered_map<T, T>& x, boost::unordered_map<T, T>& y) { return x != y; } template <typename T> void call_swap( boost::unordered_multimap<T, T>& x, boost::unordered_multimap<T, T>& y) { swap(x, y); } template <typename T> bool call_equals( boost::unordered_multimap<T, T>& x, boost::unordered_multimap<T, T>& y) { return x == y; } template <typename T> bool call_not_equals( boost::unordered_multimap<T, T>& x, boost::unordered_multimap<T, T>& y) { return x != y; } #include <boost/unordered_map.hpp> #endif #include "../helpers/test.hpp" #ifdef BOOST_UNORDERED_FOA_TESTS typedef boost::unordered_flat_map<int, int> int_map; typedef boost::unordered_node_map<int, int> int_node_map; #else typedef boost::unordered_map<int, int> int_map; typedef boost::unordered_multimap<int, int> int_multimap; #endif UNORDERED_AUTO_TEST (use_map_fwd_declared_function) { int_map x, y; x[1] = 2; y[2] = 1; call_swap(x, y); BOOST_TEST(y.find(1) != y.end() && y.find(1)->second == 2); BOOST_TEST(y.find(2) == y.end()); BOOST_TEST(x.find(1) == x.end()); BOOST_TEST(x.find(2) != x.end() && x.find(2)->second == 1); BOOST_TEST(!call_equals(x, y)); BOOST_TEST(call_not_equals(x, y)); } #ifdef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (use_node_map_fwd_declared_function) { int_node_map x, y; x[1] = 2; y[2] = 1; call_swap(x, y); BOOST_TEST(y.find(1) != y.end() && y.find(1)->second == 2); BOOST_TEST(y.find(2) == y.end()); BOOST_TEST(x.find(1) == x.end()); BOOST_TEST(x.find(2) != x.end() && x.find(2)->second == 1); BOOST_TEST(!call_equals(x, y)); BOOST_TEST(call_not_equals(x, y)); } #endif #ifndef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (use_multimap_fwd_declared_function) { int_multimap x, y; call_swap(x, y); BOOST_TEST(call_equals(x, y)); BOOST_TEST(!call_not_equals(x, y)); } #endif RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/hash_is_avalanching_test.cpp
// Copyright 2022 Peter Dimov // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt // an imitation of a third-party header specializing hash_is_avalanching // (boost/container_hash/hash.hpp is an example doing that) #include <boost/type_traits/integral_constant.hpp> struct X3 { }; namespace boost { namespace unordered { template<class T> struct hash_is_avalanching; template<> struct hash_is_avalanching< ::X3 >: boost::true_type {}; } // namespace unordered } // namespace boost // #include <boost/unordered/hash_traits.hpp> #include <boost/core/lightweight_test_trait.hpp> struct X1 { }; struct X2 { typedef void is_avalanching; }; int main() { using boost::unordered::hash_is_avalanching; BOOST_TEST_TRAIT_FALSE((hash_is_avalanching<X1>)); BOOST_TEST_TRAIT_TRUE((hash_is_avalanching<X2>)); BOOST_TEST_TRAIT_TRUE((hash_is_avalanching<X3>)); return boost::report_errors(); }
0
repos/unordered/test
repos/unordered/test/unordered/equality_tests.cpp
// Copyright 2008-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) #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include <boost/preprocessor/seq.hpp> #include <list> // TODO: this test needs to be someday cleaned up to not be so heavily // macro-generated // namespace equality_tests { struct mod_compare { bool alt_hash_; explicit mod_compare(bool alt_hash = false) : alt_hash_(alt_hash) {} bool operator()(int x, int y) const { return x % 1000 == y % 1000; } std::size_t operator()(int x) const { return alt_hash_ ? static_cast<std::size_t>(x % 250) : static_cast<std::size_t>((x + 5) % 250); } }; #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>; using boost_unordered_node_set = boost::unordered_node_set<int, mod_compare, mod_compare>; using boost_unordered_node_map = boost::unordered_node_map<int, int, mod_compare, mod_compare>; #define UNORDERED_EQUALITY_MULTISET_TEST(seq1, op, seq2) \ { \ } #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) \ { \ boost::unordered_multiset<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) \ { \ 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, map2, seq2) \ BOOST_TEST(map1 op map2); \ } #endif #ifdef BOOST_UNORDERED_FOA_TESTS #define UNORDERED_EQUALITY_SET_TEST(seq1, op, seq2) \ { \ 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); \ } \ { \ boost_unordered_node_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); \ } #else #define UNORDERED_EQUALITY_SET_TEST(seq1, op, seq2) \ { \ 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); \ } #endif #ifdef BOOST_UNORDERED_FOA_TESTS #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, map2, seq2) \ BOOST_TEST(map1 op map2); \ } \ \ { \ boost_unordered_node_map map1, map2; \ BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map1, seq1) \ BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2) \ BOOST_TEST(map1 op map2); \ } #else #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, map2, seq2) \ BOOST_TEST(map1 op map2); \ } #endif #define UNORDERED_SET_INSERT(r, set, item) set.insert(item); #define UNORDERED_MAP_INSERT(r, map, item) \ map.insert(std::pair<int const, int> BOOST_PP_SEQ_TO_TUPLE(item)); UNORDERED_AUTO_TEST (equality_size_tests) { #ifdef BOOST_UNORDERED_FOA_TESTS { boost::unordered_flat_set<int> x1, x2; BOOST_TEST(x1 == x2); BOOST_TEST(!(x1 != x2)); x1.insert(1); BOOST_TEST(x1 != x2); BOOST_TEST(!(x1 == x2)); BOOST_TEST(x2 != x1); BOOST_TEST(!(x2 == x1)); x2.insert(1); BOOST_TEST(x1 == x2); BOOST_TEST(!(x1 != x2)); x2.insert(2); BOOST_TEST(x1 != x2); BOOST_TEST(!(x1 == x2)); BOOST_TEST(x2 != x1); BOOST_TEST(!(x2 == x1)); } { boost::unordered_node_set<int> x1, x2; BOOST_TEST(x1 == x2); BOOST_TEST(!(x1 != x2)); x1.insert(1); BOOST_TEST(x1 != x2); BOOST_TEST(!(x1 == x2)); BOOST_TEST(x2 != x1); BOOST_TEST(!(x2 == x1)); x2.insert(1); BOOST_TEST(x1 == x2); BOOST_TEST(!(x1 != x2)); x2.insert(2); BOOST_TEST(x1 != x2); BOOST_TEST(!(x1 == x2)); BOOST_TEST(x2 != x1); BOOST_TEST(!(x2 == x1)); } #else boost::unordered_set<int> x1, x2; BOOST_TEST(x1 == x2); BOOST_TEST(!(x1 != x2)); x1.insert(1); BOOST_TEST(x1 != x2); BOOST_TEST(!(x1 == x2)); BOOST_TEST(x2 != x1); BOOST_TEST(!(x2 == x1)); x2.insert(1); BOOST_TEST(x1 == x2); BOOST_TEST(!(x1 != x2)); x2.insert(2); BOOST_TEST(x1 != x2); BOOST_TEST(!(x1 == x2)); BOOST_TEST(x2 != x1); BOOST_TEST(!(x2 == x1)); #endif } UNORDERED_AUTO_TEST (equality_key_value_tests) { UNORDERED_EQUALITY_MULTISET_TEST((1), !=, (2)) UNORDERED_EQUALITY_SET_TEST((2), ==, (2)) UNORDERED_EQUALITY_MAP_TEST(((1)(1))((2)(1)), !=, ((1)(1))((3)(1))) } UNORDERED_AUTO_TEST (equality_collision_test) { UNORDERED_EQUALITY_MULTISET_TEST((1), !=, (501)) UNORDERED_EQUALITY_MULTISET_TEST((1)(251), !=, (1)(501)) UNORDERED_EQUALITY_MULTIMAP_TEST(((251)(1))((1)(1)), !=, ((501)(1))((1)(1))) UNORDERED_EQUALITY_MULTISET_TEST((1)(501), ==, (1)(501)) UNORDERED_EQUALITY_SET_TEST((1)(501), ==, (501)(1)) } UNORDERED_AUTO_TEST (equality_group_size_test) { UNORDERED_EQUALITY_MULTISET_TEST((10)(20)(20), !=, (10)(10)(20)) UNORDERED_EQUALITY_MULTIMAP_TEST( ((10)(1))((20)(1))((20)(1)), !=, ((10)(1))((20)(1))((10)(1))) UNORDERED_EQUALITY_MULTIMAP_TEST( ((20)(1))((10)(1))((10)(1)), ==, ((10)(1))((20)(1))((10)(1))) } UNORDERED_AUTO_TEST (equality_map_value_test) { UNORDERED_EQUALITY_MAP_TEST(((1)(1)), !=, ((1)(2))) UNORDERED_EQUALITY_MAP_TEST(((1)(1)), ==, ((1)(1))) UNORDERED_EQUALITY_MULTIMAP_TEST(((1)(1)), !=, ((1)(2))) UNORDERED_EQUALITY_MULTIMAP_TEST(((1)(1))((1)(1)), !=, ((1)(1))((1)(2))) UNORDERED_EQUALITY_MULTIMAP_TEST(((1)(2))((1)(1)), ==, ((1)(1))((1)(2))) UNORDERED_EQUALITY_MULTIMAP_TEST(((1)(2))((1)(1)), !=, ((1)(1))((1)(3))) } UNORDERED_AUTO_TEST (equality_predicate_test) { UNORDERED_EQUALITY_SET_TEST((1), !=, (1001)) UNORDERED_EQUALITY_MAP_TEST(((1)(2))((1001)(1)), !=, ((1001)(2))((1)(1))) } UNORDERED_AUTO_TEST (equality_multiple_group_test) { UNORDERED_EQUALITY_MULTISET_TEST( (1)(1)(1)(1001)(2001)(2001)(2)(1002)(3)(1003)(2003), ==, (3)(1003)(2003)(1002)(2)(2001)(2001)(1)(1001)(1)(1)) } // Test that equality still works when the two containers have // different hash functions but the same equality predicate. UNORDERED_AUTO_TEST (equality_different_hash_test) { #ifdef BOOST_UNORDERED_FOA_TESTS { typedef boost_unordered_set set; set set1(0, mod_compare(false), mod_compare(false)); set set2(0, mod_compare(true), mod_compare(true)); BOOST_TEST(set1 == set2); set1.insert(1); set2.insert(2); BOOST_TEST(set1 != set2); set1.insert(2); set2.insert(1); BOOST_TEST(set1 == set2); set1.insert(10); set2.insert(20); BOOST_TEST(set1 != set2); set1.insert(20); set2.insert(10); BOOST_TEST(set1 == set2); } { typedef boost_unordered_node_set set; set set1(0, mod_compare(false), mod_compare(false)); set set2(0, mod_compare(true), mod_compare(true)); BOOST_TEST(set1 == set2); set1.insert(1); set2.insert(2); BOOST_TEST(set1 != set2); set1.insert(2); set2.insert(1); BOOST_TEST(set1 == set2); set1.insert(10); set2.insert(20); BOOST_TEST(set1 != set2); set1.insert(20); set2.insert(10); BOOST_TEST(set1 == set2); } #else typedef boost_unordered_set set; set set1(0, mod_compare(false), mod_compare(false)); set set2(0, mod_compare(true), mod_compare(true)); BOOST_TEST(set1 == set2); set1.insert(1); set2.insert(2); BOOST_TEST(set1 != set2); set1.insert(2); set2.insert(1); BOOST_TEST(set1 == set2); set1.insert(10); set2.insert(20); BOOST_TEST(set1 != set2); set1.insert(20); set2.insert(10); BOOST_TEST(set1 == set2); #endif } } // namespace equality_tests RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/extract_tests.cpp
// 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/unordered.hpp" #include "../helpers/equivalent.hpp" #include "../helpers/helpers.hpp" #include "../helpers/invariants.hpp" #include "../helpers/random_values.hpp" #include "../helpers/test.hpp" #include "../helpers/tracker.hpp" #include "../objects/test.hpp" namespace extract_tests { test::seed_t initialize_seed(85638); template <class Container> void extract_tests1(Container*, test::random_generator generator) { BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Extract by key.\n"; { test::check_instances check_; test::random_values<Container> v(1000, generator); Container x(v.begin(), v.end()); int iterations = 0; for (typename test::random_values<Container>::iterator it = v.begin(); it != v.end(); ++it) { std::size_t count = x.count(test::get_key<Container>(*it)); std::size_t old_size = x.size(); std::size_t new_count = count ? count - 1 : count; std::size_t new_size = count ? old_size - 1 : old_size; typename Container::node_type n = x.extract(test::get_key<Container>(*it)); BOOST_TEST((n ? true : false) == (count ? true : false)); BOOST_TEST(x.size() == new_size); BOOST_TEST(x.count(test::get_key<Container>(*it)) == new_count); if (!new_count) { BOOST_TEST(x.find(test::get_key<Container>(*it)) == x.end()); } else { BOOST_TEST(x.find(test::get_key<Container>(*it)) != x.end()); } if (++iterations % 20 == 0) test::check_equivalent_keys(x); } BOOST_TEST(x.empty()); } BOOST_LIGHTWEIGHT_TEST_OSTREAM << "extract(begin()).\n"; { test::check_instances check_; test::random_values<Container> v(1000, generator); Container x(v.begin(), v.end()); std::size_t size = x.size(); int iterations = 0; while (size > 0 && !x.empty()) { typename Container::key_type key = test::get_key<Container>(*x.begin()); std::size_t count = x.count(key); typename Container::node_type n = x.extract(x.begin()); BOOST_TEST(n); --size; BOOST_TEST(x.count(key) == count - 1); BOOST_TEST(x.size() == size); if (++iterations % 20 == 0) test::check_equivalent_keys(x); } BOOST_TEST(x.empty()); } BOOST_LIGHTWEIGHT_TEST_OSTREAM << "extract(random position).\n"; { test::check_instances check_; test::random_values<Container> v(1000, generator); Container x(v.begin(), v.end()); std::size_t size = x.size(); int iterations = 0; while (size > 0 && !x.empty()) { using namespace std; int index = rand() % (int)x.size(); typename Container::const_iterator prev, pos, next; if (index == 0) { prev = pos = x.begin(); } else { prev = test::next(x.begin(), index - 1); pos = test::next(prev); } next = test::next(pos); typename Container::key_type key = test::get_key<Container>(*pos); std::size_t count = x.count(key); typename Container::node_type n = x.extract(pos); BOOST_TEST(n); --size; if (size > 0) BOOST_TEST(index == 0 ? next == x.begin() : next == test::next(prev)); BOOST_TEST(x.count(key) == count - 1); BOOST_TEST(x.size() == size); if (++iterations % 20 == 0) test::check_equivalent_keys(x); } BOOST_TEST(x.empty()); } BOOST_LIGHTWEIGHT_TEST_OSTREAM << "\n"; } using test::default_generator; using test::generate_collisions; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_node_map; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_node_set; UNORDERED_TEST(extract_tests1, ((test_node_map)(test_node_set))((default_generator)(generate_collisions))) #else boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_multiset; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_map; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_multimap; UNORDERED_TEST( extract_tests1, ((test_set)(test_multiset)(test_map)(test_multimap))( (default_generator)(generate_collisions))) #endif } // namespace extract_tests RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/prime_fmod_tests.cpp
// Copyright 2022 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 <boost/unordered/detail/prime_fmod.hpp> #include <boost/core/detail/splitmix64.hpp> #include <boost/core/lightweight_test.hpp> #include <limits> #if defined(BOOST_MSVC) // conditional expression is constant #pragma warning(disable : 4127) #endif void macros_test() { if (std::numeric_limits<std::size_t>::digits >= 64) { #if !defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T) BOOST_ERROR("std::numeric_limits<size_t>::digits >= 64, but " "BOOST_UNORDERED_FCA_HAS_64B_SIZE_T is not defined"); #endif } else { #if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T) BOOST_ERROR("std::numeric_limits<size_t>::digits < 64, but " "BOOST_UNORDERED_FCA_HAS_64B_SIZE_T is defined"); #endif } } // Pretty inefficient, but the test is fast enough. // Might be too slow if we had larger primes? bool is_prime(std::size_t x) { if (x == 2) { return true; } if (x == 1 || x % 2 == 0) { return false; } // y*y <= x is susceptible to overflow, so instead make sure to use y <= (x/y) for (std::size_t y = 3; y <= (x / y); y += 2) { if (x % y == 0) { return false; } } return true; } void prime_sizes_test() { // just some basic sanity checks // BOOST_TEST(!is_prime(0)); BOOST_TEST(!is_prime(1)); BOOST_TEST(is_prime(2)); BOOST_TEST(is_prime(3)); BOOST_TEST(is_prime(13)); BOOST_TEST(!is_prime(4)); BOOST_TEST(!is_prime(100)); BOOST_TEST(!is_prime(49)); std::size_t const* sizes = boost::unordered::detail::prime_fmod_size<>::sizes; std::size_t sizes_len = boost::unordered::detail::prime_fmod_size<>::sizes_len; // prove every number in our sizes array is prime // BOOST_TEST_GT(sizes_len, 0u); for (std::size_t i = 0; i < sizes_len; ++i) { BOOST_TEST(is_prime(sizes[i])); } // prove that every subsequent number in the sequence is larger than the // previous // for (std::size_t i = 1; i < sizes_len; ++i) { BOOST_TEST_GT(sizes[i], sizes[i - 1]); } #if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T) // now we wish to prove that if we do have the reciprocals stored, we have the // correct amount of them, i.e. one for every entry in sizes[] that fits in 32 // bits // boost::uint64_t const* inv_sizes32 = boost::unordered::detail::prime_fmod_size<>::inv_sizes32; std::size_t inv_sizes32_len = boost::unordered::detail::prime_fmod_size<>::inv_sizes32_len; std::size_t count = 0; for (std::size_t i = 0; i < sizes_len; ++i) { if (sizes[i] <= UINT32_MAX) { ++count; } } BOOST_TEST_GT(inv_sizes32_len, 0u); BOOST_TEST_EQ(inv_sizes32_len, count); // these values should also be monotonically decreasing // for (std::size_t i = 1; i < inv_sizes32_len; ++i) { BOOST_TEST_LT(inv_sizes32[i], inv_sizes32[i - 1]); } // now make sure the values in inv_sizes32 are what they should be as derived // from the paper // for (std::size_t i = 0; i < inv_sizes32_len; ++i) { std::size_t const size = sizes[i]; BOOST_TEST_LE(size, UINT_MAX); boost::uint32_t d = static_cast<boost::uint32_t>(sizes[i]); boost::uint64_t M = ((boost::ulong_long_type(0xffffffff) << 32) + boost::ulong_long_type(0xffffffff)) / d + 1; BOOST_TEST_EQ(inv_sizes32[i], M); } #endif } void get_remainder_test() { #if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T) struct { // boost::unordered::detail::prime_fmod_size<>::get_remainder // uses several internal implementations depending on the availability of // certain intrinsics or 128 bit integer support, defaulting to a slow, // portable routine. The following is a transcription of the portable // routine used here for verification purposes. // boost::uint64_t operator()(boost::uint64_t f, boost::uint32_t d) { boost::uint64_t r1 = (f & UINT32_MAX) * d; boost::uint64_t r2 = (f >> 32) * d; r2 += r1 >> 32; return r2 >> 32; } } get_remainder; boost::detail::splitmix64 rng; for (std::size_t i = 0; i < 1000000u; ++i) { boost::uint64_t f = rng(); boost::uint32_t d = rng() & 0xffffffffu; boost::uint64_t r1 = boost::unordered::detail::prime_fmod_size<>::get_remainder(f, d); boost::uint64_t r2 = get_remainder(f, d); if (!BOOST_TEST_EQ(r1, r2)) { std::cerr << "f: " << f << ", d: " << d << std::endl; return; } } #endif } void modulo_test() { std::size_t const* sizes = boost::unordered::detail::prime_fmod_size<>::sizes; std::size_t const sizes_len = boost::unordered::detail::prime_fmod_size<>::sizes_len; boost::detail::splitmix64 rng; for (std::size_t i = 0; i < 1000000u; ++i) { std::size_t hash = static_cast<std::size_t>(-1) & rng(); for (std::size_t j = 0; j < sizes_len; ++j) { std::size_t h = hash; #if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T) if (sizes[j] <= UINT_MAX) { h = boost::uint32_t(h & 0xffffffffu) + boost::uint32_t(h >> 32); } #endif std::size_t p1 = boost::unordered::detail::prime_fmod_size<>::position(hash, j); std::size_t p2 = h % sizes[j]; if (!BOOST_TEST_EQ(p1, p2)) { std::cerr << "hash: " << hash << ", j: " << j << ", h: " << h << ", sizes[" << j << "]: " << sizes[j] << std::endl; return; } } } } int main() { macros_test(); prime_sizes_test(); get_remainder_test(); modulo_test(); return boost::report_errors(); }
0
repos/unordered/test
repos/unordered/test/unordered/narrow_cast_tests.cpp
// Copyright 2022 Christian Mazakas // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #include <boost/unordered/detail/narrow_cast.hpp> #include <boost/core/lightweight_test.hpp> #include <boost/cstdint.hpp> // want to prove that for the wider type, the higher bits of the value // represenation don't affect the results of the narrowing, which in this case // is masking out the high bits when comapred to the narrow type static void signed_integral_narrowing() { // test positive range, fits // [0, 127] for (boost::int32_t i = 0x00; i < 0x80; ++i) { boost::int8_t k = (boost::int8_t)i; BOOST_TEST_GE(k, 0); BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(i), k); } // test positive range, doesn't fit // [0xff00, 0xff7f] for (boost::int32_t i = 0x00; i < 0x80; ++i) { boost::int32_t j = i + 0xff00; boost::int8_t k = (boost::int8_t)i; BOOST_TEST_GE(k, 0); BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j), k); } // test negative range, fits // [-128, -1] for (boost::int32_t i = 0x00; i < 0x80; ++i) { boost::int32_t j = i + (boost::int32_t)0xffffff80; boost::int8_t k = (boost::int8_t)j; BOOST_TEST_LT(j, 0); BOOST_TEST_LT(k, 0); BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j), k); } // test negative range, doesn't fit for (boost::int32_t i = 0x00; i < 0x80; ++i) { boost::int32_t j = i + (boost::int32_t)0x80000000; boost::int8_t k = (boost::int8_t)(i); BOOST_TEST_LT(j, 0); BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j), k); } for (boost::int32_t i = 0x00; i < 0x100; ++i) { boost::int32_t j = (boost::int32_t)0x80ff0000 + i; BOOST_TEST_LT(j, 0); BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j), (boost::int8_t)i); } // test special values { boost::int32_t x = 0xff; BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x), -1); } { boost::int32_t x = (boost::int32_t)0xffffff00; BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x), (boost::int8_t)0x00); } { boost::int32_t x = (boost::int32_t)0xffffff7f; BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x), (boost::int8_t)0x7f); } { boost::int32_t x = (boost::int32_t)0xffffffff; BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x), (boost::int8_t)-1); } } static void unsigned_integral_narrowing() { // test range: [0x00, 0xff] for (boost::uint32_t i = 0x00; i < 0x100; ++i) { BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::uint8_t>(i), (boost::uint8_t)(i & 0xff)); } // test range: [0xffffff00, 0xffffffff] boost::uint32_t i = 0xffffff00; for (; i < 0xffffffff; ++i) { BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::uint8_t>(i), (boost::uint8_t)(i & 0xff)); } BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::uint8_t>(i), (boost::uint8_t)(i & 0xff)); } int main() { signed_integral_narrowing(); unsigned_integral_narrowing(); return boost::report_errors(); }
0
repos/unordered/test
repos/unordered/test/unordered/serialization_tests.cpp
// Copyright (C) 2023 Joaquin M Lopez Munoz // 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) // temporary #define till all transitive includes comply with // https://github.com/boostorg/core/commit/5f6fe65 #define BOOST_ALLOW_DEPRECATED_HEADERS #include "../helpers/unordered.hpp" #include "../objects/test.hpp" #include "../helpers/random_values.hpp" #include <algorithm> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/config.hpp> #include <boost/config/workaround.hpp> #include <boost/serialization/nvp.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/vector.hpp> #include <cstddef> #include <cstdio> #include <fstream> #include <random> namespace { template <class Container, typename ArchivePair> void serialization_tests( Container*, ArchivePair*, test::random_generator generator) { typedef typename Container::iterator iterator; typedef std::vector<iterator> iterator_vector; typedef typename ArchivePair::first_type output_archive; typedef typename ArchivePair::second_type input_archive; BOOST_LIGHTWEIGHT_TEST_OSTREAM << "serialization_tests1\n"; { Container c; iterator it = c.end(); std::ostringstream oss; { output_archive oa(oss); oa << boost::serialization::make_nvp("container", c); oa << boost::serialization::make_nvp("iterator", it); } test::random_values<Container> values(100, generator); Container c2(values.begin(), values.end()); iterator it2 = c2.begin(); std::istringstream iss(oss.str()); input_archive ia(iss); ia >> boost::serialization::make_nvp("container", c2); ia >> boost::serialization::make_nvp("iterator", it2); BOOST_TEST(c2.empty()); BOOST_TEST(it2 == c2.end()); } BOOST_LIGHTWEIGHT_TEST_OSTREAM << "serialization_tests2\n"; { test::random_values<Container> values(100, generator); Container c(values.begin(), values.end()); iterator_vector v; for (iterator first = c.begin(), last=c.end(); ; ) { v.push_back(first); if(first == last) break; ++first; } #ifdef BOOST_UNORDERED_TEST_USE_STD_RANDOM_SHUFFLE std::random_shuffle(v.begin(), v.end()); #else std::shuffle(v.begin(), v.end(), std::mt19937(4213)); #endif std::ostringstream oss; { output_archive oa(oss); oa << boost::serialization::make_nvp("container", c); oa << boost::serialization::make_nvp("iterators", v); } Container c2; iterator_vector v2; std::istringstream iss(oss.str()); input_archive ia(iss); ia >> boost::serialization::make_nvp("container", c2); ia >> boost::serialization::make_nvp("iterators", v2); BOOST_TEST(c == c2); BOOST_TEST_EQ(v.size(), v2.size()); for (std::size_t i=0; i < v.size(); ++i) { iterator it = v[i]; iterator it2 = v2[i]; if (it == c.end()) { BOOST_TEST(it2 == c2.end()); } else { BOOST_TEST(it2 != c2.end()); BOOST_TEST(*it == *it2); } } } } // used by legacy_serialization_test, passed as argv[1] const char* test_dir="."; using test::default_generator; std::pair< boost::archive::text_oarchive, boost::archive::text_iarchive>* text_archive; std::pair< boost::archive::xml_oarchive, boost::archive::xml_iarchive>* xml_archive; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_map< test::object, test::object, test::hash, test::equal_to>* test_flat_map; boost::unordered_node_map< test::object, test::object, test::hash, test::equal_to>* test_node_map; boost::unordered_flat_set< test::object, test::hash, test::equal_to>* test_flat_set; boost::unordered_node_set< test::object, test::hash, test::equal_to>* test_node_set; UNORDERED_TEST(serialization_tests, ((test_flat_map)(test_node_map)(test_flat_set)(test_node_set)) ((text_archive)(xml_archive)) ((default_generator))) #else boost::unordered_map< test::object, test::object, test::hash, test::equal_to>* test_map; boost::unordered_multimap< test::object, test::object, test::hash, test::equal_to>* test_multimap; boost::unordered_set< test::object, test::hash, test::equal_to>* test_set; boost::unordered_multiset< test::object, test::hash, test::equal_to>* test_multiset; UNORDERED_TEST(serialization_tests, ((test_map)(test_multimap)(test_set)(test_multiset)) ((text_archive)(xml_archive)) ((default_generator))) template<typename T> struct non_const { typedef T type; }; template<typename T> struct non_const<const T> { typedef typename non_const<T>::type type; }; template<typename T, typename Q> struct non_const<std::pair<T, Q> > { typedef std::pair< typename non_const<T>::type, typename non_const<Q>::type> type; }; template<typename T> struct labeled { labeled(const char* label_): label(label_) {} const char* label; }; template <class Container, typename Archive> void legacy_serialization_test(labeled<Container> lc, labeled<Archive> la) { typedef typename Container::value_type value_type; typedef std::vector<typename non_const<value_type>::type> value_vector; static const std::size_t sizes[] = {0, 10, 100}; BOOST_LIGHTWEIGHT_TEST_OSTREAM << "legacy_serialization_test\n"; for(int i = 0; i < sizeof(sizes)/sizeof(sizes[0]); ++i) { char filename[1024]; std::sprintf( filename, "%s/legacy_archives/%s_%d.%s", test_dir, lc.label, (int)sizes[i], la.label); std::ifstream ifs(filename); Archive ia(ifs); Container c; value_vector v; ia >> boost::serialization::make_nvp("container", c); ia >> boost::serialization::make_nvp("values", v); BOOST_TEST(v.size() >= sizes[i]); // values generated with repetition BOOST_TEST((c==Container(v.begin(),v.end()))); } } labeled<boost::unordered_map<int, int> > labeled_map_int("map_int"); labeled<boost::unordered_map<std::string, std::string> > labeled_map_string("map_string"); labeled<boost::unordered_multimap<int, int> > labeled_multimap_int("multimap_int"); labeled<boost::unordered_multimap<std::string, std::string> > labeled_multimap_string("multimap_string"); labeled<boost::unordered_set<int> > labeled_set_int("set_int"); labeled<boost::unordered_set<std::string> > labeled_set_string("set_string"); labeled<boost::unordered_multiset<int> > labeled_multiset_int("multiset_int"); labeled<boost::unordered_multiset<std::string> > labeled_multiset_string("multiset_string"); labeled<boost::archive::text_iarchive> labeled_text_iarchive("txt"); labeled<boost::archive::xml_iarchive> labeled_xml_iarchive("xml"); UNORDERED_TEST(legacy_serialization_test, ((labeled_map_int)(labeled_map_string) (labeled_multimap_int)(labeled_multimap_string) (labeled_set_int)(labeled_set_string) (labeled_multiset_int)(labeled_multiset_string)) ((labeled_text_iarchive)(labeled_xml_iarchive))) #endif } int main(int argc, char* argv[]) { if (argc > 1) test_dir = argv[1]; BOOST_UNORDERED_TEST_COMPILER_INFO() ::test::get_state().run_tests(); return boost::report_errors(); }
0
repos/unordered/test
repos/unordered/test/unordered/erase_equiv_tests.cpp
// Copyright 2006-2009 Daniel James. // 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) // The code for erasing elements from containers with equivalent keys is very // hairy with several tricky edge cases - so explicitly test each one. // clang-format off #include "../helpers/prefix.hpp" #include <boost/unordered_map.hpp> #include "../helpers/postfix.hpp" // clang-format on #include "../helpers/test.hpp" #include "../helpers/list.hpp" #include "../helpers/invariants.hpp" #include "../helpers/helpers.hpp" #include <algorithm> #include <set> #include <iterator> #include "../objects/test.hpp" #if BOOST_WORKAROUND(BOOST_MSVC, < 1400) #pragma warning(disable : 4267) // conversion from 'size_t' to 'unsigned int', // possible loss of data. #endif struct write_pair_type { template <class X1, class X2> void operator()(std::pair<X1, X2> const& x) const { BOOST_LIGHTWEIGHT_TEST_OSTREAM << "(" << x.first << "," << x.second << ")"; } } write_pair; template <class Container> void write_container(Container const& x) { std::for_each(x.begin(), x.end(), write_pair); BOOST_LIGHTWEIGHT_TEST_OSTREAM << "\n"; } // Make everything collide - for testing erase in a single bucket. struct collision_hash { std::size_t operator()(int) const { return 0; } }; // For testing erase in 2 buckets. struct collision2_hash { std::size_t operator()(int x) const { return static_cast<std::size_t>(x & 1); } }; // For testing erase in lots of buckets. struct collision3_hash { std::size_t operator()(int x) const { return static_cast<std::size_t>(x); } }; typedef boost::unordered_multimap<int, int, collision_hash, std::equal_to<int>, test::allocator1<std::pair<int const, int> > > collide_map; typedef boost::unordered_multimap<int, int, collision2_hash, std::equal_to<int>, test::allocator2<std::pair<int const, int> > > collide_map2; typedef boost::unordered_multimap<int, int, collision3_hash, std::equal_to<int>, test::allocator2<std::pair<int const, int> > > collide_map3; typedef collide_map::value_type collide_value; typedef test::list<collide_value> collide_list; UNORDERED_AUTO_TEST (empty_range_tests) { collide_map x; x.erase(x.begin(), x.end()); x.erase(x.begin(), x.begin()); x.erase(x.end(), x.end()); test::check_equivalent_keys(x); } UNORDERED_AUTO_TEST (single_item_tests) { collide_list init; init.push_back(collide_value(1, 1)); collide_map x(init.begin(), init.end()); x.erase(x.begin(), x.begin()); BOOST_TEST(x.count(1) == 1 && x.size() == 1); test::check_equivalent_keys(x); x.erase(x.end(), x.end()); BOOST_TEST(x.count(1) == 1 && x.size() == 1); test::check_equivalent_keys(x); x.erase(x.begin(), x.end()); BOOST_TEST(x.count(1) == 0 && x.size() == 0); test::check_equivalent_keys(x); } UNORDERED_AUTO_TEST (two_equivalent_item_tests) { collide_list init; init.push_back(collide_value(1, 1)); init.push_back(collide_value(1, 2)); { collide_map x(init.begin(), init.end()); x.erase(x.begin(), x.end()); BOOST_TEST(x.count(1) == 0 && x.size() == 0); test::check_equivalent_keys(x); } { collide_map x(init.begin(), init.end()); int value = test::next(x.begin())->second; x.erase(x.begin(), test::next(x.begin())); BOOST_TEST(x.count(1) == 1 && x.size() == 1 && x.begin()->first == 1 && x.begin()->second == value); test::check_equivalent_keys(x); } { collide_map x(init.begin(), init.end()); int value = x.begin()->second; x.erase(test::next(x.begin()), x.end()); BOOST_TEST(x.count(1) == 1 && x.size() == 1 && x.begin()->first == 1 && x.begin()->second == value); test::check_equivalent_keys(x); } } // More automated tests... template <class Range1, class Range2> bool compare(Range1 const& x, Range2 const& y) { collide_list a(x.begin(), x.end()); collide_list b(y.begin(), y.end()); a.sort(); b.sort(); return a == b; } template <class Container> bool general_erase_range_test(Container& x, std::size_t start, std::size_t end) { collide_list l(x.begin(), x.end()); l.erase(test::next(l.begin(), start), test::next(l.begin(), end)); x.erase(test::next(x.begin(), start), test::next(x.begin(), end)); test::check_equivalent_keys(x); return compare(l, x); } template <class Container> void erase_subrange_tests(Container const& x) { for (std::size_t length = 0; length < x.size(); ++length) { for (std::size_t position = 0; position < x.size() - length; ++position) { Container y(x); collide_list init(y.begin(), y.end()); if (!general_erase_range_test(y, position, position + length)) { BOOST_ERROR("general_erase_range_test failed."); BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Erase: [" << position << "," << position + length << ")\n"; write_container(init); write_container(y); } } } } template <class Container> void x_by_y_erase_range_tests(Container*, int values, int duplicates) { Container y; for (int i = 0; i < values; ++i) { for (int j = 0; j < duplicates; ++j) { y.insert(collide_value(i, j)); } } BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Values: " << values << ", Duplicates: " << duplicates << "\n"; erase_subrange_tests(y); } template <class Container> void exhaustive_erase_tests(Container* x, int num_values, int num_duplicated) { for (int i = 0; i < num_values; ++i) { for (int j = 0; j < num_duplicated; ++j) { x_by_y_erase_range_tests(x, i, j); } } } UNORDERED_AUTO_TEST (exhaustive_collide_tests) { BOOST_LIGHTWEIGHT_TEST_OSTREAM << "exhaustive_collide_tests:\n"; collide_map m; exhaustive_erase_tests((collide_map*)0, 4, 4); BOOST_LIGHTWEIGHT_TEST_OSTREAM << "\n"; } UNORDERED_AUTO_TEST (exhaustive_collide2_tests) { BOOST_LIGHTWEIGHT_TEST_OSTREAM << "exhaustive_collide2_tests:\n"; exhaustive_erase_tests((collide_map2*)0, 8, 4); BOOST_LIGHTWEIGHT_TEST_OSTREAM << "\n"; } UNORDERED_AUTO_TEST (exhaustive_collide3_tests) { BOOST_LIGHTWEIGHT_TEST_OSTREAM << "exhaustive_collide3_tests:\n"; exhaustive_erase_tests((collide_map3*)0, 8, 4); BOOST_LIGHTWEIGHT_TEST_OSTREAM << "\n"; } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/simple_tests.cpp
// 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) // This test checks the runtime requirements of containers. #include "../helpers/unordered.hpp" #include "../helpers/equivalent.hpp" #include "../helpers/generators.hpp" #include "../helpers/test.hpp" #include <algorithm> #include <cstdlib> test::seed_t initialize_seed(14878); template <class X> void simple_test(X const& a) { test::unordered_equivalence_tester<X> equivalent(a); { X u; BOOST_TEST(u.size() == 0); BOOST_TEST(X().size() == 0); } { BOOST_TEST(equivalent(X(a))); } { X u(a); BOOST_TEST(equivalent(u)); } { X u = a; BOOST_TEST(equivalent(u)); } { X b(a); BOOST_TEST(b.begin() == const_cast<X const&>(b).cbegin()); BOOST_TEST(b.end() == const_cast<X const&>(b).cend()); } { X b(a); X c; BOOST_TEST(equivalent(b)); BOOST_TEST(c.empty()); b.swap(c); BOOST_TEST(b.empty()); BOOST_TEST(equivalent(c)); b.swap(c); BOOST_TEST(c.empty()); BOOST_TEST(equivalent(b)); } { X u; X& r = u; BOOST_TEST(&(r = r) == &r); BOOST_TEST(r.empty()); BOOST_TEST(&(r = a) == &r); BOOST_TEST(equivalent(r)); BOOST_TEST(&(r = r) == &r); BOOST_TEST(equivalent(r)); } { BOOST_TEST(a.size() == static_cast<typename X::size_type>( std::distance(a.begin(), a.end()))); } { BOOST_TEST(a.empty() == (a.size() == 0)); } { BOOST_TEST(a.empty() == (a.begin() == a.end())); X u; BOOST_TEST(u.begin() == u.end()); } } template <class X> static void simple_set_tests(X*) { X x; simple_test(x); x.insert(1); x.insert(2); x.insert(1456); simple_test(x); } #ifndef BOOST_UNORDERED_FOA_TESTS template <class X> static void simple_multiset_tests(X*) { X x; simple_test(x); for (int i1 = 0; i1 < 1000; ++i1) { int count = rand() % 10, index = rand(); for (int j = 0; j < count; ++j) x.insert(index); } simple_test(x); } #endif template <class X> static void simple_map_tests(X*) { X x; for (int i2 = 0; i2 < 1000; ++i2) { x.insert(std::pair<const int, int>(rand(), rand())); } simple_test(x); } #ifndef BOOST_UNORDERED_FOA_TESTS template <class X> static void simple_multimap_tests(X*) { X x; for (int i3 = 0; i3 < 1000; ++i3) { int count = rand() % 10, index = rand(); for (int j = 0; j < count; ++j) x.insert(std::pair<const int, int>(index, rand())); } simple_test(x); } #endif #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_set<int>* flat_set; static boost::unordered_flat_map<int, int>* flat_map; static boost::unordered_node_set<int>* node_set; static boost::unordered_node_map<int, int>* node_map; UNORDERED_TEST(simple_map_tests, ((flat_map)(node_map))) UNORDERED_TEST(simple_set_tests, ((flat_set)(node_set))) #else static boost::unordered_set<int>* set; static boost::unordered_map<int, int>* map; static boost::unordered_multiset<int>* multiset; static boost::unordered_multimap<int, int>* multimap; UNORDERED_TEST(simple_set_tests, ((set))) UNORDERED_TEST(simple_map_tests, ((map))) UNORDERED_TEST(simple_multiset_tests, ((multiset))) UNORDERED_TEST(simple_multimap_tests, ((multimap))) #endif RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/explicit_alloc_ctor_tests.cpp
// Copyright 2024 Joaquin M Lopez Muoz. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at htT://www.boost.org/LICENSE_1_0.txt) #ifdef BOOST_UNORDERED_CFOA_TESTS #include <boost/unordered/concurrent_flat_map.hpp> #include <boost/unordered/concurrent_flat_set.hpp> #else #include "../helpers/unordered.hpp" #endif #include "../helpers/test.hpp" #include <boost/type_traits/make_void.hpp> #include <memory> #include <vector> template <class T> struct explicit_allocator { typedef T value_type; explicit_allocator() {} template <class U> explicit explicit_allocator(const explicit_allocator<U>&) {} template<class U> bool operator==(explicit_allocator<U> const &) const noexcept { return true; } template<class U> bool operator!=(explicit_allocator<U> const &) const noexcept { return false; } T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); } void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); } }; template<typename Container,typename = void> struct has_extract: std::false_type {}; template<typename Container> struct has_extract< Container, boost::void_t<typename Container::insert_return_type> >: std::true_type {}; template <class Container> void test_explicit_alloc_ctor_extract( Container&, std::false_type) { } template <class Container> void test_explicit_alloc_ctor_extract( Container& c, std::true_type) { auto n = c.extract(c.begin()); c.insert(std::move(n)); n = c.extract(typename Container::key_type()); c.insert(std::move(n)); } template <class Container> void test_explicit_alloc_ctor_extract(Container& c) { test_explicit_alloc_ctor_extract(c, has_extract<Container>()); } template <class Container> void test_explicit_alloc_ctor() { using key_type = typename Container::key_type; using value_type = typename Container::value_type; using hasher = typename Container::hasher; using allocator_type = typename Container::allocator_type; allocator_type al; std::initializer_list<value_type> il{}; hasher h; std::vector<value_type> v; Container c, c2(0), c3(v.begin(), v.end()), c4(c3), c5(std::move(c4)), c6(v.begin(), v.end(), al), c7(al), c8(c7, al), c9(std::move(c8), al), c10(il), c11(0, al), c12(0, h, al), c13(v.begin(), v.end(), 0, al), c14(v.begin(), v.end(), 0, h, al), c15(il, al), c16(il, 0, al), c17(il, 0, h, al); value_type x{}; key_type k{}; c = c2; c = std::move(c2); c = il; c.swap(c3); c.insert(x); #ifdef BOOST_UNORDERED_CFOA_TESTS (void) c.visit(k, [](const value_type&) {}); #else (void) c.find(k); #endif test_explicit_alloc_ctor_extract(c); } UNORDERED_AUTO_TEST (explicit_alloc_ctor) { #if defined(BOOST_UNORDERED_CFOA_TESTS) test_explicit_alloc_ctor<boost::concurrent_flat_map<int, int, boost::hash<int>, std::equal_to<int>, explicit_allocator<std::pair<const int, int> > > >(); test_explicit_alloc_ctor<boost::concurrent_flat_set< int, boost::hash<int>, std::equal_to<int>, explicit_allocator<int> > >(); #elif defined(BOOST_UNORDERED_FOA_TESTS) test_explicit_alloc_ctor<boost::unordered_flat_map<int, int, boost::hash<int>, std::equal_to<int>, explicit_allocator<std::pair<const int, int> > > >(); test_explicit_alloc_ctor<boost::unordered_flat_set< int, boost::hash<int>, std::equal_to<int>, explicit_allocator<int> > >(); test_explicit_alloc_ctor<boost::unordered_node_map<int, int, boost::hash<int>, std::equal_to<int>, explicit_allocator<std::pair<const int, int> > > >(); test_explicit_alloc_ctor<boost::unordered_node_set< int, boost::hash<int>, std::equal_to<int>, explicit_allocator<int> > >(); #else test_explicit_alloc_ctor<boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>, explicit_allocator<std::pair<const int, int> > > >(); test_explicit_alloc_ctor<boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>, explicit_allocator<std::pair<const int, int> > > >(); test_explicit_alloc_ctor<boost::unordered_set< int, boost::hash<int>, std::equal_to<int>, explicit_allocator<int> > >(); test_explicit_alloc_ctor<boost::unordered_multiset< int, boost::hash<int>, std::equal_to<int>, explicit_allocator<int> > >(); #endif } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/emplace_tests.cpp
// // Copyright 2016 Daniel James. // Copyright 2022 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/unordered.hpp" #include "../helpers/count.hpp" #include "../helpers/test.hpp" #include <boost/container_hash/hash.hpp> #include <boost/tuple/tuple.hpp> #include <string> #include <type_traits> // Test that various emplace methods work with different numbers of // arguments. BOOST_UNORDERED_STATIC_ASSERT(std::is_same<std::piecewise_construct_t, boost::unordered::piecewise_construct_t>::value); namespace emplace_tests { // Constructible with 2 to 10 arguments struct emplace_value : private test::counted_object { typedef int A0; typedef std::string A1; typedef char A2; typedef int A3; typedef int A4; typedef int A5; typedef int A6; typedef int A7; typedef int A8; typedef int A9; int arg_count; A0 a0; A1 a1; A2 a2; A3 a3; A4 a4; A5 a5; A6 a6; A7 a7; A8 a8; A9 a9; emplace_value(A0 const& b0, A1 const& b1) : arg_count(2), a0(b0), a1(b1), a2('\0'), a3(-1), a4(-1), a5(-1), a6(-1), a7(-1), a8(-1), a9(-1) { } emplace_value(A0 const& b0, A1 const& b1, A2 const& b2) : arg_count(3), a0(b0), a1(b1), a2(b2), a3(-1), a4(-1), a5(-1), a6(-1), a7(-1), a8(-1), a9(-1) { } emplace_value(A0 const& b0, A1 const& b1, A2 const& b2, A3 const& b3) : arg_count(4), a0(b0), a1(b1), a2(b2), a3(b3), a4(-1), a5(-1), a6(-1), a7(-1), a8(-1), a9(-1) { } emplace_value( A0 const& b0, A1 const& b1, A2 const& b2, A3 const& b3, A4 const& b4) : arg_count(5), a0(b0), a1(b1), a2(b2), a3(b3), a4(b4), a5(-1), a6(-1), a7(-1), a8(-1), a9(-1) { } emplace_value(A0 const& b0, A1 const& b1, A2 const& b2, A3 const& b3, A4 const& b4, A5 const& b5) : arg_count(6), a0(b0), a1(b1), a2(b2), a3(b3), a4(b4), a5(b5), a6(-1), a7(-1), a8(-1), a9(-1) { } emplace_value(A0 const& b0, A1 const& b1, A2 const& b2, A3 const& b3, A4 const& b4, A5 const& b5, A6 const& b6) : arg_count(7), a0(b0), a1(b1), a2(b2), a3(b3), a4(b4), a5(b5), a6(b6), a7(-1), a8(-1), a9(-1) { } emplace_value(A0 const& b0, A1 const& b1, A2 const& b2, A3 const& b3, A4 const& b4, A5 const& b5, A6 const& b6, A7 const& b7) : arg_count(8), a0(b0), a1(b1), a2(b2), a3(b3), a4(b4), a5(b5), a6(b6), a7(b7), a8(-1), a9(-1) { } emplace_value(A0 const& b0, A1 const& b1, A2 const& b2, A3 const& b3, A4 const& b4, A5 const& b5, A6 const& b6, A7 const& b7, A8 const& b8) : arg_count(9), a0(b0), a1(b1), a2(b2), a3(b3), a4(b4), a5(b5), a6(b6), a7(b7), a8(b8), a9(-1) { } emplace_value(A0 const& b0, A1 const& b1, A2 const& b2, A3 const& b3, A4 const& b4, A5 const& b5, A6 const& b6, A7 const& b7, A8 const& b8, A9 const& b9) : arg_count(10), a0(b0), a1(b1), a2(b2), a3(b3), a4(b4), a5(b5), a6(b6), a7(b7), a8(b8), a9(b9) { } friend std::size_t hash_value(emplace_value const& x) { std::size_t r1 = 23894278u; if (x.arg_count >= 1) boost::hash_combine(r1, x.a0); if (x.arg_count >= 2) boost::hash_combine(r1, x.a1); if (x.arg_count >= 3) boost::hash_combine(r1, x.a2); if (x.arg_count >= 4) boost::hash_combine(r1, x.a3); if (x.arg_count >= 5) boost::hash_combine(r1, x.a4); if (x.arg_count >= 6) boost::hash_combine(r1, x.a5); if (x.arg_count >= 7) boost::hash_combine(r1, x.a6); if (x.arg_count >= 8) boost::hash_combine(r1, x.a7); if (x.arg_count >= 9) boost::hash_combine(r1, x.a8); if (x.arg_count >= 10) boost::hash_combine(r1, x.a9); return r1; } friend bool operator==(emplace_value const& x, emplace_value const& y) { if (x.arg_count != y.arg_count) { return false; } if (x.arg_count >= 1 && x.a0 != y.a0) { return false; } if (x.arg_count >= 2 && x.a1 != y.a1) { return false; } if (x.arg_count >= 3 && x.a2 != y.a2) { return false; } if (x.arg_count >= 4 && x.a3 != y.a3) { return false; } if (x.arg_count >= 5 && x.a4 != y.a4) { return false; } if (x.arg_count >= 6 && x.a5 != y.a5) { return false; } if (x.arg_count >= 7 && x.a6 != y.a6) { return false; } if (x.arg_count >= 8 && x.a7 != y.a7) { return false; } if (x.arg_count >= 9 && x.a8 != y.a8) { return false; } if (x.arg_count >= 10 && x.a9 != y.a9) { return false; } return true; } #ifdef BOOST_UNORDERED_FOA_TESTS emplace_value() = delete; emplace_value(emplace_value const&) = default; emplace_value(emplace_value&&) = default; #else private: emplace_value(); emplace_value(emplace_value const&); #endif }; template <class X> static void emplace_set(X*) { test::check_instances check_; typedef X container; typedef typename container::iterator iterator; typedef std::pair<iterator, bool> return_type; container x(10); iterator i1; return_type r1, r2; // 2 args emplace_value v1(10, "x"); r1 = x.emplace(10, std::string("x")); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(r1.second); BOOST_TEST(*r1.first == v1); BOOST_TEST(r1.first == x.find(v1)); BOOST_TEST_EQ(check_.instances(), 2); #ifdef BOOST_UNORDERED_FOA_TESTS // 1 from v1 // 1 from constructing the value_type() so we can pluck the key // 1 from move-constructing which invokes the counted_object base // constructor BOOST_TEST_EQ(check_.constructions(), 3); #else BOOST_TEST_EQ(check_.constructions(), 2); #endif // 3 args emplace_value v2(3, "foo", 'a'); r1 = x.emplace(3, "foo", 'a'); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(r1.second); BOOST_TEST(*r1.first == v2); BOOST_TEST(r1.first == x.find(v2)); BOOST_TEST_EQ(check_.instances(), 4); #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST_EQ(check_.constructions(), 6); #else BOOST_TEST_EQ(check_.constructions(), 4); #endif // 7 args with hint + duplicate emplace_value v3(25, "something", 'z', 4, 5, 6, 7); i1 = x.emplace_hint(r1.first, 25, "something", 'z', 4, 5, 6, 7); BOOST_TEST_EQ(x.size(), 3u); BOOST_TEST(*i1 == v3); BOOST_TEST(i1 == x.find(v3)); BOOST_TEST_EQ(check_.instances(), 6); #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST_EQ(check_.constructions(), 9); #else BOOST_TEST_EQ(check_.constructions(), 6); #endif r2 = x.emplace(25, "something", 'z', 4, 5, 6, 7); BOOST_TEST_EQ(x.size(), 3u); BOOST_TEST(!r2.second); BOOST_TEST(i1 == r2.first); // The container has to construct an object in order to check // whether it can emplace, so there's an extra construction // here. BOOST_TEST_EQ(check_.instances(), 6); #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST_EQ(check_.constructions(), 10); #else BOOST_TEST_EQ(check_.constructions(), 7); #endif // 10 args + hint duplicate std::string s1; emplace_value v4(10, s1, 'a', 4, 5, 6, 7, 8, 9, 10); r1 = x.emplace(10, s1, 'a', 4, 5, 6, 7, 8, 9, 10); BOOST_TEST_EQ(x.size(), 4u); BOOST_TEST(r1.second); BOOST_TEST(*r1.first == v4); BOOST_TEST(r1.first == x.find(v4)); BOOST_TEST_EQ(check_.instances(), 8); #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST_EQ(check_.constructions(), 13); #else BOOST_TEST_EQ(check_.constructions(), 9); #endif BOOST_TEST( r1.first == x.emplace_hint(r1.first, 10, "", 'a', 4, 5, 6, 7, 8, 9, 10)); BOOST_TEST( r1.first == x.emplace_hint(r2.first, 10, "", 'a', 4, 5, 6, 7, 8, 9, 10)); BOOST_TEST( r1.first == x.emplace_hint(x.end(), 10, "", 'a', 4, 5, 6, 7, 8, 9, 10)); BOOST_TEST_EQ(check_.instances(), 8); #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST_EQ(check_.constructions(), 16); #else BOOST_TEST_EQ(check_.constructions(), 12); #endif BOOST_TEST_EQ(x.size(), 4u); BOOST_TEST(x.count(v1) == 1); BOOST_TEST(x.count(v2) == 1); BOOST_TEST(x.count(v3) == 1); BOOST_TEST(x.count(v4) == 1); } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_set<emplace_value, boost::hash<emplace_value> >* test_set; static boost::unordered_node_set<emplace_value, boost::hash<emplace_value> >* test_node_set; UNORDERED_TEST(emplace_set, ((test_set)(test_node_set))) #else static boost::unordered_set<emplace_value, boost::hash<emplace_value> >* test_set; UNORDERED_TEST(emplace_set, ((test_set))) #endif #ifndef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (emplace_multiset) { test::check_instances check_; typedef boost::unordered_multiset<emplace_value, boost::hash<emplace_value> > container; typedef container::iterator iterator; container x(10); iterator i1, i2; // 2 args. emplace_value v1(10, "x"); i1 = x.emplace(10, std::string("x")); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(i1 == x.find(v1)); BOOST_TEST_EQ(check_.instances(), 2); BOOST_TEST_EQ(check_.constructions(), 2); // 4 args + duplicate emplace_value v2(4, "foo", 'a', 15); i1 = x.emplace(4, "foo", 'a', 15); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(i1 == x.find(v2)); BOOST_TEST_EQ(check_.instances(), 4); BOOST_TEST_EQ(check_.constructions(), 4); i2 = x.emplace(4, "foo", 'a', 15); BOOST_TEST_EQ(x.size(), 3u); BOOST_TEST(i1 != i2); BOOST_TEST(*i1 == *i2); BOOST_TEST(x.count(*i1) == 2); BOOST_TEST_EQ(check_.instances(), 5); BOOST_TEST_EQ(check_.constructions(), 5); // 7 args + duplicate using hint. emplace_value v3(7, "", 'z', 4, 5, 6, 7); i1 = x.emplace(7, "", 'z', 4, 5, 6, 7); BOOST_TEST_EQ(x.size(), 4u); BOOST_TEST_EQ(i1->a2, 'z'); BOOST_TEST(x.count(*i1) == 1); BOOST_TEST(i1 == x.find(v3)); BOOST_TEST_EQ(check_.instances(), 7); BOOST_TEST_EQ(check_.constructions(), 7); i2 = x.emplace_hint(i1, 7, "", 'z', 4, 5, 6, 7); BOOST_TEST_EQ(x.size(), 5u); BOOST_TEST(*i1 == *i2); BOOST_TEST(i1 != i2); BOOST_TEST(x.count(*i1) == 2); BOOST_TEST_EQ(check_.instances(), 8); BOOST_TEST_EQ(check_.constructions(), 8); // 10 args with bad hint + duplicate emplace_value v4(10, "", 'a', 4, 5, 6, 7, 8, 9, 10); i1 = x.emplace_hint(i2, 10, "", 'a', 4, 5, 6, 7, 8, 9, 10); BOOST_TEST_EQ(x.size(), 6u); BOOST_TEST_EQ(i1->arg_count, 10); BOOST_TEST(i1 == x.find(v4)); BOOST_TEST_EQ(check_.instances(), 10); BOOST_TEST_EQ(check_.constructions(), 10); i2 = x.emplace_hint(x.end(), 10, "", 'a', 4, 5, 6, 7, 8, 9, 10); BOOST_TEST_EQ(x.size(), 7u); BOOST_TEST(*i1 == *i2); BOOST_TEST(i1 != i2); BOOST_TEST(x.count(*i1) == 2); BOOST_TEST_EQ(check_.instances(), 11); BOOST_TEST_EQ(check_.constructions(), 11); BOOST_TEST_EQ(x.count(v1), 1u); BOOST_TEST_EQ(x.count(v2), 2u); BOOST_TEST_EQ(x.count(v3), 2u); } #endif template <class X> static void emplace_map(X*) { #ifdef BOOST_UNORDERED_FOA_TESTS test::check_instances check_; typedef X container; typedef typename container::iterator iterator; typedef std::pair<iterator, bool> return_type; container x(10); return_type r1, r2; // 5/8 args + duplicate emplace_value k1(5, "", 'b', 4, 5); BOOST_TEST_EQ(check_.constructions(), 1); emplace_value m1(8, "xxx", 'z', 4, 5, 6, 7, 8); BOOST_TEST_EQ(check_.constructions(), 2); r1 = x.emplace(std::piecewise_construct, std::make_tuple(5, "", 'b', 4, 5), std::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(r1.second); BOOST_TEST(x.find(k1) == r1.first); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST_EQ(check_.instances(), 4); BOOST_TEST_EQ(check_.constructions(), 6); r2 = x.emplace(std::piecewise_construct, std::make_tuple(5, "", 'b', 4, 5), std::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(!r2.second); BOOST_TEST(r1.first == r2.first); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST_EQ(check_.instances(), 4); // constructions could possibly be 5 if the implementation only // constructed the key. BOOST_TEST_EQ(check_.constructions(), 8); // 9/3 args + duplicates with hints, different mapped value. emplace_value k2(9, "", 'b', 4, 5, 6, 7, 8, 9); emplace_value m2(3, "aaa", 'm'); r1 = x.emplace(std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(3, "aaa", 'm')); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(r1.second); BOOST_TEST(r1.first->first.arg_count == 9); BOOST_TEST(r1.first->second.arg_count == 3); BOOST_TEST(x.find(k2) == r1.first); BOOST_TEST(x.find(k2)->second == m2); BOOST_TEST_EQ(check_.instances(), 8); BOOST_TEST_EQ(check_.constructions(), 14); BOOST_TEST(r1.first == x.emplace_hint(r1.first, std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(15, "jkjk"))); BOOST_TEST(r1.first == x.emplace_hint(r2.first, std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(275, "xxx", 'm', 6))); BOOST_TEST(r1.first == x.emplace_hint(x.end(), std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(-10, "blah blah", '\0'))); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(x.find(k2)->second == m2); BOOST_TEST_EQ(check_.instances(), 8); BOOST_TEST_EQ(check_.constructions(), 20); #else { test::check_instances check_; typedef X container; typedef typename container::iterator iterator; typedef std::pair<iterator, bool> return_type; container x(10); return_type r1, r2; // 5/8 args + duplicate emplace_value k1(5, "", 'b', 4, 5); emplace_value m1(8, "xxx", 'z', 4, 5, 6, 7, 8); r1 = x.emplace(std::piecewise_construct, std::make_tuple(5, "", 'b', 4, 5), std::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(r1.second); BOOST_TEST(x.find(k1) == r1.first); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST_EQ(check_.instances(), 4); BOOST_TEST_EQ(check_.constructions(), 4); r2 = x.emplace(std::piecewise_construct, std::make_tuple(5, "", 'b', 4, 5), std::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(!r2.second); BOOST_TEST(r1.first == r2.first); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST_EQ(check_.instances(), 4); // constructions could possibly be 5 if the implementation only // constructed the key. BOOST_TEST_EQ(check_.constructions(), 6); // 9/3 args + duplicates with hints, different mapped value. emplace_value k2(9, "", 'b', 4, 5, 6, 7, 8, 9); emplace_value m2(3, "aaa", 'm'); r1 = x.emplace(std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(3, "aaa", 'm')); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(r1.second); BOOST_TEST(r1.first->first.arg_count == 9); BOOST_TEST(r1.first->second.arg_count == 3); BOOST_TEST(x.find(k2) == r1.first); BOOST_TEST(x.find(k2)->second == m2); BOOST_TEST_EQ(check_.instances(), 8); BOOST_TEST_EQ(check_.constructions(), 10); BOOST_TEST(r1.first == x.emplace_hint(r1.first, std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(15, "jkjk"))); BOOST_TEST(r1.first == x.emplace_hint(r2.first, std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(275, "xxx", 'm', 6))); BOOST_TEST(r1.first == x.emplace_hint(x.end(), std::piecewise_construct, std::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), std::make_tuple(-10, "blah blah", '\0'))); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(x.find(k2)->second == m2); BOOST_TEST_EQ(check_.instances(), 8); BOOST_TEST_EQ(check_.constructions(), 16); } { test::check_instances check_; typedef X container; typedef typename container::iterator iterator; typedef std::pair<iterator, bool> return_type; container x(10); return_type r1, r2; // 5/8 args + duplicate emplace_value k1(5, "", 'b', 4, 5); emplace_value m1(8, "xxx", 'z', 4, 5, 6, 7, 8); r1 = x.emplace(boost::unordered::piecewise_construct, boost::make_tuple(5, "", 'b', 4, 5), boost::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(r1.second); BOOST_TEST(x.find(k1) == r1.first); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST_EQ(check_.instances(), 4); BOOST_TEST_EQ(check_.constructions(), 4); r2 = x.emplace(boost::unordered::piecewise_construct, boost::make_tuple(5, "", 'b', 4, 5), boost::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(!r2.second); BOOST_TEST(r1.first == r2.first); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST_EQ(check_.instances(), 4); // constructions could possibly be 5 if the implementation only // constructed the key. BOOST_TEST_EQ(check_.constructions(), 6); // 9/3 args + duplicates with hints, different mapped value. emplace_value k2(9, "", 'b', 4, 5, 6, 7, 8, 9); emplace_value m2(3, "aaa", 'm'); r1 = x.emplace(boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(3, "aaa", 'm')); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(r1.second); BOOST_TEST(r1.first->first.arg_count == 9); BOOST_TEST(r1.first->second.arg_count == 3); BOOST_TEST(x.find(k2) == r1.first); BOOST_TEST(x.find(k2)->second == m2); BOOST_TEST_EQ(check_.instances(), 8); BOOST_TEST_EQ(check_.constructions(), 10); BOOST_TEST(r1.first == x.emplace_hint(r1.first, boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(15, "jkjk"))); BOOST_TEST(r1.first == x.emplace_hint(r2.first, boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(275, "xxx", 'm', 6))); BOOST_TEST(r1.first == x.emplace_hint(x.end(), boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(-10, "blah blah", '\0'))); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(x.find(k2)->second == m2); BOOST_TEST_EQ(check_.instances(), 8); BOOST_TEST_EQ(check_.constructions(), 16); } #endif } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_map<emplace_value, emplace_value, boost::hash<emplace_value> >* test_map; static boost::unordered_node_map<emplace_value, emplace_value, boost::hash<emplace_value> >* test_node_map; UNORDERED_TEST(emplace_map, ((test_map)(test_node_map))) #else static boost::unordered_map<emplace_value, emplace_value, boost::hash<emplace_value> >* test_map; UNORDERED_TEST(emplace_map, ((test_map))) #endif #ifndef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (emplace_multimap) { test::check_instances check_; typedef boost::unordered_multimap<emplace_value, emplace_value, boost::hash<emplace_value> > container; typedef container::iterator iterator; container x(10); iterator i1, i2, i3, i4; // 5/8 args + duplicate emplace_value k1(5, "", 'b', 4, 5); emplace_value m1(8, "xxx", 'z', 4, 5, 6, 7, 8); i1 = x.emplace(boost::unordered::piecewise_construct, boost::make_tuple(5, "", 'b', 4, 5), boost::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST(x.find(k1) == i1); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST_EQ(check_.instances(), 4); BOOST_TEST_EQ(check_.constructions(), 4); emplace_value m1a(8, "xxx", 'z', 4, 5, 6, 7, 8); i2 = x.emplace(boost::unordered::piecewise_construct, boost::make_tuple(5, "", 'b', 4, 5), boost::make_tuple(8, "xxx", 'z', 4, 5, 6, 7, 8)); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST(i1 != i2); BOOST_TEST(i1->second == m1); BOOST_TEST(i2->second == m1a); BOOST_TEST_EQ(check_.instances(), 7); BOOST_TEST_EQ(check_.constructions(), 7); // 9/3 args + duplicates with hints, different mapped value. emplace_value k2(9, "", 'b', 4, 5, 6, 7, 8, 9); emplace_value m2(3, "aaa", 'm'); i1 = x.emplace(boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(3, "aaa", 'm')); BOOST_TEST_EQ(x.size(), 3u); BOOST_TEST(i1->first.arg_count == 9); BOOST_TEST(i1->second.arg_count == 3); BOOST_TEST_EQ(check_.instances(), 11); BOOST_TEST_EQ(check_.constructions(), 11); emplace_value m2a(15, "jkjk"); i2 = x.emplace_hint(i2, boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(15, "jkjk")); emplace_value m2b(275, "xxx", 'm', 6); i3 = x.emplace_hint(i1, boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(275, "xxx", 'm', 6)); emplace_value m2c(-10, "blah blah", '\0'); i4 = x.emplace_hint(x.end(), boost::unordered::piecewise_construct, boost::make_tuple(9, "", 'b', 4, 5, 6, 7, 8, 9), boost::make_tuple(-10, "blah blah", '\0')); BOOST_TEST_EQ(x.size(), 6u); BOOST_TEST(x.find(k2)->second == m2); BOOST_TEST_EQ(check_.instances(), 20); BOOST_TEST_EQ(check_.constructions(), 20); } #endif template <class X> static void try_emplace(X*) { test::check_instances check_; typedef X container; typedef typename container::iterator iterator; typedef std::pair<iterator, bool> return_type; container x(10); return_type r1, r2, r3; int k1 = 3; emplace_value m1(414, "grr"); r1 = x.try_emplace(3, 414, "grr"); BOOST_TEST(r1.second); BOOST_TEST(r1.first->first == k1); BOOST_TEST(r1.first->second == m1); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST_EQ(check_.instances(), 2); BOOST_TEST_EQ(check_.constructions(), 2); int k2 = 10; emplace_value m2(25, "", 'z'); r2 = x.try_emplace(10, 25, std::string(""), 'z'); BOOST_TEST(r2.second); BOOST_TEST(r2.first->first == k2); BOOST_TEST(r2.first->second == m2); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST_EQ(check_.instances(), 4); BOOST_TEST_EQ(check_.constructions(), 4); BOOST_TEST(x.find(k1)->second == m1); BOOST_TEST(x.find(k2)->second == m2); r3 = x.try_emplace(k2, 68, "jfeoj", 'p', 49309, 2323); BOOST_TEST(!r3.second); BOOST_TEST(r3.first == r2.first); BOOST_TEST(r3.first->second == m2); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST_EQ(check_.instances(), 4); BOOST_TEST_EQ(check_.constructions(), 4); BOOST_TEST(r2.first == x.try_emplace(r2.first, k2, 808709, "what")); BOOST_TEST(r2.first == x.try_emplace(r2.first, k2, 10, "xxx", 'a', 4, 5, 6, 7, 8, 9, 10)); BOOST_TEST(r2.first->second == m2); BOOST_TEST_EQ(x.size(), 2u); } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_map<int, emplace_value>* test_int_map; static boost::unordered_node_map<int, emplace_value>* test_int_node_map; UNORDERED_TEST(try_emplace, ((test_int_map)(test_int_node_map))) #else static boost::unordered_map<int, emplace_value>* test_int_map; UNORDERED_TEST(try_emplace, ((test_int_map))) #endif } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/emplace_smf_tests.cpp
// // Copyright 2023-2024 Braden Ganetsky. // 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/unordered.hpp" #include "../helpers/count.hpp" #include "../helpers/test.hpp" namespace emplace_smf_tests { using test::smf_count; using test::smf_counted_object; using converting_key = smf_counted_object<struct cvt_key_tag_>; using converting_value = smf_counted_object<struct cvt_value_tag_>; class counted_key : public smf_counted_object<struct key_tag_> { public: using smf_counted_object::smf_counted_object; counted_key() = default; counted_key(const converting_key& k) : counted_key(k.index_) {} }; class counted_value : public smf_counted_object<struct value_tag_> { public: using smf_counted_object::smf_counted_object; counted_value() = default; counted_value(const converting_value& v) : counted_value(v.index_) {} }; class immovable_key : public smf_counted_object<struct imm_key_tag_> { public: using smf_counted_object::smf_counted_object; immovable_key(immovable_key&&) = delete; immovable_key& operator=(immovable_key&&) = delete; }; class immovable_value : public smf_counted_object<struct imm_value_tag_> { public: using smf_counted_object::smf_counted_object; immovable_value(immovable_value&&) = delete; immovable_value& operator=(immovable_value&&) = delete; }; void reset_counts() { counted_key::reset_count(); counted_value::reset_count(); converting_key::reset_count(); converting_value::reset_count(); immovable_key::reset_count(); immovable_value::reset_count(); } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_map<counted_key, counted_value>* test_smf_map; static boost::unordered_node_map<counted_key, counted_value>* test_smf_node_map; static boost::unordered_flat_set<counted_value>* test_smf_set; static boost::unordered_node_set<counted_value>* test_smf_node_set; #define EMPLACE_SMF_TESTS_MAP_ARGS ((test_smf_map)(test_smf_node_map)) #define EMPLACE_SMF_TESTS_SET_ARGS ((test_smf_set)(test_smf_node_set)) #else static boost::unordered_map<counted_key, counted_value>* test_smf_map; static boost::unordered_set<counted_value>* test_smf_set; #define EMPLACE_SMF_TESTS_MAP_ARGS ((test_smf_map)) #define EMPLACE_SMF_TESTS_SET_ARGS ((test_smf_set)) #endif template <class X> static void emplace_smf_value_type_map(X*) { using container = X; using value_type = typename container::value_type; container x; { value_type val{counted_key{}, counted_value{}}; x.clear(); reset_counts(); x.emplace(val); BOOST_TEST_EQ(counted_key::count, (smf_count{0, 1, 0, 0, 0, 0})); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 1, 0, 0, 0, 0})); } { value_type val{counted_key{}, counted_value{}}; x.clear(); reset_counts(); x.emplace(std::move(val)); BOOST_TEST_EQ(counted_key::count, (smf_count{0, 1, 0, 0, 0, 0})); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 0, 1, 0, 0, 0})); } { x.clear(); reset_counts(); x.emplace(value_type{counted_key{}, counted_value{}}); BOOST_TEST_EQ(counted_key::count, (smf_count{1, 1, 1, 0, 0, 2})); BOOST_TEST_EQ(counted_value::count, (smf_count{1, 0, 2, 0, 0, 2})); } { counted_key key{}; counted_value value{}; x.clear(); reset_counts(); x.emplace(value_type{std::move(key), std::move(value)}); BOOST_TEST_EQ(counted_key::count, (smf_count{0, 1, 1, 0, 0, 1})); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 0, 2, 0, 0, 1})); } } UNORDERED_TEST(emplace_smf_value_type_map, EMPLACE_SMF_TESTS_MAP_ARGS) template <class X> static void emplace_smf_init_type_map(X*) { using container = X; #ifdef BOOST_UNORDERED_FOA_TESTS using init_type = typename container::init_type; #else using raw_key = typename std::remove_const<typename container::key_type>::type; using init_type = std::pair<raw_key, typename container::mapped_type>; #endif container x; { init_type val{counted_key{}, counted_value{}}; x.clear(); reset_counts(); x.emplace(val); BOOST_TEST_EQ(counted_key::count, (smf_count{0, 1, 0, 0, 0, 0})); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 1, 0, 0, 0, 0})); } { init_type val{counted_key{}, counted_value{}}; x.clear(); reset_counts(); x.emplace(std::move(val)); BOOST_TEST_EQ(counted_key::count, (smf_count{0, 0, 1, 0, 0, 0})); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 0, 1, 0, 0, 0})); } { x.clear(); reset_counts(); x.emplace(init_type{counted_key{}, counted_value{}}); BOOST_TEST_EQ(counted_key::count, (smf_count{1, 0, 2, 0, 0, 2})); BOOST_TEST_EQ(counted_value::count, (smf_count{1, 0, 2, 0, 0, 2})); } { counted_key key{}; counted_value value{}; x.clear(); reset_counts(); x.emplace(init_type{std::move(key), std::move(value)}); BOOST_TEST_EQ(counted_key::count, (smf_count{0, 0, 2, 0, 0, 1})); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 0, 2, 0, 0, 1})); } } UNORDERED_TEST(emplace_smf_init_type_map, EMPLACE_SMF_TESTS_MAP_ARGS) template <class X> static void emplace_smf_value_type_set(X*) { using container = X; using value_type = typename container::value_type; #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_STATIC_ASSERT( std::is_same<value_type, typename container::init_type>::value); #endif BOOST_STATIC_ASSERT(std::is_same<value_type, counted_value>::value); container x; { counted_value val{}; x.clear(); reset_counts(); x.emplace(val); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 1, 0, 0, 0, 0})); } { counted_value val{}; x.clear(); reset_counts(); x.emplace(std::move(val)); BOOST_TEST_EQ(counted_value::count, (smf_count{0, 0, 1, 0, 0, 0})); } { x.clear(); reset_counts(); x.emplace(counted_value{}); BOOST_TEST_EQ(counted_value::count, (smf_count{1, 0, 1, 0, 0, 1})); } } UNORDERED_TEST(emplace_smf_value_type_set, EMPLACE_SMF_TESTS_SET_ARGS) enum emplace_kind { copy, move }; enum emplace_status { fail, success }; struct counted_key_checker_type { using key_type = counted_key; void operator()(emplace_kind kind, emplace_status status) { int copies = (kind == copy && status == success) ? 1 : 0; int moves = (kind == move && status == success) ? 1 : 0; BOOST_TEST_EQ(counted_key::count, (smf_count{0, copies, moves, 0, 0, 0})); } } counted_key_checker; struct converting_key_checker_type { using key_type = converting_key; void operator()(emplace_kind, emplace_status status) { int moves = (status == success) ? 1 : 0; BOOST_TEST_EQ(counted_key::count, (smf_count{1, 0, moves, 0, 0, 1})); } } converting_key_checker; struct counted_value_checker_type { using mapped_type = counted_value; void operator()(emplace_kind kind, emplace_status status) { int copies = (kind == copy && status == success) ? 1 : 0; int moves = (kind == move && status == success) ? 1 : 0; BOOST_TEST_EQ( counted_value::count, (smf_count{0, copies, moves, 0, 0, 0})); } } counted_value_checker; struct converting_value_checker_type { using mapped_type = converting_value; void operator()(emplace_kind, emplace_status status) { int ctors = (status == success) ? 1 : 0; BOOST_TEST_EQ(counted_value::count, (smf_count{ctors, 0, 0, 0, 0, 0})); } } converting_value_checker; template <class X, class KC, class VC> static void emplace_smf_key_value_map( X*, emplace_kind kind, KC key_checker, VC value_checker) { using container = X; using key_type = typename KC::key_type; using mapped_type = typename VC::mapped_type; container x; key_type key{}; key_type key2 = key; mapped_type value{}; mapped_type value2 = value; { reset_counts(); auto ret = (kind == copy) ? x.emplace(key, value) : x.emplace(std::move(key), std::move(value)); BOOST_TEST_EQ(ret.second, true); key_checker(kind, success); value_checker(kind, success); BOOST_TEST_EQ(converting_key::count, (smf_count{0, 0, 0, 0, 0, 0})); BOOST_TEST_EQ(converting_value::count, (smf_count{0, 0, 0, 0, 0, 0})); } { reset_counts(); auto ret = x.emplace(key2, value2); BOOST_TEST_EQ(ret.second, false); key_checker(kind, fail); value_checker(kind, fail); BOOST_TEST_EQ(converting_key::count, (smf_count{0, 0, 0, 0, 0, 0})); BOOST_TEST_EQ(converting_value::count, (smf_count{0, 0, 0, 0, 0, 0})); } { reset_counts(); auto ret = x.emplace(std::move(key2), std::move(value2)); BOOST_TEST_EQ(ret.second, false); key_checker(kind, fail); value_checker(kind, fail); BOOST_TEST_EQ(converting_key::count, (smf_count{0, 0, 0, 0, 0, 0})); BOOST_TEST_EQ(converting_value::count, (smf_count{0, 0, 0, 0, 0, 0})); } } // clang-format off UNORDERED_TEST( emplace_smf_key_value_map, EMPLACE_SMF_TESTS_MAP_ARGS ((copy)(move)) ((counted_key_checker)(converting_key_checker)) ((counted_value_checker)(converting_value_checker)) ) // clang-format on template <class X> static void emplace_smf_key_value_map_immovable_key(X*) { #ifndef BOOST_UNORDERED_NO_INIT_TYPE_TESTS using container = X; BOOST_STATIC_ASSERT( std::is_same<immovable_key, typename X::key_type>::value); using mapped_type = typename X::mapped_type; container x; { reset_counts(); auto ret = x.emplace(0, 0); BOOST_TEST_EQ(ret.second, true); BOOST_TEST_EQ(immovable_key::count, (smf_count{1, 0, 0, 0, 0, 0})); BOOST_TEST_EQ(mapped_type::count, (smf_count{1, 0, 0, 0, 0, 0})); } { reset_counts(); auto ret = x.emplace(0, 1); BOOST_TEST_EQ(ret.second, false); BOOST_TEST_EQ(immovable_key::count, (smf_count{1, 0, 0, 0, 0, 1})); BOOST_TEST_EQ(mapped_type::count, (smf_count{1, 0, 0, 0, 0, 1})); } #endif } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_node_map<immovable_key, counted_value>* test_smf_node_map_immovable_key_counted_value; static boost::unordered_node_map<immovable_key, immovable_value>* test_smf_node_map_immovable_key_immovable_value; #define EMPLACE_SMF_TESTS_MAP_IMMOVABLE_ARGS \ ((test_smf_node_map_immovable_key_counted_value)(test_smf_node_map_immovable_key_immovable_value)) #else static boost::unordered_map<immovable_key, counted_value>* test_smf_map_immovable_key_counted_value; static boost::unordered_map<immovable_key, immovable_value>* test_smf_map_immovable_key_immovable_value; #define EMPLACE_SMF_TESTS_MAP_IMMOVABLE_ARGS \ ((test_smf_map_immovable_key_counted_value)(test_smf_map_immovable_key_immovable_value)) #endif // clang-format off UNORDERED_TEST( emplace_smf_key_value_map_immovable_key, EMPLACE_SMF_TESTS_MAP_IMMOVABLE_ARGS ) // clang-format on } // namespace emplace_smf_tests RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/erase_if.cpp
// Copyright 2021-2022 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/unordered.hpp" #include "../helpers/test.hpp" #include <boost/config.hpp> #include <string> #define UNORDERED_LVALUE_QUAL & namespace test { struct is_even { bool operator()(std::pair<std::string const, int>& key_value) { int const v = key_value.second; return (v % 2 == 0); } bool operator()(int const& value) { int const v = value; return (v % 2 == 0); } }; struct is_too_large { bool operator()(std::pair<std::string const, int>& key_value) { int const v = key_value.second; return v >= 1000; } bool operator()(int const& value) { int const v = value; return v >= 1000; } }; } // namespace test template <class UnorderedMap> void test_map_erase_if() { typedef UnorderedMap map_type; typedef typename map_type::size_type size_type; map_type map; size_type num_erased = erase_if(map, test::is_even()); BOOST_TEST(map.empty()); BOOST_TEST_EQ(num_erased, 0u); map.emplace("a", 1); map.emplace("b", 2); map.emplace("b", 4); map.emplace("b", 8); map.emplace("b", 16); map.emplace("c", 3); size_type size = map.size(); num_erased = erase_if(map, test::is_too_large()); BOOST_TEST_EQ(map.size(), size); BOOST_TEST_EQ(num_erased, 0u); num_erased = erase_if(map, test::is_even()); BOOST_TEST_EQ(map.size(), 2u); BOOST_TEST_EQ(num_erased, size - map.size()); } template <class UnorderedSet> void test_set_erase_if() { typedef UnorderedSet set_type; typedef typename set_type::size_type size_type; set_type set; size_type num_erased = erase_if(set, test::is_even()); BOOST_TEST(set.empty()); BOOST_TEST_EQ(num_erased, 0u); set.emplace(1); set.emplace(2); set.emplace(2); set.emplace(2); set.emplace(2); set.emplace(3); size_type size = set.size(); num_erased = erase_if(set, test::is_too_large()); BOOST_TEST_EQ(set.size(), size); BOOST_TEST_EQ(num_erased, 0u); num_erased = erase_if(set, test::is_even()); BOOST_TEST_EQ(set.size(), 2u); BOOST_TEST_EQ(num_erased, size - set.size()); } UNORDERED_AUTO_TEST (unordered_erase_if) { #ifdef BOOST_UNORDERED_FOA_TESTS test_map_erase_if<boost::unordered_flat_map<std::string, int> >(); test_set_erase_if<boost::unordered_flat_set<int> >(); test_map_erase_if<boost::unordered_node_map<std::string, int> >(); test_set_erase_if<boost::unordered_node_set<int> >(); #else test_map_erase_if<boost::unordered_map<std::string, int> >(); test_map_erase_if<boost::unordered_multimap<std::string, int> >(); test_set_erase_if<boost::unordered_set<int> >(); test_set_erase_if<boost::unordered_multiset<int> >(); #endif } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/compile_set.cpp
// 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) // This test creates the containers with members that meet their minimum // requirements. Makes sure everything compiles and is defined correctly. #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include "../objects/minimal.hpp" #include "./compile_tests.hpp" // Explicit instantiation to catch compile-time errors #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> >; #else #define INSTANTIATE(type) \ template class boost::unordered::detail::instantiate_##type INSTANTIATE(set)<int, boost::hash<int>, std::equal_to<int>, test::minimal::allocator<int> >; INSTANTIATE(multiset)<int const, boost::hash<int>, std::equal_to<int>, test::minimal::allocator<int> >; INSTANTIATE(set)<test::minimal::assignable const, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<int> >; INSTANTIATE(multiset)<test::minimal::assignable, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<int> >; #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); Set<int> int_set; Set<int, boost::hash<int>, std::equal_to<int>, test::minimal::cxx11_allocator<int> > int_set2; Set<test::minimal::assignable, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<test::minimal::assignable> > set; container_test(int_set, 0); container_test(int_set2, 0); container_test(set, assignable); } 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>(); #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; Set<int, boost::hash<int>, std::equal_to<int>, test::minimal::cxx11_allocator<int> > int_set2; Set<test::minimal::copy_constructible_equality_comparable, 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>(); #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>, 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); } #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; 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>(); #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; 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_unique_test(set, assignable); unordered_set_test(set, assignable); unordered_copyable_test(set, assignable, assignable, hash, equal_to); unordered_set_member_test(set, assignable); } #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>(); #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 movable1_tests_impl() { test::minimal::constructor_param x; test::minimal::movable1 movable1(x); test::minimal::hash<test::minimal::movable1> hash(x); test::minimal::equal_to<test::minimal::movable1> equal_to(x); Set<test::minimal::movable1, test::minimal::hash<test::minimal::movable1>, test::minimal::equal_to<test::minimal::movable1>, test::minimal::allocator<test::minimal::movable1> > set; // 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); } 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>(); #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 movable2_tests_impl() { test::minimal::constructor_param x; test::minimal::movable2 movable2(x); test::minimal::hash<test::minimal::movable2> hash(x); test::minimal::equal_to<test::minimal::movable2> equal_to(x); Set<test::minimal::movable2, test::minimal::hash<test::minimal::movable2>, test::minimal::equal_to<test::minimal::movable2>, test::minimal::allocator<test::minimal::movable2> > set; // unordered_unique_test(set, movable2); unordered_set_test(set, movable2); unordered_movable_test(set, movable2, movable2, hash, equal_to); } UNORDERED_AUTO_TEST (movable2_tests) { #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>(); #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, 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>(); #endif } // Test for ambiguity when using key convertible from iterator // See LWG2059 struct lwg2059_key { int value; template <typename T> lwg2059_key(T v) : value(v) {} }; std::size_t hash_value(lwg2059_key x) { return static_cast<std::size_t>(x.value); } bool operator==(lwg2059_key x, lwg2059_key y) { return x.value == y.value; } UNORDERED_AUTO_TEST (lwg2059) { #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()); } #else { boost::unordered_set<lwg2059_key> x; x.emplace(lwg2059_key(10)); x.erase(x.begin()); } { boost::unordered_multiset<lwg2059_key> x; x.emplace(lwg2059_key(10)); x.erase(x.begin()); } #endif } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/merge_tests.cpp
// Copyright 2016 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) #include "../helpers/unordered.hpp" #include "../helpers/count.hpp" #include "../helpers/helpers.hpp" #include "../helpers/invariants.hpp" #include "../helpers/random_values.hpp" #include "../helpers/test.hpp" #include "../helpers/tracker.hpp" #include "../objects/test.hpp" namespace merge_tests { template <class X> static void merge_set(X*) { X x, y; x.merge(y); BOOST_TEST(x.empty()); BOOST_TEST(y.empty()); x.insert(10); x.merge(y); BOOST_TEST(x.size() == 1); BOOST_TEST(x.count(10) == 1); BOOST_TEST(y.empty()); y.merge(x); BOOST_TEST(x.empty()); BOOST_TEST(y.size() == 1); BOOST_TEST(y.count(10) == 1); x.insert(10); x.insert(50); y.insert(70); y.insert(80); x.merge(y); BOOST_TEST_EQ(x.size(), 4u); BOOST_TEST_EQ(y.size(), 1u); BOOST_TEST_EQ(x.count(10), 1u); BOOST_TEST_EQ(x.count(50), 1u); BOOST_TEST_EQ(x.count(70), 1u); BOOST_TEST_EQ(x.count(80), 1u); BOOST_TEST_EQ(y.count(10), 1u); BOOST_TEST_EQ(y.count(50), 0u); BOOST_TEST_EQ(y.count(70), 0u); BOOST_TEST_EQ(y.count(80), 0u); test::check_equivalent_keys(x); test::check_equivalent_keys(y); } #ifndef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (merge_multiset) { boost::unordered_multiset<int> x; boost::unordered_multiset<int> y; x.merge(y); BOOST_TEST(x.empty()); BOOST_TEST(y.empty()); x.insert(10); x.merge(y); BOOST_TEST(x.size() == 1); BOOST_TEST(x.count(10) == 1); BOOST_TEST(y.empty()); y.merge(x); BOOST_TEST(x.empty()); BOOST_TEST(y.size() == 1); BOOST_TEST(y.count(10) == 1); x.insert(10); x.insert(50); y.insert(70); y.insert(80); x.merge(y); BOOST_TEST_EQ(x.size(), 5u); BOOST_TEST_EQ(y.size(), 0u); BOOST_TEST_EQ(x.count(10), 2u); BOOST_TEST_EQ(x.count(50), 1u); BOOST_TEST_EQ(x.count(70), 1u); BOOST_TEST_EQ(x.count(80), 1u); BOOST_TEST_EQ(y.count(10), 0u); BOOST_TEST_EQ(y.count(50), 0u); BOOST_TEST_EQ(y.count(70), 0u); BOOST_TEST_EQ(y.count(80), 0u); test::check_equivalent_keys(x); test::check_equivalent_keys(y); } UNORDERED_AUTO_TEST (merge_set_and_multiset) { boost::unordered_set<int> x; boost::unordered_multiset<int> y; x.merge(y); BOOST_TEST(x.empty()); BOOST_TEST(y.empty()); x.insert(10); x.merge(y); BOOST_TEST(x.size() == 1); BOOST_TEST(x.count(10) == 1); BOOST_TEST(y.empty()); y.merge(x); BOOST_TEST(x.empty()); BOOST_TEST(y.size() == 1); BOOST_TEST(y.count(10) == 1); x.insert(10); x.insert(50); y.insert(70); y.insert(80); x.merge(y); BOOST_TEST_EQ(x.size(), 4u); BOOST_TEST_EQ(y.size(), 1u); BOOST_TEST_EQ(x.count(10), 1u); BOOST_TEST_EQ(x.count(50), 1u); BOOST_TEST_EQ(x.count(70), 1u); BOOST_TEST_EQ(x.count(80), 1u); BOOST_TEST_EQ(y.count(10), 1u); BOOST_TEST_EQ(y.count(50), 0u); BOOST_TEST_EQ(y.count(70), 0u); BOOST_TEST_EQ(y.count(80), 0u); test::check_equivalent_keys(x); test::check_equivalent_keys(y); } #endif template <class X1, class X2> void merge_empty_test(X1*, X2*, test::random_generator generator) { test::check_instances check_; test::random_values<X1> v(1000, generator); X1 x1(v.begin(), v.end()); X2 x2; x1.merge(x2); test::check_container(x1, v); BOOST_TEST(x2.empty()); test::check_equivalent_keys(x1); test::check_equivalent_keys(x2); } template <class X> void merge_into_empty_test(X*, test::random_generator generator) { test::check_instances check_; test::random_values<X> v(1000, generator); X x1; X x2(v.begin(), v.end()); x1.merge(x2); test::check_container(x1, v); BOOST_TEST(x2.empty()); test::check_equivalent_keys(x1); test::check_equivalent_keys(x2); } template <class X1, class X2> void merge_into_unique_keys_test(X1*, X2*, int hash_equal1, int hash_equal2, test::random_generator generator) { test::check_instances check_; test::random_values<X1> v1(1000, generator); test::random_values<X2> v2(1000, generator); v1.insert(v2.begin(), test::next(v2.begin(), 100)); v2.insert(v1.begin(), test::next(v1.begin(), 100)); X1 x1(v1.begin(), v1.end(), 0, test::hash(hash_equal1), test::equal_to(hash_equal1)); X2 x2(v2.begin(), v2.end(), 0, test::hash(hash_equal2), test::equal_to(hash_equal2)); test::ordered<X1> tracker1 = test::create_ordered(x1); test::ordered<X2> tracker2 = test::create_ordered(x2); tracker1.insert(v1.begin(), v1.end()); for (typename X2::iterator it = x2.begin(); it != x2.end(); ++it) { if (!tracker1.insert(*it).second) { tracker2.insert(*it); } } x1.merge(x2); tracker1.compare(x1); tracker2.compare(x2); test::check_equivalent_keys(x1); test::check_equivalent_keys(x2); } template <class X1, class X2> void merge_into_equiv_keys_test(X1*, X2*, int hash_equal1, int hash_equal2, test::random_generator generator) { test::check_instances check_; test::random_values<X1> v1(1000, generator); test::random_values<X2> v2(1000, generator); v1.insert(v2.begin(), test::next(v2.begin(), 100)); v2.insert(v1.begin(), test::next(v1.begin(), 100)); X1 x1(v1.begin(), v1.end(), 0, test::hash(hash_equal1), test::equal_to(hash_equal1)); X2 x2(v2.begin(), v2.end(), 0, test::hash(hash_equal2), test::equal_to(hash_equal2)); x1.merge(x2); test::ordered<X1> tracker1 = test::create_ordered(x1); test::ordered<X2> tracker2 = test::create_ordered(x2); tracker1.insert(v1.begin(), v1.end()); tracker2.insert(v2.begin(), v2.end()); tracker1.insert(tracker2.begin(), tracker2.end()); tracker2.clear(); tracker1.compare(x1); tracker2.compare(x2); test::check_equivalent_keys(x1); test::check_equivalent_keys(x2); } using test::default_generator; using test::generate_collisions; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_set<int>* int_set; boost::unordered_flat_set<test::movable, test::hash, test::equal_to, std::allocator<test::movable> >* test_set_std_alloc; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, std::allocator<test::object> >* test_map_std_alloc; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set; boost::unordered_flat_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_map; // clang-format off UNORDERED_TEST(merge_set, ((int_set))) UNORDERED_TEST(merge_empty_test, ((test_set_std_alloc)) ((test_set_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_map_std_alloc)) ((test_map_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_set)) ((test_set)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_map)) ((test_map)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_set_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_map_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_set)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_map)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_set_std_alloc)) ((test_set_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_map_std_alloc)) ((test_map_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_set)) ((test_set)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_map)) ((test_map)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) // clang-format on boost::unordered_node_set<int>* int_node_set; boost::unordered_node_set<test::movable, test::hash, test::equal_to, std::allocator<test::movable> >* test_node_set_std_alloc; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, std::allocator<test::object> >* test_node_map_std_alloc; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_node_set; boost::unordered_node_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_node_map; // clang-format off UNORDERED_TEST(merge_set, ((int_node_set))) UNORDERED_TEST(merge_empty_test, ((test_node_set_std_alloc)) ((test_node_set_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_node_map_std_alloc)) ((test_node_map_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_node_set)) ((test_node_set)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_node_map)) ((test_node_map)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_node_set_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_node_map_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_node_set)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_node_map)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_node_set_std_alloc)) ((test_node_set_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_node_map_std_alloc)) ((test_node_map_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_node_set)) ((test_node_set)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_node_map)) ((test_node_map)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) // clang-format on #else boost::unordered_set<int>* int_set; boost::unordered_set<test::movable, test::hash, test::equal_to, std::allocator<test::movable> >* test_set_std_alloc; boost::unordered_multiset<test::movable, test::hash, test::equal_to, std::allocator<test::movable> >* test_multiset_std_alloc; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, std::allocator<test::object> >* test_map_std_alloc; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, std::allocator<test::object> >* test_multimap_std_alloc; boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_multiset; boost::unordered_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_map; boost::unordered_multimap<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_multimap; // clang-format off UNORDERED_TEST(merge_set, ((int_set))) UNORDERED_TEST(merge_empty_test, ((test_set_std_alloc)(test_multiset_std_alloc)) ((test_set_std_alloc)(test_multiset_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_map_std_alloc)(test_multimap_std_alloc)) ((test_map_std_alloc)(test_multimap_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_set)(test_multiset)) ((test_set)(test_multiset)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_empty_test, ((test_map)(test_multimap)) ((test_map)(test_multimap)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_set_std_alloc)(test_multiset_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_map_std_alloc)(test_multimap_std_alloc)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_set)(test_multiset)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_empty_test, ((test_map)(test_multimap)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_set_std_alloc)) ((test_set_std_alloc)(test_multiset_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_map_std_alloc)) ((test_map_std_alloc)(test_multimap_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_set)) ((test_set)(test_multiset)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_unique_keys_test, ((test_map)) ((test_map)(test_multimap)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_equiv_keys_test, ((test_multiset_std_alloc)) ((test_set_std_alloc)(test_multiset_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_equiv_keys_test, ((test_multimap_std_alloc)) ((test_map_std_alloc)(test_multimap_std_alloc)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_equiv_keys_test, ((test_multiset)) ((test_set)(test_multiset)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) UNORDERED_TEST(merge_into_equiv_keys_test, ((test_multimap)) ((test_map)(test_multimap)) ((0)(1)(2)) ((0)(1)(2)) ((default_generator)(generate_collisions))) // clang-format on #endif } // namespace merge_tests RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/allocator_traits.cpp
// Copyright 2011 Daniel James. // 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 <boost/core/lightweight_test.hpp> #include <boost/limits.hpp> #include <boost/static_assert.hpp> #include <boost/type_traits/is_same.hpp> #include <boost/unordered/detail/implementation.hpp> // Boilerplate #define ALLOCATOR_METHODS(name) \ template <typename U> struct rebind \ { \ typedef name<U> other; \ }; \ \ name() {} \ template <typename Y> name(name<Y> const&) {} \ T* address(T& r) { return &r; } \ T const* address(T const& r) { return &r; } \ T* allocate(std::size_t n) \ { \ return static_cast<T*>(::operator new(n * sizeof(T))); \ } \ T* allocate(std::size_t n, void const*) \ { \ return static_cast<T*>(::operator new(n * sizeof(T))); \ } \ void deallocate(T* p, std::size_t) { ::operator delete((void*)p); } \ void construct(T* p, T const& t) { new (p) T(t); } \ void destroy(T* p) { p->~T(); } \ std::size_t max_size() const \ { \ return (std::numeric_limits<std::size_t>::max)(); \ } \ bool operator==(name<T> const&) const { return true; } \ bool operator!=(name<T> const&) const { return false; } \ /**/ #define ALLOCATOR_METHODS_TYPEDEFS(name) \ template <typename U> struct rebind \ { \ typedef name<U> other; \ }; \ \ name() {} \ template <typename Y> name(name<Y> const&) {} \ pointer address(T& r) { return &r; } \ const_pointer address(T const& r) { return &r; } \ pointer allocate(std::size_t n) \ { \ return pointer(::operator new(n * sizeof(T))); \ } \ pointer allocate(std::size_t n, void const*) \ { \ return pointer(::operator new(n * sizeof(T))); \ } \ void deallocate(pointer p, std::size_t) { ::operator delete((void*)p); } \ void construct(T* p, T const& t) { new (p) T(t); } \ void destroy(T* p) { p->~T(); } \ size_type max_size() const \ { \ return (std::numeric_limits<size_type>::max)(); \ } \ bool operator==(name<T> const&) const { return true; } \ bool operator!=(name<T> const&) const { return false; } \ /**/ struct yes_type { enum { value = true }; }; struct no_type { enum { value = false }; }; // For tracking calls... static int selected; void reset() { selected = 0; } template <typename Allocator> int call_select() { typedef boost::unordered::detail::allocator_traits<Allocator> traits; Allocator a; reset(); BOOST_TEST(traits::select_on_container_copy_construction(a) == a); return selected; } // Empty allocator test template <typename T> struct empty_allocator { typedef T value_type; ALLOCATOR_METHODS(empty_allocator) }; void test_empty_allocator() { typedef empty_allocator<int> allocator; typedef boost::unordered::detail::allocator_traits<allocator> traits; BOOST_STATIC_ASSERT((boost::is_same<traits::size_type, std::make_unsigned<std::ptrdiff_t>::type>::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::difference_type, std::ptrdiff_t>::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::pointer, int*>::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::const_pointer, int const*>::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::value_type, int>::value)); BOOST_TEST(!traits::propagate_on_container_copy_assignment::value); BOOST_TEST(!traits::propagate_on_container_move_assignment::value); BOOST_TEST(!traits::propagate_on_container_swap::value); BOOST_TEST(traits::is_always_equal::value); BOOST_TEST(call_select<allocator>() == 0); } // allocator 1 template <typename T> struct allocator1 { typedef T value_type; ALLOCATOR_METHODS(allocator1) typedef yes_type propagate_on_container_copy_assignment; typedef yes_type propagate_on_container_move_assignment; typedef yes_type propagate_on_container_swap; typedef yes_type is_always_equal; allocator1<T> select_on_container_copy_construction() const { ++selected; return allocator1<T>(); } }; void test_allocator1() { typedef allocator1<int> allocator; typedef boost::unordered::detail::allocator_traits<allocator> traits; BOOST_STATIC_ASSERT((boost::is_same<traits::size_type, std::make_unsigned<std::ptrdiff_t>::type>::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::difference_type, std::ptrdiff_t>::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::pointer, int*>::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::const_pointer, int const*>::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::value_type, int>::value)); BOOST_TEST(traits::propagate_on_container_copy_assignment::value); BOOST_TEST(traits::propagate_on_container_move_assignment::value); BOOST_TEST(traits::propagate_on_container_swap::value); BOOST_TEST(traits::is_always_equal::value); BOOST_TEST(call_select<allocator>() == 1); } // allocator 2 template <typename Alloc> struct allocator2_base { Alloc select_on_container_copy_construction() const { ++selected; return Alloc(); } }; template <typename T> struct allocator2 : allocator2_base<allocator2<T> > { typedef T value_type; typedef T* pointer; typedef T const* const_pointer; typedef std::size_t size_type; ALLOCATOR_METHODS(allocator2) typedef no_type propagate_on_container_copy_assignment; typedef no_type propagate_on_container_move_assignment; typedef no_type propagate_on_container_swap; typedef no_type is_always_equal; }; void test_allocator2() { typedef allocator2<int> allocator; typedef boost::unordered::detail::allocator_traits<allocator> traits; BOOST_STATIC_ASSERT((boost::is_same<traits::size_type, std::size_t>::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::difference_type, std::ptrdiff_t>::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::pointer, int*>::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::const_pointer, int const*>::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::value_type, int>::value)); BOOST_TEST(!traits::propagate_on_container_copy_assignment::value); BOOST_TEST(!traits::propagate_on_container_move_assignment::value); BOOST_TEST(!traits::propagate_on_container_swap::value); BOOST_TEST(!traits::is_always_equal::value); BOOST_TEST(call_select<allocator>() == 1); } // allocator 3 template <typename T> struct ptr { T* value_; ptr(void* v) : value_((T*)v) {} T& operator*() const { return *value_; } }; template <> struct ptr<void> { void* value_; ptr(void* v) : value_(v) {} }; template <> struct ptr<const void> { void const* value_; ptr(void const* v) : value_(v) {} }; template <typename T> struct allocator3 { typedef T value_type; typedef ptr<T> pointer; typedef ptr<T const> const_pointer; typedef unsigned short size_type; int x; // Just to make it non-empty, so that is_always_equal is false. ALLOCATOR_METHODS_TYPEDEFS(allocator3) typedef yes_type propagate_on_container_copy_assignment; typedef no_type propagate_on_container_move_assignment; allocator3<T> select_on_container_copy_construction() const { ++selected; allocator3<T> a; a.x = 0; return a; } }; void test_allocator3() { typedef allocator3<int> allocator; typedef boost::unordered::detail::allocator_traits<allocator> traits; BOOST_STATIC_ASSERT( (boost::is_same<traits::size_type, unsigned short>::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::difference_type, std::ptrdiff_t>::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::pointer, ptr<int> >::value)); BOOST_STATIC_ASSERT( (boost::is_same<traits::const_pointer, ptr<int const> >::value)); BOOST_STATIC_ASSERT((boost::is_same<traits::value_type, int>::value)); BOOST_TEST(traits::propagate_on_container_copy_assignment::value); BOOST_TEST(!traits::propagate_on_container_move_assignment::value); BOOST_TEST(!traits::propagate_on_container_swap::value); BOOST_TEST(!traits::is_always_equal::value); BOOST_TEST(call_select<allocator>() == 1); } int main() { test_empty_allocator(); test_allocator1(); test_allocator2(); test_allocator3(); return boost::report_errors(); }
0
repos/unordered/test
repos/unordered/test/unordered/copy_tests.cpp
// Copyright 2006-2009 Daniel James. // Copyright (C) 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) #include "../helpers/unordered.hpp" #include "../helpers/equivalent.hpp" #include "../helpers/invariants.hpp" #include "../helpers/random_values.hpp" #include "../helpers/test.hpp" #include "../helpers/tracker.hpp" #include "../objects/cxx11_allocator.hpp" #include "../objects/test.hpp" test::seed_t initialize_seed(9063); namespace copy_tests { template <class T> void copy_construct_tests1(T*, test::random_generator const& generator) { typedef typename T::allocator_type allocator_type; typename T::hasher hf; typename T::key_equal eq; typename T::allocator_type al; { test::check_instances check_; T x; T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; T x(0); T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; test::random_values<T> v(1000, generator); T x(v.begin(), v.end()); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); test::check_equivalent_keys(y); } { test::check_instances check_; // In this test I drop the original containers max load factor, so it // is much lower than the load factor. The hash table is not allowed // to rehash, but the destination container should probably allocate // enough buckets to decrease the load factor appropriately. test::random_values<T> v(1000, generator); T x(v.begin(), v.end()); x.max_load_factor(x.load_factor() / 4); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); // This isn't guaranteed: BOOST_TEST(y.load_factor() < y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); test::check_equivalent_keys(y); } } template <class T> void copy_construct_tests2(T*, test::random_generator const& generator) { typename T::hasher hf(1); typename T::key_equal eq(1); typename T::allocator_type al(1); typename T::allocator_type al2(2); typedef typename T::allocator_type allocator_type; { test::check_instances check_; T x(0, hf, eq, al); T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; T x(10000, hf, eq, al); T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); test::check_equivalent_keys(y); } { test::check_instances check_; T x(0, hf, eq, al); T y(x, al2); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al2)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; T x(1000, hf, eq, al); T y(x, al2); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al2)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); test::check_equivalent_keys(y); } { test::check_instances check_; test::random_values<T> v; T x(v.begin(), v.end(), 0, hf, eq, al); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(test::detail::tracker.count_allocations == 0); } { test::check_instances check_; test::random_values<T> v(1000, generator); T x(v.begin(), v.end(), 0, hf, eq, al); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::selected_count(y.get_allocator()) == (allocator_type::is_select_on_copy)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); } { test::check_instances check_; test::random_values<T> v; T x(v.begin(), v.end(), 0, hf, eq, al); T y(x, al2); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); BOOST_TEST(test::equivalent(y.get_allocator(), al2)); BOOST_TEST(test::detail::tracker.count_allocations == 0); } { test::check_instances check_; test::random_values<T> v(500, generator); T x(v.begin(), v.end(), 0, hf, eq, al); T y(x, al2); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); BOOST_TEST(test::equivalent(y.get_allocator(), al2)); } } template <class T> void copy_construct_tests_std_allocator1( T*, test::random_generator const& generator) { typename T::hasher hf; typename T::key_equal eq; typename T::allocator_type al; { test::check_instances check_; T x; T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; T x(0); T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; test::random_values<T> v(1000, generator); T x(v.begin(), v.end()); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); } { test::check_instances check_; // In this test I drop the original containers max load factor, so it // is much lower than the load factor. The hash table is not allowed // to rehash, but the destination container should probably allocate // enough buckets to decrease the load factor appropriately. test::random_values<T> v(1000, generator); T x(v.begin(), v.end()); x.max_load_factor(x.load_factor() / 4); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); // This isn't guaranteed: BOOST_TEST(y.load_factor() < y.max_load_factor()); test::check_equivalent_keys(y); } } template <class T> void copy_construct_tests_std_allocator2( T*, test::random_generator const& generator) { typename T::hasher hf(1); typename T::key_equal eq(1); typename T::allocator_type al; { test::check_instances check_; T x(0, hf, eq, al); T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; T x(10000, hf, eq, al); T y(x); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); test::check_equivalent_keys(y); } { test::check_instances check_; T x(0, hf, eq, al); T y(x, al); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); BOOST_TEST(test::detail::tracker.count_allocations == 0); test::check_equivalent_keys(y); } { test::check_instances check_; T x(1000, hf, eq, al); T y(x, al); BOOST_TEST(y.empty()); BOOST_TEST(test::equivalent(y.hash_function(), hf)); BOOST_TEST(test::equivalent(y.key_eq(), eq)); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(x.max_load_factor() == y.max_load_factor()); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); test::check_equivalent_keys(y); } { test::check_instances check_; test::random_values<T> v; T x(v.begin(), v.end(), 0, hf, eq, al); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(test::detail::tracker.count_allocations == 0); } { test::check_instances check_; test::random_values<T> v(1000, generator); T x(v.begin(), v.end(), 0, hf, eq, al); T y(x); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::equivalent(y.get_allocator(), al)); } { test::check_instances check_; test::random_values<T> v; T x(v.begin(), v.end(), 0, hf, eq, al); T y(x, al); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); BOOST_TEST(test::equivalent(y.get_allocator(), al)); BOOST_TEST(test::detail::tracker.count_allocations == 0); } { test::check_instances check_; test::random_values<T> v(500, generator); T x(v.begin(), v.end(), 0, hf, eq, al); T y(x, al); test::unordered_equivalence_tester<T> equivalent(x); BOOST_TEST(equivalent(y)); test::check_equivalent_keys(y); BOOST_TEST(test::selected_count(y.get_allocator()) == 0); BOOST_TEST(test::equivalent(y.get_allocator(), al)); } } using test::default_generator; using test::generate_collisions; using test::limited_range; #ifdef BOOST_UNORDERED_FOA_TESTS template <class T> struct allocator { using value_type = T; allocator() = default; allocator(allocator const&) = default; allocator(allocator&&) = default; template <class U> allocator(allocator<U> const&) {} T* allocate(std::size_t n) { return static_cast<T*>(::operator new(sizeof(value_type) * n)); } void deallocate(T* p, std::size_t) { ::operator delete(p); } friend inline bool operator==(allocator const&, allocator const&) { return true; } friend inline bool operator!=(allocator const&, allocator const&) { return false; } }; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_map; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_set_select_copy; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_map_select_copy; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_set_no_select_copy; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_map_no_select_copy; boost::unordered_flat_set<int, test::hash, test::equal_to, test::allocator1<int> >* test_set_trivially_copyable; boost::unordered_flat_map<int, int, test::hash, test::equal_to, test::allocator1<std::pair<int const, int> > >* test_map_trivially_copyable; boost::unordered_flat_set<int, test::hash, test::equal_to, std::allocator<int> >* test_set_trivially_copyable_std_allocator; boost::unordered_flat_map<int, int, test::hash, test::equal_to, std::allocator<std::pair<int const, int> > >* test_map_trivially_copyable_std_allocator; boost::unordered_flat_set<int, test::hash, test::equal_to, allocator<int> >* test_set_trivially_copyable_no_construct; boost::unordered_flat_map<int, int, test::hash, test::equal_to, allocator<std::pair<int const, int> > >* test_map_trivially_copyable_no_construct; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_node_set; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_node_map; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_node_set_select_copy; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_node_map_select_copy; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_node_set_no_select_copy; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_node_map_no_select_copy; boost::unordered_node_set<int, test::hash, test::equal_to, test::allocator1<int> >* test_node_set_trivially_copyable; boost::unordered_node_map<int, int, test::hash, test::equal_to, test::allocator1<std::pair<int const, int> > >* test_node_map_trivially_copyable; boost::unordered_node_set<int, test::hash, test::equal_to, std::allocator<int> >* test_node_set_trivially_copyable_std_allocator; boost::unordered_node_map<int, int, test::hash, test::equal_to, std::allocator<std::pair<int const, int> > >* test_node_map_trivially_copyable_std_allocator; boost::unordered_node_set<int, test::hash, test::equal_to, allocator<int> >* test_node_set_trivially_copyable_no_construct; boost::unordered_node_map<int, int, test::hash, test::equal_to, allocator<std::pair<int const, int> > >* test_node_map_trivially_copyable_no_construct; // clang-format off UNORDERED_TEST(copy_construct_tests1, ((test_set)(test_map)(test_set_select_copy)(test_map_select_copy) (test_set_no_select_copy)(test_map_no_select_copy) (test_set_trivially_copyable)(test_map_trivially_copyable) (test_node_set)(test_node_map)(test_node_set_select_copy)(test_node_map_select_copy) (test_node_set_no_select_copy)(test_node_map_no_select_copy) (test_node_set_trivially_copyable)(test_node_map_trivially_copyable)) ((default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(copy_construct_tests2, ((test_set)(test_map)(test_set_select_copy)(test_map_select_copy) (test_set_no_select_copy)(test_map_no_select_copy) (test_set_trivially_copyable)(test_map_trivially_copyable) (test_node_set)(test_node_map)(test_node_set_select_copy)(test_node_map_select_copy) (test_node_set_no_select_copy)(test_node_map_no_select_copy) (test_node_set_trivially_copyable)(test_node_map_trivially_copyable)) ((default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(copy_construct_tests_std_allocator1, ((test_set_trivially_copyable_std_allocator) (test_map_trivially_copyable_std_allocator) (test_set_trivially_copyable_no_construct) (test_map_trivially_copyable_no_construct) (test_node_set_trivially_copyable_std_allocator) (test_node_map_trivially_copyable_std_allocator) (test_node_set_trivially_copyable_no_construct) (test_node_map_trivially_copyable_no_construct)) ((default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(copy_construct_tests_std_allocator2, ((test_set_trivially_copyable_std_allocator) (test_map_trivially_copyable_std_allocator) (test_set_trivially_copyable_no_construct) (test_map_trivially_copyable_no_construct) (test_node_set_trivially_copyable_std_allocator) (test_node_map_trivially_copyable_std_allocator) (test_node_set_trivially_copyable_no_construct) (test_node_map_trivially_copyable_no_construct)) ((default_generator)(generate_collisions)(limited_range))) // clang-format on #else boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_multiset; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_map; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_multimap; boost::unordered_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_set_select_copy; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_multiset_select_copy; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_map_select_copy; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::select_copy> >* test_multimap_select_copy; boost::unordered_set<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_set_no_select_copy; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_multiset_no_select_copy; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_map_no_select_copy; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::cxx11_allocator<test::object, test::no_select_copy> >* test_multimap_no_select_copy; // clang-format off UNORDERED_TEST(copy_construct_tests1, ((test_set)(test_multiset)(test_map)(test_multimap) (test_set_select_copy)(test_multiset_select_copy) (test_map_select_copy)(test_multimap_select_copy) (test_set_no_select_copy)(test_multiset_no_select_copy) (test_map_no_select_copy)(test_multimap_no_select_copy))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(copy_construct_tests2, ((test_set)(test_multiset)(test_map)(test_multimap) (test_set_select_copy)(test_multiset_select_copy) (test_map_select_copy)(test_multimap_select_copy) (test_set_no_select_copy)(test_multiset_no_select_copy) (test_map_no_select_copy)(test_multimap_no_select_copy))( (default_generator)(generate_collisions)(limited_range))) // clang-format on #endif } // namespace copy_tests RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/link_test_1.cpp
// 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) #include "../helpers/unordered.hpp" #ifdef BOOST_UNORDERED_FOA_TESTS #include <boost/unordered/concurrent_flat_map.hpp> void foo(boost::unordered_flat_set<int>&, boost::unordered_flat_map<int, int>&, boost::unordered_node_set<int>&, boost::unordered_node_map<int, int>&, boost::concurrent_flat_map<int, int>&); int main() { boost::unordered_flat_set<int> x1; boost::unordered_flat_map<int, int> x2; boost::unordered_node_set<int> x3; boost::unordered_node_map<int, int> x4; boost::concurrent_flat_map<int, int> x5; foo(x1, x2, x3, x4, x5); return 0; } #else void foo(boost::unordered_set<int>&, boost::unordered_map<int, int>&, boost::unordered_multiset<int>&, boost::unordered_multimap<int, int>&); int main() { boost::unordered_set<int> x1; boost::unordered_map<int, int> x2; boost::unordered_multiset<int> x3; boost::unordered_multimap<int, int> x4; foo(x1, x2, x3, x4); return 0; } #endif
0
repos/unordered/test
repos/unordered/test/unordered/scoped_allocator.cpp
// Copyright 2021-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 <boost/config.hpp> #include <boost/config/pragma_message.hpp> #include <boost/config/workaround.hpp> #if BOOST_CXX_VERSION <= 199711L || \ BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800) || \ BOOST_WORKAROUND(BOOST_MSVC, == 1900) BOOST_PRAGMA_MESSAGE( "scoped allocator adaptor tests only work under C++11 and above") int main() {} #else // This test is based on a user-submitted issue found here: // https://github.com/boostorg/unordered/issues/22 // #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include <boost/cstdint.hpp> #include <boost/core/ignore_unused.hpp> #include <scoped_allocator> #include <string> #include <utility> #include <vector> namespace test { template <class T> struct allocator { typedef T value_type; allocator() = delete; allocator(int) {} allocator(allocator const&) = default; allocator(allocator&&) = default; template <class U> allocator(allocator<U> const&) {} BOOST_ATTRIBUTE_NODISCARD T* allocate(std::size_t n) { return static_cast<T*>(::operator new(n * sizeof(T))); } void deallocate(T* p, std::size_t) noexcept { ::operator delete(p); } bool operator==(allocator const&) const { return true; } bool operator!=(allocator const&) const { return false; } }; } // namespace test typedef std::vector<boost::uint64_t, test::allocator<boost::uint64_t> > vector_type; typedef std::pair<boost::uint64_t, vector_type> pair_type; typedef std::scoped_allocator_adaptor<test::allocator<pair_type>, test::allocator<boost::uint64_t> > allocator_type; template <class X> static void scoped_allocator(X*) { allocator_type alloc( test::allocator<pair_type>(1337), test::allocator<boost::uint64_t>(7331)); X map(alloc); for (unsigned i = 0; i < 10; ++i) { boost::ignore_unused(map[i]); } BOOST_TEST(map.size() == 10); } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_map<const boost::uint64_t, vector_type, boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>* test_map; static boost::unordered_node_map<const boost::uint64_t, vector_type, boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>* test_node_map; UNORDERED_TEST(scoped_allocator, ((test_map)(test_node_map))) #else static boost::unordered_map<const boost::uint64_t, vector_type, boost::hash<boost::uint64_t>, std::equal_to<boost::uint64_t>, allocator_type>* test_map; UNORDERED_TEST(scoped_allocator, ((test_map))) #endif RUN_TESTS() #endif
0
repos/unordered/test
repos/unordered/test/unordered/insert_hint_tests.cpp
// Copyright 2016 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) #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include "../helpers/invariants.hpp" #include <map> #include <set> namespace insert_hint { #ifndef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (insert_hint_empty) { typedef boost::unordered_multiset<int> container; container x; x.insert(x.cbegin(), 10); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST_EQ(x.count(10), 1u); test::check_equivalent_keys(x); } UNORDERED_AUTO_TEST (insert_hint_empty2) { typedef boost::unordered_multimap<std::string, int> container; container x; x.emplace_hint(x.cbegin(), "hello", 50); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST_EQ(x.count("hello"), 1u); BOOST_TEST_EQ(x.find("hello")->second, 50); test::check_equivalent_keys(x); } UNORDERED_AUTO_TEST (insert_hint_single) { typedef boost::unordered_multiset<std::string> container; container x; x.insert("equal"); x.insert(x.cbegin(), "equal"); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST_EQ(x.count("equal"), 2u); test::check_equivalent_keys(x); } UNORDERED_AUTO_TEST (insert_hint_single2) { typedef boost::unordered_multimap<int, std::string> container; container x; x.emplace(10, "one"); x.emplace_hint(x.cbegin(), 10, "two"); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST_EQ(x.count(10), 2u); container::iterator it = x.find(10); std::string v0 = (it++)->second; std::string v1 = (it++)->second; BOOST_TEST(v0 == "one" || v0 == "two"); BOOST_TEST(v1 == "one" || v1 == "two"); BOOST_TEST(v0 != v1); test::check_equivalent_keys(x); } UNORDERED_AUTO_TEST (insert_hint_multiple) { for (unsigned int size = 0; size < 10; ++size) { for (unsigned int offset = 0; offset <= size; ++offset) { typedef boost::unordered_multiset<std::string> container; container x; for (unsigned int i = 0; i < size; ++i) { x.insert("multiple"); } BOOST_TEST_EQ(x.size(), size); container::const_iterator position = x.cbegin(); for (unsigned int i = 0; i < offset; ++i) { ++position; } x.insert(position, "multiple"); BOOST_TEST_EQ(x.size(), size + 1u); BOOST_TEST_EQ(x.count("multiple"), size + 1u); test::check_equivalent_keys(x); } } } #endif template <class X> static void insert_hint_unique(X*) { typedef X container; container x; x.insert(x.cbegin(), 10); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST_EQ(x.count(10), 1u); test::check_equivalent_keys(x); } template <class X> static void insert_hint_unique_single(X*) { typedef X container; container x; x.insert(10); x.insert(x.cbegin(), 10); BOOST_TEST_EQ(x.size(), 1u); BOOST_TEST_EQ(x.count(10), 1u); test::check_equivalent_keys(x); x.insert(x.cbegin(), 20); BOOST_TEST_EQ(x.size(), 2u); BOOST_TEST_EQ(x.count(10), 1u); BOOST_TEST_EQ(x.count(20), 1u); test::check_equivalent_keys(x); } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_set<int>* test_set; static boost::unordered_node_set<int>* test_node_set; UNORDERED_TEST(insert_hint_unique, ((test_set)(test_node_set))) UNORDERED_TEST(insert_hint_unique_single, ((test_set)(test_node_set))) #else static boost::unordered_set<int>* test_set; UNORDERED_TEST(insert_hint_unique, ((test_set))) UNORDERED_TEST(insert_hint_unique_single, ((test_set))) #endif } // namespace insert_hint RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/load_factor_tests.cpp
// 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) #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include <boost/limits.hpp> #include "../helpers/random_values.hpp" #if defined(BOOST_MSVC) #pragma warning(push) #pragma warning(disable : 4127) // conditional expression is constant #endif namespace load_factor_tests { test::seed_t initialize_seed(783656); template <class X> void set_load_factor_tests(X*) { X x; #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST(x.max_load_factor() == boost::unordered::detail::foa::mlf); BOOST_TEST(x.load_factor() == 0); // A valid implementation could fail these tests, but I think they're // reasonable. x.max_load_factor(2.0); BOOST_TEST(x.max_load_factor() == boost::unordered::detail::foa::mlf); x.max_load_factor(0.5); BOOST_TEST(x.max_load_factor() == boost::unordered::detail::foa::mlf); #else BOOST_TEST(x.max_load_factor() == 1.0); BOOST_TEST(x.load_factor() == 0); // A valid implementation could fail these tests, but I think they're // reasonable. x.max_load_factor(2.0); BOOST_TEST(x.max_load_factor() == 2.0); x.max_load_factor(0.5); BOOST_TEST(x.max_load_factor() == 0.5); #endif } template <class X> void insert_test(X*, float mlf, test::random_generator generator) { X x; x.max_load_factor(mlf); float b = x.max_load_factor(); test::random_values<X> values(1000, generator); for (typename test::random_values<X>::const_iterator it = values.begin(), end = values.end(); it != end; ++it) { typename X::size_type old_size = x.size(), old_bucket_count = x.bucket_count(); x.insert(*it); if (static_cast<double>(old_size + 1) <= b * static_cast<double>(old_bucket_count)) BOOST_TEST(x.bucket_count() == old_bucket_count); } } template <class X> void load_factor_insert_tests(X* ptr, test::random_generator generator) { insert_test(ptr, 1.0f, generator); insert_test(ptr, 0.1f, generator); insert_test(ptr, 100.0f, generator); insert_test(ptr, (std::numeric_limits<float>::min)(), generator); if (std::numeric_limits<float>::has_infinity) insert_test(ptr, std::numeric_limits<float>::infinity(), generator); } using test::default_generator; using test::generate_collisions; using test::limited_range; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_set<int>* int_set_ptr; boost::unordered_flat_map<int, int>* int_map_ptr; boost::unordered_node_set<int>* int_node_set_ptr; boost::unordered_node_map<int, int>* int_node_map_ptr; // clang-format off UNORDERED_TEST(set_load_factor_tests, ((int_set_ptr)(int_map_ptr)(int_node_set_ptr)(int_node_map_ptr))) UNORDERED_TEST(load_factor_insert_tests, ((int_set_ptr)(int_map_ptr)(int_node_set_ptr)(int_node_map_ptr))( (default_generator)(generate_collisions)(limited_range))) // clang-format on #else boost::unordered_set<int>* int_set_ptr; boost::unordered_multiset<int>* int_multiset_ptr; boost::unordered_map<int, int>* int_map_ptr; boost::unordered_multimap<int, int>* int_multimap_ptr; UNORDERED_TEST(set_load_factor_tests, ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr))) UNORDERED_TEST(load_factor_insert_tests, ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr))( (default_generator)(generate_collisions)(limited_range))) #endif } RUN_TESTS() #if defined(BOOST_MSVC) #pragma warning(pop) #pragma warning(disable : 4127) // conditional expression is constant #endif
0
repos/unordered/test
repos/unordered/test/unordered/bucket_tests.cpp
// Copyright 2006-2009 Daniel James. // 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) // clang-format off #include "../helpers/prefix.hpp" #include <boost/unordered_set.hpp> #include <boost/unordered_map.hpp> #include "../helpers/postfix.hpp" // clang-format on #include "../helpers/test.hpp" #include <algorithm> #include "../objects/test.hpp" #include "../helpers/random_values.hpp" #include "../helpers/helpers.hpp" #include "../helpers/metafunctions.hpp" #if BOOST_WORKAROUND(BOOST_MSVC, < 1400) #pragma warning(disable : 4267) // conversion from 'size_t' to 'unsigned int', // possible loss of data. #endif namespace bucket_tests { test::seed_t initialize_seed(54635); template <class X> void tests(X*, test::random_generator generator) { test::check_instances check_; typedef typename X::size_type size_type; typedef typename X::const_local_iterator const_local_iterator; typedef typename X::value_type value_type; test::random_values<X> v(1000, generator); X x(v.begin(), v.end()); BOOST_TEST(x.bucket_count() <= x.max_bucket_count()); if (!(x.bucket_count() <= x.max_bucket_count())) { BOOST_LIGHTWEIGHT_TEST_OSTREAM << x.bucket_count() << "<=" << x.max_bucket_count() << "\n"; } for (typename test::random_values<X>::const_iterator it = v.begin(), end = v.end(); it != end; ++it) { size_type bucket = x.bucket(test::get_key<X>(*it)); BOOST_TEST(bucket < x.bucket_count()); if (bucket < x.bucket_count()) { // lit? lend?? I need a new naming scheme. const_local_iterator lit = x.begin(bucket), lend = x.end(bucket); while (lit != lend && test::get_key<X>(*it) != test::get_key<X>(*lit)) { ++lit; } BOOST_TEST(lit != lend); } } for (size_type i = 0; i < x.bucket_count(); ++i) { { auto begin = x.begin(i); auto end = x.end(i); BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(std::distance(begin, end))); for (auto pos = begin; pos != end; ++pos) { using pointer_type = typename std::conditional<test::is_set<X>::value, value_type const*, value_type*>::type; pointer_type p = pos.operator->(); BOOST_TEST_EQ(p, std::addressof(*pos)); } auto cbegin = x.cbegin(i); auto cend = x.cend(i); BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(std::distance(cbegin, cend))); for (auto pos = cbegin; pos != cend; ++pos) { value_type const* p = pos.operator->(); BOOST_TEST_EQ(p, std::addressof(*pos)); } } { X const& x_ref = x; BOOST_TEST_TRAIT_SAME( decltype(x_ref.begin()), decltype(x_ref.cbegin())); BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(std::distance( x_ref.begin(i), x_ref.end(i)))); BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(std::distance( x_ref.cbegin(i), x_ref.cend(i)))); } } } boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, std::allocator<test::object> >* test_multimap_std_alloc; boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_set; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_multiset; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_map; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_multimap; using test::default_generator; using test::generate_collisions; using test::limited_range; UNORDERED_TEST(tests, ((test_multimap_std_alloc)(test_set)(test_multiset)(test_map)( test_multimap))((default_generator)(generate_collisions)(limited_range))) } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/compile_tests.hpp
// Copyright 2005-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) #if defined(BOOST_MSVC) #pragma warning(push) #pragma warning(disable : 4100) // unreferenced formal parameter #pragma warning(disable : 4610) // class can never be instantiated #pragma warning(disable : 4510) // default constructor could not be generated #endif #include <boost/concept_check.hpp> #if defined(BOOST_MSVC) #pragma warning(pop) #endif #include "../helpers/check_return_type.hpp" #include <boost/core/invoke_swap.hpp> #include <boost/core/pointer_traits.hpp> #include <boost/limits.hpp> #include <boost/predef.h> #include <boost/static_assert.hpp> #include <boost/tuple/tuple.hpp> #include <boost/type_traits/cv_traits.hpp> #include <boost/type_traits/is_const.hpp> #include <boost/type_traits/is_convertible.hpp> #include <boost/type_traits/is_same.hpp> typedef long double comparison_type; template <class T> void sink(T const&) {} template <class T> T rvalue(T const& v) { return v; } template <class T> T rvalue_default() { return T(); } template <class T> T implicit_construct() { return {}; } #define TEST_NOEXCEPT_EXPR(x) BOOST_STATIC_ASSERT((noexcept(x))); template <class X, class T> void container_test(X& r, T const&) { typedef typename X::iterator iterator; typedef typename X::const_iterator const_iterator; typedef typename X::difference_type difference_type; typedef typename X::size_type size_type; typedef typename std::iterator_traits<iterator>::value_type iterator_value_type; typedef typename std::iterator_traits<const_iterator>::value_type const_iterator_value_type; typedef typename std::iterator_traits<iterator>::difference_type iterator_difference_type; typedef typename std::iterator_traits<const_iterator>::difference_type const_iterator_difference_type; typedef typename X::value_type value_type; typedef typename X::reference reference; typedef typename X::const_reference const_reference; #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename X::node_type node_type; #endif typedef typename X::allocator_type allocator_type; typedef typename X::pointer pointer; typedef typename X::const_pointer const_pointer; BOOST_STATIC_ASSERT((boost::is_same<pointer, typename boost::allocator_pointer<allocator_type>::type>::value)); BOOST_STATIC_ASSERT((boost::is_same<const_pointer, typename boost::allocator_const_pointer<allocator_type>::type>::value)); // value_type BOOST_STATIC_ASSERT((boost::is_same<T, value_type>::value)); boost::function_requires<boost::CopyConstructibleConcept<X> >(); // reference_type / const_reference_type BOOST_STATIC_ASSERT((boost::is_same<T&, reference>::value)); BOOST_STATIC_ASSERT((boost::is_same<T const&, const_reference>::value)); // iterator boost::function_requires<boost::InputIteratorConcept<iterator> >(); BOOST_STATIC_ASSERT((boost::is_same<T, iterator_value_type>::value)); BOOST_STATIC_ASSERT((boost::is_convertible<iterator, const_iterator>::value)); // const_iterator boost::function_requires<boost::InputIteratorConcept<const_iterator> >(); BOOST_STATIC_ASSERT((boost::is_same<T, const_iterator_value_type>::value)); // node_type #ifndef BOOST_UNORDERED_FOA_TESTS BOOST_STATIC_ASSERT(( boost::is_same<allocator_type, typename node_type::allocator_type>::value)); #endif // difference_type BOOST_STATIC_ASSERT(std::numeric_limits<difference_type>::is_signed); BOOST_STATIC_ASSERT(std::numeric_limits<difference_type>::is_integer); BOOST_STATIC_ASSERT( (boost::is_same<difference_type, iterator_difference_type>::value)); BOOST_STATIC_ASSERT( (boost::is_same<difference_type, const_iterator_difference_type>::value)); // size_type BOOST_STATIC_ASSERT(!std::numeric_limits<size_type>::is_signed); BOOST_STATIC_ASSERT(std::numeric_limits<size_type>::is_integer); // size_type can represent any non-negative value type of difference_type // I'm not sure about either of these tests... size_type max_diff = static_cast<size_type>((std::numeric_limits<difference_type>::max)()); difference_type converted_diff(static_cast<difference_type>(max_diff)); BOOST_TEST((std::numeric_limits<difference_type>::max)() == converted_diff); BOOST_TEST( static_cast<comparison_type>((std::numeric_limits<size_type>::max)()) > static_cast<comparison_type>( (std::numeric_limits<difference_type>::max)())); // Constructors // I don't test the runtime post-conditions here. // It isn't specified in the container requirements that the no argument // constructor is implicit, but it is defined that way in the concrete // container specification. X u_implicit = {}; sink(u_implicit); X u; BOOST_TEST(u.size() == 0); BOOST_TEST(X().size() == 0); X a, b; X a_const; sink(X(a)); X u2(a); X u3 = a; X u4(rvalue(a_const)); X u5 = rvalue(a_const); a.swap(b); boost::core::invoke_swap(a, b); test::check_return_type<X>::equals_ref(r = a); // Allocator test::check_return_type<allocator_type>::equals(a_const.get_allocator()); allocator_type m = a.get_allocator(); sink(X(m)); X c(m); sink(X(a_const, m)); X c2(a_const, m); sink(X(rvalue(a_const), m)); X c3(rvalue(a_const), m); #ifndef BOOST_UNORDERED_FOA_TESTS // node_type implicit_construct<node_type const>(); #if !BOOST_COMP_GNUC || BOOST_COMP_GNUC >= BOOST_VERSION_NUMBER(4, 8, 0) TEST_NOEXCEPT_EXPR(node_type()); #endif node_type n1; node_type n2(rvalue_default<node_type>()); #if !BOOST_COMP_GNUC || BOOST_COMP_GNUC >= BOOST_VERSION_NUMBER(4, 8, 0) TEST_NOEXCEPT_EXPR(node_type(std::move(n1))); #endif node_type n3; n3 = std::move(n2); n1.swap(n3); swap(n1, n3); // TODO: noexcept for swap? // value, key, mapped tests in map and set specific testing. node_type const n_const; BOOST_TEST(n_const ? 0 : 1); TEST_NOEXCEPT_EXPR(n_const ? 0 : 1); test::check_return_type<bool>::equals(!n_const); test::check_return_type<bool>::equals(n_const.empty()); TEST_NOEXCEPT_EXPR(!n_const); TEST_NOEXCEPT_EXPR(n_const.empty()); #endif // Avoid unused variable warnings: sink(u); sink(u2); sink(u3); sink(u4); sink(u5); sink(c); sink(c2); sink(c3); } template <class X> void unordered_destructible_test(X&) { typedef typename X::iterator iterator; typedef typename X::const_iterator const_iterator; typedef typename X::size_type size_type; X x1; X x2(rvalue_default<X>()); X x3 = rvalue_default<X>(); // This can only be done if propagate_on_container_move_assignment::value // is true. // x2 = rvalue_default<X>(); X* ptr = new X(); X& a1 = *ptr; (&a1)->~X(); ::operator delete((void*)(&a1)); X a, b; X const a_const; test::check_return_type<iterator>::equals(a.begin()); test::check_return_type<const_iterator>::equals(a_const.begin()); test::check_return_type<const_iterator>::equals(a.cbegin()); test::check_return_type<const_iterator>::equals(a_const.cbegin()); test::check_return_type<iterator>::equals(a.end()); test::check_return_type<const_iterator>::equals(a_const.end()); test::check_return_type<const_iterator>::equals(a.cend()); test::check_return_type<const_iterator>::equals(a_const.cend()); a.swap(b); boost::core::invoke_swap(a, b); test::check_return_type<size_type>::equals(a.size()); test::check_return_type<size_type>::equals(a.max_size()); test::check_return_type<bool>::convertible(a.empty()); // Allocator typedef typename X::allocator_type allocator_type; test::check_return_type<allocator_type>::equals(a_const.get_allocator()); } template <class X, class Key> void unordered_set_test(X& r, Key const&) { typedef typename X::value_type value_type; typedef typename X::key_type key_type; BOOST_STATIC_ASSERT((boost::is_same<value_type, key_type>::value)); // iterator pointer / const_pointer_type typedef typename X::iterator iterator; typedef typename X::const_iterator const_iterator; #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename X::local_iterator local_iterator; typedef typename X::const_local_iterator const_local_iterator; #endif typedef typename std::iterator_traits<iterator>::pointer iterator_pointer; typedef typename std::iterator_traits<const_iterator>::pointer const_iterator_pointer; #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename std::iterator_traits<local_iterator>::pointer local_iterator_pointer; typedef typename std::iterator_traits<const_local_iterator>::pointer const_local_iterator_pointer; #endif BOOST_STATIC_ASSERT( (boost::is_same<value_type const*, iterator_pointer>::value)); BOOST_STATIC_ASSERT( (boost::is_same<value_type const*, const_iterator_pointer>::value)); #ifndef BOOST_UNORDERED_FOA_TESTS BOOST_STATIC_ASSERT( (boost::is_same<value_type const*, local_iterator_pointer>::value)); BOOST_STATIC_ASSERT( (boost::is_same<value_type const*, const_local_iterator_pointer>::value)); #endif // pointer_traits<iterator> BOOST_STATIC_ASSERT((boost::is_same<iterator, typename boost::pointer_traits<iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type const, typename boost::pointer_traits<iterator>::element_type>::value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<iterator>::difference_type>::value)); // pointer_traits<const_iterator> BOOST_STATIC_ASSERT((boost::is_same<const_iterator, typename boost::pointer_traits<const_iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type const, typename boost::pointer_traits<const_iterator>::element_type>::value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<const_iterator>::difference_type>::value)); (void) r; #ifndef BOOST_UNORDERED_FOA_TESTS // pointer_traits<local_iterator> BOOST_STATIC_ASSERT((boost::is_same<local_iterator, typename boost::pointer_traits<local_iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type const, typename boost::pointer_traits<local_iterator>::element_type>::value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<local_iterator>::difference_type>::value)); // pointer_traits<const_local_iterator> BOOST_STATIC_ASSERT((boost::is_same<const_local_iterator, typename boost::pointer_traits<const_local_iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type const, typename boost::pointer_traits<const_local_iterator>::element_type>:: value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<const_local_iterator>::difference_type>:: value)); typedef typename X::node_type node_type; typedef typename node_type::value_type node_value_type; BOOST_STATIC_ASSERT((boost::is_same<value_type, node_value_type>::value)); // Call node_type functions. test::minimal::constructor_param v; Key k_lvalue(v); r.emplace(std::move(k_lvalue)); node_type n1 = r.extract(r.begin()); test::check_return_type<value_type>::equals_ref(n1.value()); #endif } template <class X, class Key, class T> void unordered_map_test(X& r, Key const& k, T const& v) { typedef typename X::value_type value_type; typedef typename X::key_type key_type; BOOST_STATIC_ASSERT( (boost::is_same<value_type, std::pair<key_type const, T> >::value)); // iterator pointer / const_pointer_type typedef typename X::iterator iterator; typedef typename X::const_iterator const_iterator; #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename X::local_iterator local_iterator; typedef typename X::const_local_iterator const_local_iterator; #endif typedef typename std::iterator_traits<iterator>::pointer iterator_pointer; typedef typename std::iterator_traits<const_iterator>::pointer const_iterator_pointer; #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename std::iterator_traits<local_iterator>::pointer local_iterator_pointer; typedef typename std::iterator_traits<const_local_iterator>::pointer const_local_iterator_pointer; #endif BOOST_STATIC_ASSERT((boost::is_same<value_type*, iterator_pointer>::value)); BOOST_STATIC_ASSERT( (boost::is_same<value_type const*, const_iterator_pointer>::value)); #ifndef BOOST_UNORDERED_FOA_TESTS BOOST_STATIC_ASSERT( (boost::is_same<value_type*, local_iterator_pointer>::value)); BOOST_STATIC_ASSERT( (boost::is_same<value_type const*, const_local_iterator_pointer>::value)); #endif // pointer_traits<iterator> BOOST_STATIC_ASSERT((boost::is_same<iterator, typename boost::pointer_traits<iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type, typename boost::pointer_traits<iterator>::element_type>::value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<iterator>::difference_type>::value)); // pointer_traits<const_iterator> BOOST_STATIC_ASSERT((boost::is_same<const_iterator, typename boost::pointer_traits<const_iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type const, typename boost::pointer_traits<const_iterator>::element_type>::value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<const_iterator>::difference_type>::value)); #ifndef BOOST_UNORDERED_FOA_TESTS // pointer_traits<local_iterator> BOOST_STATIC_ASSERT((boost::is_same<local_iterator, typename boost::pointer_traits<local_iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type, typename boost::pointer_traits<local_iterator>::element_type>::value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<local_iterator>::difference_type>::value)); // pointer_traits<const_local_iterator> BOOST_STATIC_ASSERT((boost::is_same<const_local_iterator, typename boost::pointer_traits<const_local_iterator>::pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<value_type const, typename boost::pointer_traits<const_local_iterator>::element_type>:: value)); BOOST_STATIC_ASSERT((boost::is_same<std::ptrdiff_t, typename boost::pointer_traits<const_local_iterator>::difference_type>:: value)); typedef typename X::node_type node_type; typedef typename node_type::key_type node_key_type; typedef typename node_type::mapped_type node_mapped_type; BOOST_STATIC_ASSERT((boost::is_same<Key, node_key_type>::value)); BOOST_STATIC_ASSERT((boost::is_same<T, node_mapped_type>::value)); // Superfluous,but just to make sure. BOOST_STATIC_ASSERT((!boost::is_const<node_key_type>::value)); #endif // Calling functions r.insert(std::pair<Key const, T>(k, v)); r.insert(r.begin(), std::pair<Key const, T>(k, v)); std::pair<Key const, T> const value(k, v); r.insert(value); r.insert(r.end(), value); Key k_lvalue(k); T v_lvalue(v); // Emplace r.emplace(k, v); r.emplace(k_lvalue, v_lvalue); r.emplace(rvalue(k), rvalue(v)); #ifdef BOOST_UNORDERED_FOA_TESTS r.emplace(std::piecewise_construct, std::make_tuple(k), std::make_tuple(v)); #else r.emplace(boost::unordered::piecewise_construct, boost::make_tuple(k), boost::make_tuple(v)); #endif // Emplace with hint r.emplace_hint(r.begin(), k, v); r.emplace_hint(r.begin(), k_lvalue, v_lvalue); r.emplace_hint(r.begin(), rvalue(k), rvalue(v)); #ifdef BOOST_UNORDERED_FOA_TESTS r.emplace_hint(r.begin(), std::piecewise_construct, std::make_tuple(k), std::make_tuple(v)); #else r.emplace_hint(r.begin(), boost::unordered::piecewise_construct, boost::make_tuple(k), boost::make_tuple(v)); #endif #ifndef BOOST_UNORDERED_FOA_TESTS // Extract test::check_return_type<node_type>::equals(r.extract(r.begin())); r.emplace(k, v); test::check_return_type<node_type>::equals(r.extract(k)); r.emplace(k, v); node_type n1 = r.extract(r.begin()); test::check_return_type<key_type>::equals_ref(n1.key()); test::check_return_type<T>::equals_ref(n1.mapped()); node_type n2 = std::move(n1); r.insert(std::move(n2)); r.insert(r.extract(r.begin())); n2 = r.extract(r.begin()); r.insert(r.begin(), std::move(n2)); r.insert(r.end(), r.extract(r.begin())); node_type n = r.extract(r.begin()); test::check_return_type<node_key_type>::equals_ref(n.key()); test::check_return_type<node_mapped_type>::equals_ref(n.mapped()); #endif } template <class X> void equality_test(X& r) { X const a = r, b = r; test::check_return_type<bool>::equals(a == b); test::check_return_type<bool>::equals(a != b); } template <class X, class T> void unordered_unique_test(X& r, T const& t) { typedef typename X::iterator iterator; test::check_return_type<std::pair<iterator, bool> >::equals(r.insert(t)); test::check_return_type<std::pair<iterator, bool> >::equals(r.emplace(t)); #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename X::node_type node_type; typedef typename X::insert_return_type insert_return_type; // insert_return_type // TODO; // boost::function_requires< // std::moveConstructibleConcept<insert_return_type> // >(); // TODO; // boost::function_requires< // std::moveAssignableConcept<insert_return_type> // >(); boost::function_requires< boost::DefaultConstructibleConcept<insert_return_type> >(); // TODO: // boost::function_requires< // boost::DestructibleConcept<insert_return_type> // >(); insert_return_type insert_return, insert_return2; test::check_return_type<bool>::equals(insert_return.inserted); test::check_return_type<iterator>::equals(insert_return.position); test::check_return_type<node_type>::equals_ref(insert_return.node); boost::core::invoke_swap(insert_return, insert_return2); #endif } template <class X, class T> void unordered_equivalent_test(X& r, T const& t) { typedef typename X::iterator iterator; test::check_return_type<iterator>::equals(r.insert(t)); test::check_return_type<iterator>::equals(r.emplace(t)); } template <class X, class Key, class T> void unordered_map_functions(X&, Key const& k, T const& v) { typedef typename X::mapped_type mapped_type; typedef typename X::iterator iterator; X a; test::check_return_type<mapped_type>::equals_ref(a[k]); test::check_return_type<mapped_type>::equals_ref(a[rvalue(k)]); test::check_return_type<mapped_type>::equals_ref(a.at(k)); test::check_return_type<std::pair<iterator, bool> >::equals( a.try_emplace(k, v)); test::check_return_type<std::pair<iterator, bool> >::equals( a.try_emplace(rvalue(k), v)); test::check_return_type<iterator>::equals(a.try_emplace(a.begin(), k, v)); test::check_return_type<iterator>::equals( a.try_emplace(a.begin(), rvalue(k), v)); test::check_return_type<std::pair<iterator, bool> >::equals( a.insert_or_assign(k, v)); test::check_return_type<std::pair<iterator, bool> >::equals( a.insert_or_assign(rvalue(k), v)); test::check_return_type<iterator>::equals( a.insert_or_assign(a.begin(), k, v)); test::check_return_type<iterator>::equals( a.insert_or_assign(a.begin(), rvalue(k), v)); X const b = a; test::check_return_type<mapped_type const>::equals_ref(b.at(k)); } template <class X, class Key, class Hash, class Pred> void unordered_test(X& x, Key& k, Hash& hf, Pred& eq) { unordered_destructible_test(x); typedef typename X::key_type key_type; typedef typename X::hasher hasher; typedef typename X::key_equal key_equal; typedef typename X::size_type size_type; typedef typename X::iterator iterator; typedef typename X::const_iterator const_iterator; #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename X::local_iterator local_iterator; typedef typename X::const_local_iterator const_local_iterator; typedef typename std::iterator_traits<iterator>::iterator_category iterator_category; typedef typename std::iterator_traits<iterator>::difference_type iterator_difference; typedef typename std::iterator_traits<iterator>::pointer iterator_pointer; typedef typename std::iterator_traits<iterator>::reference iterator_reference; typedef typename std::iterator_traits<local_iterator>::iterator_category local_iterator_category; typedef typename std::iterator_traits<local_iterator>::difference_type local_iterator_difference; typedef typename std::iterator_traits<local_iterator>::pointer local_iterator_pointer; typedef typename std::iterator_traits<local_iterator>::reference local_iterator_reference; typedef typename std::iterator_traits<const_iterator>::iterator_category const_iterator_category; typedef typename std::iterator_traits<const_iterator>::difference_type const_iterator_difference; typedef typename std::iterator_traits<const_iterator>::pointer const_iterator_pointer; typedef typename std::iterator_traits<const_iterator>::reference const_iterator_reference; typedef typename std::iterator_traits<const_local_iterator>::iterator_category const_local_iterator_category; typedef typename std::iterator_traits<const_local_iterator>::difference_type const_local_iterator_difference; typedef typename std::iterator_traits<const_local_iterator>::pointer const_local_iterator_pointer; typedef typename std::iterator_traits<const_local_iterator>::reference const_local_iterator_reference; #endif typedef typename X::allocator_type allocator_type; BOOST_STATIC_ASSERT((boost::is_same<Key, key_type>::value)); // boost::function_requires<boost::CopyConstructibleConcept<key_type> >(); // boost::function_requires<boost::AssignableConcept<key_type> >(); BOOST_STATIC_ASSERT((boost::is_same<Hash, hasher>::value)); test::check_return_type<std::size_t>::equals(hf(k)); BOOST_STATIC_ASSERT((boost::is_same<Pred, key_equal>::value)); test::check_return_type<bool>::convertible(eq(k, k)); #ifndef BOOST_UNORDERED_FOA_TESTS boost::function_requires<boost::InputIteratorConcept<local_iterator> >(); BOOST_STATIC_ASSERT( (boost::is_same<local_iterator_category, iterator_category>::value)); BOOST_STATIC_ASSERT( (boost::is_same<local_iterator_difference, iterator_difference>::value)); BOOST_STATIC_ASSERT( (boost::is_same<local_iterator_pointer, iterator_pointer>::value)); BOOST_STATIC_ASSERT( (boost::is_same<local_iterator_reference, iterator_reference>::value)); boost::function_requires< boost::InputIteratorConcept<const_local_iterator> >(); BOOST_STATIC_ASSERT((boost::is_same<const_local_iterator_category, const_iterator_category>::value)); BOOST_STATIC_ASSERT((boost::is_same<const_local_iterator_difference, const_iterator_difference>::value)); BOOST_STATIC_ASSERT((boost::is_same<const_local_iterator_pointer, const_iterator_pointer>::value)); BOOST_STATIC_ASSERT((boost::is_same<const_local_iterator_reference, const_iterator_reference>::value)); #endif X a; allocator_type m = a.get_allocator(); // Constructors X(10, hf, eq); X a1(10, hf, eq); X(10, hf); X a2(10, hf); X(10); X a3(10); X(); X a4; X(10, hf, eq, m); X a1a(10, hf, eq, m); X(10, hf, m); X a2a(10, hf, m); X(10, m); X a3a(10, m); (X(m)); X a4a(m); test::check_return_type<size_type>::equals(a.erase(k)); const_iterator q1 = a.cbegin(), q2 = a.cend(); test::check_return_type<iterator>::equals(a.erase(q1, q2)); TEST_NOEXCEPT_EXPR(a.clear()) a.clear(); X const b; test::check_return_type<hasher>::equals(b.hash_function()); test::check_return_type<key_equal>::equals(b.key_eq()); test::check_return_type<iterator>::equals(a.find(k)); test::check_return_type<const_iterator>::equals(b.find(k)); test::check_return_type<size_type>::equals(b.count(k)); test::check_return_type<std::pair<iterator, iterator> >::equals( a.equal_range(k)); test::check_return_type<std::pair<const_iterator, const_iterator> >::equals( b.equal_range(k)); test::check_return_type<size_type>::equals(b.bucket_count()); #ifndef BOOST_UNORDERED_FOA_TESTS test::check_return_type<size_type>::equals(b.max_bucket_count()); test::check_return_type<size_type>::equals(b.bucket(k)); test::check_return_type<size_type>::equals(b.bucket_size(0)); test::check_return_type<local_iterator>::equals(a.begin(0)); test::check_return_type<const_local_iterator>::equals(b.begin(0)); test::check_return_type<local_iterator>::equals(a.end(0)); test::check_return_type<const_local_iterator>::equals(b.end(0)); test::check_return_type<const_local_iterator>::equals(a.cbegin(0)); test::check_return_type<const_local_iterator>::equals(b.cbegin(0)); test::check_return_type<const_local_iterator>::equals(a.cend(0)); test::check_return_type<const_local_iterator>::equals(b.cend(0)); #endif test::check_return_type<float>::equals(b.load_factor()); test::check_return_type<float>::equals(b.max_load_factor()); a.max_load_factor((float)2.0); a.rehash(100); a.merge(a2); a.merge(rvalue_default<X>()); // Avoid unused variable warnings: sink(a); sink(a1); sink(a2); sink(a3); sink(a4); sink(a1a); sink(a2a); sink(a3a); sink(a4a); } template <class X, class Key, class T, class Hash, class Pred> void unordered_copyable_test(X& x, Key& k, T& t, Hash& hf, Pred& eq) { unordered_test(x, k, hf, eq); typedef typename X::iterator iterator; typedef typename X::const_iterator const_iterator; typedef typename X::allocator_type allocator_type; X a; allocator_type m = a.get_allocator(); typename X::value_type* i = 0; typename X::value_type* j = 0; // Constructors X(i, j, 10, hf, eq); X a5(i, j, 10, hf, eq); X(i, j, 10, hf); X a6(i, j, 10, hf); X(i, j, 10); X a7(i, j, 10); X(i, j); X a8(i, j); X(i, j, 10, hf, eq, m); X a5a(i, j, 10, hf, eq, m); X(i, j, 10, hf, m); X a6a(i, j, 10, hf, m); X(i, j, 10, m); X a7a(i, j, 10, m); // Not specified for some reason (maybe ambiguity with another constructor?) // X(i, j, m); // X a8a(i, j, m); // sink(a8a); std::size_t min_buckets = 10; X({t}); X({t}, min_buckets); X({t}, min_buckets, hf); X({t}, min_buckets, hf, eq); // X({t}, m); X({t}, min_buckets, m); X({t}, min_buckets, hf, m); X({t}, min_buckets, hf, eq, m); X const b; sink(X(b)); X a9(b); a = b; sink(X(b, m)); X a9a(b, m); X b1; b1.insert(t); X a9b(b1); sink(a9b); X a9c(b1, m); sink(a9c); const_iterator q = a.cbegin(); test::check_return_type<iterator>::equals(a.insert(q, t)); test::check_return_type<iterator>::equals(a.emplace_hint(q, t)); a.insert(i, j); std::initializer_list<T> list = {t}; a.insert(list); a.insert({t, t, t}); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900) && \ (!defined(__clang__) || __clang_major__ >= 4 || \ (__clang_major__ == 3 && __clang_minor__ >= 4)) a.insert({}); a.insert({t}); a.insert({t, t}); #endif X a10; a10.insert(t); q = a10.cbegin(); #ifdef BOOST_UNORDERED_FOA_TESTS test::check_return_type<iterator>::convertible(a10.erase(q)); #else test::check_return_type<iterator>::equals(a10.erase(q)); #endif // Avoid unused variable warnings: sink(a); sink(a5); sink(a6); sink(a7); sink(a8); sink(a9); sink(a5a); sink(a6a); sink(a7a); sink(a9a); #ifndef BOOST_UNORDERED_FOA_TESTS typedef typename X::node_type node_type; typedef typename X::allocator_type allocator_type; node_type const n_const = a.extract(a.begin()); test::check_return_type<allocator_type>::equals(n_const.get_allocator()); #endif } template <class X, class Key, class T, class Hash, class Pred> void unordered_movable_test(X& x, Key& k, T& /* t */, Hash& hf, Pred& eq) { unordered_test(x, k, hf, eq); typedef typename X::iterator iterator; typedef typename X::const_iterator const_iterator; typedef typename X::allocator_type allocator_type; X x1(rvalue_default<X>()); X x2(std::move(x1)); x1 = rvalue_default<X>(); x2 = std::move(x1); X a; allocator_type m = a.get_allocator(); test::minimal::constructor_param* i = 0; test::minimal::constructor_param* j = 0; // Constructors X(i, j, 10, hf, eq); X a5(i, j, 10, hf, eq); X(i, j, 10, hf); X a6(i, j, 10, hf); X(i, j, 10); X a7(i, j, 10); X(i, j); X a8(i, j); X(i, j, 10, hf, eq, m); X a5a(i, j, 10, hf, eq, m); X(i, j, 10, hf, m); X a6a(i, j, 10, hf, m); X(i, j, 10, m); X a7a(i, j, 10, m); // Not specified for some reason (maybe ambiguity with another constructor?) // X(i, j, m); // X a8a(i, j, m); // sink(a8a); const_iterator q = a.cbegin(); test::minimal::constructor_param v; a.emplace(v); test::check_return_type<iterator>::equals(a.emplace_hint(q, v)); T v1(v); a.emplace(std::move(v1)); T v2(v); a.insert(std::move(v2)); T v3(v); test::check_return_type<iterator>::equals(a.emplace_hint(q, std::move(v3))); T v4(v); test::check_return_type<iterator>::equals(a.insert(q, std::move(v4))); a.insert(i, j); X a10; T v5(v); a10.insert(std::move(v5)); q = a10.cbegin(); #ifdef BOOST_UNORDERED_FOA_TESTS test::check_return_type<iterator>::convertible(a10.erase(q)); #else test::check_return_type<iterator>::equals(a10.erase(q)); #endif // Avoid unused variable warnings: sink(a); sink(a5); sink(a6); sink(a7); sink(a8); sink(a5a); sink(a6a); sink(a7a); sink(a10); } template <class X, class T> void unordered_set_member_test(X& x, T& t) { X x1(x); x1.insert(t); x1.begin()->dummy_member(); x1.cbegin()->dummy_member(); } template <class X, class T> void unordered_map_member_test(X& x, T& t) { X x1(x); x1.insert(t); x1.begin()->first.dummy_member(); x1.cbegin()->first.dummy_member(); x1.begin()->second.dummy_member(); x1.cbegin()->second.dummy_member(); }
0
repos/unordered/test
repos/unordered/test/unordered/max_load_tests.cpp
// Copyright 2022-2023 Christian Mazakas. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or move at http://www.boost.org/LICENSE_1_0.txt) #if !defined(BOOST_UNORDERED_FOA_TESTS) #error "max_load_tests is currently only supported by open-addressed containers" #else #include "../helpers/unordered.hpp" #include "../helpers/helpers.hpp" #include "../helpers/random_values.hpp" #include "../helpers/test.hpp" #include "../helpers/tracker.hpp" #include "../objects/test.hpp" template <class X> void max_load_tests(X*, test::random_generator generator) { typedef typename X::size_type size_type; test::reset_sequence(); X x; x.reserve(1000); size_type max_load = x.max_load(); BOOST_TEST_GT(max_load, 0u); size_type bucket_count = x.bucket_count(); BOOST_TEST_GE(bucket_count, 1000u); test::ordered<X> tracker; { test::random_values<X> v(max_load, generator); x.insert(v.begin(), v.end()); tracker.insert_range(v.begin(), v.end()); BOOST_TEST_EQ(x.bucket_count(), bucket_count); BOOST_TEST_EQ(x.max_load(), max_load); BOOST_TEST_EQ(x.size(), max_load); } { test::random_values<X> v(100, generator); x.insert(v.begin(), v.end()); tracker.insert_range(v.begin(), v.end()); BOOST_TEST_GT(x.bucket_count(), bucket_count); BOOST_TEST_GT(x.max_load(), max_load); BOOST_TEST_GT(x.size(), max_load); } tracker.compare(x); } using test::default_generator; using test::generate_collisions; using test::limited_range; using test::sequential; boost::unordered_flat_set<int>* int_set_ptr; boost::unordered_flat_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_map_ptr; boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_set_tracking; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_map_tracking; boost::unordered_node_set<int>* int_node_set_ptr; boost::unordered_node_map<test::movable, test::movable, test::hash, test::equal_to, test::allocator2<test::movable> >* test_node_map_ptr; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_node_set_tracking; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::allocator1<std::pair<test::object const, test::object> > >* test_node_map_tracking; // clang-format off UNORDERED_TEST(max_load_tests, ((int_set_ptr)(test_map_ptr)(test_set_tracking)(test_map_tracking) (int_node_set_ptr)(test_node_map_ptr) (test_node_set_tracking)(test_node_map_tracking))( (sequential))) // clang-format on #endif RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/equivalent_keys_tests.cpp
// 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) #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include <algorithm> #include <map> #include "../helpers/list.hpp" #include "../helpers/tracker.hpp" #include "../helpers/invariants.hpp" template <class Container, class Iterator> void test_equal_insertion(Iterator begin, Iterator end) { typedef test::ordered<Container> tracker; Container x1; tracker x2 = test::create_ordered(x1); for (Iterator it = begin; it != end; ++it) { x1.insert(*it); x2.insert(*it); x2.compare_key(x1, *it); } x2.compare(x1); test::check_equivalent_keys(x1); } UNORDERED_AUTO_TEST (set_tests) { int values[][5] = {{1}, {54, 23}, {-13, 65}, {77, 77}, {986, 25, 986}}; #ifdef BOOST_UNORDERED_FOA_TESTS typedef boost::unordered_flat_set<int> set; test_equal_insertion<set>(values[0], values[0] + 1); test_equal_insertion<set>(values[1], values[1] + 2); test_equal_insertion<set>(values[2], values[2] + 2); test_equal_insertion<set>(values[3], values[3] + 2); test_equal_insertion<set>(values[4], values[4] + 3); typedef boost::unordered_node_set<int> node_set; test_equal_insertion<node_set>(values[0], values[0] + 1); test_equal_insertion<node_set>(values[1], values[1] + 2); test_equal_insertion<node_set>(values[2], values[2] + 2); test_equal_insertion<node_set>(values[3], values[3] + 2); test_equal_insertion<node_set>(values[4], values[4] + 3); #else typedef boost::unordered_set<int> set; test_equal_insertion<set>(values[0], values[0] + 1); test_equal_insertion<set>(values[1], values[1] + 2); test_equal_insertion<set>(values[2], values[2] + 2); test_equal_insertion<set>(values[3], values[3] + 2); test_equal_insertion<set>(values[4], values[4] + 3); typedef boost::unordered_multiset<int> multiset; test_equal_insertion<multiset>(values[0], values[0] + 1); test_equal_insertion<multiset>(values[1], values[1] + 2); test_equal_insertion<multiset>(values[2], values[2] + 2); test_equal_insertion<multiset>(values[3], values[3] + 2); test_equal_insertion<multiset>(values[4], values[4] + 3); #endif } UNORDERED_AUTO_TEST (map_tests) { typedef test::list<std::pair<int const, int> > values_type; values_type v[5]; v[0].push_back(std::pair<int const, int>(1, 1)); v[1].push_back(std::pair<int const, int>(28, 34)); v[1].push_back(std::pair<int const, int>(16, 58)); v[1].push_back(std::pair<int const, int>(-124, 62)); v[2].push_back(std::pair<int const, int>(432, 12)); v[2].push_back(std::pair<int const, int>(9, 13)); v[2].push_back(std::pair<int const, int>(432, 24)); #ifdef BOOST_UNORDERED_FOA_TESTS for (int i = 0; i < 5; ++i) test_equal_insertion<boost::unordered_flat_map<int, int> >( v[i].begin(), v[i].end()); for (int i2 = 0; i2 < 5; ++i2) test_equal_insertion<boost::unordered_node_map<int, int> >( v[i2].begin(), v[i2].end()); #else for (int i = 0; i < 5; ++i) test_equal_insertion<boost::unordered_map<int, int> >( v[i].begin(), v[i].end()); for (int i2 = 0; i2 < 5; ++i2) test_equal_insertion<boost::unordered_multimap<int, int> >( v[i2].begin(), v[i2].end()); #endif } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/reserve_tests.cpp
// Copyright 2021-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/unordered.hpp" #include "../helpers/test.hpp" #include <boost/config.hpp> #include <boost/container_hash/hash.hpp> #include <cmath> #include <functional> std::size_t total_allocation = 0; std::size_t num_allocations = 0; template <typename T> struct A { typedef T value_type; static int count; int i; A() : i(++count) {} template <class U> A(const A<U>& a) noexcept : i(a.i) {} T* allocate(std::size_t n) { total_allocation += n * sizeof(T); ++num_allocations; return (T*)(::operator new(n * sizeof(T))); } void deallocate(T* p, std::size_t n) noexcept { total_allocation -= n * sizeof(T); ::operator delete(p); } bool operator==(A const& a) const { return i == a.i; } bool operator!=(A const& a) const { return i != a.i; } }; template <class T> int A<T>::count = 0; template <class UnorderedContainer> void bucket_count_constructor(UnorderedContainer*) { BOOST_TEST_EQ(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); { std::size_t count = 50000; UnorderedContainer s(count); BOOST_TEST_GE(total_allocation, count * sizeof(void*)); BOOST_TEST_GE(s.bucket_count(), count); } BOOST_TEST_GT(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); num_allocations = 0; } template <class UnorderedContainer> void range_bucket_constructor(UnorderedContainer*) { BOOST_TEST_EQ(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); { UnorderedContainer s1; std::size_t count = 50000; UnorderedContainer s2(s1.begin(), s1.end(), count); BOOST_TEST_GE(total_allocation, count * sizeof(void*)); BOOST_TEST_GE(s2.bucket_count(), count); } BOOST_TEST_GT(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); num_allocations = 0; } template <class UnorderedContainer> void reserve_tests(UnorderedContainer*) { BOOST_TEST_EQ(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); { UnorderedContainer s; // simple math for the test: // max_load_factor = max_size / bucket_count, before a rehashing occurs // // reserve() respects max load factor and its argument implies the max size // // reserve(count) => bucket_count = ceil(count / max_load_factor) // internal policies reshape bucket_count accordingly but guarantee count as // a minimum // std::size_t count = 50000; s.max_load_factor(0.37f); s.reserve(count); std::size_t expected_bucket_count = static_cast<std::size_t>( std::ceil(static_cast<float>(count) / s.max_load_factor())); BOOST_TEST_GE(total_allocation, expected_bucket_count * sizeof(void*)); BOOST_TEST_GE(s.bucket_count(), expected_bucket_count); std::size_t prev_allocations = num_allocations; s.reserve(count); BOOST_TEST_EQ(num_allocations, prev_allocations); } BOOST_TEST_GT(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); num_allocations = 0; } template <class UnorderedContainer> void rehash_tests(UnorderedContainer*) { BOOST_TEST_EQ(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); { UnorderedContainer s; std::size_t count = 1000; s.rehash(count); // test that an initial allocation occurs // BOOST_TEST_GE(total_allocation, count * sizeof(void*)); BOOST_TEST_GE(s.bucket_count(), count); // prove idempotence, that rehashing with the exact same bucket count causes // no reallocations // std::size_t prev_allocations = num_allocations; std::size_t prev_total_allocation = total_allocation; s.rehash(count); BOOST_TEST_EQ(num_allocations, prev_allocations); BOOST_TEST_EQ(total_allocation, prev_total_allocation); // prove that when we rehash, exceeding the current bucket count, that we // properly deallocate the current bucket array and then reallocate the // larger one // std::size_t prev_count = s.bucket_count(); count = s.bucket_count() + 2; s.rehash(count); BOOST_TEST_GT(num_allocations, prev_allocations); BOOST_TEST_GE(total_allocation, count * sizeof(void*)); BOOST_TEST_GE(s.bucket_count(), count); // concurrent memory usage here should be less than the sum of the memory // required for the previous bucket array and our current one // note, the test is vulnerable to cases where the next calculated bucket // count can exceed `prev_count + count` // #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST_LT(s.bucket_count(), prev_count + count); BOOST_TEST_LE(total_allocation, (prev_count + count) * sizeof(typename UnorderedContainer::value_type) + ((prev_count + count) / 15 + 1) * 16); #else std::size_t const estimated_bucket_group_size = 3 * sizeof(void*) + sizeof(std::size_t); std::size_t const estimated_bucket_groups = s.bucket_count() / (sizeof(std::size_t) * 8); BOOST_TEST_LT(s.bucket_count(), prev_count + count); BOOST_TEST_LE(total_allocation, (prev_count + count) * sizeof(void*) + estimated_bucket_group_size * estimated_bucket_groups); #endif } BOOST_TEST_GT(num_allocations, 0u); BOOST_TEST_EQ(total_allocation, 0u); num_allocations = 0; } UNORDERED_AUTO_TEST (allocator_check) { // prove Allocator invariants // from cppref: // Given: // * A, an Allocator type for type T // * B, the corresponding Allocator type for some cv-unqualified object type // U (as obtained by rebinding A) // // Expression: // A a(b) // // Return Type: // Constructs `a` such that `B(a)==b` and `A(b)==a`. // (Note: This implies that all allocators related by rebind maintain each // other's resources, such as memory pools.) // // typedef boost::allocator_rebind<A<int>, float>::type alloc_rebound; alloc_rebound b; A<int> a(b); BOOST_TEST(alloc_rebound(a) == b); BOOST_TEST(A<int>(b) == a); } #ifdef BOOST_UNORDERED_FOA_TESTS static boost::unordered_flat_set<int*, boost::hash<int*>, std::equal_to<int*>, A<int*> >* test_set; static boost::unordered_flat_map<int*, int*, boost::hash<int*>, std::equal_to<int*>, A<std::pair<int const*, int*> > >* test_map; static boost::unordered_node_set<int*, boost::hash<int*>, std::equal_to<int*>, A<int*> >* test_node_set; static boost::unordered_node_map<int*, int*, boost::hash<int*>, std::equal_to<int*>, A<std::pair<int const*, int*> > >* test_node_map; // clang-format off UNORDERED_TEST(bucket_count_constructor, ((test_set)(test_map)(test_node_set)(test_node_map))) UNORDERED_TEST(range_bucket_constructor, ((test_set)(test_map)(test_node_set)(test_node_map))) UNORDERED_TEST(reserve_tests, ((test_set)(test_map)(test_node_set)(test_node_map))) UNORDERED_TEST(rehash_tests, ((test_set)(test_map)(test_node_set)(test_node_map))) // clang-format on #else static boost::unordered_set<int, boost::hash<int>, std::equal_to<int>, A<int> >* test_set; static boost::unordered_multiset<int, boost::hash<int>, std::equal_to<int>, A<int> >* test_multiset; static boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>, A<std::pair<int const, int> > >* test_map; static boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>, A<std::pair<int const, int> > >* test_multimap; // clang-format off UNORDERED_TEST(bucket_count_constructor, ((test_set)(test_map)(test_multiset)(test_multimap))) UNORDERED_TEST(range_bucket_constructor, ((test_set)(test_map)(test_multiset)(test_multimap))) UNORDERED_TEST(reserve_tests, ((test_set)(test_map)(test_multiset)(test_multimap))) UNORDERED_TEST(rehash_tests, ((test_set)(test_map)(test_multiset)(test_multimap))) // clang-format on #endif RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/minimal_allocator.cpp
// Copyright 2011 Daniel James. // 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 "../objects/test.hpp" #include <boost/core/lightweight_test.hpp> #include <boost/static_assert.hpp> #include <boost/type_traits/is_same.hpp> #include <boost/unordered/detail/implementation.hpp> template <class Tp> struct SimpleAllocator { typedef Tp value_type; SimpleAllocator() {} template <class T> SimpleAllocator(const SimpleAllocator<T>&) {} Tp* allocate(std::size_t n) { return static_cast<Tp*>(::operator new(n * sizeof(Tp))); } void deallocate(Tp* p, std::size_t) { ::operator delete((void*)p); } }; template <typename T> void test_simple_allocator() { test::check_instances check_; typedef boost::unordered::detail::allocator_traits<SimpleAllocator<T> > traits; BOOST_STATIC_ASSERT((boost::is_same<typename traits::allocator_type, SimpleAllocator<T> >::value)); BOOST_STATIC_ASSERT((boost::is_same<typename traits::value_type, T>::value)); BOOST_STATIC_ASSERT((boost::is_same<typename traits::pointer, T*>::value)); BOOST_STATIC_ASSERT( (boost::is_same<typename traits::const_pointer, T const*>::value)); // BOOST_STATIC_ASSERT((boost::is_same<typename traits::void_pointer, void* // >::value)); // BOOST_STATIC_ASSERT((boost::is_same<typename traits::const_void_pointer, // void const*>::value)); BOOST_STATIC_ASSERT( (boost::is_same<typename traits::difference_type, std::ptrdiff_t>::value)); BOOST_STATIC_ASSERT((boost::is_same<typename traits::size_type, std::make_unsigned<std::ptrdiff_t>::type>::value)); BOOST_TEST(!traits::propagate_on_container_copy_assignment::value); BOOST_TEST(!traits::propagate_on_container_move_assignment::value); BOOST_TEST(!traits::propagate_on_container_swap::value); // rebind_alloc // rebind_traits SimpleAllocator<T> a; T* ptr1 = traits::allocate(a, 1); // T* ptr2 = traits::allocate(a, 1, static_cast<void const*>(ptr1)); traits::construct(a, ptr1, T(10)); // traits::construct(a, ptr2, T(30), ptr1); BOOST_TEST(*ptr1 == T(10)); // BOOST_TEST(*ptr2 == T(30)); traits::destroy(a, ptr1); // traits::destroy(a, ptr2); // traits::deallocate(a, ptr2, 1); traits::deallocate(a, ptr1, 1); traits::max_size(a); } int main() { test_simple_allocator<int>(); test_simple_allocator<test::object>(); return boost::report_errors(); }
0
repos/unordered/test
repos/unordered/test/unordered/contains_tests.cpp
// Copyright 2021-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/unordered.hpp" #include "../helpers/test.hpp" struct key { int x_; static int count_; key(int x) : x_(x) { ++count_; } key(key const& k) : x_(k.x_) { ++count_; } }; int key::count_; std::ostream& operator<<(std::ostream& os, key const& k) { os << "key { x_: " << k.x_ << " }"; return os; } bool operator==(key const& k, int const x) { return k.x_ == x; } bool operator==(int const x, key const& k) { return k.x_ == x; } struct transparent_hasher { typedef void is_transparent; std::size_t operator()(key const& k) const { return boost::hash<int>()(k.x_); } std::size_t operator()(int const k) const { return boost::hash<int>()(k); } }; struct transparent_key_equal { typedef void is_transparent; bool operator()(key const& k1, key const& k2) const { return k1.x_ == k2.x_; } bool operator()(int const x, key const& k1) const { return k1 == x; } bool operator()(key const& k1, int const x) const { return k1 == x; } }; struct hasher { std::size_t operator()(key const& k) const { return boost::hash<int>()(k.x_); } }; struct key_equal { bool operator()(key const& k1, key const& k2) const { return k1.x_ == k2.x_; } }; void count_reset() { key::count_ = 0; } template <class UnorderedMap> void test_map_transparent_contains() { count_reset(); UnorderedMap map; bool contains = map.contains(0); BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, 0); map.insert(std::make_pair(0, 1337)); map.insert(std::make_pair(0, 1338)); map.insert(std::make_pair(0, 1339)); map.insert(std::make_pair(1, 1340)); int const expected_key_count = key::count_; contains = map.contains(0); BOOST_TEST(contains); contains = map.contains(1); BOOST_TEST(contains); contains = map.contains(2); BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, expected_key_count); } template <class UnorderedMap> void test_map_non_transparent_contains() { count_reset(); UnorderedMap map; bool contains = map.contains(0); BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, 1); map.insert(std::make_pair(0, 1337)); map.insert(std::make_pair(0, 1338)); map.insert(std::make_pair(0, 1339)); map.insert(std::make_pair(1, 1340)); int key_count = key::count_; contains = map.contains(0); ++key_count; BOOST_TEST(contains); contains = map.contains(1); ++key_count; BOOST_TEST(contains); contains = map.contains(2); ++key_count; BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, key_count); } void test_map() { #ifdef BOOST_UNORDERED_FOA_TESTS typedef boost::unordered_flat_map<key, int, transparent_hasher, transparent_key_equal> transparent_map; typedef boost::unordered_flat_map<key, int, transparent_hasher, key_equal> non_transparent_map1; typedef boost::unordered_flat_map<key, int, hasher, transparent_key_equal> non_transparent_map2; typedef boost::unordered_flat_map<key, int, hasher, key_equal> non_transparent_map3; typedef boost::unordered_node_map<key, int, transparent_hasher, transparent_key_equal> transparent_node_map; typedef boost::unordered_node_map<key, int, transparent_hasher, key_equal> non_transparent_node_map1; typedef boost::unordered_node_map<key, int, hasher, transparent_key_equal> non_transparent_node_map2; typedef boost::unordered_node_map<key, int, hasher, key_equal> non_transparent_node_map3; test_map_transparent_contains<transparent_node_map>(); test_map_non_transparent_contains<non_transparent_node_map1>(); test_map_non_transparent_contains<non_transparent_node_map2>(); test_map_non_transparent_contains<non_transparent_node_map3>(); #else typedef boost::unordered_map<key, int, transparent_hasher, transparent_key_equal> transparent_map; typedef boost::unordered_map<key, int, transparent_hasher, key_equal> non_transparent_map1; typedef boost::unordered_map<key, int, hasher, transparent_key_equal> non_transparent_map2; typedef boost::unordered_map<key, int, hasher, key_equal> non_transparent_map3; #endif test_map_transparent_contains<transparent_map>(); test_map_non_transparent_contains<non_transparent_map1>(); test_map_non_transparent_contains<non_transparent_map2>(); test_map_non_transparent_contains<non_transparent_map3>(); } #ifndef BOOST_UNORDERED_FOA_TESTS void test_multimap() { typedef boost::unordered_multimap<key, int, transparent_hasher, transparent_key_equal> transparent_multimap; typedef boost::unordered_multimap<key, int, transparent_hasher, key_equal> non_transparent_multimap1; typedef boost::unordered_multimap<key, int, hasher, transparent_key_equal> non_transparent_multimap2; typedef boost::unordered_multimap<key, int, hasher, key_equal> non_transparent_multimap3; test_map_transparent_contains<transparent_multimap>(); test_map_non_transparent_contains<non_transparent_multimap1>(); test_map_non_transparent_contains<non_transparent_multimap2>(); test_map_non_transparent_contains<non_transparent_multimap3>(); } #endif template <class UnorderedSet> void test_set_transparent_contains() { count_reset(); UnorderedSet set; bool contains = set.contains(0); BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, 0); set.insert(0); set.insert(0); set.insert(0); set.insert(1); int const expected_key_count = key::count_; contains = set.contains(0); BOOST_TEST(contains); contains = set.contains(1); BOOST_TEST(contains); contains = set.contains(2); BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, expected_key_count); } template <class UnorderedSet> void test_set_non_transparent_contains() { count_reset(); UnorderedSet set; bool contains = set.contains(0); BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, 1); set.insert(0); set.insert(0); set.insert(0); set.insert(1); int key_count = key::count_; contains = set.contains(0); ++key_count; BOOST_TEST(contains); contains = set.contains(1); ++key_count; BOOST_TEST(contains); contains = set.contains(2); ++key_count; BOOST_TEST(!contains); BOOST_TEST_EQ(key::count_, key_count); } void test_set() { #ifdef BOOST_UNORDERED_FOA_TESTS typedef boost::unordered_flat_set<key, transparent_hasher, transparent_key_equal> transparent_set; typedef boost::unordered_flat_set<key, transparent_hasher, key_equal> non_transparent_set1; typedef boost::unordered_flat_set<key, hasher, transparent_key_equal> non_transparent_set2; typedef boost::unordered_flat_set<key, hasher, key_equal> non_transparent_set3; typedef boost::unordered_node_set<key, transparent_hasher, transparent_key_equal> transparent_node_set; typedef boost::unordered_node_set<key, transparent_hasher, key_equal> non_transparent_node_set1; typedef boost::unordered_node_set<key, hasher, transparent_key_equal> non_transparent_node_set2; typedef boost::unordered_node_set<key, hasher, key_equal> non_transparent_node_set3; test_set_transparent_contains<transparent_node_set>(); test_set_non_transparent_contains<non_transparent_node_set1>(); test_set_non_transparent_contains<non_transparent_node_set2>(); test_set_non_transparent_contains<non_transparent_node_set3>(); #else typedef boost::unordered_set<key, transparent_hasher, transparent_key_equal> transparent_set; typedef boost::unordered_set<key, transparent_hasher, key_equal> non_transparent_set1; typedef boost::unordered_set<key, hasher, transparent_key_equal> non_transparent_set2; typedef boost::unordered_set<key, hasher, key_equal> non_transparent_set3; #endif test_set_transparent_contains<transparent_set>(); test_set_non_transparent_contains<non_transparent_set1>(); test_set_non_transparent_contains<non_transparent_set2>(); test_set_non_transparent_contains<non_transparent_set3>(); } #ifndef BOOST_UNORDERED_FOA_TESTS void test_multiset() { typedef boost::unordered_multiset<key, transparent_hasher, transparent_key_equal> transparent_multiset; typedef boost::unordered_multiset<key, transparent_hasher, key_equal> non_transparent_multiset1; typedef boost::unordered_multiset<key, hasher, transparent_key_equal> non_transparent_multiset2; typedef boost::unordered_multiset<key, hasher, key_equal> non_transparent_multiset3; test_set_transparent_contains<transparent_multiset>(); test_set_non_transparent_contains<non_transparent_multiset1>(); test_set_non_transparent_contains<non_transparent_multiset2>(); test_set_non_transparent_contains<non_transparent_multiset3>(); } #endif UNORDERED_AUTO_TEST (contains_) { // avoid -Wshadow warning with `bool contains` test_map(); test_set(); #ifndef BOOST_UNORDERED_FOA_TESTS test_multimap(); test_multiset(); #endif } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/find_tests.cpp
// Copyright 2006-2009 Daniel James. // Copyright (C) 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) #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include "../objects/test.hpp" #include "../helpers/random_values.hpp" #include "../helpers/tracker.hpp" #include "../helpers/helpers.hpp" namespace find_tests { test::seed_t initialize_seed(78937); template <class X> void find_tests1(X*, test::random_generator generator) { typedef typename X::iterator iterator; { test::check_instances check_; test::random_values<X> v(500, generator); X x(v.begin(), v.end()); X const& x_const = x; test::ordered<X> tracker = test::create_ordered(x); tracker.insert_range(v.begin(), v.end()); for (typename test::ordered<X>::const_iterator it1 = tracker.begin(); it1 != tracker.end(); ++it1) { typename X::key_type key = test::get_key<X>(*it1); typename X::const_iterator const_pos = x_const.find(key); iterator pos = x.find(key); BOOST_TEST(const_pos != x_const.end()); BOOST_TEST(const_pos != x_const.end() && x_const.key_eq()(key, test::get_key<X>(*const_pos))); BOOST_TEST(pos != x.end()); BOOST_TEST(pos != x.end() && x.key_eq()(key, test::get_key<X>(*pos))); BOOST_TEST(x.count(key) == tracker.count(key)); test::compare_pairs(x.equal_range(key), tracker.equal_range(key), (typename X::value_type*)0); test::compare_pairs(x_const.equal_range(key), tracker.equal_range(key), (typename X::value_type*)0); } test::random_values<X> v2(500, generator); for (typename test::random_values<X>::const_iterator it2 = v2.begin(); it2 != v2.end(); ++it2) { typename X::key_type key = test::get_key<X>(*it2); if (tracker.find(test::get_key<X>(key)) == tracker.end()) { BOOST_TEST(x.find(key) == x.end()); BOOST_TEST(x_const.find(key) == x_const.end()); BOOST_TEST(x.count(key) == 0); std::pair<iterator, iterator> range = x.equal_range(key); BOOST_TEST(range.first == range.second); } } } { test::check_instances check_; X x; test::random_values<X> v2(5, generator); for (typename test::random_values<X>::const_iterator it3 = v2.begin(); it3 != v2.end(); ++it3) { typename X::key_type key = test::get_key<X>(*it3); BOOST_TEST(x.find(key) == x.end()); BOOST_TEST(x.count(key) == 0); std::pair<iterator, iterator> range = x.equal_range(key); BOOST_TEST(range.first == range.second); } } } struct compatible_key { test::object o_; compatible_key(test::object const& o) : o_(o) {} }; struct compatible_hash { test::hash hash_; std::size_t operator()(compatible_key const& k) const { return hash_(k.o_); } }; struct compatible_predicate { test::equal_to equal_; bool operator()(compatible_key const& k1, compatible_key const& k2) const { return equal_(k1.o_, k2.o_); } }; template <class X> void find_compatible_keys_test(X*, test::random_generator generator) { typedef typename test::random_values<X>::iterator value_iterator; test::random_values<X> v(500, generator); X x(v.begin(), v.end()); compatible_hash h; compatible_predicate eq; for (value_iterator it = v.begin(), end = v.end(); it != end; ++it) { typename X::key_type key = test::get_key<X>(*it); BOOST_TEST(x.find(key) == x.find(compatible_key(key), h, eq)); } test::random_values<X> v2(20, generator); for (value_iterator it = v2.begin(), end = v2.end(); it != end; ++it) { typename X::key_type key = test::get_key<X>(*it); BOOST_TEST(x.find(key) == x.find(compatible_key(key), h, eq)); } } using test::default_generator; using test::generate_collisions; using test::limited_range; #ifdef BOOST_UNORDERED_FOA_TESTS boost::unordered_flat_set<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_set; boost::unordered_flat_map<test::object, test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_map; boost::unordered_node_set<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_node_set; boost::unordered_node_map<test::object, test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_node_map; UNORDERED_TEST( find_tests1, ((test_set)(test_map)(test_node_set)(test_node_map))( (default_generator)(generate_collisions)(limited_range))) #else boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_set; boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_multiset; boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator2<test::object> >* test_map; boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator1<test::object> >* test_multimap; UNORDERED_TEST( find_tests1, ((test_set)(test_multiset)(test_map)(test_multimap))( (default_generator)(generate_collisions)(limited_range))) UNORDERED_TEST(find_compatible_keys_test, ((test_set)(test_multiset)(test_map)(test_multimap))( (default_generator)(generate_collisions)(limited_range))) #endif } RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/fwd_set_test.cpp
// Copyright 2008-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) // clang-format off #include "../helpers/prefix.hpp" #ifdef BOOST_UNORDERED_FOA_TESTS #include <boost/unordered/unordered_flat_set_fwd.hpp> #include <boost/unordered/unordered_node_set_fwd.hpp> #include <boost/unordered/detail/implementation.hpp> #else #include <boost/unordered/unordered_set_fwd.hpp> #endif #include "../helpers/postfix.hpp" // clang-format on struct true_type { char x[100]; }; struct false_type { char x; }; false_type is_unordered_set_impl(void*); #ifdef BOOST_UNORDERED_FOA_TESTS template <class Value, class Hash, class Pred, class Alloc> true_type is_unordered_set_impl( boost::unordered_flat_set<Value, Hash, Pred, Alloc>*); template <class Value, class Hash, class Pred, class Alloc> true_type is_unordered_set_impl( boost::unordered_node_set<Value, Hash, Pred, Alloc>*); template <typename T> void call_swap(boost::unordered_flat_set<T>& x, boost::unordered_flat_set<T>& y) { swap(x, y); } template <typename T> void call_swap(boost::unordered_node_set<T>& x, boost::unordered_node_set<T>& y) { swap(x, y); } template <typename T> bool call_equals( boost::unordered_flat_set<T>& x, boost::unordered_flat_set<T>& y) { return x == y; } template <typename T> bool call_equals( boost::unordered_node_set<T>& x, boost::unordered_node_set<T>& y) { return x == y; } template <typename T> bool call_not_equals( boost::unordered_flat_set<T>& x, boost::unordered_flat_set<T>& y) { return x != y; } template <typename T> bool call_not_equals( boost::unordered_node_set<T>& x, boost::unordered_node_set<T>& y) { return x != y; } #else template <class Value, class Hash, class Pred, class Alloc> true_type is_unordered_set_impl( boost::unordered_set<Value, Hash, Pred, Alloc>*); template <typename T> void call_swap(boost::unordered_set<T>& x, boost::unordered_set<T>& y) { swap(x, y); } template <typename T> bool call_equals(boost::unordered_set<T>& x, boost::unordered_set<T>& y) { return x == y; } template <typename T> bool call_not_equals(boost::unordered_set<T>& x, boost::unordered_set<T>& y) { return x != y; } #endif #ifndef BOOST_UNORDERED_FOA_TESTS template <typename T> void call_swap(boost::unordered_multiset<T>& x, boost::unordered_multiset<T>& y) { swap(x, y); } template <typename T> bool call_equals( boost::unordered_multiset<T>& x, boost::unordered_multiset<T>& y) { return x == y; } template <typename T> bool call_not_equals( boost::unordered_multiset<T>& x, boost::unordered_multiset<T>& y) { return x != y; } #endif #include "../helpers/test.hpp" #ifdef BOOST_UNORDERED_FOA_TESTS typedef boost::unordered_flat_set<int> int_set; typedef boost::unordered_node_set<int> int_node_set; #else typedef boost::unordered_set<int> int_set; typedef boost::unordered_multiset<int> int_multiset; #endif UNORDERED_AUTO_TEST (use_fwd_declared_trait_without_definition) { BOOST_TEST(sizeof(is_unordered_set_impl((int_set*)0)) == sizeof(true_type)); #ifdef BOOST_UNORDERED_FOA_TESTS BOOST_TEST( sizeof(is_unordered_set_impl((int_node_set*)0)) == sizeof(true_type)); #endif } #ifdef BOOST_UNORDERED_FOA_TESTS #include <boost/unordered/unordered_flat_set.hpp> #include <boost/unordered/unordered_node_set.hpp> #else #include <boost/unordered_set.hpp> #endif UNORDERED_AUTO_TEST (use_fwd_declared_trait) { int_set x; BOOST_TEST(sizeof(is_unordered_set_impl(&x)) == sizeof(true_type)); BOOST_TEST(sizeof(is_unordered_set_impl((int*)0)) == sizeof(false_type)); } #ifdef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (use_node_fwd_declared_trait) { int_node_set x; BOOST_TEST(sizeof(is_unordered_set_impl(&x)) == sizeof(true_type)); BOOST_TEST(sizeof(is_unordered_set_impl((int*)0)) == sizeof(false_type)); } #endif UNORDERED_AUTO_TEST (use_set_fwd_declared_function) { int_set x, y; x.insert(1); y.insert(2); call_swap(x, y); BOOST_TEST(y.find(1) != y.end()); BOOST_TEST(y.find(2) == y.end()); BOOST_TEST(x.find(1) == x.end()); BOOST_TEST(x.find(2) != x.end()); BOOST_TEST(!call_equals(x, y)); BOOST_TEST(call_not_equals(x, y)); } #ifdef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (use_node_set_fwd_declared_function) { int_node_set x, y; x.insert(1); y.insert(2); call_swap(x, y); BOOST_TEST(y.find(1) != y.end()); BOOST_TEST(y.find(2) == y.end()); BOOST_TEST(x.find(1) == x.end()); BOOST_TEST(x.find(2) != x.end()); BOOST_TEST(!call_equals(x, y)); BOOST_TEST(call_not_equals(x, y)); } #endif #ifndef BOOST_UNORDERED_FOA_TESTS UNORDERED_AUTO_TEST (use_multiset_fwd_declared_function) { int_multiset x, y; call_swap(x, y); BOOST_TEST(call_equals(x, y)); BOOST_TEST(!call_not_equals(x, y)); } #endif RUN_TESTS()
0
repos/unordered/test
repos/unordered/test/unordered/compile_map.cpp
// 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) // This test creates the containers with members that meet their minimum // requirements. Makes sure everything compiles and is defined correctly. #include "../helpers/unordered.hpp" #include "../helpers/test.hpp" #include "../objects/minimal.hpp" #include "./compile_tests.hpp" // Explicit instantiation to catch compile-time errors #ifdef BOOST_UNORDERED_FOA_TESTS // emulates what was already done for previous tests but without leaking to // the detail namespace // template <typename K, typename T, typename H, typename P, typename A> class instantiate_flat_map { typedef boost::unordered_flat_map<K, T, H, P, A> container; container x; }; template class instantiate_flat_map<int, int, boost::hash<int>, std::equal_to<int>, test::minimal::allocator<int> >; template class instantiate_flat_map<test::minimal::assignable const, test::minimal::default_assignable const, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<int> >; template <typename K, typename T, typename H, typename P, typename A> class instantiate_node_map { typedef boost::unordered_node_map<K, T, H, P, A> container; container x; }; template class instantiate_node_map<int, int, boost::hash<int>, std::equal_to<int>, test::minimal::allocator<int> >; template class instantiate_node_map<test::minimal::assignable const, test::minimal::default_assignable const, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<int> >; #else #define INSTANTIATE(type) \ template class boost::unordered::detail::instantiate_##type INSTANTIATE(map)<int, int, boost::hash<int>, std::equal_to<int>, test::minimal::allocator<int> >; INSTANTIATE(multimap)<int const, int const, boost::hash<int>, std::equal_to<int>, test::minimal::allocator<int> >; INSTANTIATE( map)<test::minimal::assignable const, test::minimal::default_assignable const, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<int> >; INSTANTIATE(multimap)<test::minimal::assignable, test::minimal::assignable, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<int> >; #endif template <template <class Key, class T, class H = boost::hash<Key>, class P = std::equal_to<Key>, class Allocator = std::allocator<std::pair<Key const, T> > > class Map> static void test0_impl() { test::minimal::constructor_param x; typedef std::pair<test::minimal::assignable const, test::minimal::assignable> value_type; value_type value(x, x); Map<int, int> int_map; Map<int, int, boost::hash<int>, std::equal_to<int>, test::minimal::cxx11_allocator<std::pair<int const, int> > > int_map2; Map<test::minimal::assignable, test::minimal::assignable, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<value_type> > map; container_test(int_map, std::pair<int const, int>(0, 0)); container_test(int_map2, std::pair<int const, int>(0, 0)); container_test(map, value); } UNORDERED_AUTO_TEST (test0) { #ifdef BOOST_UNORDERED_FOA_TESTS test0_impl<boost::unordered_flat_map>(); test0_impl<boost::unordered_node_map>(); #else test0_impl<boost::unordered_map>(); test0_impl<boost::unordered_multimap>(); #endif } template <template <class Key, class T, class H = boost::hash<Key>, class P = std::equal_to<Key>, class Allocator = std::allocator<std::pair<Key const, T> > > class Map> static void equality_tests_impl() { typedef std::pair<test::minimal::copy_constructible_equality_comparable const, test::minimal::copy_constructible_equality_comparable> value_type; Map<int, int> int_map; Map<int, int, boost::hash<int>, std::equal_to<int>, test::minimal::cxx11_allocator<std::pair<int const, int> > > int_map2; Map<test::minimal::copy_constructible_equality_comparable, test::minimal::copy_constructible_equality_comparable, test::minimal::hash<test::minimal::copy_constructible_equality_comparable>, test::minimal::equal_to< test::minimal::copy_constructible_equality_comparable>, test::minimal::allocator<value_type> > map; equality_test(int_map); equality_test(int_map2); equality_test(map); } UNORDERED_AUTO_TEST (equality_tests) { #ifdef BOOST_UNORDERED_FOA_TESTS equality_tests_impl<boost::unordered_flat_map>(); equality_tests_impl<boost::unordered_node_map>(); #else equality_tests_impl<boost::unordered_map>(); equality_tests_impl<boost::unordered_multimap>(); #endif } template <template <class Key, class T, class H = boost::hash<Key>, class P = std::equal_to<Key>, class Allocator = std::allocator<std::pair<Key const, T> > > class Map> static void test1_unique_impl() { boost::hash<int> hash; std::equal_to<int> equal_to; int value = 0; std::pair<int const, int> map_value(0, 0); Map<int, int> map; Map<int, int, boost::hash<int>, std::equal_to<int>, test::minimal::cxx11_allocator<std::pair<int const, int> > > map2; unordered_unique_test(map, map_value); unordered_map_test(map, value, value); unordered_copyable_test(map, value, map_value, hash, equal_to); unordered_map_functions(map, value, value); unordered_unique_test(map2, map_value); unordered_map_test(map2, value, value); unordered_copyable_test(map2, value, map_value, hash, equal_to); unordered_map_functions(map2, value, value); } #ifndef BOOST_UNORDERED_FOA_TESTS template <template <class Key, class T, class H = boost::hash<Key>, class P = std::equal_to<Key>, class Allocator = std::allocator<std::pair<Key const, T> > > class Map> static void test1_equivalent_impl() { boost::hash<int> hash; std::equal_to<int> equal_to; int value = 0; std::pair<int const, int> map_value(0, 0); Map<int, int> map; Map<int, int, boost::hash<int>, std::equal_to<int>, test::minimal::cxx11_allocator<std::pair<int const, int> > > map2; unordered_equivalent_test(map, map_value); unordered_map_test(map, value, value); unordered_copyable_test(map, value, map_value, hash, equal_to); unordered_equivalent_test(map2, map_value); unordered_map_test(map2, value, value); unordered_copyable_test(map2, value, map_value, hash, equal_to); } #endif UNORDERED_AUTO_TEST (test1) { #ifdef BOOST_UNORDERED_FOA_TESTS test1_unique_impl<boost::unordered_flat_map>(); test1_unique_impl<boost::unordered_node_map>(); #else test1_unique_impl<boost::unordered_map>(); test1_equivalent_impl<boost::unordered_multimap>(); #endif } template <template <class Key, class T, class H = boost::hash<Key>, class P = std::equal_to<Key>, class Allocator = std::allocator<std::pair<Key const, T> > > class Map> static void test2_unique_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); typedef std::pair<test::minimal::assignable const, test::minimal::assignable> map_value_type; map_value_type map_value(assignable, assignable); Map<test::minimal::assignable, test::minimal::assignable, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<map_value_type> > map; unordered_unique_test(map, map_value); unordered_map_test(map, assignable, assignable); unordered_copyable_test(map, assignable, map_value, hash, equal_to); unordered_map_member_test(map, map_value); Map<test::minimal::assignable, test::minimal::default_assignable, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<map_value_type> > map2; test::minimal::default_assignable default_assignable; unordered_map_functions(map2, assignable, default_assignable); } #ifndef BOOST_UNORDERED_FOA_TESTS template <template <class Key, class T, class H = boost::hash<Key>, class P = std::equal_to<Key>, class Allocator = std::allocator<std::pair<Key const, T> > > class Map> 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); typedef std::pair<test::minimal::assignable const, test::minimal::assignable> map_value_type; map_value_type map_value(assignable, assignable); Map<test::minimal::assignable, test::minimal::assignable, test::minimal::hash<test::minimal::assignable>, test::minimal::equal_to<test::minimal::assignable>, test::minimal::allocator<map_value_type> > map; unordered_equivalent_test(map, map_value); unordered_map_test(map, assignable, assignable); unordered_copyable_test(map, assignable, map_value, hash, equal_to); unordered_map_member_test(map, map_value); } #endif UNORDERED_AUTO_TEST (test2) { #ifdef BOOST_UNORDERED_FOA_TESTS test2_unique_impl<boost::unordered_flat_map>(); test2_unique_impl<boost::unordered_node_map>(); #else test2_unique_impl<boost::unordered_map>(); test2_equivalent_impl<boost::unordered_multimap>(); #endif } // Test for ambiguity when using key convertible from iterator // See LWG2059 struct lwg2059_key { int value; template <typename T> lwg2059_key(T v) : value(v) {} }; std::size_t hash_value(lwg2059_key x) { return static_cast<std::size_t>(x.value); } bool operator==(lwg2059_key x, lwg2059_key y) { return x.value == y.value; } UNORDERED_AUTO_TEST (lwg2059) { #ifdef BOOST_UNORDERED_FOA_TESTS { boost::unordered_flat_map<lwg2059_key, int> x; x.emplace(lwg2059_key(10), 5); x.erase(x.begin()); } { boost::unordered_node_map<lwg2059_key, int> x; x.emplace(lwg2059_key(10), 5); x.erase(x.begin()); } #else { boost::unordered_map<lwg2059_key, int> x; x.emplace(lwg2059_key(10), 5); x.erase(x.begin()); } { boost::unordered_multimap<lwg2059_key, int> x; x.emplace(lwg2059_key(10), 5); x.erase(x.begin()); } #endif } RUN_TESTS()
0
repos/unordered
repos/unordered/doc/preview.md
# Building Boost from the Tip of Develop To build Boost from the tip of the develop branch without cloning the entire history, use the command below: Linux: ```bash cwd=$(pwd) \ && cd ~ \ && git clone -b develop --depth 1 --recurse-submodules --shallow-submodules --jobs 8 https://github.com/boostorg/boost.git boost-develop-beta \ && cd boost-develop-beta \ && ./bootstrap.sh \ && ./b2 install --prefix=boost-install cxxstd=17 \ && echo "Boost successfully installed into $(realpath boost-install)! Add this to your CMake toolchain file. list(APPEND CMAKE_PREFIX_PATH $(realpath boost-install)) " \ && cd $cwd ``` Windows (using plain Command Prompt): ```bat cmd /v set cwd=%cd% ^ && cd %homepath% ^ && git clone -b develop --depth 1 --recurse-submodules --shallow-submodules --jobs 8 https://github.com/boostorg/boost.git boost-develop-beta ^ && cd boost-develop-beta ^ && bootstrap.bat ^ && b2 install --prefix=boost-install cxxstd=17 address-model=64 ^ && echo Boost successfully installed into !cd!\boost-install! ^ && echo Add this to your CMake toolchain file. ^ && echo list(APPEND CMAKE_PREFIX_PATH !cd:\=/!/boost-install) ^ && cd !cwd! ^ && exit ``` Note: you can build Boost with a specific compiler by setting the toolset in the `./b2` command above. To build with clang, specify `toolset=clang`; to build with a specific version of gcc, clang or msvc, specify e.g. `toolset=gcc-12` for GCC 12, `clang-14` for Clang 14, `msvc-14.3` for MSVC 14.3. The value of `cxxstd` can also be set to other values such as 11 for C++11, 14 for C++14, etc. For more info on what's possible, check out this link on b2: https://www.boost.org/doc/libs/develop/tools/build/doc/html/index.html#bbv2.overview.builtins.features This should hopefully cover the two most common cases of building a dependency, setting the compiler and C++ standard used.
0
repos/unordered
repos/unordered/doc/roadmap.md
# Refactoring Roadmap [Proof of concept](https://github.com/joaquintides/fca_unordered) implementation for a fast closed-addressing implementation. ## Plan of Refactoring * remove `ptr_node` and `ptr_bucket` * see if the code can survive a lack of the `extra_node` or maybe we hard-code it in * implement bucket groups as they are in `fca` but don't use them directly yet, add alongside the `buckets_` data member in `struct table` * try to remove `bucket_info_` from the node structure (breaks all call-sites that use `get_bucket()` and dependents) * make sure `fca` can successfully handle multi-variants at this stage + supports mutable iterators for `map`/`multimap` * do a hard-break: * update code to no longer use one single linked list across all buckets (each bucket contains its own unique list) * integrate the `bucket_group<Node>` structure into the `table` (update iterator call-sites to include `bucket_iterator`s) Blockers: * how to handle `multi` variants with new `fca` prototype ## Implementation Differences ### Unordered ### Node Type Bullet Points: * reify node type into a single one * come up with implementation for multi- variants * code that touches `get_bucket()` and `*_in_group()` member functions may need updating There are two node types in Unordered, `struct node` and `struct ptr_node`, and the node type is selected conditionally based on the Allocator's pointer type: ```c++ template <typename A, typename T, typename NodePtr, typename BucketPtr> struct pick_node2 { typedef boost::unordered::detail::node<A, T> node; // ... }; template <typename A, typename T> struct pick_node2<A, T, boost::unordered::detail::ptr_node<T>*, boost::unordered::detail::ptr_bucket*> { typedef boost::unordered::detail::ptr_node<T> node; // ... }; template <typename A, typename T> struct pick_node { typedef typename boost::remove_const<T>::type nonconst; typedef boost::unordered::detail::allocator_traits< typename boost::unordered::detail::rebind_wrap<A, boost::unordered::detail::ptr_node<nonconst> >::type> tentative_node_traits; typedef boost::unordered::detail::allocator_traits< typename boost::unordered::detail::rebind_wrap<A, boost::unordered::detail::ptr_bucket>::type> tentative_bucket_traits; typedef pick_node2<A, nonconst, typename tentative_node_traits::pointer, typename tentative_bucket_traits::pointer> pick; typedef typename pick::node node; typedef typename pick::bucket bucket; typedef typename pick::link_pointer link_pointer; }; ``` The node types are identical in terms of interface and the only difference is that `node` is chosen when the Allocator uses fancy pointers and `ptr_node` is chosen when the Allocator's pointer type is `T*`. Nodes in Unorderd store `bucket_info_`: ```cpp template <typename A, typename T> struct node : boost::unordered::detail::value_base<T> { link_pointer next_; std::size_t bucket_info_; node() : next_(), bucket_info_(0) {} // ... }; ``` `bucket_info_` maps each node back to its corresponding bucket via the member function: ```cpp std::size_t get_bucket() const { return bucket_info_ & ((std::size_t)-1 >> 1); } ``` `bucket_info_` is also used to demarcate the start of equivalent nodes in the containers via: ```cpp // Note that nodes start out as the first in their group, as `bucket_info_` defaults to 0. std::size_t is_first_in_group() const { return !(bucket_info_ & ~((std::size_t)-1 >> 1)); } void set_first_in_group() { bucket_info_ = bucket_info_ & ((std::size_t)-1 >> 1); } void reset_first_in_group() { bucket_info_ = bucket_info_ | ~((std::size_t)-1 >> 1); } ``` A goal of refactoring is to simply have one node type: ```cpp template<class T> struct node { node *next; T value; }; ``` that is used unconditionally. This also requires updating the code that touches the `bucket_info_` along with the code that that touches the `*_in_group()` member functions. ### Bucket Type Bullet points: * reify bucket structure into a single one * figure out how to add `bucket_group`s to the table struct Buckets are similar to nodes in that there are two variations: `template<class NodePointer> struct bucket` and `struct ptr_bucket`. The buckets exist to contain a pointer to a node, however they contain an `enum { extra_node = true };` or `enum { extra_node = false }` to determine whether or not the code should explicitly allocate a default constructed node whose address assigned as the dummy node at the end of the bucket array. `extra_node` is used in the creation and deletion of the bucket array but it is not inherently clear what its intended purpose is. ### Iterators Iterators are currently templated on the type of Node they store. Because `fca` constructs iterators with two arguments, all the call-sites that instantiate iterators will need to be updated but this a straight-forward mechanical change. Iterators are selected, as of now, via the `detail::map` and `detail::set` class templates. For example, for `unordered_map`, `iterator` is defined as: ```cpp typedef boost::unordered::detail::map<A, K, T, H, P> types; typedef typename types::table table; typedef typename table::iterator iterator; ``` The iterator is a member typedef of the `table` which is `types::table`. Examining `types` (aka `detail::map<...>`), we see: ```cpp template <typename A, typename K, typename M, typename H, typename P> struct map { // ... typedef boost::unordered::detail::table<types> table; // ... }; ``` Examining the `detail::table<types>` struct, we see: ```cpp template <typename Types> struct table { // ... typedef typename Types::iterator iterator; // ... } ``` Collapsing all of this, we see that our iterator types are defined here: ```cpp template <typename A, typename K, typename M, typename H, typename P> struct map { // ... typedef boost::unordered::detail::pick_node<A, value_type> pick; typedef typename pick::node node; typedef boost::unordered::iterator_detail::iterator<node> iterator; typedef boost::unordered::iterator_detail::c_iterator<node> c_iterator; typedef boost::unordered::iterator_detail::l_iterator<node> l_iterator; typedef boost::unordered::iterator_detail::cl_iterator<node> cl_iterator; // ... }; ``` This is similarly designed for `detail::set`: ```cpp typedef boost::unordered::iterator_detail::c_iterator<node> iterator; typedef boost::unordered::iterator_detail::c_iterator<node> c_iterator; typedef boost::unordered::iterator_detail::cl_iterator<node> l_iterator; typedef boost::unordered::iterator_detail::cl_iterator<node> cl_iterator; ``` The only difference here is that `set::iterator` is always a `c_iterator`, a `const_iterator` type.
0
repos
repos/tomlz/gyro.zzz
pkgs: tomlz: version: 0.2.0 description: "A TOML parsing library for Zig" license: MIT source_url: "https://github.com/mattyhall/tomlz" tags: parsing toml root: src/main.zig files: README.md LICENSE src/*.zig build.zig
0
repos
repos/tomlz/README.md
# tomlz A TOML parser for Zig targeting TOML v1.0.0, an easy API and safety. Also supports encoding/serializing values(implemented by @0x5a4)! ```zig const std = @import("std") const tomlz = @import("tomlz") var gpa = std.heap.page_allocator; var table = try tomlz.parse(gpa, \\foo = 1 \\bar = 2 ); defer table.deinit(gpa); table.getInteger("foo").?; // 1 // --- or --- const S = struct { foo: i64, bar: i64 }; const s = try tomlz.decode(S, gpa, \\foo = 1 \\bar = 2 ); // serialize a value like this (also see the examples) try tomlz.serialize( gpa, std.io.getStdout.writer() s, ); // foo = 1 // bar = 2 ``` ## Current status All types other than datetimes are supported. We pass 321/334 of the [toml tests](https://github.com/BurntSushi/toml-test) 11 of those are due to not having datetime support and the other two are minor lexing issues (allowing whitespace between the square brackets of an array header). We allow both parsing into a special TOML type, a `Table`, but also support decoding into a struct directly - including types that must be allocated like arrays and strings. The Serializer allows encoding every kind of zig type, overwriting it's default behaviour by implementing a function called `tomlzSerialize`, has the option to work without an allocator and can therefore even work at `comptime`! Note that for some types like `std.HashMap` its not possible to just encode all their fields, so custom logic is needed. We can't provide this, but it's not too difficult to implement it yourself(See examples). ## Installation tomlz supports being included as a module. Create a file called `build.zig.zon` if you do not already have one, and add `tomlz` as a dependency ``` .{ .name = "myproject", .version = "0.1.0", .dependencies = .{ .tomlz = .{ .url = "https://github.com/mattyhall/tomlz/archive/<commit-hash>.tar.gz", .hash = "12206cf9e90462ee6e14f593ea6e0802b9fe434429ba10992a1451e32900f741005c", }, } } ``` You'll have to replace the `<commit-hash>` part with an actual, recent commit-hash. The hash also needs changing, but `zig build` will complain and give you the correct one. In your `build.zig` file create and use the dependency ``` pub fn build(b: *std.Build) void { // ... setup ... const tomlz = b.dependency("tomlz", .{ .target = target, .optimize = optimize, }); // add the tomlz module exe.root_module.addImport("tomlz", tomlz.module("tomlz")); // .. continue ... } ``` ## Usage ### Table We currently provide a single entry point for parsing which returns a toml `Table` type. This type has helper methods for getting values out: ```zig const std = @import("std"); const tomlz = @import("tomlz"); var gpa = std.heap.page_allocator; var table = try tomlz.parse(gpa, \\int = 1 \\float = 2.0 \\boolean = true \\string = "hello, world" \\array = [1, 2, 3] \\table = { subvalue = 1, we.can.nest.keys = 2 } ); defer table.deinit(gpa); table.getInteger("int"); table.getFloat("float"); table.getBool("boolean"); table.getString("string"); table.getArray("array"); table.getTable("table"); ``` A simple example is [provided](https://github.com/mattyhall/tomlz/tree/main/examples/simple/). ### Decode ```zig const std = @import("std"); const tomlz = @import("tomlz"); var gpa = std.heap.page_allocator; const TripleCrowns = struct { worlds: i64, masters: i64, uks: i64 }; const Player = struct { name: []const u8, age: i64, hobbies: []const []const u8, triplecrowns: TripleCrowns, const Self = @This(); pub fn deinit(self: *Self, gpa: std.mem.Allocator) void { gpa.free(self.name); for (self.hobbies) |hobby| { gpa.free(hobby); } gpa.free(self.hobbies); } }; const Game = struct { name: []const u8, goat: Player, const Self = @This(); pub fn deinit(self: *Self, gpa: std.mem.Allocator) void { gpa.free(self.name); self.goat.deinit(gpa); } }; var s = try tomlz.decode(Game, gpa, \\name = "snooker" \\ \\[goat] \\name = "Ronnie o' Sullivan" \\age = 46 # as of Nov 2022 \\hobbies = ["running", "hustling at pool"] \\ \\[goat.triplecrowns] \\worlds = 7 \\masters = 7 \\uks = 7 ); defer s.deinit(gpa); ``` ### Encode Have a look at [the example](examples/serialize/src/main.zig). ## Goals and non-goals Goals and non-goals are subject to change based on how the project is used and my own time constraints. If you feel a goal or non-goal isn't quite right please open an issue and we can discuss it. ### Goals - TOML v1.0.0. The datetime portion of this is probably going to be unachievable until Zig gets a good standard library type for it or a library gets dominance. Other than that however we should pass all the [tests](https://github.com/BurntSushi/toml-test) - A nice API. Getting values from the `Value` type should be painless as possible and we should also provide deserialising a `Table` into a struct, similarly to how `std.json` does it - Easy installation. We should try to make using the library as a vendored dependency and as a package - on e.g. [gyro](https://github.com/mattnite/gyro) \- as easy as possible - Safety. The parser should never crash no matter the input. To achieve this we should run fuzzing against it - Support Zig master and the latest tagged release until Zig v1.0. This will be done by having the main branch track Zig master and a branch for each Zig release. Any improvements should be backported to the most recent release branch - Good error messages ### Non-goals - Super duper performance. We want to be as performant as possible without making the code harder to read. It is unlikely that parsing a TOML file is going to be the bottleneck in your application so "good" performance should be sufficient - Previous versions of TOML
0
repos
repos/tomlz/build.zig
const std = @import("std"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const lib = b.addStaticLibrary(.{ .name = "tomlz", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); b.installArtifact(lib); _ = b.addModule("tomlz", .{ .root_source_file = b.path("src/main.zig") }); const main_tests = b.addTest(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); const run_main_tests = b.addRunArtifact(main_tests); const test_step = b.step("test", "Run library tests"); test_step.dependOn(&run_main_tests.step); const fuzz_exe = b.addExecutable(.{ .name = "fuzz", .root_source_file = b.path("src/fuzz.zig"), .target = target, }); fuzz_exe.linkLibC(); b.installArtifact(fuzz_exe); const fuzz_compile_run = b.step("fuzz", "Build executable for fuzz testing afl-fuzz"); fuzz_compile_run.dependOn(&fuzz_exe.step); }
0
repos
repos/tomlz/flake.nix
{ description = "A TOML library for Zig"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; flake-utils.url = "github:numtide/flake-utils"; zls = { url = "github:zigtools/zls"; inputs.nixpkgs.follows = "nixpkgs"; }; flake-compat = { url = "github:edolstra/flake-compat"; flake = false; }; zig = { url = "github:mitchellh/zig-overlay"; inputs.nixpkgs.follows = "nixpkgs"; }; }; outputs = {self, nixpkgs, flake-utils, zls, flake-compat, zig}: let overlays = [ (final: prev: { zigpkgs = zig.packages.${prev.system}; }) (final: prev: { zlspkgs = zls.packages.${prev.system}; }) ]; systems = builtins.attrNames zig.packages; in flake-utils.lib.eachSystem systems (system: let pkgs = import nixpkgs { inherit overlays system; }; in { devShell = pkgs.mkShell { buildInputs = (with pkgs; [ zigpkgs.master-2024-06-18 zlspkgs.default bashInteractive gdb lldb ]); }; } ); }
0
repos
repos/tomlz/flake.lock
{ "nodes": { "flake-compat": { "flake": false, "locked": { "lastModified": 1696426674, "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", "owner": "edolstra", "repo": "flake-compat", "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", "type": "github" }, "original": { "owner": "edolstra", "repo": "flake-compat", "type": "github" } }, "flake-compat_2": { "flake": false, "locked": { "lastModified": 1696426674, "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", "owner": "edolstra", "repo": "flake-compat", "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", "type": "github" }, "original": { "owner": "edolstra", "repo": "flake-compat", "type": "github" } }, "flake-compat_3": { "flake": false, "locked": { "lastModified": 1696426674, "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", "owner": "edolstra", "repo": "flake-compat", "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", "type": "github" }, "original": { "owner": "edolstra", "repo": "flake-compat", "type": "github" } }, "flake-utils": { "inputs": { "systems": "systems" }, "locked": { "lastModified": 1710146030, "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "owner": "numtide", "repo": "flake-utils", "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "type": "github" }, "original": { "owner": "numtide", "repo": "flake-utils", "type": "github" } }, "flake-utils_2": { "inputs": { "systems": "systems_2" }, "locked": { "lastModified": 1705309234, "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "owner": "numtide", "repo": "flake-utils", "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "type": "github" }, "original": { "owner": "numtide", "repo": "flake-utils", "type": "github" } }, "flake-utils_3": { "inputs": { "systems": "systems_3" }, "locked": { "lastModified": 1710146030, "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "owner": "numtide", "repo": "flake-utils", "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "type": "github" }, "original": { "owner": "numtide", "repo": "flake-utils", "type": "github" } }, "flake-utils_4": { "inputs": { "systems": "systems_4" }, "locked": { "lastModified": 1705309234, "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "owner": "numtide", "repo": "flake-utils", "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "type": "github" }, "original": { "owner": "numtide", "repo": "flake-utils", "type": "github" } }, "gitignore": { "inputs": { "nixpkgs": [ "zls", "nixpkgs" ] }, "locked": { "lastModified": 1709087332, "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", "owner": "hercules-ci", "repo": "gitignore.nix", "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", "type": "github" }, "original": { "owner": "hercules-ci", "repo": "gitignore.nix", "type": "github" } }, "langref": { "flake": false, "locked": { "narHash": "sha256-O6p2tiKD8ZMhSX+DeA/o5hhAvcPkU2J9lFys/r11peY=", "type": "file", "url": "https://raw.githubusercontent.com/ziglang/zig/0fb2015fd3422fc1df364995f9782dfe7255eccd/doc/langref.html.in" }, "original": { "type": "file", "url": "https://raw.githubusercontent.com/ziglang/zig/0fb2015fd3422fc1df364995f9782dfe7255eccd/doc/langref.html.in" } }, "nixpkgs": { "locked": { "lastModified": 1718606988, "narHash": "sha256-pmjP5ePc1jz+Okona3HxD7AYT0wbrCwm9bXAlj08nDM=", "owner": "NixOS", "repo": "nixpkgs", "rev": "38d3352a65ac9d621b0cd3074d3bef27199ff78f", "type": "github" }, "original": { "owner": "NixOS", "ref": "nixpkgs-unstable", "repo": "nixpkgs", "type": "github" } }, "root": { "inputs": { "flake-compat": "flake-compat", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "zig": "zig", "zls": "zls" } }, "systems": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "owner": "nix-systems", "repo": "default", "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "type": "github" }, "original": { "owner": "nix-systems", "repo": "default", "type": "github" } }, "systems_2": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "owner": "nix-systems", "repo": "default", "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "type": "github" }, "original": { "owner": "nix-systems", "repo": "default", "type": "github" } }, "systems_3": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "owner": "nix-systems", "repo": "default", "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "type": "github" }, "original": { "owner": "nix-systems", "repo": "default", "type": "github" } }, "systems_4": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "owner": "nix-systems", "repo": "default", "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "type": "github" }, "original": { "owner": "nix-systems", "repo": "default", "type": "github" } }, "zig": { "inputs": { "flake-compat": "flake-compat_2", "flake-utils": "flake-utils_2", "nixpkgs": [ "nixpkgs" ] }, "locked": { "lastModified": 1718798994, "narHash": "sha256-aEZaj+rFFuLXIE9ohjYFo65t7xKbQ0asrhETNtDGF/0=", "owner": "mitchellh", "repo": "zig-overlay", "rev": "e1a9faa2f863ff1134685b4c0cfdf792ee24d762", "type": "github" }, "original": { "owner": "mitchellh", "repo": "zig-overlay", "type": "github" } }, "zig-overlay": { "inputs": { "flake-compat": "flake-compat_3", "flake-utils": "flake-utils_4", "nixpkgs": [ "zls", "nixpkgs" ] }, "locked": { "lastModified": 1718539737, "narHash": "sha256-hvQ900gSqzGnJWMRQwv65TixciIbC44iX0Nh5ENRwCU=", "owner": "mitchellh", "repo": "zig-overlay", "rev": "6eb42ce6f85d247b1aecf854c45d80902821d0ad", "type": "github" }, "original": { "owner": "mitchellh", "repo": "zig-overlay", "type": "github" } }, "zls": { "inputs": { "flake-utils": "flake-utils_3", "gitignore": "gitignore", "langref": "langref", "nixpkgs": [ "nixpkgs" ], "zig-overlay": "zig-overlay" }, "locked": { "lastModified": 1718724404, "narHash": "sha256-BnXu0u0H74cyVOEneESQoCqMCf9zgSrkSx2QVuYztVY=", "owner": "zigtools", "repo": "zls", "rev": "cfea2d55798418cccdf27b1b1bde0f70bff1b8f2", "type": "github" }, "original": { "owner": "zigtools", "repo": "zls", "type": "github" } } }, "root": "root", "version": 7 }
0
repos
repos/tomlz/zigmod.yml
id: qbh3mfo5ck9p835t13fbskhxo2izt9saho18u4lx0dczafln name: tomlz main: src/main.zig license: MIT description: A toml parser for Zig dependencies:
0
repos
repos/tomlz/toml.dict
key_value="a.b=\"c\"" unicode="\\u1234" unicode_long="\\u12345678" true="true" false="false" multiline_literal="'''" multiline="\"\"\"" integer="+1_2_3_4" negative_integer="-1" hex="0xde_ad" oct="0o6" bin="0b1" float="-6_3.6e-05" nan="nan" inf="inf" time="1979-05-27T07:32:00Z" array="[1,2]" table="[a]" inline_table="a={1=2,3=4}" array_table="[[a]]"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/duplicate-key-table.toml
[fruit] type = "apple" [fruit.type] apple = "yes"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/array-missing-bracket.toml
[[albums] name = "Born to Run"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/append-with-dotted-keys-2.toml
# This is the same issue as in injection-1.toml, except that nests one level # deeper. See that file for a more complete description. [a.b.c.d] z = 9 [a] b.c.d.k.t = "Using dotted keys to add to [a.b.c.d] after explicitly defining it above is not allowed"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/duplicate.toml
[a] b = 1 [a] c = 2
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/nested-brackets-close.toml
[a]b] zyx = 42
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/rrbrace.toml
[[table] ]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/whitespace.toml
[invalid key]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/array-implicit.toml
# This test is a bit tricky. It should fail because the first use of # `[[albums.songs]]` without first declaring `albums` implies that `albums` # must be a table. The alternative would be quite weird. Namely, it wouldn't # comply with the TOML spec: "Each double-bracketed sub-table will belong to # the most *recently* defined table element *above* it." # # This is in contrast to the *valid* test, table-array-implicit where # `[[albums.songs]]` works by itself, so long as `[[albums]]` isn't declared # later. (Although, `[albums]` could be.) [[albums.songs]] name = "Glory Days" [[albums]] name = "Born in the USA"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/nested-brackets-open.toml
[a[b] zyx = 42
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/append-with-dotted-keys-1.toml
# First a.b.c defines a table: a.b.c = {z=9} # # Then we define a.b.c.t = "str" to add a str to the above table, making it: # # a.b.c = {z=9, t="..."} # # While this makes sense, logically, it was decided this is not valid TOML as # it's too confusing/convoluted. # # See: https://github.com/toml-lang/toml/issues/846 # https://github.com/toml-lang/toml/pull/859 [a.b.c] z = 9 [a] b.c.t = "Using dotted keys to add to [a.b.c] after explicitly defining it above is not allowed"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/quoted-no-close.toml
["where will it end] name = value
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/empty.toml
[]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/text-after-table.toml
[error] this shouldn't be here
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/duplicate-table-array2.toml
[[tbl]] [tbl]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/array-empty.toml
[[]] name = "Born to Run"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/equals-sign.toml
[name=bad]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/duplicate-table-array.toml
[tbl] [[tbl]]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/duplicate-key-dotted-table2.toml
[fruit] apple.taste.sweet = true [fruit.apple.taste] # INVALID
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/llbrace.toml
[ [table]]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/empty-implicit-table.toml
[naughty..naughty]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/duplicate-key-dotted-table.toml
[fruit] apple.color = "red" [fruit.apple] # INVALID
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/redefine.toml
# Define b as int, and try to use it as a table: error [a] b = 1 [a.b] c = 2
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/table/with-pound.toml
[key#group] answer = 42
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/encoding/utf16.toml
null
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/quoted-unclosed-1.toml
"key = x
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/dotted-redefine-table.toml
# Defined a.b as int a.b = 1 # Tries to access it as table: error a.b.c = 2
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/duplicate.toml
# DO NOT DO THIS name = "Tom" name = "Pradyun"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/after-table.toml
[error] this = "should not be here"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/multiline.toml
"""long key""" = 1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/hash.toml
a# = 1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/without-value-2.toml
key =
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/start-dot.toml
.key = 1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/no-eol.toml
a = 1 b = 2
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/after-value.toml
first = "Tom" last = "Preston-Werner" # INVALID
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/space.toml
a b = 1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/open-bracket.toml
[abc = 1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/start-bracket.toml
[a] [xyz = 5 [b]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/two-equals3.toml
a=b=1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/partial-quoted.toml
partial"quoted" = 5
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/duplicate-keys.toml
dupe = false dupe = true
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/without-value-3.toml
"key"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/special-character.toml
μ = "greek small letter mu"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/bare-invalid-character.toml
bare!key = 123
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/empty.toml
= 1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/newline.toml
barekey = 123
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/single-open-bracket.toml
[
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/without-value-4.toml
"key" =
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/after-array.toml
[[agencies]] owner = "S Cjelli"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/quoted-unclosed-2.toml
"key
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/two-equals2.toml
a==1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/escape.toml
\u00c0 = "latin capital letter A with grave"
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/without-value-1.toml
key
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/key/two-equals.toml
key= = 1
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/array/text-in-array.toml
array = [ "Entry 1", I don't belong, "Entry 2", ]
0
repos/tomlz/tests/invalid
repos/tomlz/tests/invalid/array/no-close.toml
long_array = [ 1, 2, 3