]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/test/run/attr_attribute_set.cpp
2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file attr_attribute_set.cpp
9 * \author Andrey Semashev
12 * \brief This header contains tests for the attribute set class.
15 #define BOOST_TEST_MODULE attr_attribute_set
22 #include <boost/test/unit_test.hpp>
23 #include <boost/log/attributes/constant.hpp>
24 #include <boost/log/attributes/attribute_set.hpp>
25 #include "char_definitions.hpp"
26 #include "attr_comparison.hpp"
28 namespace logging
= boost::log
;
29 namespace attrs
= logging::attributes
;
31 // The test checks construction and assignment
32 BOOST_AUTO_TEST_CASE(construction
)
34 typedef logging::attribute_set attr_set
;
35 typedef test_data
< char > data
;
37 attrs::constant
< int > attr1(10);
38 attrs::constant
< double > attr2(5.5);
39 attrs::constant
< std::string
> attr3("Hello, world!");
42 BOOST_CHECK(set1
.empty());
43 BOOST_CHECK_EQUAL(set1
.size(), 0UL);
46 BOOST_CHECK(set2
.empty());
47 BOOST_CHECK_EQUAL(set2
.size(), 0UL);
49 set2
[data::attr1()] = attr1
;
50 set2
[data::attr2()] = attr2
;
51 BOOST_CHECK(set1
.empty());
52 BOOST_CHECK_EQUAL(set1
.size(), 0UL);
53 BOOST_CHECK(!set2
.empty());
54 BOOST_CHECK_EQUAL(set2
.size(), 2UL);
57 BOOST_CHECK(!set3
.empty());
58 BOOST_CHECK_EQUAL(set3
.size(), 2UL);
59 BOOST_CHECK_EQUAL(set3
.count(data::attr1()), 1UL);
60 BOOST_CHECK_EQUAL(set3
.count(data::attr2()), 1UL);
61 BOOST_CHECK_EQUAL(set3
.count(data::attr3()), 0UL);
63 set1
[data::attr3()] = attr3
;
64 BOOST_CHECK(!set1
.empty());
65 BOOST_CHECK_EQUAL(set1
.size(), 1UL);
66 BOOST_CHECK_EQUAL(set1
.count(data::attr3()), 1UL);
69 BOOST_REQUIRE_EQUAL(set1
.size(), set2
.size());
70 BOOST_CHECK(std::equal(set1
.begin(), set1
.end(), set2
.begin()));
73 // The test checks lookup methods
74 BOOST_AUTO_TEST_CASE(lookup
)
76 typedef logging::attribute_set attr_set
;
77 typedef test_data
< char > data
;
78 typedef std::basic_string
< char > string
;
80 attrs::constant
< int > attr1(10);
81 attrs::constant
< double > attr2(5.5);
84 set1
[data::attr1()] = attr1
;
85 set1
[data::attr2()] = attr2
;
87 // Traditional find methods
88 attr_set::iterator it
= set1
.find(data::attr1());
89 BOOST_CHECK(it
!= set1
.end());
90 BOOST_CHECK_EQUAL(it
->second
, attr1
);
92 string s1
= data::attr2();
94 BOOST_CHECK(it
!= set1
.end());
95 BOOST_CHECK_EQUAL(it
->second
, attr2
);
97 it
= set1
.find(data::attr1());
98 BOOST_CHECK(it
!= set1
.end());
99 BOOST_CHECK_EQUAL(it
->second
, attr1
);
101 it
= set1
.find(data::attr3());
102 BOOST_CHECK(it
== set1
.end());
104 // Subscript operator
105 logging::attribute p
= set1
[data::attr1()];
106 BOOST_CHECK_EQUAL(p
, attr1
);
107 BOOST_CHECK_EQUAL(set1
.size(), 2UL);
110 BOOST_CHECK_EQUAL(p
, attr2
);
111 BOOST_CHECK_EQUAL(set1
.size(), 2UL);
113 p
= set1
[data::attr1()];
114 BOOST_CHECK_EQUAL(p
, attr1
);
115 BOOST_CHECK_EQUAL(set1
.size(), 2UL);
117 p
= set1
[data::attr3()];
119 BOOST_CHECK_EQUAL(set1
.size(), 2UL);
122 BOOST_CHECK_EQUAL(set1
.count(data::attr1()), 1UL);
123 BOOST_CHECK_EQUAL(set1
.count(s1
), 1UL);
124 BOOST_CHECK_EQUAL(set1
.count(data::attr1()), 1UL);
125 BOOST_CHECK_EQUAL(set1
.count(data::attr3()), 0UL);
128 // The test checks insertion methods
129 BOOST_AUTO_TEST_CASE(insertion
)
131 typedef logging::attribute_set attr_set
;
132 typedef test_data
< char > data
;
133 typedef std::basic_string
< char > string
;
135 attrs::constant
< int > attr1(10);
136 attrs::constant
< double > attr2(5.5);
137 attrs::constant
< std::string
> attr3("Hello, world!");
141 // Traditional insert methods
142 std::pair
< attr_set::iterator
, bool > res
= set1
.insert(data::attr1(), attr1
);
143 BOOST_CHECK(res
.second
);
144 BOOST_CHECK(res
.first
!= set1
.end());
145 BOOST_CHECK(res
.first
->first
== data::attr1());
146 BOOST_CHECK_EQUAL(res
.first
->second
, attr1
);
147 BOOST_CHECK(!set1
.empty());
148 BOOST_CHECK_EQUAL(set1
.size(), 1UL);
150 res
= set1
.insert(std::make_pair(attr_set::key_type(data::attr2()), attr2
));
151 BOOST_CHECK(res
.second
);
152 BOOST_CHECK(res
.first
!= set1
.end());
153 BOOST_CHECK(res
.first
->first
== data::attr2());
154 BOOST_CHECK_EQUAL(res
.first
->second
, attr2
);
155 BOOST_CHECK(!set1
.empty());
156 BOOST_CHECK_EQUAL(set1
.size(), 2UL);
158 // Insertion attempt of an attribute with the name of an already existing attribute
159 res
= set1
.insert(std::make_pair(attr_set::key_type(data::attr2()), attr3
));
160 BOOST_CHECK(!res
.second
);
161 BOOST_CHECK(res
.first
!= set1
.end());
162 BOOST_CHECK(res
.first
->first
== data::attr2());
163 BOOST_CHECK_EQUAL(res
.first
->second
, attr2
);
164 BOOST_CHECK(!set1
.empty());
165 BOOST_CHECK_EQUAL(set1
.size(), 2UL);
168 typedef attr_set::key_type key_type
;
169 std::list
< std::pair
< key_type
, logging::attribute
> > elems
;
170 elems
.push_back(std::make_pair(key_type(data::attr2()), attr2
));
171 elems
.push_back(std::make_pair(key_type(data::attr1()), attr1
));
172 elems
.push_back(std::make_pair(key_type(data::attr3()), attr3
));
173 // ... with element duplication
174 elems
.push_back(std::make_pair(key_type(data::attr1()), attr3
));
177 set2
.insert(elems
.begin(), elems
.end());
178 BOOST_CHECK(!set2
.empty());
179 BOOST_REQUIRE_EQUAL(set2
.size(), 3UL);
180 typedef attr_set::mapped_type mapped_type
;
181 BOOST_CHECK_EQUAL(static_cast< mapped_type
>(set2
[data::attr1()]), attr1
);
182 BOOST_CHECK_EQUAL(static_cast< mapped_type
>(set2
[data::attr2()]), attr2
);
183 BOOST_CHECK_EQUAL(static_cast< mapped_type
>(set2
[data::attr3()]), attr3
);
185 // The same, but with insertion results collection
186 std::vector
< std::pair
< attr_set::iterator
, bool > > results
;
189 set3
.insert(elems
.begin(), elems
.end(), std::back_inserter(results
));
190 BOOST_REQUIRE_EQUAL(results
.size(), elems
.size());
191 BOOST_CHECK(!set3
.empty());
192 BOOST_REQUIRE_EQUAL(set3
.size(), 3UL);
193 attr_set::iterator it
= set3
.find(data::attr1());
194 BOOST_REQUIRE(it
!= set3
.end());
195 BOOST_CHECK(it
->first
== data::attr1());
196 BOOST_CHECK_EQUAL(it
->second
, attr1
);
197 BOOST_CHECK(it
== results
[1].first
);
198 it
= set3
.find(data::attr2());
199 BOOST_REQUIRE(it
!= set3
.end());
200 BOOST_CHECK(it
->first
== data::attr2());
201 BOOST_CHECK_EQUAL(it
->second
, attr2
);
202 BOOST_CHECK(it
== results
[0].first
);
203 it
= set3
.find(data::attr3());
204 BOOST_REQUIRE(it
!= set3
.end());
205 BOOST_CHECK(it
->first
== data::attr3());
206 BOOST_CHECK_EQUAL(it
->second
, attr3
);
207 BOOST_CHECK(it
== results
[2].first
);
209 BOOST_CHECK(results
[0].second
);
210 BOOST_CHECK(results
[1].second
);
211 BOOST_CHECK(results
[2].second
);
212 BOOST_CHECK(!results
[3].second
);
214 // Subscript operator
217 logging::attribute
& p1
= (set4
[data::attr1()] = attr1
);
218 BOOST_CHECK_EQUAL(set4
.size(), 1UL);
219 BOOST_CHECK_EQUAL(p1
, attr1
);
221 logging::attribute
& p2
= (set4
[string(data::attr2())] = attr2
);
222 BOOST_CHECK_EQUAL(set4
.size(), 2UL);
223 BOOST_CHECK_EQUAL(p2
, attr2
);
225 logging::attribute
& p3
= (set4
[key_type(data::attr3())] = attr3
);
226 BOOST_CHECK_EQUAL(set4
.size(), 3UL);
227 BOOST_CHECK_EQUAL(p3
, attr3
);
229 // subscript operator can replace existing elements
230 logging::attribute
& p4
= (set4
[data::attr3()] = attr1
);
231 BOOST_CHECK_EQUAL(set4
.size(), 3UL);
232 BOOST_CHECK_EQUAL(p4
, attr1
);
235 // The test checks erasure methods
236 BOOST_AUTO_TEST_CASE(erasure
)
238 typedef logging::attribute_set attr_set
;
239 typedef test_data
< char > data
;
241 attrs::constant
< int > attr1(10);
242 attrs::constant
< double > attr2(5.5);
243 attrs::constant
< std::string
> attr3("Hello, world!");
246 set1
[data::attr1()] = attr1
;
247 set1
[data::attr2()] = attr2
;
248 set1
[data::attr3()] = attr3
;
250 attr_set set2
= set1
;
251 BOOST_REQUIRE_EQUAL(set2
.size(), 3UL);
253 BOOST_CHECK_EQUAL(set2
.erase(data::attr1()), 1UL);
254 BOOST_CHECK_EQUAL(set2
.size(), 2UL);
255 BOOST_CHECK_EQUAL(set2
.count(data::attr1()), 0UL);
257 BOOST_CHECK_EQUAL(set2
.erase(data::attr1()), 0UL);
258 BOOST_CHECK_EQUAL(set2
.size(), 2UL);
260 set2
.erase(set2
.begin());
261 BOOST_CHECK_EQUAL(set2
.size(), 1UL);
262 BOOST_CHECK_EQUAL(set2
.count(data::attr2()), 0UL);
265 BOOST_REQUIRE_EQUAL(set2
.size(), 3UL);
267 attr_set::iterator it
= set2
.begin();
268 set2
.erase(++it
, set2
.end());
269 BOOST_CHECK_EQUAL(set2
.size(), 1UL);
270 BOOST_CHECK_EQUAL(set2
.count(data::attr1()), 1UL);
271 BOOST_CHECK_EQUAL(set2
.count(data::attr2()), 0UL);
272 BOOST_CHECK_EQUAL(set2
.count(data::attr3()), 0UL);
275 BOOST_REQUIRE_EQUAL(set2
.size(), 3UL);
278 BOOST_CHECK(set2
.empty());
279 BOOST_CHECK_EQUAL(set2
.size(), 0UL);