• 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   util_manip_dump.cpp
9  * \author Andrey Semashev
10  * \date   09.01.2009
11  *
12  * \brief  This header contains tests for the dump manipulator.
13  */
14 
15 #define BOOST_TEST_MODULE util_manip_dump
16 
17 #include <vector>
18 #include <string>
19 #include <iomanip>
20 #include <sstream>
21 #include <algorithm>
22 #include <boost/test/unit_test.hpp>
23 #include <boost/log/utility/manipulators/dump.hpp>
24 #include "char_definitions.hpp"
25 
26 namespace logging = boost::log;
27 
28 // Test a short data region
BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_lowercase_short_dump,CharT,char_types)29 BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_lowercase_short_dump, CharT, char_types)
30 {
31     typedef CharT char_type;
32     typedef std::basic_ostringstream< char_type > ostream_type;
33 
34     std::vector< unsigned char > data;
35     data.push_back(1);
36     data.push_back(2);
37     data.push_back(3);
38     data.push_back(100);
39     data.push_back(110);
40     data.push_back(120);
41     data.push_back(200);
42     data.push_back(210);
43     data.push_back(220);
44 
45     ostream_type strm_dump;
46     strm_dump << logging::dump(&data[0], data.size());
47 
48     ostream_type strm_correct;
49     strm_correct << "01 02 03 64 6e 78 c8 d2 dc";
50 
51     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
52 }
53 
54 // Test a short data region with uppercase formatting
BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_uppercase_short_dump,CharT,char_types)55 BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_uppercase_short_dump, CharT, char_types)
56 {
57     typedef CharT char_type;
58     typedef std::basic_ostringstream< char_type > ostream_type;
59 
60     std::vector< unsigned char > data;
61     data.push_back(1);
62     data.push_back(2);
63     data.push_back(3);
64     data.push_back(100);
65     data.push_back(110);
66     data.push_back(120);
67     data.push_back(200);
68     data.push_back(210);
69     data.push_back(220);
70 
71     ostream_type strm_dump;
72     strm_dump << std::uppercase << logging::dump(&data[0], data.size());
73 
74     ostream_type strm_correct;
75     strm_correct << "01 02 03 64 6E 78 C8 D2 DC";
76 
77     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
78 }
79 
80 // Test void pointer handling
BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_pvoid_dump,CharT,char_types)81 BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_pvoid_dump, CharT, char_types)
82 {
83     typedef CharT char_type;
84     typedef std::basic_ostringstream< char_type > ostream_type;
85 
86     std::vector< unsigned char > data;
87     data.push_back(1);
88     data.push_back(2);
89     data.push_back(3);
90     data.push_back(100);
91     data.push_back(110);
92     data.push_back(120);
93     data.push_back(200);
94     data.push_back(210);
95     data.push_back(220);
96 
97     ostream_type strm_dump;
98     strm_dump << logging::dump((void*)&data[0], data.size());
99 
100     ostream_type strm_correct;
101     strm_correct << "01 02 03 64 6e 78 c8 d2 dc";
102 
103     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
104 }
105 
106 // Test a large data region
BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_large_dump,CharT,char_types)107 BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_large_dump, CharT, char_types)
108 {
109     typedef CharT char_type;
110     typedef std::basic_ostringstream< char_type > ostream_type;
111 
112     std::vector< unsigned char > data;
113     ostream_type strm_correct;
114     for (unsigned int i = 0; i < 1024; ++i)
115     {
116         unsigned char n = static_cast< unsigned char >(i);
117         data.push_back(n);
118         if (i > 0)
119             strm_correct << " ";
120         strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n);
121     }
122 
123     ostream_type strm_dump;
124     strm_dump << logging::dump(&data[0], data.size());
125 
126     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
127 }
128 
129 // Test SIMD tail handling
BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_tail_dump,CharT,char_types)130 BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_binary_tail_dump, CharT, char_types)
131 {
132     typedef CharT char_type;
133     typedef std::basic_ostringstream< char_type > ostream_type;
134 
135     std::vector< unsigned char > data;
136     ostream_type strm_correct;
137     // 1023 makes it very unlikely for the buffer to end at 16 or 32 byte boundary, which makes the dump algorithm to process the tail in a special way
138     for (unsigned int i = 0; i < 1023; ++i)
139     {
140         unsigned char n = static_cast< unsigned char >(i);
141         data.push_back(n);
142         if (i > 0)
143             strm_correct << " ";
144         strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n);
145     }
146 
147     ostream_type strm_dump;
148     strm_dump << logging::dump(&data[0], data.size());
149 
150     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
151 }
152 
153 // Test bounded dump
BOOST_AUTO_TEST_CASE_TEMPLATE(bounded_binary_dump,CharT,char_types)154 BOOST_AUTO_TEST_CASE_TEMPLATE(bounded_binary_dump, CharT, char_types)
155 {
156     typedef CharT char_type;
157     typedef std::basic_ostringstream< char_type > ostream_type;
158 
159     std::vector< unsigned char > data;
160     ostream_type strm_correct;
161     for (unsigned int i = 0; i < 1024; ++i)
162     {
163         unsigned char n = static_cast< unsigned char >(i);
164         data.push_back(n);
165 
166         if (i < 500)
167         {
168             if (i > 0)
169                 strm_correct << " ";
170             strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n);
171         }
172     }
173 
174     strm_correct << std::dec << std::setfill(static_cast< char_type >(' ')) << " and " << 524u << " bytes more";
175 
176     ostream_type strm_dump;
177     strm_dump << logging::dump(&data[0], data.size(), 500);
178 
179     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
180 }
181 
182 // Test array dump
BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_element_dump,CharT,char_types)183 BOOST_AUTO_TEST_CASE_TEMPLATE(unbounded_element_dump, CharT, char_types)
184 {
185     typedef CharT char_type;
186     typedef std::basic_ostringstream< char_type > ostream_type;
187 
188     std::vector< unsigned int > data;
189     data.push_back(0x01020a0b);
190     data.push_back(0x03040c0d);
191     data.push_back(0x05060e0f);
192 
193     ostream_type strm_dump;
194     strm_dump << logging::dump_elements(&data[0], data.size());
195 
196     ostream_type strm_correct;
197     const unsigned char* p = reinterpret_cast< const unsigned char* >(&data[0]);
198     std::size_t size = data.size() * sizeof(unsigned int);
199     for (unsigned int i = 0; i < size; ++i)
200     {
201         unsigned char n = p[i];
202         if (i > 0)
203             strm_correct << " ";
204         strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n);
205     }
206 
207     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
208 }
209 
210 // Test bounded array dump
BOOST_AUTO_TEST_CASE_TEMPLATE(bounded_element_dump,CharT,char_types)211 BOOST_AUTO_TEST_CASE_TEMPLATE(bounded_element_dump, CharT, char_types)
212 {
213     typedef CharT char_type;
214     typedef std::basic_ostringstream< char_type > ostream_type;
215 
216     std::vector< unsigned int > data;
217     data.push_back(0x01020a0b);
218     data.push_back(0x03040c0d);
219     data.push_back(0x05060e0f);
220 
221     ostream_type strm_dump;
222     strm_dump << logging::dump_elements(&data[0], data.size(), 2);
223 
224     ostream_type strm_correct;
225     const unsigned char* p = reinterpret_cast< const unsigned char* >(&data[0]);
226     std::size_t size = 2 * sizeof(unsigned int);
227     for (unsigned int i = 0; i < size; ++i)
228     {
229         unsigned char n = p[i];
230         if (i > 0)
231             strm_correct << " ";
232         strm_correct << std::hex << std::setw(2) << std::setfill(static_cast< char_type >('0')) << static_cast< unsigned int >(n);
233     }
234 
235     strm_correct << std::dec << std::setfill(static_cast< char_type >(' ')) << " and " << (data.size() - 2) * sizeof(unsigned int) << " bytes more";
236 
237     BOOST_CHECK(equal_strings(strm_dump.str(), strm_correct.str()));
238 }
239 
240