1 /*
2 * Copyright Andrey Semashev 2020.
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 util_manip_tuple.cpp
9 * \author Andrey Semashev
10 * \date 11.05.2020
11 *
12 * \brief This header contains tests for the tuple manipulator.
13 */
14
15 #define BOOST_TEST_MODULE util_manip_tuple
16
17 #include <string>
18 #include <sstream>
19 #include <utility>
20 #include <boost/config.hpp>
21 #include <boost/tuple/tuple.hpp>
22 #include <boost/test/unit_test.hpp>
23 #include <boost/fusion/include/std_pair.hpp>
24 #include <boost/fusion/include/boost_tuple.hpp>
25 #include <boost/log/utility/manipulators/tuple.hpp>
26 #include "char_definitions.hpp"
27
28 namespace logging = boost::log;
29
30 struct my_int_wrapper
31 {
32 int n;
33
my_int_wrappermy_int_wrapper34 my_int_wrapper(int x = 0) BOOST_NOEXCEPT : n(x) {}
35 };
36
37 template< typename CharT, typename TraitsT >
operator <<(std::basic_ostream<CharT,TraitsT> & strm,my_int_wrapper miw)38 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, my_int_wrapper miw)
39 {
40 if (BOOST_LIKELY(strm.good()))
41 strm << '{' << miw.n << '}';
42
43 return strm;
44 }
45
BOOST_AUTO_TEST_CASE_TEMPLATE(pair_no_separator,CharT,char_types)46 BOOST_AUTO_TEST_CASE_TEMPLATE(pair_no_separator, CharT, char_types)
47 {
48 typedef CharT char_type;
49 typedef test_data< char_type > test_data_type;
50 typedef std::basic_ostringstream< char_type > ostream_type;
51 typedef std::basic_string< char_type > string_type;
52
53 std::pair< int, string_type > data(1, test_data_type::abc());
54
55 ostream_type strm_dump;
56 strm_dump << logging::tuple_manip(data);
57
58 ostream_type strm_correct;
59 strm_correct << "1abc";
60
61 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
62 }
63
BOOST_AUTO_TEST_CASE_TEMPLATE(tuple_char_separator,CharT,char_types)64 BOOST_AUTO_TEST_CASE_TEMPLATE(tuple_char_separator, CharT, char_types)
65 {
66 typedef CharT char_type;
67 typedef test_data< char_type > test_data_type;
68 typedef std::basic_ostringstream< char_type > ostream_type;
69 typedef std::basic_string< char_type > string_type;
70
71 boost::tuple< int, my_int_wrapper, string_type > data(1, my_int_wrapper(5), string_type(test_data_type::abc()));
72
73 ostream_type strm_dump;
74 strm_dump << logging::tuple_manip(data, ' ');
75
76 ostream_type strm_correct;
77 strm_correct << "1 {5} abc";
78
79 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
80 }
81
BOOST_AUTO_TEST_CASE_TEMPLATE(tuple_string_separator,CharT,char_types)82 BOOST_AUTO_TEST_CASE_TEMPLATE(tuple_string_separator, CharT, char_types)
83 {
84 typedef CharT char_type;
85 typedef test_data< char_type > test_data_type;
86 typedef std::basic_ostringstream< char_type > ostream_type;
87 typedef std::basic_string< char_type > string_type;
88
89 boost::tuple< int, my_int_wrapper, string_type > data(1, my_int_wrapper(5), string_type(test_data_type::abc()));
90
91 ostream_type strm_dump;
92 strm_dump << logging::tuple_manip(data, ", ");
93
94 ostream_type strm_correct;
95 strm_correct << "1, {5}, abc";
96
97 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
98 }
99
BOOST_AUTO_TEST_CASE_TEMPLATE(tuple_std_string_separator,CharT,char_types)100 BOOST_AUTO_TEST_CASE_TEMPLATE(tuple_std_string_separator, CharT, char_types)
101 {
102 typedef CharT char_type;
103 typedef test_data< char_type > test_data_type;
104 typedef std::basic_ostringstream< char_type > ostream_type;
105 typedef std::basic_string< char_type > string_type;
106
107 boost::tuple< int, my_int_wrapper, string_type > data(1, my_int_wrapper(5), string_type(test_data_type::abc()));
108
109 string_type separator;
110 separator.push_back(',');
111 separator.push_back(' ');
112
113 ostream_type strm_dump;
114 strm_dump << logging::tuple_manip(data, separator);
115
116 ostream_type strm_correct;
117 strm_correct << "1, {5}, abc";
118
119 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
120 }
121
BOOST_AUTO_TEST_CASE_TEMPLATE(tie_string_separator,CharT,char_types)122 BOOST_AUTO_TEST_CASE_TEMPLATE(tie_string_separator, CharT, char_types)
123 {
124 typedef CharT char_type;
125 typedef test_data< char_type > test_data_type;
126 typedef std::basic_ostringstream< char_type > ostream_type;
127 typedef std::basic_string< char_type > string_type;
128
129 int x = 1;
130 my_int_wrapper y(5);
131 string_type z(test_data_type::abc());
132
133 ostream_type strm_dump;
134 strm_dump << logging::tuple_manip(boost::tie(x, y, z), ", ");
135
136 ostream_type strm_correct;
137 strm_correct << "1, {5}, abc";
138
139 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
140 }
141