File: filt_attr.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (412 lines) | stat: -rw-r--r-- 13,467 bytes parent folder | download | duplicates (11)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 *          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   filt_attr.cpp
 * \author Andrey Semashev
 * \date   31.01.2009
 *
 * \brief  This header contains tests for the \c attr filter.
 */

#define BOOST_TEST_MODULE filt_attr

#include <boost/predef/os.h>

// Try including WinAPI config as soon as possible so that any other headers don't include Windows SDK headers.
// This is important to select the target Windows version, as windows.h will be included by other Boost libraries, e.g. Regex.
#if defined(BOOST_OS_WINDOWS_AVAILABLE)
#define BOOST_USE_WINDOWS_H
#include <boost/winapi/config.hpp>
#endif

#include <memory>
#include <string>
#include <algorithm>
#include <boost/regex.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/log/attributes/constant.hpp>
#include <boost/log/attributes/attribute_set.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include <boost/log/utility/type_dispatch/standard_types.hpp>
#include <boost/log/support/regex.hpp>
#include <boost/log/expressions.hpp>
#include "char_definitions.hpp"

namespace phoenix = boost::phoenix;

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

// The test checks that general conditions work
BOOST_AUTO_TEST_CASE(general_conditions)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_values values1(set1, set2, set3);
    values1.freeze();

    filter f = expr::attr< int >(data::attr1()) == 10;
    BOOST_CHECK(f(values1));

    f = expr::attr< int >(data::attr1()) < 0;
    BOOST_CHECK(!f(values1));

    f = expr::attr< float >(data::attr1()).or_throw() > 0;
    BOOST_CHECK_THROW(f(values1), logging::runtime_error);
    f = expr::attr< float >(data::attr1()) > 0;
    BOOST_CHECK(!f(values1));

    f = expr::attr< int >(data::attr4()).or_throw() >= 1;
    BOOST_CHECK_THROW(f(values1), logging::runtime_error);
    f = expr::attr< int >(data::attr4()) >= 1;
    BOOST_CHECK(!f(values1));

    f = expr::attr< int >(data::attr4()) < 1;
    BOOST_CHECK(!f(values1));

    f = expr::attr< logging::numeric_types >(data::attr2()) > 5;
    BOOST_CHECK(f(values1));

    f = expr::attr< std::string >(data::attr3()) == "Hello, world!";
    BOOST_CHECK(f(values1));

    f = expr::attr< std::string >(data::attr3()) > "AAA";
    BOOST_CHECK(f(values1));
}

// The test checks that is_in_range condition works
BOOST_AUTO_TEST_CASE(in_range_check)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_values values1(set1, set2, set3);
    values1.freeze();

    filter f = expr::is_in_range(expr::attr< int >(data::attr1()), 5, 20);
    BOOST_CHECK(f(values1));

    f = expr::is_in_range(expr::attr< int >(data::attr1()), 5, 10);
    BOOST_CHECK(!f(values1));

    f = expr::is_in_range(expr::attr< int >(data::attr1()), 10, 20);
    BOOST_CHECK(f(values1));

    f = expr::is_in_range(expr::attr< logging::numeric_types >(data::attr2()), 5, 6);
    BOOST_CHECK(f(values1));

    f = expr::is_in_range(expr::attr< std::string >(data::attr3()), "AAA", "zzz");
    BOOST_CHECK(f(values1));

    // Check that strings are saved into the filter by value
    char buf1[128];
    char buf2[128];
    std::strcpy(buf1, "AAA");
    std::strcpy(buf2, "zzz");
    f = expr::is_in_range(expr::attr< std::string >(data::attr3()), buf1, buf2);
    std::fill_n(buf1, sizeof(buf1), static_cast< char >(0));
    std::fill_n(buf2, sizeof(buf2), static_cast< char >(0));
    BOOST_CHECK(f(values1));

    std::strcpy(buf1, "AAA");
    std::strcpy(buf2, "zzz");
    f = expr::is_in_range(expr::attr< std::string >(data::attr3()),
        static_cast< const char* >(buf1), static_cast< const char* >(buf2));
    std::fill_n(buf1, sizeof(buf1), static_cast< char >(0));
    std::fill_n(buf2, sizeof(buf2), static_cast< char >(0));
    BOOST_CHECK(f(values1));
}

namespace {

    struct predicate
    {
        typedef bool result_type;

        explicit predicate(unsigned int& present_counter, bool& result) :
            m_PresentCounter(present_counter),
            m_Result(result)
        {
        }

        template< typename T, typename TagT >
        result_type operator() (logging::value_ref< T, TagT > const& val) const
        {
            m_PresentCounter += !val.empty();
            return m_Result;
        }

    private:
        unsigned int& m_PresentCounter;
        bool& m_Result;
    };

} // namespace

// The test checks that phoenix::bind interaction works
BOOST_AUTO_TEST_CASE(bind_support_check)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_values values1(set1, set2, set3);
    values1.freeze();

    unsigned int present_counter = 0;
    bool predicate_result = false;

    filter f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr1()));
    BOOST_CHECK_EQUAL(f(values1), predicate_result);
    BOOST_CHECK_EQUAL(present_counter, 1U);

    predicate_result = true;
    BOOST_CHECK_EQUAL(f(values1), predicate_result);
    BOOST_CHECK_EQUAL(present_counter, 2U);

    f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< logging::numeric_types >(data::attr2()));
    BOOST_CHECK_EQUAL(f(values1), predicate_result);
    BOOST_CHECK_EQUAL(present_counter, 3U);

    f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr2()).or_throw());
    BOOST_CHECK_THROW(f(values1), logging::runtime_error);
    f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr2()));
    BOOST_CHECK_EQUAL(f(values1), true);
    BOOST_CHECK_EQUAL(present_counter, 3U);

    f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr4()).or_throw());
    BOOST_CHECK_THROW(f(values1), logging::runtime_error);
    f = phoenix::bind(predicate(present_counter, predicate_result), expr::attr< int >(data::attr4()));
    BOOST_CHECK_EQUAL(f(values1), true);
    BOOST_CHECK_EQUAL(present_counter, 3U);
}

// The test checks that begins_with condition works
BOOST_AUTO_TEST_CASE(begins_with_check)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_values values1(set1, set2, set3);
    values1.freeze();

    filter f = expr::begins_with(expr::attr< std::string >(data::attr3()), "Hello");
    BOOST_CHECK(f(values1));

    f = expr::begins_with(expr::attr< std::string >(data::attr3()), "hello");
    BOOST_CHECK(!f(values1));

    f = expr::begins_with(expr::attr< std::string >(data::attr3()).or_throw(), "Bye");
    BOOST_CHECK(!f(values1));

    f = expr::begins_with(expr::attr< std::string >(data::attr3()).or_throw(), "world!");
    BOOST_CHECK(!f(values1));

    f = expr::begins_with(expr::attr< std::string >(data::attr2()), "Hello");
    BOOST_CHECK(!f(values1));

    f = expr::begins_with(expr::attr< std::string >(data::attr4()), "Hello");
    BOOST_CHECK(!f(values1));
}

// The test checks that ends_with condition works
BOOST_AUTO_TEST_CASE(ends_with_check)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_values values1(set1, set2, set3);
    values1.freeze();

    filter f = expr::ends_with(expr::attr< std::string >(data::attr3()), "world!");
    BOOST_CHECK(f(values1));

    f = expr::ends_with(expr::attr< std::string >(data::attr3()), "World!");
    BOOST_CHECK(!f(values1));

    f = expr::ends_with(expr::attr< std::string >(data::attr3()).or_throw(), "Bye");
    BOOST_CHECK(!f(values1));

    f = expr::ends_with(expr::attr< std::string >(data::attr3()).or_throw(), "Hello");
    BOOST_CHECK(!f(values1));

    f = expr::ends_with(expr::attr< std::string >(data::attr2()), "world!");
    BOOST_CHECK(!f(values1));

    f = expr::ends_with(expr::attr< std::string >(data::attr4()), "world!");
    BOOST_CHECK(!f(values1));
}

// The test checks that contains condition works
BOOST_AUTO_TEST_CASE(contains_check)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_values values1(set1, set2, set3);
    values1.freeze();

    filter f = expr::contains(expr::attr< std::string >(data::attr3()), "Hello");
    BOOST_CHECK(f(values1));

    f = expr::contains(expr::attr< std::string >(data::attr3()), "hello");
    BOOST_CHECK(!f(values1));

    f = expr::contains(expr::attr< std::string >(data::attr3()).or_throw(), "o, w");
    BOOST_CHECK(f(values1));

    f = expr::contains(expr::attr< std::string >(data::attr3()).or_throw(), "world!");
    BOOST_CHECK(f(values1));

    f = expr::contains(expr::attr< std::string >(data::attr2()), "Hello");
    BOOST_CHECK(!f(values1));

    f = expr::contains(expr::attr< std::string >(data::attr4()), "Hello");
    BOOST_CHECK(!f(values1));
}

// The test checks that matches condition works
BOOST_AUTO_TEST_CASE(matches_check)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    attr_values values1(set1, set2, set3);
    values1.freeze();

    boost::regex rex("hello");
    filter f = expr::matches(expr::attr< std::string >(data::attr3()), rex);
    BOOST_CHECK(!f(values1));

    rex = ".*world.*";
    f = expr::matches(expr::attr< std::string >(data::attr3()).or_throw(), rex);
    BOOST_CHECK(f(values1));

    rex = ".*";
    f = expr::matches(expr::attr< std::string >(data::attr2()), rex);
    BOOST_CHECK(!f(values1));

    f = expr::matches(expr::attr< std::string >(data::attr4()), rex);
    BOOST_CHECK(!f(values1));
}

// The test checks that the filter composition works
BOOST_AUTO_TEST_CASE(composition_check)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef logging::filter filter;
    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, set2, set3;
    attr_values values1(set1, set2, set3);
    values1.freeze();
    set1[data::attr2()] = attr2;
    attr_values values2(set1, set2, set3);
    values2.freeze();
    set1[data::attr3()] = attr3;
    set1[data::attr1()] = attr1;
    attr_values values3(set1, set2, set3);
    values3.freeze();

    filter f =
        expr::attr< int >(data::attr1()) <= 10 ||
        expr::is_in_range(expr::attr< double >(data::attr2()), 2.2, 7.7);
    BOOST_CHECK(!f(values1));
    BOOST_CHECK(f(values2));
    BOOST_CHECK(f(values3));

    f = expr::attr< int >(data::attr1()) == 10 &&
        expr::begins_with(expr::attr< std::string >(data::attr3()), "Hello");
    BOOST_CHECK(!f(values1));
    BOOST_CHECK(!f(values2));
    BOOST_CHECK(f(values3));
}