File: attr_value_visitation.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 (238 lines) | stat: -rw-r--r-- 7,006 bytes parent folder | download | duplicates (13)
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
/*
 *          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_value_visitation.cpp
 * \author Andrey Semashev
 * \date   21.01.2009
 *
 * \brief  This header contains tests for the attribute value extraction helpers.
 */

#define BOOST_TEST_MODULE attr_value_visitation

#include <string>
#include <boost/mpl/vector.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/log/attributes/value_visitation.hpp>
#include <boost/log/attributes/constant.hpp>
#include <boost/log/attributes/attribute_set.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include "char_definitions.hpp"

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

namespace {

    // The receiver functional object that verifies the extracted attribute values
    struct my_receiver
    {
        typedef void result_type;

        enum type_expected
        {
            none_expected,
            int_expected,
            double_expected,
            string_expected
        };

        my_receiver() : m_Expected(none_expected), m_Int(0), m_Double(0.0) {}

        void set_expected()
        {
            m_Expected = none_expected;
        }
        void set_expected(int value)
        {
            m_Expected = int_expected;
            m_Int = value;
        }
        void set_expected(double value)
        {
            m_Expected = double_expected;
            m_Double = value;
        }
        void set_expected(std::string const& value)
        {
            m_Expected = string_expected;
            m_String = value;
        }

        // Implement visitation logic for all supported types
        void operator() (int const& value)
        {
            BOOST_CHECK_EQUAL(m_Expected, int_expected);
            BOOST_CHECK_EQUAL(m_Int, value);
        }
        void operator() (double const& value)
        {
            BOOST_CHECK_EQUAL(m_Expected, double_expected);
            BOOST_CHECK_CLOSE(m_Double, value, 0.001);
        }
        void operator() (std::string const& value)
        {
            BOOST_CHECK_EQUAL(m_Expected, string_expected);
            BOOST_CHECK_EQUAL(m_String, value);
        }
        void operator() (char value)
        {
            // This one should not be called
            BOOST_ERROR("The unexpected operator() has been called");
        }

    private:
        type_expected m_Expected;
        int m_Int;
        double m_Double;
        std::string m_String;
    };

} // namespace

// The test checks invokers specialized on a single attribute value type
BOOST_AUTO_TEST_CASE(single_type)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    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;

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

    my_receiver recv;

    logging::value_visitor_invoker< int > invoker1;
    logging::value_visitor_invoker< double > invoker2;
    logging::value_visitor_invoker< std::string > invoker3;
    logging::value_visitor_invoker< char > invoker4;

    // These two extractors will find their values in the set
    recv.set_expected(10);
    BOOST_CHECK(invoker1(data::attr1(), values1, recv));

    recv.set_expected(5.5);
    BOOST_CHECK(invoker2(data::attr2(), values1, recv));

    // This one will not
    recv.set_expected();
    BOOST_CHECK(!invoker3(data::attr3(), values1, recv));

    // But it will find it in this set
    set1[data::attr3()] = attr3;

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

    recv.set_expected("Hello, world!");
    BOOST_CHECK(invoker3(data::attr3(), values2, recv));

    // This one will find the sought attribute value, but it will have an incorrect type
    recv.set_expected();
    BOOST_CHECK(!invoker4(data::attr1(), values1, recv));

    // This one is the same, but there is a value of the requested type in the set
    BOOST_CHECK(!invoker1(data::attr2(), values1, recv));
}


// The test checks invokers specialized with type lists
BOOST_AUTO_TEST_CASE(multiple_types)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef test_data< char > data;
    typedef mpl::vector< int, double, std::string, char >::type types;

    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;

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

    my_receiver recv;

    logging::value_visitor_invoker< types > invoker;

    // These two extractors will find their values in the set
    recv.set_expected(10);
    BOOST_CHECK(invoker(data::attr1(), values1, recv));

    recv.set_expected(5.5);
    BOOST_CHECK(invoker(data::attr2(), values1, recv));

    // This one will not
    recv.set_expected();
    BOOST_CHECK(!invoker(data::attr3(), values1, recv));

    // But it will find it in this set
    set1[data::attr3()] = attr3;

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

    recv.set_expected("Hello, world!");
    BOOST_CHECK(invoker(data::attr3(), values2, recv));
}

// The test verifies the visit function
BOOST_AUTO_TEST_CASE(visit_function)
{
    typedef logging::attribute_set attr_set;
    typedef logging::attribute_value_set attr_values;
    typedef test_data< char > data;
    typedef mpl::vector< int, double, std::string, char >::type types;

    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;

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

    my_receiver recv;

    // These two extractors will find their values in the set
    recv.set_expected(10);
    BOOST_CHECK(logging::visit< types >(data::attr1(), values1, recv));

    recv.set_expected(5.5);
    BOOST_CHECK(logging::visit< double >(data::attr2(), values1, recv));

    // These will not
    recv.set_expected();
    BOOST_CHECK(!logging::visit< types >(data::attr3(), values1, recv));
    BOOST_CHECK(!logging::visit< char >(data::attr1(), values1, recv));

    // But it will find it in this set
    set1[data::attr3()] = attr3;

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

    recv.set_expected("Hello, world!");
    BOOST_CHECK(logging::visit< std::string >(data::attr3(), values2, recv));
}