File: test_map_nogc.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 (234 lines) | stat: -rw-r--r-- 10,015 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
// 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_MAP_TEST_MAP_NOGC_H
#define CDSUNIT_MAP_TEST_MAP_NOGC_H

#include "test_map_data.h"

// forward declaration
namespace cds { namespace container {} }

namespace cds_test {

    class container_map_nogc: public map_fixture
    {
    public:
        static size_t const kSize = 1000;

    protected:
        template <class Map>
        void test( Map& m )
        {
            // Precondition: map is empty
            // Postcondition: map is empty

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

            typedef typename Map::value_type map_pair;
            typedef typename Map::iterator   iterator;
            size_t const kkSize = kSize;

            std::vector<key_type> arrKeys;
            for ( int i = 0; i < static_cast<int>(kkSize); ++i )
                arrKeys.push_back( key_type( i ));
            shuffle( arrKeys.begin(), arrKeys.end());

            std::vector< value_type > arrVals;
            for ( size_t i = 0; i < kkSize; ++i ) {
                value_type val;
                val.nVal = static_cast<int>( i );
                val.strVal = std::to_string( i );
                arrVals.push_back( val );
            }

            // insert/find
            for ( auto const& i : arrKeys ) {
                value_type const& val( arrVals.at( i.nKey ));

                ASSERT_FALSE( m.contains( i.nKey ) != m.end());
                ASSERT_FALSE( m.contains( i ) != m.end());
                ASSERT_FALSE( m.contains( other_item( i.nKey ), other_less()) != m.end());

                std::pair< iterator, bool > updResult;
                iterator it;
                switch ( i.nKey % 16 ) {
                case 0:
                    it = m.insert( i );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert( i ) == m.end());
                    it->second.nVal = it->first.nKey;
                    it->second.strVal = std::to_string( it->first.nKey );
                    break;
                case 1:
                    it = m.insert( i.nKey );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert( i.nKey ) == m.end());
                    it->second.nVal = it->first.nKey;
                    it->second.strVal = std::to_string( it->first.nKey );
                    break;
                case 2:
                    it = m.insert( std::to_string( i.nKey ));
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert( std::to_string( i.nKey )) == m.end());
                    it->second.nVal = it->first.nKey;
                    it->second.strVal = std::to_string( it->first.nKey );
                    break;
                case 3:
                    it = m.insert( i, val );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert( i, val ) == m.end());
                    break;
                case 4:
                    it = m.insert( i.nKey, val.strVal );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert( i.nKey, val.strVal ) == m.end());
                    break;
                case 5:
                    it = m.insert( val.strVal, i.nKey );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert( val.strVal, i.nKey ) == m.end());
                    break;
                case 6:
                    it = m.insert_with( i, []( map_pair& v ) {
                        v.second.nVal = v.first.nKey;
                        v.second.strVal = std::to_string( v.first.nKey );
                    } );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert_with( i, []( map_pair& ) {
                        EXPECT_TRUE( false );
                    } ) == m.end());
                    break;
                case 7:
                    it = m.insert_with( i.nKey, []( map_pair& v ) {
                        v.second.nVal = v.first.nKey;
                        v.second.strVal = std::to_string( v.first.nKey );
                    } );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert_with( i.nKey, []( map_pair& ) {
                        EXPECT_TRUE( false );
                    } ) == m.end());
                    break;
                case 8:
                    it = m.insert_with( val.strVal, []( map_pair& v ) {
                        v.second.nVal = v.first.nKey;
                        v.second.strVal = std::to_string( v.first.nKey );
                    } );
                    ASSERT_FALSE( it == m.end());
                    ASSERT_TRUE( m.insert_with( val.strVal, []( map_pair& ) {
                        EXPECT_TRUE( false );
                    } ) == m.end());
                    break;
                case 9:
                    updResult = m.update( i.nKey, false );
                    ASSERT_TRUE( updResult.first == m.end());
                    ASSERT_FALSE( updResult.second );

                    updResult = m.update( i.nKey );
                    ASSERT_TRUE( updResult.first != m.end());
                    ASSERT_TRUE( updResult.second );
                    updResult.first->second.nVal = updResult.first->first.nKey;

                    updResult = m.update( i.nKey, false );
                    ASSERT_TRUE( updResult.first != m.end());
                    ASSERT_FALSE( updResult.second );
                    EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
                    updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
                    break;
                case 10:
                    updResult = m.update( i, false );
                    ASSERT_TRUE( updResult.first == m.end());
                    ASSERT_FALSE( updResult.second );

                    updResult = m.update( i );
                    ASSERT_TRUE( updResult.first != m.end());
                    ASSERT_TRUE( updResult.second );
                    updResult.first->second.nVal = updResult.first->first.nKey;

                    updResult = m.update( i );
                    ASSERT_TRUE( updResult.first != m.end());
                    ASSERT_FALSE( updResult.second );
                    EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
                    updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
                    break;
                case 11:
                    updResult = m.update( val.strVal, false );
                    ASSERT_TRUE( updResult.first == m.end());
                    ASSERT_FALSE( updResult.second );

                    updResult = m.update( val.strVal );
                    ASSERT_TRUE( updResult.first != m.end());
                    ASSERT_TRUE( updResult.second );
                    updResult.first->second.nVal = updResult.first->first.nKey;

                    updResult = m.update( val.strVal, false );
                    ASSERT_TRUE( updResult.first != m.end());
                    ASSERT_FALSE( updResult.second );
                    EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
                    updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
                    break;
                case 12:
                    it = m.emplace( i.nKey );
                    ASSERT_TRUE( it != m.end());
                    ASSERT_FALSE( m.emplace( i.nKey ) != m.end());
                    it->second.nVal = it->first.nKey;
                    it->second.strVal = std::to_string( it->first.nKey );
                    break;
                case 13:
                    it = m.emplace( i, i.nKey );
                    ASSERT_TRUE( it != m.end());
                    ASSERT_FALSE( m.emplace( i, i.nKey ) != m.end());
                    break;
                case 14:
                    {
                        std::string str = val.strVal;
                        it = m.emplace( i, std::move( str ));
                        ASSERT_TRUE( it != m.end());
                        ASSERT_TRUE( str.empty());
                        str = val.strVal;
                        ASSERT_FALSE( m.emplace( i, std::move( str )) != m.end());
                        ASSERT_TRUE( str.empty());
                    }
                    break;
                case 15:
                    {
                        std::string str = val.strVal;
                        it = m.emplace( i, i.nKey, std::move( str ));
                        ASSERT_TRUE( it != m.end());
                        ASSERT_TRUE( str.empty());
                        str = val.strVal;
                        ASSERT_FALSE( m.emplace( i, i.nKey, std::move( str )) != m.end());
                        ASSERT_TRUE( str.empty());
                    }
                    break;
                }

                it = m.contains( i.nKey );
                ASSERT_TRUE( it != m.end());
                ASSERT_TRUE( m.contains( i ) == it );
                ASSERT_TRUE( m.contains( other_item( i.nKey ), other_less()) == it );
                EXPECT_EQ( it->first.nKey, it->second.nVal );
                EXPECT_EQ( std::to_string( it->first.nKey ), it->second.strVal );
            }
            ASSERT_FALSE( m.empty());
            ASSERT_CONTAINER_SIZE( m, kkSize );
            ASSERT_FALSE( m.begin() == m.end());
            ASSERT_FALSE( m.cbegin() == m.cend());

            // clear

            m.clear();

            ASSERT_TRUE( m.empty());
            ASSERT_CONTAINER_SIZE( m, 0 );
            ASSERT_TRUE( m.begin() == m.end());
            ASSERT_TRUE( m.cbegin() == m.cend());
        }
    };

} // namespace cds_test

#endif // #ifndef CDSUNIT_MAP_TEST_MAP_NOGC_H