File: attr_attribute_set.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (280 lines) | stat: -rw-r--r-- 9,043 bytes parent folder | download | duplicates (14)
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
/*
 *          Copyright Andrey Semashev 2007 - 2015.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
/*!
 * \file   attr_attribute_set.cpp
 * \author Andrey Semashev
 * \date   24.01.2009
 *
 * \brief  This header contains tests for the attribute set class.
 */

#define BOOST_TEST_MODULE attr_attribute_set

#include <list>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <boost/test/unit_test.hpp>
#include <boost/log/attributes/constant.hpp>
#include <boost/log/attributes/attribute_set.hpp>
#include "char_definitions.hpp"
#include "attr_comparison.hpp"

namespace logging = boost::log;
namespace attrs = logging::attributes;

// The test checks construction and assignment
BOOST_AUTO_TEST_CASE(construction)
{
    typedef logging::attribute_set attr_set;
    typedef test_data< char > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);
    attrs::constant< std::string > attr3("Hello, world!");

    attr_set set1;
    BOOST_CHECK(set1.empty());
    BOOST_CHECK_EQUAL(set1.size(), 0UL);

    attr_set set2 = set1;
    BOOST_CHECK(set2.empty());
    BOOST_CHECK_EQUAL(set2.size(), 0UL);

    set2[data::attr1()] = attr1;
    set2[data::attr2()] = attr2;
    BOOST_CHECK(set1.empty());
    BOOST_CHECK_EQUAL(set1.size(), 0UL);
    BOOST_CHECK(!set2.empty());
    BOOST_CHECK_EQUAL(set2.size(), 2UL);

    attr_set set3 = set2;
    BOOST_CHECK(!set3.empty());
    BOOST_CHECK_EQUAL(set3.size(), 2UL);
    BOOST_CHECK_EQUAL(set3.count(data::attr1()), 1UL);
    BOOST_CHECK_EQUAL(set3.count(data::attr2()), 1UL);
    BOOST_CHECK_EQUAL(set3.count(data::attr3()), 0UL);

    set1[data::attr3()] = attr3;
    BOOST_CHECK(!set1.empty());
    BOOST_CHECK_EQUAL(set1.size(), 1UL);
    BOOST_CHECK_EQUAL(set1.count(data::attr3()), 1UL);

    set2 = set1;
    BOOST_REQUIRE_EQUAL(set1.size(), set2.size());
    BOOST_CHECK(std::equal(set1.begin(), set1.end(), set2.begin()));
}

// The test checks lookup methods
BOOST_AUTO_TEST_CASE(lookup)
{
    typedef logging::attribute_set attr_set;
    typedef test_data< char > data;
    typedef std::basic_string< char > string;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);

    attr_set set1;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;

    // Traditional find methods
    attr_set::iterator it = set1.find(data::attr1());
    BOOST_CHECK(it != set1.end());
    BOOST_CHECK_EQUAL(it->second, attr1);

    string s1 = data::attr2();
    it = set1.find(s1);
    BOOST_CHECK(it != set1.end());
    BOOST_CHECK_EQUAL(it->second, attr2);

    it = set1.find(data::attr1());
    BOOST_CHECK(it != set1.end());
    BOOST_CHECK_EQUAL(it->second, attr1);

    it = set1.find(data::attr3());
    BOOST_CHECK(it == set1.end());

    // Subscript operator
    logging::attribute p = set1[data::attr1()];
    BOOST_CHECK_EQUAL(p, attr1);
    BOOST_CHECK_EQUAL(set1.size(), 2UL);

    p = set1[s1];
    BOOST_CHECK_EQUAL(p, attr2);
    BOOST_CHECK_EQUAL(set1.size(), 2UL);

    p = set1[data::attr1()];
    BOOST_CHECK_EQUAL(p, attr1);
    BOOST_CHECK_EQUAL(set1.size(), 2UL);

    p = set1[data::attr3()];
    BOOST_CHECK(!p);
    BOOST_CHECK_EQUAL(set1.size(), 2UL);

    // Counting elements
    BOOST_CHECK_EQUAL(set1.count(data::attr1()), 1UL);
    BOOST_CHECK_EQUAL(set1.count(s1), 1UL);
    BOOST_CHECK_EQUAL(set1.count(data::attr1()), 1UL);
    BOOST_CHECK_EQUAL(set1.count(data::attr3()), 0UL);
}

// The test checks insertion methods
BOOST_AUTO_TEST_CASE(insertion)
{
    typedef logging::attribute_set attr_set;
    typedef test_data< char > data;
    typedef std::basic_string< char > string;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);
    attrs::constant< std::string > attr3("Hello, world!");

    attr_set set1;

    // Traditional insert methods
    std::pair< attr_set::iterator, bool > res = set1.insert(data::attr1(), attr1);
    BOOST_CHECK(res.second);
    BOOST_CHECK(res.first != set1.end());
    BOOST_CHECK(res.first->first == data::attr1());
    BOOST_CHECK_EQUAL(res.first->second, attr1);
    BOOST_CHECK(!set1.empty());
    BOOST_CHECK_EQUAL(set1.size(), 1UL);

    res = set1.insert(std::make_pair(attr_set::key_type(data::attr2()), attr2));
    BOOST_CHECK(res.second);
    BOOST_CHECK(res.first != set1.end());
    BOOST_CHECK(res.first->first == data::attr2());
    BOOST_CHECK_EQUAL(res.first->second, attr2);
    BOOST_CHECK(!set1.empty());
    BOOST_CHECK_EQUAL(set1.size(), 2UL);

    // Insertion attempt of an attribute with the name of an already existing attribute
    res = set1.insert(std::make_pair(attr_set::key_type(data::attr2()), attr3));
    BOOST_CHECK(!res.second);
    BOOST_CHECK(res.first != set1.end());
    BOOST_CHECK(res.first->first == data::attr2());
    BOOST_CHECK_EQUAL(res.first->second, attr2);
    BOOST_CHECK(!set1.empty());
    BOOST_CHECK_EQUAL(set1.size(), 2UL);

    // Mass insertion
    typedef attr_set::key_type key_type;
    std::list< std::pair< key_type, logging::attribute > > elems;
    elems.push_back(std::make_pair(key_type(data::attr2()), attr2));
    elems.push_back(std::make_pair(key_type(data::attr1()), attr1));
    elems.push_back(std::make_pair(key_type(data::attr3()), attr3));
    // ... with element duplication
    elems.push_back(std::make_pair(key_type(data::attr1()), attr3));

    attr_set set2;
    set2.insert(elems.begin(), elems.end());
    BOOST_CHECK(!set2.empty());
    BOOST_REQUIRE_EQUAL(set2.size(), 3UL);
    typedef attr_set::mapped_type mapped_type;
    BOOST_CHECK_EQUAL(static_cast< mapped_type >(set2[data::attr1()]), attr1);
    BOOST_CHECK_EQUAL(static_cast< mapped_type >(set2[data::attr2()]), attr2);
    BOOST_CHECK_EQUAL(static_cast< mapped_type >(set2[data::attr3()]), attr3);

    // The same, but with insertion results collection
    std::vector< std::pair< attr_set::iterator, bool > > results;

    attr_set set3;
    set3.insert(elems.begin(), elems.end(), std::back_inserter(results));
    BOOST_REQUIRE_EQUAL(results.size(), elems.size());
    BOOST_CHECK(!set3.empty());
    BOOST_REQUIRE_EQUAL(set3.size(), 3UL);
    attr_set::iterator it = set3.find(data::attr1());
    BOOST_REQUIRE(it != set3.end());
    BOOST_CHECK(it->first == data::attr1());
    BOOST_CHECK_EQUAL(it->second, attr1);
    BOOST_CHECK(it == results[1].first);
    it = set3.find(data::attr2());
    BOOST_REQUIRE(it != set3.end());
    BOOST_CHECK(it->first == data::attr2());
    BOOST_CHECK_EQUAL(it->second, attr2);
    BOOST_CHECK(it == results[0].first);
    it = set3.find(data::attr3());
    BOOST_REQUIRE(it != set3.end());
    BOOST_CHECK(it->first == data::attr3());
    BOOST_CHECK_EQUAL(it->second, attr3);
    BOOST_CHECK(it == results[2].first);

    BOOST_CHECK(results[0].second);
    BOOST_CHECK(results[1].second);
    BOOST_CHECK(results[2].second);
    BOOST_CHECK(!results[3].second);

    // Subscript operator
    attr_set set4;

    logging::attribute& p1 = (set4[data::attr1()] = attr1);
    BOOST_CHECK_EQUAL(set4.size(), 1UL);
    BOOST_CHECK_EQUAL(p1, attr1);

    logging::attribute& p2 = (set4[string(data::attr2())] = attr2);
    BOOST_CHECK_EQUAL(set4.size(), 2UL);
    BOOST_CHECK_EQUAL(p2, attr2);

    logging::attribute& p3 = (set4[key_type(data::attr3())] = attr3);
    BOOST_CHECK_EQUAL(set4.size(), 3UL);
    BOOST_CHECK_EQUAL(p3, attr3);

    // subscript operator can replace existing elements
    logging::attribute& p4 = (set4[data::attr3()] = attr1);
    BOOST_CHECK_EQUAL(set4.size(), 3UL);
    BOOST_CHECK_EQUAL(p4, attr1);
}

// The test checks erasure methods
BOOST_AUTO_TEST_CASE(erasure)
{
    typedef logging::attribute_set attr_set;
    typedef test_data< char > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);
    attrs::constant< std::string > attr3("Hello, world!");

    attr_set set1;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_set set2 = set1;
    BOOST_REQUIRE_EQUAL(set2.size(), 3UL);

    BOOST_CHECK_EQUAL(set2.erase(data::attr1()), 1UL);
    BOOST_CHECK_EQUAL(set2.size(), 2UL);
    BOOST_CHECK_EQUAL(set2.count(data::attr1()), 0UL);

    BOOST_CHECK_EQUAL(set2.erase(data::attr1()), 0UL);
    BOOST_CHECK_EQUAL(set2.size(), 2UL);

    set2.erase(set2.begin());
    BOOST_CHECK_EQUAL(set2.size(), 1UL);
    BOOST_CHECK_EQUAL(set2.count(data::attr2()), 0UL);

    set2 = set1;
    BOOST_REQUIRE_EQUAL(set2.size(), 3UL);

    attr_set::iterator it = set2.begin();
    set2.erase(++it, set2.end());
    BOOST_CHECK_EQUAL(set2.size(), 1UL);
    BOOST_CHECK_EQUAL(set2.count(data::attr1()), 1UL);
    BOOST_CHECK_EQUAL(set2.count(data::attr2()), 0UL);
    BOOST_CHECK_EQUAL(set2.count(data::attr3()), 0UL);

    set2 = set1;
    BOOST_REQUIRE_EQUAL(set2.size(), 3UL);

    set2.clear();
    BOOST_CHECK(set2.empty());
    BOOST_CHECK_EQUAL(set2.size(), 0UL);
}