File: attr_sets_insertion_lookup.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 (101 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download | duplicates (18)
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
/*
 *          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_sets_insertion_lookup.cpp
 * \author Andrey Semashev
 * \date   21.06.2014
 *
 * \brief  This header contains tests for the attribute and attribute value sets. This test performs special checks
 *         for insert() and find() methods that depend on the attribute name ids and the order in which
 *         insert() operations are invoked, see https://sourceforge.net/p/boost-log/discussion/710022/thread/e883db9a/.
 */

#define BOOST_TEST_MODULE attr_sets_insertion_lookup

#include <string>
#include <sstream>
#include <boost/config.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>

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

namespace {

template< typename SetT, typename ValueT >
void test_insertion_lookup(SetT& values, ValueT const& value)
{
    // Initialize attribute names. Each name will gain a consecutive id.
    logging::attribute_name names[20];
    for (unsigned int i = 0; i < sizeof(names) / sizeof(*names); ++i)
    {
        std::ostringstream strm;
        strm << "Attr" << i;
        names[i] = logging::attribute_name(strm.str());
    }

    // Insert attribute values in this exact order so that different cases in the hash table implementation are tested.
    values.insert(names[17], value);
    values.insert(names[1], value);
    values.insert(names[8], value);
    values.insert(names[9], value);
    values.insert(names[10], value);
    values.insert(names[16], value);
    values.insert(names[0], value);
    values.insert(names[11], value);
    values.insert(names[12], value);
    values.insert(names[13], value);
    values.insert(names[14], value);
    values.insert(names[15], value);
    values.insert(names[18], value);
    values.insert(names[19], value);
    values.insert(names[4], value);
    values.insert(names[5], value);
    values.insert(names[7], value);
    values.insert(names[6], value);
    values.insert(names[2], value);
    values.insert(names[3], value);

    // Check that all values are accessible through iteration and find()
    for (unsigned int i = 0; i < sizeof(names) / sizeof(*names); ++i)
    {
        BOOST_CHECK_MESSAGE(values.find(names[i]) != values.end(), "Attribute " << names[i] << " (id: " << names[i].id() << ") not found by find()");

        bool found_by_iteration = false;
        for (typename SetT::const_iterator it = values.begin(), end = values.end(); it != end; ++it)
        {
            if (it->first == names[i])
            {
                found_by_iteration = true;
                break;
            }
        }

        BOOST_CHECK_MESSAGE(found_by_iteration, "Attribute " << names[i] << " (id: " << names[i].id() << ") not found by iteration");
    }
}

} // namespace

BOOST_AUTO_TEST_CASE(attributes)
{
    logging::attribute_set values;
    attrs::constant< int > attr(10);

    test_insertion_lookup(values, attr);
}

BOOST_AUTO_TEST_CASE(attribute_values)
{
    logging::attribute_value_set values;
    attrs::constant< int > attr(10);

    test_insertion_lookup(values, attr.get_value());
}