Add tests for unordered nonintrusive and kv lists.
[libcds.git] / tests / test-hdr / unordered_list / hdr_lazy.h
1 //$$CDS-header$$
2
3 #ifndef CDSTEST_HDR_LAZY_H
4 #define CDSTEST_HDR_LAZY_H
5
6 #include "cppunit/cppunit_proxy.h"
7 #include <cds/container/details/lazy_list_base.h>
8
9 namespace unordlist {
10     namespace cc = cds::container;
11     namespace co = cds::container::opt;
12
13     class UnorderedLazyListTestHeader: public CppUnitMini::TestCase
14     {
15     public:
16         struct stat {
17             int nEnsureExistsCall;
18             int nEnsureNewCall;
19
20             stat()
21             {
22                 nEnsureExistsCall
23                     = nEnsureNewCall
24                     = 0;
25             }
26         };
27
28         struct item {
29             int     nKey;
30             int     nVal;
31
32             stat    s;
33
34             item(int key)
35                 : nKey( key )
36                 , nVal( key * 2 )
37                 , s()
38             {}
39
40             item(int key, int val)
41                 : nKey( key )
42                 , nVal(val)
43                 , s()
44             {}
45
46             item( item const& v )
47                 : nKey( v.nKey )
48                 , nVal( v.nVal )
49                 , s()
50             {}
51
52             int key() const
53             {
54                 return nKey;
55             }
56         };
57
58         template <typename T>
59         struct lt
60         {
61             bool operator ()(const T& v1, const T& v2 ) const
62             {
63                 return v1.key() < v2.key();
64             }
65
66             template <typename Q>
67             bool operator ()(const T& v1, const Q& v2 ) const
68             {
69                 return v1.key() < v2;
70             }
71
72             template <typename Q>
73             bool operator ()(const Q& v1, const T& v2 ) const
74             {
75                 return v1 < v2.key();
76             }
77         };
78
79         template <typename T>
80         struct cmp {
81             int operator ()(const T& v1, const T& v2 ) const
82             {
83                 if ( v1.key() < v2.key() )
84                     return -1;
85                 return v1.key() > v2.key() ? 1 : 0;
86             }
87
88             template <typename Q>
89             int operator ()(const T& v1, const Q& v2 ) const
90             {
91                 if ( v1.key() < v2 )
92                     return -1;
93                 return v1.key() > v2 ? 1 : 0;
94             }
95
96             template <typename Q>
97             int operator ()(const Q& v1, const T& v2 ) const
98             {
99                 if ( v1 < v2.key() )
100                     return -1;
101                 return v1 > v2.key() ? 1 : 0;
102             }
103         };
104
105         template <typename T>
106         struct equal_to {
107             int operator ()(const T& v1, const T& v2 ) const
108             {
109                 return v1.key() == v2.key();
110             }
111
112             template <typename Q>
113             int operator ()(const T& v1, const Q& v2 ) const
114             {
115                 return v1.key() == v2;
116             }
117
118             template <typename Q>
119             int operator ()(const Q& v1, const T& v2 ) const
120             {
121                 return v1 == v2.key();
122             }
123         };
124
125     protected:
126         template <class UnordList>
127         void nogc_test()
128         {
129             typedef UnordList list;
130             typedef typename list::value_type    value_type;
131             typedef std::pair<typename list::iterator, bool> ensure_result;
132
133             typename list::iterator it;
134
135             list l;
136             CPPUNIT_ASSERT( l.empty() );
137             CPPUNIT_ASSERT( l.insert(50) != l.end() );
138             CPPUNIT_ASSERT( !l.empty() );
139
140             ensure_result eres = l.ensure( item(100, 33) );
141             CPPUNIT_ASSERT( eres.second );
142             CPPUNIT_ASSERT( eres.first != l.end() );
143             CPPUNIT_ASSERT( l.insert( item(150) ) != l.end() );
144
145             CPPUNIT_ASSERT( l.insert(100) == l.end() );
146             eres = l.ensure( item(50, 33) );
147             CPPUNIT_ASSERT( !eres.second );
148             CPPUNIT_ASSERT( eres.first->nVal == eres.first->nKey * 2 );
149             eres.first->nVal = 63;
150
151             it = l.find( 33 );
152             CPPUNIT_ASSERT( it == l.end() );
153
154             it = l.find( 50 );
155             CPPUNIT_ASSERT( it != l.end() );
156             CPPUNIT_ASSERT( it->nKey == 50 );
157             CPPUNIT_ASSERT( it->nVal == 63 );
158
159             it = l.find( 100 );
160             CPPUNIT_ASSERT( it != l.end() );
161             CPPUNIT_ASSERT( it->nKey == 100 );
162             CPPUNIT_ASSERT( it->nVal == 33 );
163
164             CPPUNIT_ASSERT( !l.empty() );
165             l.clear();
166             CPPUNIT_ASSERT( l.empty() );
167
168             // insert test
169             CPPUNIT_ASSERT( l.emplace( 501 ) != l.end());
170             CPPUNIT_ASSERT( l.emplace( 251, 152 ) != l.end());
171             CPPUNIT_ASSERT( l.emplace( item( 1001 )) != l.end());
172
173             // insert failed - such key exists
174             CPPUNIT_ASSERT( l.emplace( 501, 2 ) == l.end());
175             CPPUNIT_ASSERT( l.emplace( 251, 10) == l.end());
176
177             it = l.find( 501 );
178             CPPUNIT_ASSERT( it != l.end() );
179             CPPUNIT_ASSERT( it->nKey == 501 );
180             CPPUNIT_ASSERT( it->nVal == 501 * 2 );
181
182             it = l.find( 1001 );
183             CPPUNIT_ASSERT( it != l.end() );
184             CPPUNIT_ASSERT( it->nKey == 1001 );
185             CPPUNIT_ASSERT( it->nVal == 1001 * 2 );
186
187             {
188                 typename UnordList::iterator it( l.begin() );
189                 typename UnordList::const_iterator cit( l.cbegin() );
190                 CPPUNIT_CHECK( it == cit );
191                 CPPUNIT_CHECK( it != l.end() );
192                 CPPUNIT_CHECK( it != l.cend() );
193                 CPPUNIT_CHECK( cit != l.end() );
194                 CPPUNIT_CHECK( cit != l.cend() );
195                 ++it;
196                 CPPUNIT_CHECK( it != cit );
197                 CPPUNIT_CHECK( it != l.end() );
198                 CPPUNIT_CHECK( it != l.cend() );
199                 CPPUNIT_CHECK( cit != l.end() );
200                 CPPUNIT_CHECK( cit != l.cend() );
201                 ++cit;
202                 CPPUNIT_CHECK( it == cit );
203                 CPPUNIT_CHECK( it != l.end() );
204                 CPPUNIT_CHECK( it != l.cend() );
205                 CPPUNIT_CHECK( cit != l.end() );
206                 CPPUNIT_CHECK( cit != l.cend() );
207             }
208
209
210             l.clear();
211             CPPUNIT_ASSERT( l.empty() );
212         }
213
214         void NOGC_cmp();
215         void NOGC_less();
216         void NOGC_equal_to();
217         void NOGC_cmpmix();
218         void NOGC_equal_to_mix();
219         void NOGC_ic();
220
221         CPPUNIT_TEST_SUITE(UnorderedLazyListTestHeader)
222             CPPUNIT_TEST(NOGC_cmp)
223             CPPUNIT_TEST(NOGC_less)
224             CPPUNIT_TEST(NOGC_equal_to)
225             CPPUNIT_TEST(NOGC_cmpmix)
226             CPPUNIT_TEST(NOGC_equal_to_mix)
227             CPPUNIT_TEST(NOGC_ic)
228         CPPUNIT_TEST_SUITE_END()
229     };
230
231 }   // namespace unordlist
232
233 #endif // #ifndef CDSTEST_HDR_LAZY_H