// Boost.Convert test and usage example // Copyright (c) 2009-2016 Vladimir Batov. // Use, modification and distribution are subject to the Boost Software License, // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. #include "./test.hpp" #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED) int main(int, char const* []) { return 0; } #else #include #include #include #include using std::string; using std::wstring; using boost::convert; namespace cnv = boost::cnv; namespace arg = boost::cnv::parameter; struct boost::cnv::by_default : boost::cnv::spirit {}; int main(int, char const* []) { char const* const c_stri ("12345"); char const* const c_strd ("123.45"); wchar_t const* const c_wstri (L"12345"); wchar_t const* const c_wstrd (L"123.45"); std::string const std_stri (c_stri); std::string const std_strd (c_strd); std::wstring const std_wstri (c_wstri); std::wstring const std_wstrd (c_wstrd); my_string const my_stri (c_stri, c_stri + strlen(c_stri)); my_string const my_strd (c_strd, c_strd + strlen(c_strd)); boost::cnv::spirit cnv; BOOST_TEST( 12345 == convert< int>( c_stri).value()); BOOST_TEST( 12345 == convert< int>( c_wstri).value()); BOOST_TEST( 12345 == convert< int>( std_stri).value()); BOOST_TEST( 12345 == convert< int>(std_wstri).value()); BOOST_TEST( 12345 == convert< int>( my_stri).value()); BOOST_TEST( 12345 == convert(c_stri).value()); BOOST_TEST( 12345 == convert( c_stri).value()); BOOST_TEST( 12345 == convert( c_wstri).value()); BOOST_TEST( 12345 == convert( std_stri).value()); BOOST_TEST( 12345 == convert(std_wstri).value()); BOOST_TEST( 12345 == convert( my_stri).value()); BOOST_TEST( 12345 == convert(c_stri).value()); BOOST_TEST( 12345 == convert(c_stri).value()); BOOST_TEST( 12345 == convert(c_stri).value()); BOOST_TEST(123.45 == convert< double>( c_strd).value()); BOOST_TEST(123.45 == convert< double>( c_wstrd).value()); // BOOST_TEST(123.45 == convert< double>( std_strd).value()); // BOOST_TEST(123.45 == convert< double>(std_wstrd).value()); BOOST_TEST(123.45 == convert< double>( my_strd).value()); BOOST_TEST(!convert< int>("uhm")); BOOST_TEST(!convert< int>(L"uhm")); BOOST_TEST(!convert("12.uhm")); BOOST_TEST(!convert("L12.uhm")); BOOST_TEST( "1234" == convert(1234).value()); BOOST_TEST( "1234" == convert(1234u).value()); BOOST_TEST( "1234" == convert(1234ll).value()); BOOST_TEST( "1234" == convert(1234ull).value()); BOOST_TEST( L"1234" == convert(1234).value()); BOOST_TEST( "12xxx" == convert(12, cnv(arg::width = 5) (arg::fill = 'x') (arg::adjust = cnv::adjust::left)).value()); BOOST_TEST(L"12xxx" == convert(12, cnv(arg::width = 5) (arg::fill = 'x') (arg::adjust = cnv::adjust::left)).value()); BOOST_TEST( "x12xx" == convert(12, cnv(arg::adjust = cnv::adjust::center)).value()); BOOST_TEST(L"x12xx" == convert(12, cnv(arg::adjust = cnv::adjust::center)).value()); // BOOST_TEST("12.34" == convert(12.34).value()); // printf("%s\n", convert(12.34).value().c_str()); return boost::report_errors(); } #endif