File: attr_attribute_value_set.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (244 lines) | stat: -rw-r--r-- 7,196 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
/*
 *          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_value_set.cpp
 * \author Andrey Semashev
 * \date   24.01.2009
 *
 * \brief  This header contains tests for the attribute value set.
 */

#define BOOST_TEST_MODULE attr_attribute_value_set

#include <vector>
#include <string>
#include <sstream>
#include <utility>
#include <iterator>
#include <boost/config.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/tools//floating_point_comparison.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/attributes/value_visitation.hpp>
#include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>
#include "char_definitions.hpp"

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

namespace {

    //! A simple attribute value receiver functional object
    template< typename T >
    struct receiver
    {
        typedef void result_type;
        receiver(T& val) : m_Val(val) {}
        result_type operator() (T const& val) const
        {
            m_Val = val;
        }

    private:
        T& m_Val;
    };

    //! The function extracts attribute value
    template< typename T >
    inline bool get_attr_value(logging::attribute_value const& val, T& res)
    {
        receiver< T > r(res);
        logging::static_type_dispatcher< T > disp(r);
        return val.dispatch(disp);
    }

} // namespace

// The test checks construction and assignment
BOOST_AUTO_TEST_CASE(construction)
{
    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!");
    attrs::constant< char > attr4('L');

    {
        attr_set set1, set2, set3;
        set1[data::attr1()] = attr1;
        set1[data::attr2()] = attr2;
        set1[data::attr3()] = attr3;

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

        BOOST_CHECK(!view1.empty());
        BOOST_CHECK_EQUAL(view1.size(), 3UL);
    }
    {
        attr_set set1, set2, set3;
        set1[data::attr1()] = attr1;
        set2[data::attr2()] = attr2;
        set3[data::attr3()] = attr3;

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

        BOOST_CHECK(!view1.empty());
        BOOST_CHECK_EQUAL(view1.size(), 3UL);

        attr_values view2 = view1;
        BOOST_CHECK(!view2.empty());
        BOOST_CHECK_EQUAL(view2.size(), 3UL);
    }

    // Check that the more prioritized attributes replace the less ones
    {
        attrs::constant< int > attr2_2(20);
        attrs::constant< double > attr4_2(10.3);
        attrs::constant< float > attr3_3(static_cast< float >(-7.2));
        attrs::constant< unsigned int > attr4_3(5);

        attr_set set1, set2, set3;
        set3[data::attr1()] = attr1;
        set3[data::attr2()] = attr2;
        set3[data::attr3()] = attr3;
        set3[data::attr4()] = attr4;

        set2[data::attr2()] = attr2_2;
        set2[data::attr4()] = attr4_2;

        set1[data::attr3()] = attr3_3;
        set1[data::attr4()] = attr4_3;

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

        BOOST_CHECK(!view1.empty());
        BOOST_CHECK_EQUAL(view1.size(), 4UL);

        int n = 0;
        BOOST_CHECK(logging::visit< int >(data::attr1(), view1, receiver< int >(n)));
        BOOST_CHECK_EQUAL(n, 10);

        BOOST_CHECK(logging::visit< int >(data::attr2(), view1, receiver< int >(n)));
        BOOST_CHECK_EQUAL(n, 20);

        float f = static_cast< float >(0.0);
        BOOST_CHECK(logging::visit< float >(data::attr3(), view1, receiver< float >(f)));
        BOOST_CHECK_CLOSE(f, static_cast< float >(-7.2), static_cast< float >(0.001));

        unsigned int m = 0;
        BOOST_CHECK(logging::visit< unsigned int >(data::attr4(), view1, receiver< unsigned int >(m)));
        BOOST_CHECK_EQUAL(m, 5U);
    }
}

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

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

    // Traditional find methods
    attr_values::const_iterator it = view1.find(data::attr1());
    BOOST_CHECK(it != view1.end());
    BOOST_CHECK(it->first == data::attr1());
    int val1 = 0;
    BOOST_CHECK(get_attr_value(it->second, val1));
    BOOST_CHECK_EQUAL(val1, 10);

    string s1 = data::attr2();
    it = view1.find(s1);
    BOOST_CHECK(it != view1.end());
    BOOST_CHECK(it->first == data::attr2());
    double val2 = 0;
    BOOST_CHECK(get_attr_value(it->second, val2));
    BOOST_CHECK_CLOSE(val2, 5.5, 0.001);

    it = view1.find(data::attr3());
    BOOST_CHECK(it != view1.end());
    BOOST_CHECK(it->first == data::attr3());
    std::string val3;
    BOOST_CHECK(get_attr_value(it->second, val3));
    BOOST_CHECK_EQUAL(val3, "Hello, world!");

    // make an additional check that the result is absent if the value type does not match the requested type
    BOOST_CHECK(!get_attr_value(it->second, val2));

    it = view1.find(data::attr4());
    BOOST_CHECK(it == view1.end());

    // Subscript operator
    logging::attribute_value p = view1[data::attr1()];
    BOOST_CHECK_EQUAL(view1.size(), 3UL);
    BOOST_CHECK(!!p);
    BOOST_CHECK(get_attr_value(p, val1));
    BOOST_CHECK_EQUAL(val1, 10);

    p = view1[s1];
    BOOST_CHECK_EQUAL(view1.size(), 3UL);
    BOOST_CHECK(!!p);
    BOOST_CHECK(get_attr_value(p, val2));
    BOOST_CHECK_CLOSE(val2, 5.5, 0.001);

    p = view1[data::attr3()];
    BOOST_CHECK_EQUAL(view1.size(), 3UL);
    BOOST_CHECK(!!p);
    BOOST_CHECK(get_attr_value(p, val3));
    BOOST_CHECK_EQUAL(val3, "Hello, world!");

    p = view1[data::attr4()];
    BOOST_CHECK(!p);
    BOOST_CHECK_EQUAL(view1.size(), 3UL);

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

// The test checks size method
BOOST_AUTO_TEST_CASE(size)
{
    typedef logging::attribute_value_set attr_values;
    attrs::constant< int > attr1(10);

    attr_values view;
    view.freeze();

    unsigned int i = 0;
    for (; i < 100; ++i)
    {
        std::ostringstream strm;
        strm << "Attr" << i;

        view.insert(attr_values::key_type(strm.str()), attr1.get_value());
    }

    BOOST_CHECK_EQUAL(view.size(), i);
}