File: test_list.h

package info (click to toggle)
libcds 2.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 15,564 kB
  • sloc: cpp: 135,002; ansic: 7,218; perl: 243; sh: 237; makefile: 6
file content (348 lines) | stat: -rw-r--r-- 11,869 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef CDSUNIT_LIST_TEST_LIST_H
#define CDSUNIT_LIST_TEST_LIST_H

#include <cds_test/check_size.h>
#include <cds_test/fixture.h>

namespace cds_test {

    class list_common : public fixture
    {
    public:
        struct item {
            int     nKey;
            int     nVal;

            item()
            {}

            item( int key )
                : nKey( key )
                , nVal( key * 2 )
            {}

            item( int key, int val )
                : nKey( key )
                , nVal( val )
            {}

            item( const item& v )
                : nKey( v.nKey )
                , nVal( v.nVal )
            {}

            int key() const
            {
                return nKey;
            }
        };

        template <typename T>
        struct lt
        {
            bool operator ()( const T& v1, const T& v2 ) const
            {
                return v1.key() < v2.key();
            }

            template <typename Q>
            bool operator ()( const T& v1, const Q& v2 ) const
            {
                return v1.key() < v2;
            }

            template <typename Q>
            bool operator ()( const Q& v1, const T& v2 ) const
            {
                return v1 < v2.key();
            }
        };

        template <typename T>
        struct cmp {
            int operator ()( const T& v1, const T& v2 ) const
            {
                if ( v1.key() < v2.key())
                    return -1;
                return v1.key() > v2.key() ? 1 : 0;
            }

            template <typename Q>
            int operator ()( const T& v1, const Q& v2 ) const
            {
                if ( v1.key() < v2 )
                    return -1;
                return v1.key() > v2 ? 1 : 0;
            }

            template <typename Q>
            int operator ()( const Q& v1, const T& v2 ) const
            {
                if ( v1 < v2.key())
                    return -1;
                return v1 > v2.key() ? 1 : 0;
            }
        };

        struct other_item
        {
            int nKey;

            other_item()
            {}

            other_item( int n )
                : nKey( n )
            {}
        };

        struct other_less
        {
            template <typename T1, typename T2>
            bool operator()( T1 const& t1, T2 const& t2 ) const
            {
                return t1.nKey < t2.nKey;
            }
        };

    protected:
        template <typename List>
        void test_common( List& l )
        {
            // Precondition: list is empty
            // Postcondition: list is empty

            static const size_t nSize = 20;
            typedef typename List::value_type value_type;
            value_type arr[nSize];

            for ( size_t i = 0; i < nSize; ++i ) {
                arr[i].nKey = static_cast<int>(i);
                arr[i].nVal = arr[i].nKey * 10;
            }
            shuffle( arr, arr + nSize );

            ASSERT_TRUE( l.empty());
            ASSERT_CONTAINER_SIZE( l, 0 );

            // insert/find
            for ( auto const& i : arr ) {
                EXPECT_FALSE( l.contains( i ));
                EXPECT_FALSE( l.contains( i.nKey ));
                EXPECT_FALSE( l.contains( other_item( i.nKey ), other_less()));
                EXPECT_FALSE( l.find( i, []( value_type&, value_type const&) {} ));
                EXPECT_FALSE( l.find( i.nKey, []( value_type&, int ) {} ));
                EXPECT_FALSE( l.find_with( other_item( i.nKey ), other_less(), []( value_type&, other_item const& ) {} ));

                switch ( i.nKey & 3 ) {
                    case 0:
                        EXPECT_TRUE( l.insert( i.nKey ));
                        EXPECT_TRUE( l.find( i.nKey, []( value_type& n, int key ) {
                            EXPECT_EQ( n.nVal, key * 2 );
                        } ));
                        EXPECT_FALSE( l.insert( i.nKey ));
                        break;
                    case 1:
                        EXPECT_TRUE( l.insert( i, []( value_type& n ) {
                            n.nVal = n.nKey * 3;
                        }));
                        EXPECT_TRUE( l.find( i.nKey, []( value_type& n, int key ) {
                            EXPECT_EQ( n.nVal, key * 3 );
                        } ));
                        EXPECT_FALSE( l.insert( i ));
                        break;
                    case 2:
                        EXPECT_TRUE( l.emplace( i.nKey, i.nKey * 100 ));
                        EXPECT_TRUE( l.find( i.nKey, []( value_type& n, int key ) {
                            EXPECT_EQ( n.nVal, key * 100 );
                        } ));
                        EXPECT_FALSE( l.insert( i ));
                        break;
                    case 3:
                    {
                        auto pair = l.update( i.nKey, []( bool bNew, value_type& n, int key ) {
                            EXPECT_TRUE( bNew );
                            EXPECT_EQ( key, n.nKey );
                            n.nVal = n.nKey * 3;
                        }, false );
                        EXPECT_FALSE( pair.first );
                        EXPECT_FALSE( pair.second );

                        pair = l.update( i.nKey, []( bool bNew, value_type& n, int key ) {
                            EXPECT_TRUE( bNew );
                            EXPECT_EQ( key, n.nKey );
                            n.nVal = n.nKey * 3;
                        });
                        EXPECT_TRUE( pair.first );
                        EXPECT_TRUE( pair.second );

                        EXPECT_TRUE( l.find( i.nKey, []( value_type& n, int key ) {
                            EXPECT_EQ( n.nVal, key * 3 );
                        } ));
                        EXPECT_FALSE( l.insert( i ));
                    }
                        break;
                }

                EXPECT_TRUE( l.contains( i ));
                EXPECT_TRUE( l.contains( i.nKey ));
                EXPECT_TRUE( l.contains( other_item( i.nKey ), other_less()));
                EXPECT_TRUE( l.find( i, []( value_type& n, value_type const& arg ) {
                    EXPECT_EQ( arg.nKey, n.nKey );
                    n.nVal = n.nKey;
                } ));
                EXPECT_TRUE( l.find( i.nKey, []( value_type& n, int key ) {
                    EXPECT_EQ( key, n.nKey );
                    EXPECT_EQ( n.nKey, n.nVal );
                    n.nVal = n.nKey * 5;
                } ));
                EXPECT_TRUE( l.find_with( other_item( i.nKey ), other_less(), []( value_type& n, other_item const& key ) {
                    EXPECT_EQ( key.nKey, n.nKey );
                    EXPECT_EQ( n.nKey * 5, n.nVal );
                } ));

                auto pair = l.update( i.nKey, []( bool bNew, value_type& n, int key ) {
                    EXPECT_FALSE( bNew );
                    EXPECT_EQ( key, n.nKey );
                    EXPECT_EQ( key * 5, n.nVal );
                    n.nVal = n.nKey * 3;
                }, false );
                EXPECT_TRUE( pair.first );
                EXPECT_FALSE( pair.second );

                EXPECT_FALSE( l.empty());
            }

            ASSERT_FALSE( l.empty());
            EXPECT_CONTAINER_SIZE( l, nSize );

            // erase
            for ( auto const&i : arr ) {
                switch ( i.nKey & 3 ) {
                    case 0:
                        EXPECT_TRUE( l.erase( i.nKey ));
                        break;
                    case 1:
                        EXPECT_TRUE( l.erase_with( other_item( i.nKey ), other_less()));
                        break;
                    case 2:
                        EXPECT_TRUE( l.erase( i, [ &i ]( value_type const& n ) {
                            EXPECT_EQ( n.nKey, i.nKey );
                            EXPECT_EQ( n.nKey * 3, n.nVal );
                        }));
                        break;
                    case 3:
                        EXPECT_TRUE( l.erase_with( other_item( i.nKey ), other_less(), [ &i ]( value_type const& n) {
                            EXPECT_EQ( n.nKey, i.nKey );
                            EXPECT_EQ( n.nKey * 3, n.nVal );
                        } ));
                }

                EXPECT_FALSE( l.contains( i ));
                EXPECT_FALSE( l.contains( i.nKey ));
                EXPECT_FALSE( l.contains( other_item( i.nKey ), other_less()));
                EXPECT_FALSE( l.find( i, []( value_type&, value_type const&) {} ));
                EXPECT_FALSE( l.find( i.nKey, []( value_type&, int ) {} ));
                EXPECT_FALSE( l.find_with( other_item( i.nKey ), other_less(), []( value_type&, other_item const& ) {} ));
            }

            ASSERT_TRUE( l.empty());
            EXPECT_CONTAINER_SIZE( l, 0 );

            // clear test
            for ( auto& i : arr )
                EXPECT_TRUE( l.insert( i ));

            ASSERT_FALSE( l.empty());
            EXPECT_CONTAINER_SIZE( l, nSize );

            l.clear();

            ASSERT_TRUE( l.empty());
            EXPECT_CONTAINER_SIZE( l, 0 );

            // empty list iterator test
            {
                List const& cl = l;
                EXPECT_TRUE( l.begin() == l.end());
                EXPECT_TRUE( l.cbegin() == l.cend());
                EXPECT_TRUE( cl.begin() == cl.end());
                EXPECT_TRUE( l.begin() == l.cend());
                EXPECT_TRUE( cl.begin() == l.end());
            }
        }

        template <typename List>
        void test_ordered_iterator( List& l )
        {
            // Precondition: list is empty
            // Postcondition: list is empty

            static const size_t nSize = 20;
            typedef typename List::value_type value_type;
            value_type arr[nSize];

            for ( size_t i = 0; i < nSize; ++i ) {
                arr[i].nKey = static_cast<int>(i);
                arr[i].nVal = arr[i].nKey;
            }
            shuffle( arr, arr + nSize );

            ASSERT_TRUE( l.empty());
            ASSERT_CONTAINER_SIZE( l, 0 );

            for ( auto& i : arr )
                EXPECT_TRUE( l.insert( i ));

            int key = 0;
            for ( auto& it : l ) {
                EXPECT_EQ( key, it.nKey );
                EXPECT_EQ( it.nVal, it.nKey );
                it.nVal = it.nKey * 10;
                ++key;
            }
            EXPECT_EQ( static_cast<size_t>(key), nSize );

            key = 0;
            for ( auto it = l.cbegin(); it != l.cend(); ++it ) {
                EXPECT_EQ( key, it->nKey );
                EXPECT_EQ( key, (*it).nKey );
                EXPECT_EQ( it->nKey * 10, it->nVal );
                ++key;
            }
            EXPECT_EQ( static_cast<size_t>(key), nSize );

            key = 0;
            for ( auto it = l.begin(); it != l.end(); ++it ) {
                EXPECT_EQ( key, it->nKey );
                EXPECT_EQ( key, (*it).nKey );
                EXPECT_EQ( it->nKey * 10, it->nVal );
                it->nVal = it->nKey * 2;
                ++key;
            }
            EXPECT_EQ( static_cast<size_t>(key), nSize );

            List const& cl = l;
            key = 0;
            for ( auto it = cl.begin(); it != cl.end(); ++it ) {
                EXPECT_EQ( key, it->nKey );
                EXPECT_EQ( key, (*it).nKey );
                EXPECT_EQ( it->nKey * 2, it->nVal );
                ++key;
            }
            EXPECT_EQ( static_cast<size_t>(key), nSize );

            l.clear();
            ASSERT_TRUE( l.empty());
            EXPECT_CONTAINER_SIZE( l, 0 );
        }
    };

} // namespace cds_test

#endif // CDSUNIT_LIST_TEST_LIST_H