1 | // Boost.Convert test and usage example |
2 | // Copyright (c) 2022 Dvir Yitzchaki. |
3 | // Use, modification and distribution are subject to the Boost Software License, |
4 | // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. |
5 | |
6 | #include "./test.hpp" |
7 | |
8 | #if !defined(BOOST_CONVERT_CXX14) || defined(BOOST_NO_CXX17_HDR_CHARCONV) |
9 | int main(int, char const* []) { return 0; } |
10 | #else |
11 | |
12 | #include <boost/convert.hpp> |
13 | #include <boost/convert/charconv.hpp> |
14 | #include <cstdio> |
15 | #include <cstdlib> |
16 | #include <stdlib.h> |
17 | |
18 | #include <boost/convert.hpp> |
19 | #include <boost/convert/printf.hpp> |
20 | #include <boost/convert/stream.hpp> |
21 | #include <boost/random/mersenne_twister.hpp> |
22 | #include <boost/random/uniform_int_distribution.hpp> |
23 | #include <boost/random/uniform_01.hpp> |
24 | #include <boost/lexical_cast.hpp> |
25 | #include <boost/utility/string_view.hpp> |
26 | |
27 | //[charconv_basic_deployment_header |
28 | #include <boost/convert.hpp> |
29 | #include <boost/convert/charconv.hpp> |
30 | |
31 | using std::string; |
32 | using boost::convert; |
33 | |
34 | namespace cnv = boost::cnv; |
35 | namespace arg = boost::cnv::parameter; |
36 | |
37 | struct cnv::by_default : boost::cnv::charconv {}; |
38 | //] |
39 | |
40 | static |
41 | void |
42 | test_str_to_uint() |
43 | { |
44 | string const bad_str = "not an int" ; |
45 | string const neg_str = "-11" ; |
46 | string const std_str = "11" ; |
47 | char const* const c_str = "12" ; |
48 | boost::string_view v_str = boost::string_view(c_str, 1); |
49 | unsigned int const imax = (std::numeric_limits<unsigned int>::max)(); |
50 | unsigned long int const lmax = (std::numeric_limits<unsigned long int>::max)(); |
51 | std::string const imax_str = boost::lexical_cast<std::string>(imax); |
52 | std::string const lmax_str = boost::lexical_cast<std::string>(lmax); |
53 | |
54 | BOOST_TEST( 0 == convert<unsigned int>(bad_str).value_or(0)); |
55 | BOOST_TEST( 0 == convert<unsigned int>(neg_str).value_or(0)); |
56 | BOOST_TEST( 0 == convert<unsigned long int>(neg_str).value_or(0)); |
57 | BOOST_TEST( 0 == convert<unsigned long int>(neg_str).value_or(0)); |
58 | BOOST_TEST(11 == convert<unsigned int>(std_str).value()); |
59 | BOOST_TEST(12 == convert<unsigned int>( c_str).value()); |
60 | BOOST_TEST( 1 == convert<unsigned int>( v_str).value_or(0)); |
61 | BOOST_TEST(11 == convert<unsigned long int>(std_str).value()); |
62 | BOOST_TEST(12 == convert<unsigned long int>( c_str).value()); |
63 | BOOST_TEST(imax == convert< unsigned int>(imax_str).value()); |
64 | BOOST_TEST(lmax == convert<unsigned long int>(lmax_str).value()); |
65 | } |
66 | |
67 | static |
68 | void |
69 | test_str_to_int() |
70 | { |
71 | //[charconv_basic_deployment |
72 | string const bad_str = "not an int" ; |
73 | string const std_str = "-11" ; |
74 | char const* const c_str = "-12" ; |
75 | boost::string_view v_str = boost::string_view(c_str, 2); |
76 | |
77 | BOOST_TEST( -1 == convert<int>(bad_str).value_or(-1)); |
78 | BOOST_TEST(-11 == convert<int>(std_str).value()); |
79 | BOOST_TEST(-12 == convert<int>( c_str).value()); |
80 | BOOST_TEST( -1 == convert<int>( v_str).value_or(0)); |
81 | //] |
82 | } |
83 | |
84 | static |
85 | void |
86 | test_int_to_str() |
87 | { |
88 | short const s_int = -123; |
89 | int const i_int = -123; |
90 | long const l_int = -123; |
91 | long long const ll_int = -123; |
92 | |
93 | BOOST_TEST( "-123" == convert< std::string> ( s_int).value()); |
94 | BOOST_TEST( "-123" == convert< std::string> ( i_int).value()); |
95 | BOOST_TEST( "-123" == convert< std::string> ( l_int).value()); |
96 | BOOST_TEST( "-123" == convert< std::string> (ll_int).value()); |
97 | |
98 | int const imin = (std::numeric_limits<int>::min)(); |
99 | int const imax = (std::numeric_limits<int>::max)(); |
100 | long int const lmin = (std::numeric_limits<long int>::min)(); |
101 | long int const lmax = (std::numeric_limits<long int>::max)(); |
102 | long long int const llmin = (std::numeric_limits<long long int>::min)(); |
103 | long long int const llmax = (std::numeric_limits<long long int>::max)(); |
104 | |
105 | std::string const imin_str = boost::lexical_cast<std::string>(imin); |
106 | std::string const imax_str = boost::lexical_cast<std::string>(imax); |
107 | std::string const lmin_str = boost::lexical_cast<std::string>(lmin); |
108 | std::string const lmax_str = boost::lexical_cast<std::string>(lmax); |
109 | std::string const llmin_str = boost::lexical_cast<std::string>(llmin); |
110 | std::string const llmax_str = boost::lexical_cast<std::string>(llmax); |
111 | |
112 | BOOST_TEST( imin_str == convert<std::string>( imin).value()); |
113 | BOOST_TEST( imax_str == convert<std::string>( imax).value()); |
114 | BOOST_TEST( lmin_str == convert<std::string>( lmin).value()); |
115 | BOOST_TEST( lmax_str == convert<std::string>( lmax).value()); |
116 | BOOST_TEST(llmin_str == convert<std::string>(llmin).value()); |
117 | BOOST_TEST(llmax_str == convert<std::string>(llmax).value()); |
118 | } |
119 | |
120 | static |
121 | void |
122 | test_uint_to_str() |
123 | { |
124 | unsigned short const us_int = 123; |
125 | unsigned int const ui_int = 123; |
126 | unsigned long const ul_int = 123; |
127 | unsigned long long const ull_int = 123; |
128 | |
129 | BOOST_TEST( "123" == convert< std::string> ( us_int).value()); |
130 | BOOST_TEST( "123" == convert< std::string> ( ui_int).value()); |
131 | BOOST_TEST( "123" == convert< std::string> ( ul_int).value()); |
132 | BOOST_TEST( "123" == convert< std::string> (ull_int).value()); |
133 | |
134 | unsigned int const uimax = (std::numeric_limits<unsigned int>::max)(); |
135 | unsigned long int const ulmax = (std::numeric_limits<unsigned long int>::max)(); |
136 | unsigned long long int const ullmax = (std::numeric_limits<unsigned long long int>::max)(); |
137 | |
138 | std::string const uimax_str = boost::lexical_cast<std::string>( uimax); |
139 | std::string const ulmax_str = boost::lexical_cast<std::string>( ulmax); |
140 | std::string const ullmax_str = boost::lexical_cast<std::string>(ullmax); |
141 | |
142 | BOOST_TEST( uimax_str == convert<std::string>( uimax).value()); |
143 | BOOST_TEST( ulmax_str == convert<std::string>( ulmax).value()); |
144 | BOOST_TEST(ullmax_str == convert<std::string>(ullmax).value()); |
145 | } |
146 | |
147 | //] |
148 | static |
149 | void |
150 | test_width() |
151 | { |
152 | //[charconv_width |
153 | boost::cnv::charconv cnv; |
154 | |
155 | string s01 = convert<string>( 12, cnv(arg::width = 4)).value(); |
156 | string s02 = convert<string>( 12, cnv(arg::width = 5) |
157 | (arg::fill = '*')).value(); |
158 | string s03 = convert<string>( 12, cnv(arg::width = 5) |
159 | (arg::fill = 'x') |
160 | (arg::adjust = cnv::adjust::left)).value(); |
161 | string s04 = convert<string>(-98, cnv(arg::width = 6) |
162 | (arg::fill = 'Z') |
163 | (arg::adjust = cnv::adjust::right)).value(); |
164 | |
165 | string s05 = convert<string>(-12.3451, cnv(arg::precision = 2) |
166 | (arg::width = 10) |
167 | (arg::fill = '*') |
168 | (arg::adjust = cnv::adjust::left)).value(); |
169 | string s06 = convert<string>(-12.3450, cnv(arg::adjust = cnv::adjust::right)).value(); |
170 | string s07 = convert<string>(-12.3450, cnv(arg::adjust = cnv::adjust::center)).value(); |
171 | |
172 | BOOST_TEST(s01 == " 12" ); |
173 | BOOST_TEST(s02 == "***12" ); |
174 | BOOST_TEST(s03 == "12xxx" ); |
175 | BOOST_TEST(s04 == "ZZZ-98" ); |
176 | BOOST_TEST(s05 == "-12.35****" ); |
177 | BOOST_TEST(s06 == "****-12.35" ); |
178 | BOOST_TEST(s07 == "**-12.35**" ); |
179 | //] |
180 | } |
181 | |
182 | static |
183 | void |
184 | test_base() |
185 | { |
186 | //[charconv_numeric_base |
187 | boost::cnv::charconv cnv; |
188 | |
189 | BOOST_TEST( "11111110" == convert< string>(254, cnv(arg::base = cnv::base::bin)).value()); |
190 | BOOST_TEST( "254" == convert< string>(254, cnv(arg::base = cnv::base::dec)).value()); |
191 | BOOST_TEST( "fe" == convert< string>(254, cnv(arg::base = cnv::base::hex)).value()); |
192 | BOOST_TEST( "376" == convert< string>(254, cnv(arg::base = cnv::base::oct)).value()); |
193 | //] |
194 | } |
195 | |
196 | static |
197 | void |
198 | test_skipws() |
199 | { |
200 | //[charconv_skipws |
201 | boost::cnv::charconv cnv; |
202 | |
203 | BOOST_TEST(-1 == convert<int>( " 12" , cnv(arg::skipws = false)).value_or(-1)); |
204 | BOOST_TEST(12 == convert<int>( " 12" , cnv(arg::skipws = true)).value_or(-1)); |
205 | //] |
206 | } |
207 | |
208 | static |
209 | void |
210 | test_notation() |
211 | { |
212 | //[charconv_notation |
213 | boost::cnv::charconv cnv; |
214 | |
215 | BOOST_TEST( "-3.14159" == convert<string>(-3.14159, cnv(arg::notation = cnv::notation::fixed)(arg::precision = 5)).value()); |
216 | BOOST_TEST("-3.142e+00" == convert<string>(-3.14159, cnv(arg::notation = cnv::notation::scientific)(arg::precision = 3)).value()); |
217 | BOOST_TEST("-1.9220p+1" == convert<string>(-3.14159, cnv(arg::notation = cnv::notation::hex)(arg::precision = 4)).value()); |
218 | //] |
219 | } |
220 | |
221 | static |
222 | void |
223 | dbl_to_str_example() |
224 | { |
225 | //[charconv_precision |
226 | boost::cnv::charconv cnv; |
227 | |
228 | BOOST_TEST( "12.3" == convert<string>(12.3456, cnv(arg::precision = 1)).value()); |
229 | BOOST_TEST( "12.35" == convert<string>(12.3456, cnv(arg::precision = 2)).value()); |
230 | BOOST_TEST("12.346" == convert<string>(12.3456, cnv(arg::precision = 3)).value()); |
231 | |
232 | BOOST_TEST( "-12.3" == convert<string>(-12.3456, cnv(arg::precision = 1)).value()); |
233 | BOOST_TEST( "-12.35" == convert<string>(-12.3456, cnv(arg::precision = 2)).value()); |
234 | BOOST_TEST("-12.346" == convert<string>(-12.3456, cnv(arg::precision = 3)).value()); |
235 | //] |
236 | } |
237 | |
238 | static |
239 | std::pair<double, int> |
240 | get_random() |
241 | { |
242 | namespace rdm = boost::random; |
243 | |
244 | static rdm::mt19937 gen (::time(0)); |
245 | static rdm::uniform_int_distribution<> precision (0, 6); |
246 | static rdm::uniform_int_distribution<> int_part (0, SHRT_MAX); |
247 | static rdm::uniform_01<double> fraction; // uniform double in [0,1) |
248 | static bool sign; |
249 | |
250 | double dbl = (int_part(gen) + fraction(gen)) * ((sign = !sign) ? 1 : -1); |
251 | |
252 | // printf("%.12f\n", dbl); |
253 | |
254 | return std::make_pair(dbl, precision(gen)); |
255 | } |
256 | |
257 | static |
258 | void |
259 | compare(std::pair<double, int> pair) |
260 | { |
261 | boost::cnv::charconv cnv1; |
262 | boost::cnv::printf cnv2; |
263 | |
264 | string s1 = convert<string>(pair.first, cnv1(arg::precision = pair.second)).value(); |
265 | string s2 = convert<string>(pair.first, cnv2(arg::precision = pair.second)).value(); |
266 | |
267 | if (s1 != s2) |
268 | printf("dbl=%.12f(%d).charconv/printf=%s/%s.\n" , pair.first, pair.second, s1.c_str(), s2.c_str()); |
269 | } |
270 | |
271 | static |
272 | void |
273 | test_str_to_dbl() |
274 | { |
275 | char const* const c_str = "1.23456" ; |
276 | |
277 | BOOST_TEST(1.2 == convert<double>(boost::string_view(c_str, 3)).value_or(0)); |
278 | BOOST_TEST(1.23 == convert<double>(boost::string_view(c_str, 4)).value_or(0)); |
279 | } |
280 | |
281 | static |
282 | void |
283 | test_dbl_to_str() |
284 | { |
285 | // double round_up_abs01 = ::rint(-0.5); |
286 | // double round_up_abs02 = ::round(-0.5); |
287 | // double round_up_abs11 = ::rint(0.5); |
288 | // double round_up_abs12 = ::round(0.5); |
289 | |
290 | // double huge_v = 987654321098765432109.123; |
291 | // |
292 | // printf("%f\n", huge_v); |
293 | // string huge = convert<string>(huge_v, cnv1(arg::precision = 2)).value(); |
294 | // printf("%s\n", huge.c_str()); |
295 | |
296 | int const num_tries = 1000000; |
297 | double const dbls[] = { 0.90, 1.0, 1.1, 0.94, 0.96, 1.04, 1.05, 1.06, 9.654, 999.888 }; |
298 | int const num_dbls = sizeof(dbls) / sizeof(dbls[0]); |
299 | |
300 | printf("cnv::charconv::%s: started with %d random numbers...\n" , __FUNCTION__, num_tries); |
301 | |
302 | BOOST_TEST( "0" == convert<string>( 0.0, cnv::charconv()(arg::precision = 0)).value()); |
303 | BOOST_TEST( "0.0" == convert<string>( 0.0, cnv::charconv()(arg::precision = 1)).value()); |
304 | BOOST_TEST("0.00" == convert<string>( 0.0, cnv::charconv()(arg::precision = 2)).value()); |
305 | BOOST_TEST( "1" == convert<string>(0.95, cnv::charconv()(arg::precision = 0)).value()); |
306 | BOOST_TEST( "0.9" == convert<string>(0.95, cnv::charconv()(arg::precision = 1)).value()); |
307 | BOOST_TEST("0.95" == convert<string>(0.95, cnv::charconv()(arg::precision = 2)).value()); |
308 | |
309 | for (int k = 0; k < num_tries; ++k) |
310 | compare(get_random()); |
311 | |
312 | for (int k = 0; k < num_dbls; ++k) |
313 | for (int precision = 0; precision < 3; ++precision) |
314 | { |
315 | compare(std::make_pair( dbls[k], precision)); |
316 | compare(std::make_pair(-dbls[k], precision)); |
317 | } |
318 | |
319 | printf("cnv::charconv::%s: finished.\n" , __FUNCTION__); |
320 | } |
321 | |
322 | static |
323 | void |
324 | test_user_string() |
325 | { |
326 | //[charconv_user_string |
327 | boost::cnv::charconv cnv; |
328 | |
329 | BOOST_TEST( "12" == convert<my_string>(12, cnv).value()); |
330 | BOOST_TEST("0.95" == convert<my_string>(0.95, cnv(arg::precision = 2)).value()); |
331 | //] |
332 | } |
333 | |
334 | static |
335 | void |
336 | test_user_type() |
337 | { |
338 | //[charconv_user_type |
339 | boost::cnv::charconv cnv; |
340 | change up_chg = change::up; |
341 | change dn_chg = change::dn; |
342 | |
343 | BOOST_TEST(convert<std::string>(up_chg, cnv, "bad" ) == "up" ); |
344 | BOOST_TEST(convert<std::string>(dn_chg, cnv, "bad" ) == "dn" ); |
345 | BOOST_TEST(convert<std::string>( 12, cnv, "bad" ) == "12" ); |
346 | |
347 | BOOST_TEST(convert<change>("up" , cnv, change::no) == change::up); |
348 | BOOST_TEST(convert<change>("dn" , cnv, change::no) == change::dn); |
349 | BOOST_TEST(convert< int>("12" , cnv, -1) == 12); |
350 | //] |
351 | } |
352 | |
353 | int |
354 | main(int, char const* []) |
355 | { |
356 | dbl_to_str_example(); |
357 | |
358 | test_str_to_int(); |
359 | test_str_to_uint(); |
360 | test_int_to_str(); |
361 | test_uint_to_str(); |
362 | test_base(); |
363 | test_skipws(); |
364 | test_notation(); |
365 | test_str_to_dbl(); |
366 | test_dbl_to_str(); |
367 | test_width(); |
368 | test_user_string(); |
369 | test_user_type(); |
370 | |
371 | return boost::report_errors(); |
372 | } |
373 | |
374 | #endif |
375 | |