]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
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_range.cpp | |
9 | * \author Andrey Semashev | |
10 | * \date 11.05.2020 | |
11 | * | |
12 | * \brief This header contains tests for the range manipulator. | |
13 | */ | |
14 | ||
15 | #define BOOST_TEST_MODULE util_manip_range | |
16 | ||
17 | #include <list> | |
18 | #include <vector> | |
19 | #include <string> | |
20 | #include <sstream> | |
21 | #include <boost/config.hpp> | |
22 | #include <boost/test/unit_test.hpp> | |
23 | #include <boost/log/utility/manipulators/range.hpp> | |
24 | #include "char_definitions.hpp" | |
25 | ||
26 | namespace logging = boost::log; | |
27 | ||
28 | struct my_int_wrapper | |
29 | { | |
30 | int n; | |
31 | ||
32 | my_int_wrapper(int x = 0) BOOST_NOEXCEPT : n(x) {} | |
33 | }; | |
34 | ||
35 | template< typename CharT, typename TraitsT > | |
36 | inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, my_int_wrapper miw) | |
37 | { | |
38 | if (BOOST_LIKELY(strm.good())) | |
39 | strm << '{' << miw.n << '}'; | |
40 | ||
41 | return strm; | |
42 | } | |
43 | ||
44 | BOOST_AUTO_TEST_CASE_TEMPLATE(int_no_separator, CharT, char_types) | |
45 | { | |
46 | typedef CharT char_type; | |
47 | typedef std::basic_ostringstream< char_type > ostream_type; | |
48 | ||
49 | std::vector< int > data; | |
50 | data.push_back(1); | |
51 | data.push_back(2); | |
52 | data.push_back(3); | |
53 | ||
54 | ostream_type strm_dump; | |
55 | strm_dump << logging::range_manip(data); | |
56 | ||
57 | ostream_type strm_correct; | |
58 | strm_correct << "123"; | |
59 | ||
60 | BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); | |
61 | } | |
62 | ||
63 | BOOST_AUTO_TEST_CASE_TEMPLATE(int_char_separator, CharT, char_types) | |
64 | { | |
65 | typedef CharT char_type; | |
66 | typedef std::basic_ostringstream< char_type > ostream_type; | |
67 | ||
68 | std::vector< int > data; | |
69 | data.push_back(1); | |
70 | data.push_back(2); | |
71 | data.push_back(3); | |
72 | ||
73 | ostream_type strm_dump; | |
74 | strm_dump << logging::range_manip(data, ' '); | |
75 | ||
76 | ostream_type strm_correct; | |
77 | strm_correct << "1 2 3"; | |
78 | ||
79 | BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); | |
80 | } | |
81 | ||
82 | BOOST_AUTO_TEST_CASE_TEMPLATE(int_string_separator, CharT, char_types) | |
83 | { | |
84 | typedef CharT char_type; | |
85 | typedef std::basic_ostringstream< char_type > ostream_type; | |
86 | ||
87 | std::vector< int > data; | |
88 | data.push_back(1); | |
89 | data.push_back(2); | |
90 | data.push_back(3); | |
91 | ||
92 | ostream_type strm_dump; | |
93 | strm_dump << logging::range_manip(data, ", "); | |
94 | ||
95 | ostream_type strm_correct; | |
96 | strm_correct << "1, 2, 3"; | |
97 | ||
98 | BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); | |
99 | } | |
100 | ||
101 | BOOST_AUTO_TEST_CASE_TEMPLATE(int_std_string_separator, CharT, char_types) | |
102 | { | |
103 | typedef CharT char_type; | |
104 | typedef std::basic_ostringstream< char_type > ostream_type; | |
105 | typedef std::basic_string< char_type > string_type; | |
106 | ||
107 | std::vector< int > data; | |
108 | data.push_back(1); | |
109 | data.push_back(2); | |
110 | data.push_back(3); | |
111 | ||
112 | string_type separator; | |
113 | separator.push_back(','); | |
114 | separator.push_back(' '); | |
115 | ||
116 | ostream_type strm_dump; | |
117 | strm_dump << logging::range_manip(data, separator); | |
118 | ||
119 | ostream_type strm_correct; | |
120 | strm_correct << "1, 2, 3"; | |
121 | ||
122 | BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); | |
123 | } | |
124 | ||
125 | BOOST_AUTO_TEST_CASE_TEMPLATE(miw_string_separator, CharT, char_types) | |
126 | { | |
127 | typedef CharT char_type; | |
128 | typedef std::basic_ostringstream< char_type > ostream_type; | |
129 | ||
130 | std::list< my_int_wrapper > data; | |
131 | data.push_back(my_int_wrapper(1)); | |
132 | data.push_back(my_int_wrapper(2)); | |
133 | data.push_back(my_int_wrapper(3)); | |
134 | ||
135 | ostream_type strm_dump; | |
136 | strm_dump << logging::range_manip(data, ", "); | |
137 | ||
138 | ostream_type strm_correct; | |
139 | strm_correct << "{1}, {2}, {3}"; | |
140 | ||
141 | BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); | |
142 | } | |
143 | ||
144 | BOOST_AUTO_TEST_CASE_TEMPLATE(array_int_string_separator, CharT, char_types) | |
145 | { | |
146 | typedef CharT char_type; | |
147 | typedef std::basic_ostringstream< char_type > ostream_type; | |
148 | ||
149 | int data[3] = { 1, 2, 3 }; | |
150 | ||
151 | ostream_type strm_dump; | |
152 | strm_dump << logging::range_manip(data, ", "); | |
153 | ||
154 | ostream_type strm_correct; | |
155 | strm_correct << "1, 2, 3"; | |
156 | ||
157 | BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str())); | |
158 | } |