2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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_LIST_TEST_KV_LIST_H
32 #define CDSUNIT_LIST_TEST_KV_LIST_H
34 #include <cds_test/check_size.h>
35 #include <cds_test/fixture.h>
39 class kv_list_common : public fixture
46 explicit key_type( int n )
50 key_type( key_type const& s )
54 key_type( key_type&& s )
73 explicit value_type( int n )
80 bool operator()( key_type const& lhs, key_type const& rhs ) const
82 return lhs.key() < rhs.key();
85 bool operator()( key_type const& lhs, int rhs ) const
87 return lhs.key() < rhs;
90 bool operator()( int lhs, key_type const& rhs ) const
92 return lhs < rhs.key();
96 bool operator ()( T const& v1, T const& v2 ) const
98 return v1.key() < v2.key();
104 int operator()( key_type const& lhs, key_type const& rhs ) const
106 return lhs.key() - rhs.key();
109 int operator()( key_type const& lhs, int rhs ) const
111 return lhs.key() - rhs;
114 int operator()( int lhs, key_type const& rhs ) const
116 return lhs - rhs.key();
119 template <typename T>
120 int operator ()( T const& lhs, T const& rhs ) const
122 return lhs.key() - rhs.key();
145 template <typename T1, typename T2>
146 bool operator()( T1 const& t1, T2 const& t2 ) const
148 return t1.key() < t2.key();
153 template <typename List>
154 void test_common( List& l )
156 // Precondition: list is empty
157 // Postcondition: list is empty
159 static const size_t nSize = 20;
160 typedef typename List::key_type list_key_type;
161 typedef typename List::mapped_type list_mapped_type;
162 typedef typename List::value_type list_value_type;
169 for ( size_t i = 0; i < nSize; ++i ) {
170 arr[i].key = static_cast<int>(i) + 1;
171 arr[i].val = arr[i].key * 10;
173 shuffle( arr, arr + nSize );
175 ASSERT_TRUE( l.empty() );
176 ASSERT_CONTAINER_SIZE( l, 0 );
179 for ( auto const& i : arr ) {
180 EXPECT_FALSE( l.contains( i.key ));
181 EXPECT_FALSE( l.contains( key_type( i.key )));
182 EXPECT_FALSE( l.contains( other_key( i.key ), other_less()));
183 EXPECT_FALSE( l.find( i.key, []( list_value_type& ) {} ));
184 EXPECT_FALSE( l.find( key_type( i.key ), []( list_value_type& ) {} ));
185 EXPECT_FALSE( l.find_with( other_key( i.key ), other_less(), []( list_value_type& ) {} ));
187 switch ( i.key % 5 ) {
189 EXPECT_TRUE( l.insert( i.key ));
190 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
191 EXPECT_EQ( n.second.val, 0 );
193 EXPECT_FALSE( l.insert( i.key ));
197 EXPECT_TRUE( l.insert( i.key, i.val ));
198 EXPECT_TRUE( l.find( key_type(i.key), []( list_value_type& n ) {
199 EXPECT_EQ( n.second.val, n.first.nKey * 10 );
201 EXPECT_FALSE( l.insert( key_type( i.key )));
205 EXPECT_TRUE( l.insert_with( i.key, []( list_value_type& n ) {
206 n.second.val = n.first.nKey * 2;
208 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
209 EXPECT_EQ( n.second.val, n.first.key() * 2 );
211 EXPECT_FALSE( l.insert_with( i.key, []( list_value_type& n ) {
212 n.second.val = n.first.nKey * 3;
214 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
215 EXPECT_EQ( n.second.val, n.first.key() * 2 );
222 EXPECT_TRUE( l.emplace( std::move(k), i.key * 100 ));
223 EXPECT_EQ( k.key(), 0 );
224 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
225 EXPECT_EQ( n.second.val, n.first.nKey * 100 );
228 EXPECT_FALSE( l.emplace( std::move( k ), i.key ));
229 //EXPECT_EQ( k.key(), i.key ); // ???
230 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
231 EXPECT_EQ( n.second.val, n.first.nKey * 100 );
237 auto pair = l.update( i.key, []( bool bNew, list_value_type& n ) {
238 ASSERT_TRUE( false );
240 EXPECT_FALSE( pair.first );
241 EXPECT_FALSE( pair.second );
243 pair = l.update( list_key_type(i.key), []( bool bNew, list_value_type& n ) {
245 n.second.val = n.first.nKey * 3;
247 EXPECT_TRUE( pair.first );
248 EXPECT_TRUE( pair.second );
250 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
251 EXPECT_EQ( n.second.val, n.first.key() * 3 );
254 pair = l.update( list_key_type(i.key), []( bool bNew, list_value_type& n ) {
255 EXPECT_FALSE( bNew );
256 n.second.val = n.first.nKey * 5;
258 EXPECT_TRUE( pair.first );
259 EXPECT_FALSE( pair.second );
261 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
262 EXPECT_EQ( n.second.val, n.first.key() * 5 );
268 EXPECT_TRUE( l.contains( i.key ));
269 EXPECT_TRUE( l.contains( list_key_type(i.key)));
270 EXPECT_TRUE( l.contains( other_key( i.key ), other_less()));
271 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
272 n.second.val = n.first.nKey;
274 EXPECT_TRUE( l.find( i.key, []( list_value_type& n ) {
275 EXPECT_EQ( n.first.nKey, n.second.val );
276 n.second.val = n.first.nKey * 5;
278 EXPECT_TRUE( l.find_with( other_key( i.key ), other_less(), []( list_value_type& n ) {
279 EXPECT_EQ( n.first.nKey * 5, n.second.val );
282 auto pair = l.update( i.key, []( bool bNew, list_value_type& n ) {
283 EXPECT_FALSE( bNew );
284 EXPECT_EQ( n.first.nKey * 5, n.second.val );
285 n.second.val = n.first.nKey * 3;
287 EXPECT_TRUE( pair.first );
288 EXPECT_FALSE( pair.second );
290 EXPECT_FALSE( l.empty() );
293 ASSERT_FALSE( l.empty() );
294 EXPECT_CONTAINER_SIZE( l, nSize );
297 for ( auto const&i : arr ) {
298 switch ( i.key & 3 ) {
300 EXPECT_TRUE( l.erase( i.key ));
303 EXPECT_TRUE( l.erase_with( other_key( i.key ), other_less()));
306 EXPECT_TRUE( l.erase( i.key, [ &i ]( list_value_type const& n ) {
307 EXPECT_EQ( n.first.nKey, i.key );
308 EXPECT_EQ( n.first.nKey * 3, n.second.val );
312 EXPECT_TRUE( l.erase_with( other_key( i.key ), other_less(), [ &i ]( list_value_type const& n) {
313 EXPECT_EQ( n.first.nKey, i.key );
314 EXPECT_EQ( n.first.nKey * 3, n.second.val );
318 EXPECT_FALSE( l.contains( i.key ));
319 EXPECT_FALSE( l.contains( key_type( i.key )));
320 EXPECT_FALSE( l.contains( other_key( i.key ), other_less()));
321 EXPECT_FALSE( l.find( key_type( i.key ), []( list_value_type& ) {} ));
322 EXPECT_FALSE( l.find( i.key, []( list_value_type& ) {} ));
323 EXPECT_FALSE( l.find_with( other_key( i.key ), other_less(), []( list_value_type& ) {} ));
326 ASSERT_TRUE( l.empty() );
327 EXPECT_CONTAINER_SIZE( l, 0 );
330 for ( auto& i : arr )
331 EXPECT_TRUE( l.insert( i.key, i.val ));
333 ASSERT_FALSE( l.empty() );
334 EXPECT_CONTAINER_SIZE( l, nSize );
338 ASSERT_TRUE( l.empty() );
339 EXPECT_CONTAINER_SIZE( l, 0 );
341 // empty list iterator test
344 EXPECT_TRUE( l.begin() == l.end());
345 EXPECT_TRUE( l.cbegin() == l.cend());
346 EXPECT_TRUE( cl.begin() == cl.end());
347 EXPECT_TRUE( l.begin() == l.cend());
348 EXPECT_TRUE( cl.begin() == l.end());
352 template <typename List>
353 void test_ordered_iterator( List& l )
355 // Precondition: list is empty
356 // Postcondition: list is empty
358 static const size_t nSize = 20;
365 for ( size_t i = 0; i < nSize; ++i ) {
366 arr[i].key = static_cast<int>(i);
367 arr[i].val = arr[i].key;
369 shuffle( arr, arr + nSize );
371 ASSERT_TRUE( l.empty() );
372 ASSERT_CONTAINER_SIZE( l, 0 );
374 for ( auto& i : arr )
375 EXPECT_TRUE( l.insert( i.key, i.val ));
378 for ( auto& it : l ) {
379 EXPECT_EQ( key, it.first.key() );
380 EXPECT_EQ( it.second.val, it.first.key() );
381 it.second.val = it.first.key() * 10;
384 EXPECT_EQ( static_cast<size_t>(key), nSize );
387 for ( auto it = l.cbegin(); it != l.cend(); ++it ) {
388 EXPECT_EQ( key, it->first.key() );
389 EXPECT_EQ( it->first.key() * 10, it->second.val );
392 EXPECT_EQ( static_cast<size_t>(key), nSize );
395 for ( auto it = l.begin(); it != l.end(); ++it ) {
396 EXPECT_EQ( key, it->first.key() );
397 EXPECT_EQ( it->first.key() * 10, it->second.val );
398 it->second.val = it->first.key() * 2;
401 EXPECT_EQ( static_cast<size_t>(key), nSize );
405 for ( auto it = cl.begin(); it != cl.end(); ++it ) {
406 EXPECT_EQ( key, it->first.nKey );
407 EXPECT_EQ( it->first.nKey * 2, it->second.val );
410 EXPECT_EQ( static_cast<size_t>(key), nSize );
413 ASSERT_TRUE( l.empty() );
414 EXPECT_CONTAINER_SIZE( l, 0 );
418 } // namespace cds_test
420 #endif // CDSUNIT_LIST_TEST_KV_LIST_H