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