spirit_converter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "./test.hpp"
  6. #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
  7. int main(int, char const* []) { return 0; }
  8. #else
  9. #include <boost/convert.hpp>
  10. #include <boost/convert/spirit.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <cstdio>
  13. using std::string;
  14. using std::wstring;
  15. using boost::convert;
  16. namespace cnv = boost::cnv;
  17. namespace arg = boost::cnv::parameter;
  18. struct boost::cnv::by_default : boost::cnv::spirit {};
  19. int
  20. main(int, char const* [])
  21. {
  22. char const* const c_stri ("12345");
  23. char const* const c_strd ("123.45");
  24. wchar_t const* const c_wstri (L"12345");
  25. wchar_t const* const c_wstrd (L"123.45");
  26. std::string const std_stri (c_stri);
  27. std::string const std_strd (c_strd);
  28. std::wstring const std_wstri (c_wstri);
  29. std::wstring const std_wstrd (c_wstrd);
  30. my_string const my_stri (c_stri, c_stri + strlen(c_stri));
  31. my_string const my_strd (c_strd, c_strd + strlen(c_strd));
  32. boost::cnv::spirit cnv;
  33. BOOST_TEST( 12345 == convert< int>( c_stri).value());
  34. BOOST_TEST( 12345 == convert< int>( c_wstri).value());
  35. BOOST_TEST( 12345 == convert< int>( std_stri).value());
  36. BOOST_TEST( 12345 == convert< int>(std_wstri).value());
  37. BOOST_TEST( 12345 == convert< int>( my_stri).value());
  38. BOOST_TEST( 12345 == convert<unsigned int>(c_stri).value());
  39. BOOST_TEST( 12345 == convert<long int>( c_stri).value());
  40. BOOST_TEST( 12345 == convert<long int>( c_wstri).value());
  41. BOOST_TEST( 12345 == convert<long int>( std_stri).value());
  42. BOOST_TEST( 12345 == convert<long int>(std_wstri).value());
  43. BOOST_TEST( 12345 == convert<long int>( my_stri).value());
  44. BOOST_TEST( 12345 == convert<unsigned long int>(c_stri).value());
  45. BOOST_TEST( 12345 == convert<long long int>(c_stri).value());
  46. BOOST_TEST( 12345 == convert<unsigned long long int>(c_stri).value());
  47. BOOST_TEST(123.45 == convert< double>( c_strd).value());
  48. BOOST_TEST(123.45 == convert< double>( c_wstrd).value());
  49. // BOOST_TEST(123.45 == convert< double>( std_strd).value());
  50. // BOOST_TEST(123.45 == convert< double>(std_wstrd).value());
  51. BOOST_TEST(123.45 == convert< double>( my_strd).value());
  52. BOOST_TEST(!convert< int>("uhm"));
  53. BOOST_TEST(!convert< int>(L"uhm"));
  54. BOOST_TEST(!convert<double>("12.uhm"));
  55. BOOST_TEST(!convert<double>("L12.uhm"));
  56. BOOST_TEST( "1234" == convert<string>(1234).value());
  57. BOOST_TEST( "1234" == convert<string>(1234u).value());
  58. BOOST_TEST( "1234" == convert<string>(1234ll).value());
  59. BOOST_TEST( "1234" == convert<string>(1234ull).value());
  60. BOOST_TEST( L"1234" == convert<wstring>(1234).value());
  61. BOOST_TEST( "12xxx" == convert<string>(12, cnv(arg::width = 5)
  62. (arg::fill = 'x')
  63. (arg::adjust = cnv::adjust::left)).value());
  64. BOOST_TEST(L"12xxx" == convert<wstring>(12, cnv(arg::width = 5)
  65. (arg::fill = 'x')
  66. (arg::adjust = cnv::adjust::left)).value());
  67. BOOST_TEST( "x12xx" == convert<string>(12, cnv(arg::adjust = cnv::adjust::center)).value());
  68. BOOST_TEST(L"x12xx" == convert<wstring>(12, cnv(arg::adjust = cnv::adjust::center)).value());
  69. // BOOST_TEST("12.34" == convert<std::string>(12.34).value());
  70. // printf("%s\n", convert<std::string>(12.34).value().c_str());
  71. return boost::report_errors();
  72. }
  73. #endif