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_optional.cpp
9 * \author Andrey Semashev
10 * \date 12.05.2020
11 *
12 * \brief This header contains tests for the optional manipulator.
13 */
14
15 #define BOOST_TEST_MODULE util_manip_optional
16
17 #include <string>
18 #include <sstream>
19 #include <boost/config.hpp>
20 #include <boost/optional.hpp>
21 #include <boost/test/unit_test.hpp>
22 #include <boost/log/utility/manipulators/optional.hpp>
23 #include "char_definitions.hpp"
24
25 namespace logging = boost::log;
26
27 struct my_int_wrapper
28 {
29 int n;
30
my_int_wrappermy_int_wrapper31 my_int_wrapper(int x = 0) BOOST_NOEXCEPT : n(x) {}
32 };
33
34 template< typename CharT, typename TraitsT >
operator <<(std::basic_ostream<CharT,TraitsT> & strm,my_int_wrapper miw)35 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, my_int_wrapper miw)
36 {
37 if (BOOST_LIKELY(strm.good()))
38 strm << '{' << miw.n << '}';
39
40 return strm;
41 }
42
BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_no_marker,CharT,char_types)43 BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_no_marker, CharT, char_types)
44 {
45 typedef CharT char_type;
46 typedef std::basic_ostringstream< char_type > ostream_type;
47 typedef std::basic_string< char_type > string_type;
48
49 boost::optional< int > data;
50
51 ostream_type strm_dump;
52 strm_dump << logging::optional_manip(data);
53
54 BOOST_CHECK(equal_strings(strm_dump.str(), string_type()));
55 }
56
BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_with_marker_char,CharT,char_types)57 BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_with_marker_char, CharT, char_types)
58 {
59 typedef CharT char_type;
60 typedef std::basic_ostringstream< char_type > ostream_type;
61
62 boost::optional< int > data;
63
64 ostream_type strm_dump;
65 strm_dump << logging::optional_manip(data, '-');
66
67 ostream_type strm_correct;
68 strm_correct << "-";
69
70 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
71 }
72
BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_with_marker_string,CharT,char_types)73 BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_with_marker_string, CharT, char_types)
74 {
75 typedef CharT char_type;
76 typedef std::basic_ostringstream< char_type > ostream_type;
77
78 boost::optional< int > data;
79
80 ostream_type strm_dump;
81 strm_dump << logging::optional_manip(data, "[none]");
82
83 ostream_type strm_correct;
84 strm_correct << "[none]";
85
86 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
87 }
88
BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_with_marker_std_string,CharT,char_types)89 BOOST_AUTO_TEST_CASE_TEMPLATE(opt_none_with_marker_std_string, CharT, char_types)
90 {
91 typedef CharT char_type;
92 typedef std::basic_ostringstream< char_type > ostream_type;
93 typedef std::basic_string< char_type > string_type;
94
95 boost::optional< int > data;
96
97 string_type marker;
98 marker.push_back('[');
99 marker.push_back('n');
100 marker.push_back('o');
101 marker.push_back('n');
102 marker.push_back('e');
103 marker.push_back(']');
104
105 ostream_type strm_dump;
106 strm_dump << logging::optional_manip(data, marker);
107
108 ostream_type strm_correct;
109 strm_correct << "[none]";
110
111 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
112 }
113
BOOST_AUTO_TEST_CASE_TEMPLATE(ptr_none_with_marker_string,CharT,char_types)114 BOOST_AUTO_TEST_CASE_TEMPLATE(ptr_none_with_marker_string, CharT, char_types)
115 {
116 typedef CharT char_type;
117 typedef std::basic_ostringstream< char_type > ostream_type;
118
119 const int* data = 0;
120
121 ostream_type strm_dump;
122 strm_dump << logging::optional_manip(data, "[none]");
123
124 ostream_type strm_correct;
125 strm_correct << "[none]";
126
127 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
128 }
129
BOOST_AUTO_TEST_CASE_TEMPLATE(opt_int_with_marker_string,CharT,char_types)130 BOOST_AUTO_TEST_CASE_TEMPLATE(opt_int_with_marker_string, CharT, char_types)
131 {
132 typedef CharT char_type;
133 typedef std::basic_ostringstream< char_type > ostream_type;
134
135 boost::optional< int > data;
136 data = 7;
137
138 ostream_type strm_dump;
139 strm_dump << logging::optional_manip(data, "[none]");
140
141 ostream_type strm_correct;
142 strm_correct << "7";
143
144 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
145 }
146
BOOST_AUTO_TEST_CASE_TEMPLATE(ptr_int_with_marker_string,CharT,char_types)147 BOOST_AUTO_TEST_CASE_TEMPLATE(ptr_int_with_marker_string, CharT, char_types)
148 {
149 typedef CharT char_type;
150 typedef std::basic_ostringstream< char_type > ostream_type;
151
152 int n = 7;
153 int* data = &n;
154
155 ostream_type strm_dump;
156 strm_dump << logging::optional_manip(data, "[none]");
157
158 ostream_type strm_correct;
159 strm_correct << "7";
160
161 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
162 }
163
BOOST_AUTO_TEST_CASE_TEMPLATE(ptr_miw_with_marker_string,CharT,char_types)164 BOOST_AUTO_TEST_CASE_TEMPLATE(ptr_miw_with_marker_string, CharT, char_types)
165 {
166 typedef CharT char_type;
167 typedef std::basic_ostringstream< char_type > ostream_type;
168
169 my_int_wrapper n(7);
170 const my_int_wrapper* data = &n;
171
172 ostream_type strm_dump;
173 strm_dump << logging::optional_manip(data, "[none]");
174
175 ostream_type strm_correct;
176 strm_correct << "{7}";
177
178 BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
179 }
180