]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/test/run/filt_attr.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / log / test / run / filt_attr.cpp
1 /*
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)
6 */
7 /*!
8 * \file filt_attr.cpp
9 * \author Andrey Semashev
10 * \date 31.01.2009
11 *
12 * \brief This header contains tests for the \c attr filter.
13 */
14
15 #define BOOST_TEST_MODULE filt_attr
16
17 #include <boost/predef/os.h>
18
19 // Try including WinAPI config as soon as possible so that any other headers don't include Windows SDK headers.
20 // This is important to select the target Windows version, as windows.h will be included by other Boost libraries, e.g. Regex.
21 #if defined(BOOST_OS_WINDOWS_AVAILABLE)
22 #define BOOST_USE_WINDOWS_H
23 #include <boost/winapi/config.hpp>
24 #endif
25
26 #include <memory>
27 #include <string>
28 #include <algorithm>
29 #include <boost/regex.hpp>
30 #include <boost/mpl/vector.hpp>
31 #include <boost/phoenix/bind.hpp>
32 #include <boost/test/unit_test.hpp>
33 #include <boost/log/attributes/constant.hpp>
34 #include <boost/log/attributes/attribute_set.hpp>
35 #include <boost/log/attributes/attribute_value_set.hpp>
36 #include <boost/log/utility/type_dispatch/standard_types.hpp>
37 #include <boost/log/support/regex.hpp>
38 #include <boost/log/expressions.hpp>
39 #include "char_definitions.hpp"
40
41 namespace phoenix = boost::phoenix;
42
43 namespace logging = boost::log;
44 namespace attrs = logging::attributes;
45 namespace expr = logging::expressions;
46
47 // The test checks that general conditions work
48 BOOST_AUTO_TEST_CASE(general_conditions)
49 {
50 typedef logging::attribute_set attr_set;
51 typedef logging::attribute_value_set attr_values;
52 typedef logging::filter filter;
53 typedef test_data< char > data;
54
55 attrs::constant< int > attr1(10);
56 attrs::constant< double > attr2(5.5);
57 attrs::constant< std::string > attr3("Hello, world!");
58
59 attr_set set1, set2, set3;
60 set1[data::attr1()] = attr1;
61 set1[data::attr2()] = attr2;
62 set1[data::attr3()] = attr3;
63
64 attr_values values1(set1, set2, set3);
65 values1.freeze();
66
67 filter f = expr::attr< int >(data::attr1()) == 10;
68 BOOST_CHECK(f(values1));
69
70 f = expr::attr< int >(data::attr1()) < 0;
71 BOOST_CHECK(!f(values1));
72
73 f = expr::attr< float >(data::attr1()).or_throw() > 0;
74 BOOST_CHECK_THROW(f(values1), logging::runtime_error);
75 f = expr::attr< float >(data::attr1()) > 0;
76 BOOST_CHECK(!f(values1));
77
78 f = expr::attr< int >(data::attr4()).or_throw() >= 1;
79 BOOST_CHECK_THROW(f(values1), logging::runtime_error);
80 f = expr::attr< int >(data::attr4()) >= 1;
81 BOOST_CHECK(!f(values1));
82
83 f = expr::attr< int >(data::attr4()) < 1;
84 BOOST_CHECK(!f(values1));
85
86 f = expr::attr< logging::numeric_types >(data::attr2()) > 5;
87 BOOST_CHECK(f(values1));
88
89 f = expr::attr< std::string >(data::attr3()) == "Hello, world!";
90 BOOST_CHECK(f(values1));
91
92 f = expr::attr< std::string >(data::attr3()) > "AAA";
93 BOOST_CHECK(f(values1));
94 }
95
96 // The test checks that is_in_range condition works
97 BOOST_AUTO_TEST_CASE(in_range_check)
98 {
99 typedef logging::attribute_set attr_set;
100 typedef logging::attribute_value_set attr_values;
101 typedef logging::filter filter;
102 typedef test_data< char > data;
103
104 attrs::constant< int > attr1(10);
105 attrs::constant< double > attr2(5.5);
106 attrs::constant< std::string > attr3("Hello, world!");
107
108 attr_set set1, set2, set3;
109 set1[data::attr1()] = attr1;
110 set1[data::attr2()] = attr2;
111 set1[data::attr3()] = attr3;
112
113 attr_values values1(set1, set2, set3);
114 values1.freeze();
115
116 filter f = expr::is_in_range(expr::attr< int >(data::attr1()), 5, 20);
117 BOOST_CHECK(f(values1));
118
119 f = expr::is_in_range(expr::attr< int >(data::attr1()), 5, 10);
120 BOOST_CHECK(!f(values1));
121
122 f = expr::is_in_range(expr::attr< int >(data::attr1()), 10, 20);
123 BOOST_CHECK(f(values1));
124
125 f = expr::is_in_range(expr::attr< logging::numeric_types >(data::attr2()), 5, 6);
126 BOOST_CHECK(f(values1));
127
128 f = expr::is_in_range(expr::attr< std::string >(data::attr3()), "AAA", "zzz");
129 BOOST_CHECK(f(values1));
130
131 // Check that strings are saved into the filter by value
132 char buf1[128];
133 char buf2[128];
134 std::strcpy(buf1, "AAA");
135 std::strcpy(buf2, "zzz");
136 f = expr::is_in_range(expr::attr< std::string >(data::attr3()), buf1, buf2);
137 std::fill_n(buf1, sizeof(buf1), static_cast< char >(0));
138 std::fill_n(buf2, sizeof(buf2), static_cast< char >(0));
139 BOOST_CHECK(f(values1));
140
141 std::strcpy(buf1, "AAA");
142 std::strcpy(buf2, "zzz");
143 f = expr::is_in_range(expr::attr< std::string >(data::attr3()),
144 static_cast< const char* >(buf1), static_cast< const char* >(buf2));
145 std::fill_n(buf1, sizeof(buf1), static_cast< char >(0));
146 std::fill_n(buf2, sizeof(buf2), static_cast< char >(0));
147 BOOST_CHECK(f(values1));
148 }
149
150 namespace {
151
152 struct predicate
153 {
154 typedef bool result_type;
155
156 explicit predicate(unsigned int& present_counter, bool& result) :
157 m_PresentCounter(present_counter),
158 m_Result(result)
159 {
160 }
161
162 template< typename T, typename TagT >
163 result_type operator() (logging::value_ref< T, TagT > const& val) const
164 {
165 m_PresentCounter += !val.empty();
166 return m_Result;
167 }
168
169 private:
170 unsigned int& m_PresentCounter;
171 bool& m_Result;
172 };
173
174 } // namespace
175
176 // The test checks that phoenix::bind interaction works
177 BOOST_AUTO_TEST_CASE(bind_support_check)
178 {
179 typedef logging::attribute_set attr_set;
180 typedef logging::attribute_value_set attr_values;
181 typedef logging::filter filter;
182 typedef test_data< char > data;
183
184 attrs::constant< int > attr1(10);
185 attrs::constant< double > attr2(5.5);
186 attrs::constant< std::string > attr3("Hello, world!");
187
188 attr_set set1, set2, set3;
189 set1[data::attr1()] = attr1;
190 set1[data::attr2()] = attr2;
191 set1[data::attr3()] = attr3;
192
193 attr_values values1(set1, set2, set3);
194 values1.freeze();
195
196 unsigned int present_counter = 0;
197 bool predicate_result = false;
198
199 filter f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr1()));
200 BOOST_CHECK_EQUAL(f(values1), predicate_result);
201 BOOST_CHECK_EQUAL(present_counter, 1U);
202
203 predicate_result = true;
204 BOOST_CHECK_EQUAL(f(values1), predicate_result);
205 BOOST_CHECK_EQUAL(present_counter, 2U);
206
207 f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< logging::numeric_types >(data::attr2()));
208 BOOST_CHECK_EQUAL(f(values1), predicate_result);
209 BOOST_CHECK_EQUAL(present_counter, 3U);
210
211 f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr2()).or_throw());
212 BOOST_CHECK_THROW(f(values1), logging::runtime_error);
213 f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr2()));
214 BOOST_CHECK_EQUAL(f(values1), true);
215 BOOST_CHECK_EQUAL(present_counter, 3U);
216
217 f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr4()).or_throw());
218 BOOST_CHECK_THROW(f(values1), logging::runtime_error);
219 f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr4()));
220 BOOST_CHECK_EQUAL(f(values1), true);
221 BOOST_CHECK_EQUAL(present_counter, 3U);
222 }
223
224 // The test checks that begins_with condition works
225 BOOST_AUTO_TEST_CASE(begins_with_check)
226 {
227 typedef logging::attribute_set attr_set;
228 typedef logging::attribute_value_set attr_values;
229 typedef logging::filter filter;
230 typedef test_data< char > data;
231
232 attrs::constant< int > attr1(10);
233 attrs::constant< double > attr2(5.5);
234 attrs::constant< std::string > attr3("Hello, world!");
235
236 attr_set set1, set2, set3;
237 set1[data::attr1()] = attr1;
238 set1[data::attr2()] = attr2;
239 set1[data::attr3()] = attr3;
240
241 attr_values values1(set1, set2, set3);
242 values1.freeze();
243
244 filter f = expr::begins_with(expr::attr< std::string >(data::attr3()), "Hello");
245 BOOST_CHECK(f(values1));
246
247 f = expr::begins_with(expr::attr< std::string >(data::attr3()), "hello");
248 BOOST_CHECK(!f(values1));
249
250 f = expr::begins_with(expr::attr< std::string >(data::attr3()).or_throw(), "Bye");
251 BOOST_CHECK(!f(values1));
252
253 f = expr::begins_with(expr::attr< std::string >(data::attr3()).or_throw(), "world!");
254 BOOST_CHECK(!f(values1));
255
256 f = expr::begins_with(expr::attr< std::string >(data::attr2()), "Hello");
257 BOOST_CHECK(!f(values1));
258
259 f = expr::begins_with(expr::attr< std::string >(data::attr4()), "Hello");
260 BOOST_CHECK(!f(values1));
261 }
262
263 // The test checks that ends_with condition works
264 BOOST_AUTO_TEST_CASE(ends_with_check)
265 {
266 typedef logging::attribute_set attr_set;
267 typedef logging::attribute_value_set attr_values;
268 typedef logging::filter filter;
269 typedef test_data< char > data;
270
271 attrs::constant< int > attr1(10);
272 attrs::constant< double > attr2(5.5);
273 attrs::constant< std::string > attr3("Hello, world!");
274
275 attr_set set1, set2, set3;
276 set1[data::attr1()] = attr1;
277 set1[data::attr2()] = attr2;
278 set1[data::attr3()] = attr3;
279
280 attr_values values1(set1, set2, set3);
281 values1.freeze();
282
283 filter f = expr::ends_with(expr::attr< std::string >(data::attr3()), "world!");
284 BOOST_CHECK(f(values1));
285
286 f = expr::ends_with(expr::attr< std::string >(data::attr3()), "World!");
287 BOOST_CHECK(!f(values1));
288
289 f = expr::ends_with(expr::attr< std::string >(data::attr3()).or_throw(), "Bye");
290 BOOST_CHECK(!f(values1));
291
292 f = expr::ends_with(expr::attr< std::string >(data::attr3()).or_throw(), "Hello");
293 BOOST_CHECK(!f(values1));
294
295 f = expr::ends_with(expr::attr< std::string >(data::attr2()), "world!");
296 BOOST_CHECK(!f(values1));
297
298 f = expr::ends_with(expr::attr< std::string >(data::attr4()), "world!");
299 BOOST_CHECK(!f(values1));
300 }
301
302 // The test checks that contains condition works
303 BOOST_AUTO_TEST_CASE(contains_check)
304 {
305 typedef logging::attribute_set attr_set;
306 typedef logging::attribute_value_set attr_values;
307 typedef logging::filter filter;
308 typedef test_data< char > data;
309
310 attrs::constant< int > attr1(10);
311 attrs::constant< double > attr2(5.5);
312 attrs::constant< std::string > attr3("Hello, world!");
313
314 attr_set set1, set2, set3;
315 set1[data::attr1()] = attr1;
316 set1[data::attr2()] = attr2;
317 set1[data::attr3()] = attr3;
318
319 attr_values values1(set1, set2, set3);
320 values1.freeze();
321
322 filter f = expr::contains(expr::attr< std::string >(data::attr3()), "Hello");
323 BOOST_CHECK(f(values1));
324
325 f = expr::contains(expr::attr< std::string >(data::attr3()), "hello");
326 BOOST_CHECK(!f(values1));
327
328 f = expr::contains(expr::attr< std::string >(data::attr3()).or_throw(), "o, w");
329 BOOST_CHECK(f(values1));
330
331 f = expr::contains(expr::attr< std::string >(data::attr3()).or_throw(), "world!");
332 BOOST_CHECK(f(values1));
333
334 f = expr::contains(expr::attr< std::string >(data::attr2()), "Hello");
335 BOOST_CHECK(!f(values1));
336
337 f = expr::contains(expr::attr< std::string >(data::attr4()), "Hello");
338 BOOST_CHECK(!f(values1));
339 }
340
341 // The test checks that matches condition works
342 BOOST_AUTO_TEST_CASE(matches_check)
343 {
344 typedef logging::attribute_set attr_set;
345 typedef logging::attribute_value_set attr_values;
346 typedef logging::filter filter;
347 typedef test_data< char > data;
348
349 attrs::constant< int > attr1(10);
350 attrs::constant< double > attr2(5.5);
351 attrs::constant< std::string > attr3("Hello, world!");
352
353 attr_set set1, set2, set3;
354 set1[data::attr1()] = attr1;
355 set1[data::attr2()] = attr2;
356 set1[data::attr3()] = attr3;
357
358 attr_values values1(set1, set2, set3);
359 values1.freeze();
360
361 boost::regex rex("hello");
362 filter f = expr::matches(expr::attr< std::string >(data::attr3()), rex);
363 BOOST_CHECK(!f(values1));
364
365 rex = ".*world.*";
366 f = expr::matches(expr::attr< std::string >(data::attr3()).or_throw(), rex);
367 BOOST_CHECK(f(values1));
368
369 rex = ".*";
370 f = expr::matches(expr::attr< std::string >(data::attr2()), rex);
371 BOOST_CHECK(!f(values1));
372
373 f = expr::matches(expr::attr< std::string >(data::attr4()), rex);
374 BOOST_CHECK(!f(values1));
375 }
376
377 // The test checks that the filter composition works
378 BOOST_AUTO_TEST_CASE(composition_check)
379 {
380 typedef logging::attribute_set attr_set;
381 typedef logging::attribute_value_set attr_values;
382 typedef logging::filter filter;
383 typedef test_data< char > data;
384
385 attrs::constant< int > attr1(10);
386 attrs::constant< double > attr2(5.5);
387 attrs::constant< std::string > attr3("Hello, world!");
388
389 attr_set set1, set2, set3;
390 attr_values values1(set1, set2, set3);
391 values1.freeze();
392 set1[data::attr2()] = attr2;
393 attr_values values2(set1, set2, set3);
394 values2.freeze();
395 set1[data::attr3()] = attr3;
396 set1[data::attr1()] = attr1;
397 attr_values values3(set1, set2, set3);
398 values3.freeze();
399
400 filter f =
401 expr::attr< int >(data::attr1()) <= 10 ||
402 expr::is_in_range(expr::attr< double >(data::attr2()), 2.2, 7.7);
403 BOOST_CHECK(!f(values1));
404 BOOST_CHECK(f(values2));
405 BOOST_CHECK(f(values3));
406
407 f = expr::attr< int >(data::attr1()) == 10 &&
408 expr::begins_with(expr::attr< std::string >(data::attr3()), "Hello");
409 BOOST_CHECK(!f(values1));
410 BOOST_CHECK(!f(values2));
411 BOOST_CHECK(f(values3));
412 }