2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSUNIT_MAP_TEST_MAP_NOGC_H
32 #define CDSUNIT_MAP_TEST_MAP_NOGC_H
34 #include "test_map_data.h"
36 // forward declaration
37 namespace cds { namespace container {} }
41 class container_map_nogc: public map_fixture
44 static size_t const kSize = 1000;
50 // Precondition: map is empty
51 // Postcondition: map is empty
53 ASSERT_TRUE( m.empty());
54 ASSERT_CONTAINER_SIZE( m, 0 );
56 typedef typename Map::value_type map_pair;
57 typedef typename Map::iterator iterator;
58 size_t const kkSize = kSize;
60 std::vector<key_type> arrKeys;
61 for ( int i = 0; i < static_cast<int>(kkSize); ++i )
62 arrKeys.push_back( key_type( i ));
63 shuffle( arrKeys.begin(), arrKeys.end());
65 std::vector< value_type > arrVals;
66 for ( size_t i = 0; i < kkSize; ++i ) {
68 val.nVal = static_cast<int>( i );
69 val.strVal = std::to_string( i );
70 arrVals.push_back( val );
74 for ( auto const& i : arrKeys ) {
75 value_type const& val( arrVals.at( i.nKey ));
77 ASSERT_FALSE( m.contains( i.nKey ) != m.end());
78 ASSERT_FALSE( m.contains( i ) != m.end());
79 ASSERT_FALSE( m.contains( other_item( i.nKey ), other_less()) != m.end());
81 std::pair< iterator, bool > updResult;
83 switch ( i.nKey % 16 ) {
86 ASSERT_FALSE( it == m.end());
87 ASSERT_TRUE( m.insert( i ) == m.end());
88 it->second.nVal = it->first.nKey;
89 it->second.strVal = std::to_string( it->first.nKey );
92 it = m.insert( i.nKey );
93 ASSERT_FALSE( it == m.end());
94 ASSERT_TRUE( m.insert( i.nKey ) == m.end());
95 it->second.nVal = it->first.nKey;
96 it->second.strVal = std::to_string( it->first.nKey );
99 it = m.insert( std::to_string( i.nKey ));
100 ASSERT_FALSE( it == m.end());
101 ASSERT_TRUE( m.insert( std::to_string( i.nKey )) == m.end());
102 it->second.nVal = it->first.nKey;
103 it->second.strVal = std::to_string( it->first.nKey );
106 it = m.insert( i, val );
107 ASSERT_FALSE( it == m.end());
108 ASSERT_TRUE( m.insert( i, val ) == m.end());
111 it = m.insert( i.nKey, val.strVal );
112 ASSERT_FALSE( it == m.end());
113 ASSERT_TRUE( m.insert( i.nKey, val.strVal ) == m.end());
116 it = m.insert( val.strVal, i.nKey );
117 ASSERT_FALSE( it == m.end());
118 ASSERT_TRUE( m.insert( val.strVal, i.nKey ) == m.end());
121 it = m.insert_with( i, []( map_pair& v ) {
122 v.second.nVal = v.first.nKey;
123 v.second.strVal = std::to_string( v.first.nKey );
125 ASSERT_FALSE( it == m.end());
126 ASSERT_TRUE( m.insert_with( i, []( map_pair& ) {
127 EXPECT_TRUE( false );
131 it = m.insert_with( i.nKey, []( map_pair& v ) {
132 v.second.nVal = v.first.nKey;
133 v.second.strVal = std::to_string( v.first.nKey );
135 ASSERT_FALSE( it == m.end());
136 ASSERT_TRUE( m.insert_with( i.nKey, []( map_pair& ) {
137 EXPECT_TRUE( false );
141 it = m.insert_with( val.strVal, []( map_pair& v ) {
142 v.second.nVal = v.first.nKey;
143 v.second.strVal = std::to_string( v.first.nKey );
145 ASSERT_FALSE( it == m.end());
146 ASSERT_TRUE( m.insert_with( val.strVal, []( map_pair& ) {
147 EXPECT_TRUE( false );
151 updResult = m.update( i.nKey, false );
152 ASSERT_TRUE( updResult.first == m.end());
153 ASSERT_FALSE( updResult.second );
155 updResult = m.update( i.nKey );
156 ASSERT_TRUE( updResult.first != m.end());
157 ASSERT_TRUE( updResult.second );
158 updResult.first->second.nVal = updResult.first->first.nKey;
160 updResult = m.update( i.nKey, false );
161 ASSERT_TRUE( updResult.first != m.end());
162 ASSERT_FALSE( updResult.second );
163 EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
164 updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
167 updResult = m.update( i, false );
168 ASSERT_TRUE( updResult.first == m.end());
169 ASSERT_FALSE( updResult.second );
171 updResult = m.update( i );
172 ASSERT_TRUE( updResult.first != m.end());
173 ASSERT_TRUE( updResult.second );
174 updResult.first->second.nVal = updResult.first->first.nKey;
176 updResult = m.update( i );
177 ASSERT_TRUE( updResult.first != m.end());
178 ASSERT_FALSE( updResult.second );
179 EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
180 updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
183 updResult = m.update( val.strVal, false );
184 ASSERT_TRUE( updResult.first == m.end());
185 ASSERT_FALSE( updResult.second );
187 updResult = m.update( val.strVal );
188 ASSERT_TRUE( updResult.first != m.end());
189 ASSERT_TRUE( updResult.second );
190 updResult.first->second.nVal = updResult.first->first.nKey;
192 updResult = m.update( val.strVal, false );
193 ASSERT_TRUE( updResult.first != m.end());
194 ASSERT_FALSE( updResult.second );
195 EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
196 updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
199 it = m.emplace( i.nKey );
200 ASSERT_TRUE( it != m.end());
201 ASSERT_FALSE( m.emplace( i.nKey ) != m.end());
202 it->second.nVal = it->first.nKey;
203 it->second.strVal = std::to_string( it->first.nKey );
206 it = m.emplace( i, i.nKey );
207 ASSERT_TRUE( it != m.end());
208 ASSERT_FALSE( m.emplace( i, i.nKey ) != m.end());
212 std::string str = val.strVal;
213 it = m.emplace( i, std::move( str ));
214 ASSERT_TRUE( it != m.end());
215 ASSERT_TRUE( str.empty());
217 ASSERT_FALSE( m.emplace( i, std::move( str )) != m.end());
218 ASSERT_TRUE( str.empty());
223 std::string str = val.strVal;
224 it = m.emplace( i, i.nKey, std::move( str ));
225 ASSERT_TRUE( it != m.end());
226 ASSERT_TRUE( str.empty());
228 ASSERT_FALSE( m.emplace( i, i.nKey, std::move( str )) != m.end());
229 ASSERT_TRUE( str.empty());
234 it = m.contains( i.nKey );
235 ASSERT_TRUE( it != m.end());
236 ASSERT_TRUE( m.contains( i ) == it );
237 ASSERT_TRUE( m.contains( other_item( i.nKey ), other_less()) == it );
238 EXPECT_EQ( it->first.nKey, it->second.nVal );
239 EXPECT_EQ( std::to_string( it->first.nKey ), it->second.strVal );
241 ASSERT_FALSE( m.empty());
242 ASSERT_CONTAINER_SIZE( m, kkSize );
243 ASSERT_FALSE( m.begin() == m.end());
244 ASSERT_FALSE( m.cbegin() == m.cend());
250 ASSERT_TRUE( m.empty());
251 ASSERT_CONTAINER_SIZE( m, 0 );
252 ASSERT_TRUE( m.begin() == m.end());
253 ASSERT_TRUE( m.cbegin() == m.cend());
257 } // namespace cds_test
259 #endif // #ifndef CDSUNIT_MAP_TEST_MAP_NOGC_H