• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   attr_sets_insertion_lookup.cpp
9  * \author Andrey Semashev
10  * \date   21.06.2014
11  *
12  * \brief  This header contains tests for the attribute and attribute value sets. This test performs special checks
13  *         for insert() and find() methods that depend on the attribute name ids and the order in which
14  *         insert() operations are invoked, see https://sourceforge.net/p/boost-log/discussion/710022/thread/e883db9a/.
15  */
16 
17 #define BOOST_TEST_MODULE attr_sets_insertion_lookup
18 
19 #include <string>
20 #include <sstream>
21 #include <boost/config.hpp>
22 #include <boost/test/unit_test.hpp>
23 #include <boost/log/attributes/constant.hpp>
24 #include <boost/log/attributes/attribute_set.hpp>
25 #include <boost/log/attributes/attribute_value_set.hpp>
26 
27 namespace logging = boost::log;
28 namespace attrs = logging::attributes;
29 
30 namespace {
31 
32 template< typename SetT, typename ValueT >
test_insertion_lookup(SetT & values,ValueT const & value)33 void test_insertion_lookup(SetT& values, ValueT const& value)
34 {
35     // Initialize attribute names. Each name will gain a consecutive id.
36     logging::attribute_name names[20];
37     for (unsigned int i = 0; i < sizeof(names) / sizeof(*names); ++i)
38     {
39         std::ostringstream strm;
40         strm << "Attr" << i;
41         names[i] = logging::attribute_name(strm.str());
42     }
43 
44     // Insert attribute values in this exact order so that different cases in the hash table implementation are tested.
45     values.insert(names[17], value);
46     values.insert(names[1], value);
47     values.insert(names[8], value);
48     values.insert(names[9], value);
49     values.insert(names[10], value);
50     values.insert(names[16], value);
51     values.insert(names[0], value);
52     values.insert(names[11], value);
53     values.insert(names[12], value);
54     values.insert(names[13], value);
55     values.insert(names[14], value);
56     values.insert(names[15], value);
57     values.insert(names[18], value);
58     values.insert(names[19], value);
59     values.insert(names[4], value);
60     values.insert(names[5], value);
61     values.insert(names[7], value);
62     values.insert(names[6], value);
63     values.insert(names[2], value);
64     values.insert(names[3], value);
65 
66     // Check that all values are accessible through iteration and find()
67     for (unsigned int i = 0; i < sizeof(names) / sizeof(*names); ++i)
68     {
69         BOOST_CHECK_MESSAGE(values.find(names[i]) != values.end(), "Attribute " << names[i] << " (id: " << names[i].id() << ") not found by find()");
70 
71         bool found_by_iteration = false;
72         for (typename SetT::const_iterator it = values.begin(), end = values.end(); it != end; ++it)
73         {
74             if (it->first == names[i])
75             {
76                 found_by_iteration = true;
77                 break;
78             }
79         }
80 
81         BOOST_CHECK_MESSAGE(found_by_iteration, "Attribute " << names[i] << " (id: " << names[i].id() << ") not found by iteration");
82     }
83 }
84 
85 } // namespace
86 
BOOST_AUTO_TEST_CASE(attributes)87 BOOST_AUTO_TEST_CASE(attributes)
88 {
89     logging::attribute_set values;
90     attrs::constant< int > attr(10);
91 
92     test_insertion_lookup(values, attr);
93 }
94 
BOOST_AUTO_TEST_CASE(attribute_values)95 BOOST_AUTO_TEST_CASE(attribute_values)
96 {
97     logging::attribute_value_set values;
98     attrs::constant< int > attr(10);
99 
100     test_insertion_lookup(values, attr.get_value());
101 }
102