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