/* Copyright (c) 2005-2021 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #if _MSC_VER #if __INTEL_COMPILER #pragma warning(disable : 2586) // decorated name length exceeded, name was truncated #else // Workaround for vs2015 and warning name was longer than the compiler limit (4096). #pragma warning (disable: 4503) #endif #endif #define TBB_DEFINE_STD_HASH_SPECIALIZATIONS 1 #define TBB_PREVIEW_CONCURRENT_HASH_MAP_EXTENSIONS 1 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //! \file test_concurrent_hash_map.cpp //! \brief Test for [containers.concurrent_hash_map containers.tbb_hash_compare] specification void TestRangeBasedFor(){ using namespace range_based_for_support_tests; INFO("testing range based for loop compatibility \n"); using ch_map = tbb::concurrent_hash_map; ch_map a_ch_map; const int sequence_length = 100; for (int i = 1; i <= sequence_length; ++i){ a_ch_map.insert(ch_map::value_type(i,i)); } REQUIRE_MESSAGE((range_based_for_accumulate(a_ch_map, pair_second_summer(), 0) == gauss_summ_of_int_sequence(sequence_length)), "incorrect accumulated value generated via range based for ?"); } // The helper to run a test only when a default construction is present. template struct do_default_construction_test { template void operator() ( FuncType func ) const { func(); } }; template <> struct do_default_construction_test { template void operator()( FuncType ) const {} }; template class test_insert_by_key { using value_type = typename Table::value_type; Table &my_c; const value_type &my_value; public: test_insert_by_key( Table &c, const value_type &value ) : my_c(c), my_value(value) {} void operator()() const { { typename Table::accessor a; CHECK(my_c.insert( a, my_value.first )); CHECK(utils::IsEqual()(a->first, my_value.first)); a->second = my_value.second; } { typename Table::const_accessor ca; CHECK(!my_c.insert( ca, my_value.first )); CHECK(utils::IsEqual()(ca->first, my_value.first)); CHECK(utils::IsEqual()(ca->second, my_value.second)); } } }; template class test_range { using value_type = typename Table::value_type; Table &my_c; const std::list &my_lst; std::vector>& my_marks; public: test_range( Table &c, const std::list &lst, std::vector> &marks ) : my_c(c), my_lst(lst), my_marks(marks) { for (std::size_t i = 0; i < my_marks.size(); ++i) { my_marks[i].store(false, std::memory_order_relaxed); } } void operator()( const Range &r ) const { do_test_range( r.begin(), r.end() ); } void do_test_range( Iterator i, Iterator j ) const { for ( Iterator it = i; it != j; ) { Iterator it_prev = it++; typename std::list::const_iterator it2 = std::search( my_lst.begin(), my_lst.end(), it_prev, it, utils::IsEqual() ); CHECK(it2 != my_lst.end()); typename std::list::difference_type dist = std::distance( my_lst.begin(), it2 ); CHECK(!my_marks[dist]); my_marks[dist].store(true); } } }; template class check_value { using const_iterator = typename Table::const_iterator; using iterator = typename Table::iterator; using size_type = typename Table::size_type; Table &my_c; public: check_value( Table &c ) : my_c(c) {} void operator()(const typename Table::value_type &value ) { const Table &const_c = my_c; CHECK(my_c.count( value.first ) == 1); { // tests with a const accessor. typename Table::const_accessor ca; // find CHECK(my_c.find( ca, value.first )); CHECK(!ca.empty() ); CHECK(utils::IsEqual()(ca->first, value.first)); CHECK(utils::IsEqual()(ca->second, value.second)); // erase CHECK(my_c.erase( ca )); CHECK(my_c.count( value.first ) == 0); // insert (pair) CHECK(my_c.insert( ca, value )); CHECK(utils::IsEqual()(ca->first, value.first)); CHECK(utils::IsEqual()(ca->second, value.second)); } { // tests with a non-const accessor. typename Table::accessor a; // find CHECK(my_c.find( a, value.first )); CHECK(!a.empty() ); CHECK(utils::IsEqual()(a->first, value.first)); CHECK(utils::IsEqual()(a->second, value.second)); // erase CHECK(my_c.erase( a )); CHECK(my_c.count( value.first ) == 0); // insert CHECK(my_c.insert( a, value )); CHECK(utils::IsEqual()(a->first, value.first)); CHECK(utils::IsEqual()(a->second, value.second)); } // erase by key CHECK(my_c.erase( value.first )); CHECK(my_c.count( value.first ) == 0); do_default_construction_test()(test_insert_by_key( my_c, value )); // insert by value CHECK(my_c.insert( value ) != default_construction_present); // equal_range std::pair r1 = my_c.equal_range( value.first ); iterator r1_first_prev = r1.first++; CHECK((utils::IsEqual()( *r1_first_prev, value ) && utils::IsEqual()( r1.first, r1.second ))); std::pair r2 = const_c.equal_range( value.first ); const_iterator r2_first_prev = r2.first++; CHECK((utils::IsEqual()( *r2_first_prev, value ) && utils::IsEqual()( r2.first, r2.second ))); } }; template struct CompareTables { template static bool IsEqual( const T& t1, const T& t2 ) { return (t1 == t2) && !(t1 != t2); } }; template struct CompareTables< std::pair, std::weak_ptr > > { template static bool IsEqual( const T&, const T& ) { /* do nothing for std::weak_ptr */ return true; } }; template void Examine( Table c, const std::list &lst) { using const_table = const Table; using const_iterator = typename Table::const_iterator; using iterator = typename Table::iterator; using value_type = typename Table::value_type; using size_type = typename Table::size_type; CHECK(!c.empty()); CHECK(c.size() == lst.size()); CHECK(c.max_size() >= c.size()); const check_value cv(c); std::for_each( lst.begin(), lst.end(), cv ); std::vector> marks( lst.size() ); test_range( c, lst, marks ).do_test_range( c.begin(), c.end() ); CHECK(std::find( marks.begin(), marks.end(), false ) == marks.end()); test_range( c, lst, marks ).do_test_range( c.begin(), c.end() ); CHECK(std::find( marks.begin(), marks.end(), false ) == marks.end()); using range_type = typename Table::range_type; tbb::parallel_for( c.range(), test_range( c, lst, marks ) ); CHECK(std::find( marks.begin(), marks.end(), false ) == marks.end()); const_table const_c = c; CHECK(CompareTables::IsEqual( c, const_c )); const size_type new_bucket_count = 2*c.bucket_count(); c.rehash( new_bucket_count ); CHECK(c.bucket_count() >= new_bucket_count); Table c2; typename std::list::const_iterator begin5 = lst.begin(); std::advance( begin5, 5 ); c2.insert( lst.begin(), begin5 ); std::for_each( lst.begin(), begin5, check_value( c2 ) ); c2.swap( c ); CHECK(CompareTables::IsEqual( c2, const_c )); CHECK(c.size() == 5); std::for_each( lst.begin(), lst.end(), check_value(c2) ); swap( c, c2 ); CHECK(CompareTables::IsEqual( c, const_c )); CHECK(c2.size() == 5); c2.clear(); CHECK(CompareTables::IsEqual( c2, Table() )); typename Table::allocator_type a = c.get_allocator(); value_type *ptr = a.allocate(1); CHECK(ptr); a.deallocate( ptr, 1 ); } template struct debug_hash_compare : public tbb::detail::d1::tbb_hash_compare {}; template void TypeTester( const std::list &lst ) { using first_type = typename Value::first_type; using key_type = typename std::remove_const::type; using second_type = typename Value::second_type; using ch_map = tbb::concurrent_hash_map; debug_hash_compare compare{}; // Construct an empty hash map. ch_map c1; c1.insert( lst.begin(), lst.end() ); Examine( c1, lst ); // Constructor from initializer_list. typename std::list::const_iterator it = lst.begin(); std::initializer_list il = { *it++, *it++, *it++ }; ch_map c2( il ); c2.insert( it, lst.end() ); Examine( c2, lst ); // Constructor from initializer_list and compare object ch_map c3( il, compare); c3.insert( it, lst.end() ); Examine( c3, lst ); // Constructor from initializer_list, compare object and allocator ch_map c4( il, compare, typename ch_map::allocator_type()); c4.insert( it, lst.end()); Examine( c4, lst ); // Copying constructor. ch_map c5(c1); Examine( c5, lst ); // Construct with non-default allocator using ch_map_debug_alloc = tbb::concurrent_hash_map, LocalCountingAllocator>>; ch_map_debug_alloc c6; c6.insert( lst.begin(), lst.end() ); Examine( c6, lst ); // Copying constructor ch_map_debug_alloc c7(c6); Examine( c7, lst ); // Construction empty table with n preallocated buckets. ch_map c8( lst.size() ); c8.insert( lst.begin(), lst.end() ); Examine( c8, lst ); ch_map_debug_alloc c9( lst.size() ); c9.insert( lst.begin(), lst.end() ); Examine( c9, lst ); // Construction with copying iteration range. ch_map c10_1( c1.begin(), c1.end() ), c10_2(c1.cbegin(), c1.cend()); Examine( c10_1, lst ); Examine( c10_2, lst ); // Construction with copying iteration range and given allocator instance. LocalCountingAllocator> allocator; ch_map_debug_alloc c11( lst.begin(), lst.end(), allocator ); Examine( c11, lst ); using ch_map_debug_hash = tbb::concurrent_hash_map, typename ch_map::allocator_type>; // Constructor with two iterators and hash_compare ch_map_debug_hash c12(c1.begin(), c1.end(), compare); Examine( c12, lst ); ch_map_debug_hash c13(c1.begin(), c1.end(), compare, typename ch_map::allocator_type()); Examine( c13, lst ); } void TestSpecificTypes() { const int NUMBER = 10; using int_int_t = std::pair; std::list arrIntInt; for ( int i=0; i( arrIntInt ); using ref_int_t = std::pair, int>; std::list arrRefInt; for ( std::list::iterator it = arrIntInt.begin(); it != arrIntInt.end(); ++it ) arrRefInt.push_back( ref_int_t( it->first, it->second ) ); TypeTester( arrRefInt ); using int_ref_t = std::pair< const int, std::reference_wrapper >; std::list arrIntRef; for ( std::list::iterator it = arrIntInt.begin(); it != arrIntInt.end(); ++it ) arrIntRef.push_back( int_ref_t( it->first, it->second ) ); TypeTester( arrIntRef ); using shr_shr_t = std::pair< const std::shared_ptr, std::shared_ptr >; std::list arrShrShr; for ( int i=0; i(i), std::make_shared(NUMBER_minus_i) ) ); } TypeTester< /*default_construction_present = */true>( arrShrShr ); using wk_wk_t = std::pair< const std::weak_ptr, std::weak_ptr >; std::list< wk_wk_t > arrWkWk; std::copy( arrShrShr.begin(), arrShrShr.end(), std::back_inserter(arrWkWk) ); TypeTester< /*default_construction_present = */true>( arrWkWk ); // Check working with deprecated hashers using pair_key_type = std::pair; using pair_int_t = std::pair; std::list arr_pair_int; for (int i = 0; i < NUMBER; ++i) { arr_pair_int.push_back(pair_int_t(pair_key_type{i, i}, i)); } TypeTester(arr_pair_int); using tbb_string_key_type = std::basic_string, tbb::tbb_allocator>; using pair_tbb_string_int_t = std::pair; std::list arr_pair_string_int; for (int i = 0; i < NUMBER; ++i) { tbb_string_key_type key(i, char(i)); arr_pair_string_int.push_back(pair_tbb_string_int_t(key, i)); } TypeTester(arr_pair_string_int); } struct custom_hash_compare { template size_t hash(const AllocatorAwareData& key) const { return my_hash_compare.hash(key.value()); } template bool equal(const AllocatorAwareData& key1, const AllocatorAwareData& key2) const { return my_hash_compare.equal(key1.value(), key2.value()); } private: tbb::tbb_hash_compare my_hash_compare; }; void TestScopedAllocator() { using allocator_data_type = AllocatorAwareData>>; using allocator_type = std::scoped_allocator_adaptor>>; using hash_map_type = tbb::concurrent_hash_map; allocator_type allocator; allocator_data_type key1(1, allocator), key2(2, allocator); allocator_data_type data1(1, allocator), data2(data1, allocator); hash_map_type map1(allocator), map2(allocator); hash_map_type::value_type v1(key1, data1), v2(key2, data2); auto init_list = { v1, v2 }; allocator_data_type::assert_on_constructions = true; map1.emplace(key1, data1); map2.emplace(key2, std::move(data2)); map1.clear(); map2.clear(); map1.insert(v1); map2.insert(std::move(v2)); map1.clear(); map2.clear(); map1.insert(init_list); map1.clear(); map2.clear(); hash_map_type::accessor a; map2.insert(a, allocator_data_type(3)); a.release(); map1 = map2; map2 = std::move(map1); hash_map_type map3(allocator); map3.rehash(1000); map3 = map2; } // A test for undocumented member function internal_fast_find // which is declared protected in concurrent_hash_map for internal TBB use void TestInternalFastFind() { typedef tbb::concurrent_hash_map basic_chmap_type; typedef basic_chmap_type::const_pointer const_pointer; class chmap : public basic_chmap_type { public: chmap() : basic_chmap_type() {} using basic_chmap_type::internal_fast_find; }; chmap m; int sz = 100; for (int i = 0; i != sz; ++i) { m.insert(std::make_pair(i, i * i)); } REQUIRE_MESSAGE(m.size() == 100, "Incorrect concurrent_hash_map size"); for (int i = 0; i != sz; ++i) { const_pointer res = m.internal_fast_find(i); REQUIRE_MESSAGE(res != nullptr, "Incorrect internal_fast_find return value for existing key"); basic_chmap_type::value_type val = *res; REQUIRE_MESSAGE(val.first == i, "Incorrect key in internal_fast_find return value"); REQUIRE_MESSAGE(val.second == i * i, "Incorrect mapped in internal_fast_find return value"); } for (int i = sz; i != 2 * sz; ++i) { const_pointer res = m.internal_fast_find(i); REQUIRE_MESSAGE(res == nullptr, "Incorrect internal_fast_find return value for not existing key"); } } struct default_container_traits { template static container_type& construct_container(typename std::aligned_storage::type& storage, iterator_type begin, iterator_type end){ container_type* ptr = reinterpret_cast(&storage); new (ptr) container_type(begin, end); return *ptr; } template static container_type& construct_container(typename std::aligned_storage::type& storage, iterator_type begin, iterator_type end, allocator_type const& a){ container_type* ptr = reinterpret_cast(&storage); new (ptr) container_type(begin, end, a); return *ptr; } }; struct hash_map_traits : default_container_traits { enum{ expected_number_of_items_to_allocate_for_steal_move = 0 }; template struct hash_compare { bool equal( const T& lhs, const T& rhs ) const { return lhs==rhs; } size_t hash( const T& k ) const { return my_hash_func(k); } std::hash my_hash_func; }; template using container_type = tbb::concurrent_hash_map, Allocator>; template using container_value_type = std::pair; template struct apply { using type = tbb::concurrent_hash_map, allocator_type>; }; using init_iterator_type = move_support_tests::FooPairIterator; template static bool equal(hash_map_type const& c, iterator begin, iterator end){ bool equal_sizes = ( static_cast(std::distance(begin, end)) == c.size() ); if (!equal_sizes) return false; for (iterator it = begin; it != end; ++it ){ if (c.count( (*it).first) == 0){ return false; } } return true; } }; template class HeterogeneousKey { public: static std::size_t heterogeneous_keys_count; int integer_key() const { return my_key; } template ::type> HeterogeneousKey(int key) : my_key(key) { ++heterogeneous_keys_count; } HeterogeneousKey(const HeterogeneousKey&) = delete; HeterogeneousKey& operator=(const HeterogeneousKey&) = delete; static void reset() { heterogeneous_keys_count = 0; } struct construct_flag {}; HeterogeneousKey( construct_flag, int key ) : my_key(key) {} private: int my_key; }; // class HeterogeneousKey template std::size_t HeterogeneousKey::heterogeneous_keys_count = 0; struct HeterogeneousHashCompare { using is_transparent = void; template std::size_t hash( const HeterogeneousKey& key ) const { return my_hash_object(key.integer_key()); } std::size_t hash( const int& key ) const { return my_hash_object(key); } bool equal( const int& key1, const int& key2 ) const { return key1 == key2; } template bool equal( const int& key1, const HeterogeneousKey& key2 ) const { return key1 == key2.integer_key(); } template bool equal( const HeterogeneousKey& key1, const int& key2 ) const { return key1.integer_key() == key2; } template bool equal( const HeterogeneousKey& key1, const HeterogeneousKey& key2 ) const { return key1.integer_key() == key2.integer_key(); } std::hash my_hash_object; }; // struct HeterogeneousHashCompare class DefaultConstructibleValue { public: DefaultConstructibleValue() : my_i(default_value) {}; int value() const { return my_i; } static constexpr int default_value = -4242; private: int my_i; }; // class DefaultConstructibleValue constexpr int DefaultConstructibleValue::default_value; void test_heterogeneous_find() { using key_type = HeterogeneousKey; using chmap_type = tbb::concurrent_hash_map; chmap_type chmap; using const_accessor = typename chmap_type::const_accessor; using accessor = typename chmap_type::accessor; const_accessor cacc; accessor acc; REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup"); key_type key(key_type::construct_flag{}, 1); bool regular_result = chmap.find(cacc, key); bool heterogeneous_result = chmap.find(cacc, int(1)); REQUIRE(!regular_result); REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous find result with const_accessor (no element)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with const_accessor (no element)"); regular_result = chmap.find(acc, key); heterogeneous_result = chmap.find(acc, int(1)); REQUIRE(!regular_result); REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous find result with accessor (no element)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with accessor (no element)"); bool tmp_result = chmap.emplace(cacc, std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100)); REQUIRE(tmp_result); regular_result = chmap.find(cacc, key); heterogeneous_result = chmap.find(cacc, int(1)); REQUIRE(regular_result); REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous find result with const_accessor (element exists)"); REQUIRE_MESSAGE(cacc->first.integer_key() == 1, "Incorrect accessor returned"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with const_accessor (element exists)"); cacc.release(); regular_result = chmap.find(acc, key); heterogeneous_result = chmap.find(acc, int(1)); REQUIRE(regular_result); REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous find result with accessor (element exists)"); REQUIRE_MESSAGE(acc->first.integer_key() == 1, "Incorrect accessor returned"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with accessor (element exists)"); key_type::reset(); } void test_heterogeneous_count() { using key_type = HeterogeneousKey; using chmap_type = tbb::concurrent_hash_map; chmap_type chmap; REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup"); key_type key(key_type::construct_flag{}, 1); typename chmap_type::size_type regular_count = chmap.count(key); typename chmap_type::size_type heterogeneous_count = chmap.count(int(1)); REQUIRE(regular_count == 0); REQUIRE_MESSAGE(regular_count == heterogeneous_count, "Incorrect heterogeneous count result (no element)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during count call (no element)"); chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100)); regular_count = chmap.count(key); heterogeneous_count = chmap.count(int(1)); REQUIRE(regular_count == 1); REQUIRE_MESSAGE(regular_count == heterogeneous_count, "Incorrect heterogeneous count result (element exists)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during count call (element exists)"); key_type::reset(); } void test_heterogeneous_equal_range() { using key_type = HeterogeneousKey; using chmap_type = tbb::concurrent_hash_map; chmap_type chmap; REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup"); using iterator = typename chmap_type::iterator; using const_iterator = typename chmap_type::const_iterator; using result = std::pair; using const_result = std::pair; key_type key(key_type::construct_flag{}, 1); result regular_result = chmap.equal_range(key); result heterogeneous_result = chmap.equal_range(int(1)); REQUIRE(regular_result.first == chmap.end()); REQUIRE(regular_result.second == chmap.end()); REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous equal_range result (non const, no element)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (non const, no element)"); const chmap_type& cchmap = chmap; const_result regular_const_result = cchmap.equal_range(key); const_result heterogeneous_const_result = cchmap.equal_range(int(1)); REQUIRE(regular_const_result.first == cchmap.end()); REQUIRE(regular_const_result.second == cchmap.end()); REQUIRE_MESSAGE(regular_const_result == heterogeneous_const_result, "Incorrect heterogeneous equal_range result (const, no element)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (const, no element)"); chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100)); regular_result = chmap.equal_range(key); heterogeneous_result = chmap.equal_range(int(1)); REQUIRE(regular_result.first != chmap.end()); REQUIRE(regular_result.first->first.integer_key() == 1); REQUIRE(regular_result.second == chmap.end()); REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous equal_range result (non const, element exists)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (non const, element exists)"); regular_const_result = cchmap.equal_range(key); heterogeneous_const_result = cchmap.equal_range(int(1)); REQUIRE_MESSAGE(regular_const_result == heterogeneous_const_result, "Incorrect heterogeneous equal_range result (const, element exists)"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (const, element exists)"); key_type::reset(); } void test_heterogeneous_insert() { using key_type = HeterogeneousKey; using chmap_type = tbb::concurrent_hash_map; chmap_type chmap; using const_accessor = typename chmap_type::const_accessor; using accessor = typename chmap_type::accessor; const_accessor cacc; accessor acc; REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup"); bool result = chmap.insert(cacc, int(1)); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 1, "Only one heterogeneous key should be created"); REQUIRE_MESSAGE(result, "Incorrect heterogeneous insert result (const_accessor)"); REQUIRE_MESSAGE(cacc->first.integer_key() == 1, "Incorrect accessor"); REQUIRE_MESSAGE(cacc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed"); result = chmap.insert(cacc, int(1)); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 1, "No extra keys should be created"); REQUIRE_MESSAGE(!result, "Incorrect heterogeneous insert result (const_accessor)"); REQUIRE_MESSAGE(cacc->first.integer_key() == 1, "Incorrect accessor"); REQUIRE_MESSAGE(cacc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed"); result = chmap.insert(acc, int(2)); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 2, "Only one extra heterogeneous key should be created"); REQUIRE_MESSAGE(result, "Incorrect heterogeneous insert result (accessor)"); REQUIRE_MESSAGE(acc->first.integer_key() == 2, "Incorrect accessor"); REQUIRE_MESSAGE(acc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed"); result = chmap.insert(acc, int(2)); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 2, "No extra keys should be created"); REQUIRE_MESSAGE(!result, "Incorrect heterogeneous insert result (accessor)"); REQUIRE_MESSAGE(acc->first.integer_key() == 2, "Incorrect accessor"); REQUIRE_MESSAGE(acc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed"); key_type::reset(); } void test_heterogeneous_erase() { using key_type = HeterogeneousKey; using chmap_type = tbb::concurrent_hash_map; chmap_type chmap; REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup"); chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100)); chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 2), std::forward_as_tuple(200)); typename chmap_type::const_accessor cacc; REQUIRE(chmap.find(cacc, int(1))); REQUIRE(chmap.find(cacc, int(2))); cacc.release(); bool result = chmap.erase(int(1)); REQUIRE_MESSAGE(result, "Erasure should be successful"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "No extra keys should be created"); REQUIRE_MESSAGE(!chmap.find(cacc, int(1)), "Element was not erased"); result = chmap.erase(int(1)); REQUIRE_MESSAGE(!result, "Erasure should fail"); REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "No extra keys should be created"); key_type::reset(); } void test_heterogeneous_lookup() { test_heterogeneous_find(); test_heterogeneous_count(); test_heterogeneous_equal_range(); } template class MinimalisticMutex { public: static constexpr bool is_rw_mutex = true; static constexpr bool is_recursive_mutex = false; static constexpr bool is_fair_mutex = false; class scoped_lock { public: constexpr scoped_lock() noexcept : my_mutex_ptr(nullptr) {} scoped_lock( MinimalisticMutex& m, bool = true ) : my_mutex_ptr(&m) { my_mutex_ptr->my_mutex.lock(); } scoped_lock( const scoped_lock& ) = delete; scoped_lock& operator=( const scoped_lock& ) = delete; ~scoped_lock() { if (my_mutex_ptr) release(); } void acquire( MinimalisticMutex& m, bool = true ) { CHECK(my_mutex_ptr == nullptr); my_mutex_ptr = &m; my_mutex_ptr->my_mutex.lock(); } bool try_acquire( MinimalisticMutex& m, bool = true ) { if (m.my_mutex.try_lock()) { my_mutex_ptr = &m; return true; } return false; } void release() { CHECK(my_mutex_ptr != nullptr); my_mutex_ptr->my_mutex.unlock(); my_mutex_ptr = nullptr; } bool upgrade_to_writer() const { // upgrade_to_writer should return false if the mutex simulates // reaquiring the lock on upgrade operation return !SimulateReacquiring; } bool downgrade_to_reader() const { // downgrade_to_reader should return false if the mutex simulates // reaquiring the lock on upgrade operation return !SimulateReacquiring; } bool is_writer() const { CHECK(my_mutex_ptr != nullptr); return true; // Always a writer } private: MinimalisticMutex* my_mutex_ptr; }; // class scoped_lock private: std::mutex my_mutex; }; // class MinimalisticMutex template void test_with_minimalistic_mutex() { using mutex_type = MinimalisticMutex; using chmap_type = tbb::concurrent_hash_map, tbb::tbb_allocator>, mutex_type>; chmap_type chmap; // Insert pre-existing elements for (int i = 0; i < 100; ++i) { bool result = chmap.emplace(i, i); CHECK(result); } // Insert elements to erase for (int i = 10000; i < 10005; ++i) { bool result = chmap.emplace(i, i); CHECK(result); } auto thread_body = [&]( const tbb::blocked_range& range ) { for (std::size_t item = range.begin(); item != range.end(); ++item) { switch(item % 4) { case 0 : // Insert new elements for (int i = 100; i < 200; ++i) { typename chmap_type::const_accessor acc; chmap.emplace(acc, i, i); CHECK(acc->first == i); CHECK(acc->second == i); } break; case 1 : // Insert pre-existing elements for (int i = 0; i < 100; ++i) { typename chmap_type::const_accessor acc; bool result = chmap.emplace(acc, i, i * 10000); CHECK(!result); CHECK(acc->first == i); CHECK(acc->second == i); } break; case 2 : // Find pre-existing elements for (int i = 0; i < 100; ++i) { typename chmap_type::const_accessor acc; bool result = chmap.find(acc, i); CHECK(result); CHECK(acc->first == i); CHECK(acc->second == i); } break; case 3 : // Erase pre-existing elements for (int i = 10000; i < 10005; ++i) { chmap.erase(i); } break; } } }; // thread_body tbb::blocked_range br(0, 1000, 8); tbb::parallel_for(br, thread_body); // Check pre-existing and new elements for (int i = 0; i < 200; ++i) { typename chmap_type::const_accessor acc; bool result = chmap.find(acc, i); REQUIRE_MESSAGE(result, "Some element was unexpectedly removed or not inserted"); REQUIRE_MESSAGE(acc->first == i, "Incorrect key"); REQUIRE_MESSAGE(acc->second == i, "Incorrect value"); } // Check elements for erasure for (int i = 10000; i < 10005; ++i) { typename chmap_type::const_accessor acc; bool result = chmap.find(acc, i); REQUIRE_MESSAGE(!result, "Some element was not removed"); } } void test_mutex_customization() { test_with_minimalistic_mutex(); test_with_minimalistic_mutex(); } //! Test of insert operation //! \brief \ref error_guessing TEST_CASE("testing range based for support"){ TestRangeBasedFor(); } //! Test concurrent_hash_map with specific key/mapped types //! \brief \ref regression \ref error_guessing TEST_CASE("testing concurrent_hash_map with specific key/mapped types") { TestSpecificTypes(); } //! Test work with scoped allocator //! \brief \ref regression TEST_CASE("testing work with scoped allocator") { TestScopedAllocator(); } //! Test internal fast find for concurrent_hash_map //! \brief \ref regression TEST_CASE("testing internal fast find for concurrent_hash_map") { TestInternalFastFind(); } //! Test constructor with move iterators //! \brief \ref error_guessing TEST_CASE("testing constructor with move iterators"){ move_support_tests::test_constructor_with_move_iterators(); } #if TBB_USE_EXCEPTIONS //! Test exception in constructors //! \brief \ref regression \ref error_guessing TEST_CASE("Test exception in constructors") { using allocator_type = StaticSharedCountingAllocator>>; using map_type = tbb::concurrent_hash_map, allocator_type>; auto init_list = {std::pair(1, 42), std::pair(2, 42), std::pair(3, 42), std::pair(4, 42), std::pair(5, 42), std::pair(6, 42)}; map_type map(init_list); allocator_type::set_limits(1); REQUIRE_THROWS_AS( [&] { map_type map1(map); utils::suppress_unused_warning(map1); }(), const std::bad_alloc); REQUIRE_THROWS_AS( [&] { map_type map2(init_list.begin(), init_list.end()); utils::suppress_unused_warning(map2); }(), const std::bad_alloc); tbb::tbb_hash_compare test_hash; REQUIRE_THROWS_AS( [&] { map_type map3(init_list.begin(), init_list.end(), test_hash); utils::suppress_unused_warning(map3); }(), const std::bad_alloc); REQUIRE_THROWS_AS( [&] { map_type map4(init_list, test_hash); utils::suppress_unused_warning(map4); }(), const std::bad_alloc); REQUIRE_THROWS_AS( [&] { map_type map5(init_list); utils::suppress_unused_warning(map5); }(), const std::bad_alloc); allocator_type::set_limits(0); map_type big_map{}; for (int i = 0; i < 1000; ++i) { big_map.insert(std::pair(i, 42)); } allocator_type::init_counters(); allocator_type::set_limits(300); REQUIRE_THROWS_AS( [&] { map_type map6(big_map); utils::suppress_unused_warning(map6); }(), const std::bad_alloc); } #endif // TBB_USE_EXCEPTIONS //! \brief \ref error_guessing TEST_CASE("swap with NotAlwaysEqualAllocator allocators") { using allocator_type = NotAlwaysEqualAllocator>; using map_type = tbb::concurrent_hash_map, allocator_type>; map_type map1{}; map_type map2({{42, 42}, {24, 42}}); map_type map3(map2); swap(map1, map2); CHECK(map2.empty()); CHECK(map1 == map3); } //! \brief \ref error_guessing TEST_CASE("test concurrent_hash_map heterogeneous lookup") { test_heterogeneous_lookup(); } //! \brief \ref error_guessing TEST_CASE("test concurrent_hash_map heterogeneous insert") { test_heterogeneous_insert(); } //! \brief \ref error_guessing TEST_CASE("test concurrent_hash_map heterogeneous erase") { test_heterogeneous_erase(); } //! \brief \ref error_guessing TEST_CASE("test concurrent_hash_map mutex customization") { test_mutex_customization(); } #if __TBB_CPP20_CONCEPTS_PRESENT template requires (... && (utils::well_formed_instantiation == ExpectSatisfies)) void test_chmap_hash_compare_constraints() {} //! \brief \ref error_guessing TEST_CASE("tbb::concurrent_hash_map hash_compare constraints") { using key_type = int; using mapped_type = int; using namespace test_concepts::hash_compare; test_chmap_hash_compare_constraints, tbb::tbb_hash_compare>(); test_chmap_hash_compare_constraints, NonDestructible, NoHash, HashNonConst, WrongInputHash, WrongReturnHash, NoEqual, EqualNonConst, WrongFirstInputEqual, WrongSecondInputEqual, WrongReturnEqual>(); } template requires (... && (utils::well_formed_instantiation, tbb::tbb_allocator>, RWMutexes> == ExpectSatisfies)) void test_chmap_mutex_constraints() {} //! \brief \ref error_guessing TEST_CASE("tbb::concurrent_hash_map rw_mutex constraints") { using key_type = int; using mapped_type = int; using namespace test_concepts::rw_mutex; test_chmap_mutex_constraints(); test_chmap_mutex_constraints(); } //! \brief \ref error_guessing TEST_CASE("container_range concept for tbb::concurrent_hash_map ranges") { static_assert(test_concepts::container_range::range_type>); static_assert(test_concepts::container_range::const_range_type>); } #endif // __TBB_CPP20_CONCEPTS_PRESENT