str_to_int.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/stream.hpp>
  11. #include <boost/convert/printf.hpp>
  12. #include <boost/convert/strtol.hpp>
  13. #include <boost/convert/lexical_cast.hpp>
  14. #include <boost/convert/spirit.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. using std::string;
  17. template<typename Converter>
  18. void
  19. int_to_str(Converter const& cnv)
  20. {
  21. string const not_int_str = "not an int";
  22. string const std_str = "-11";
  23. char const* const c_str = "-12";
  24. char const array_str[] = "-15";
  25. ////////////////////////////////////////////////////////////////////////////
  26. // Testing int-to-string conversion with various string
  27. // containers as the fallback values.
  28. ////////////////////////////////////////////////////////////////////////////
  29. string s01 = boost::convert< string>(-1, cnv).value_or(std_str);
  30. string s02 = boost::convert< string>(-2, cnv).value_or(c_str);
  31. string s05 = boost::convert< string>(-5, cnv).value_or(array_str);
  32. boost::optional< string> rs01 = boost::convert< string>(-1, cnv);
  33. boost::optional< string> rs02 = boost::convert< string>(-2, cnv);
  34. boost::optional< string> rs05 = boost::convert< string>(-5, cnv);
  35. BOOST_TEST(s01 == "-1"); BOOST_TEST(rs01 && rs01.value() == "-1");
  36. BOOST_TEST(s02 == "-2"); BOOST_TEST(rs02 && rs02.value() == "-2");
  37. BOOST_TEST(s05 == "-5"); BOOST_TEST(rs05 && rs05.value() == "-5");
  38. string s11 = boost::convert< string>(-1, cnv).value();
  39. boost::optional< string> rs11 = boost::convert< string>(-1, cnv);
  40. BOOST_TEST( s11 == "-1");
  41. BOOST_TEST(rs11 && rs11.value() == "-1");
  42. }
  43. template<typename Converter>
  44. void
  45. str_to_int(Converter const& cnv)
  46. {
  47. string const not_int_str = "not an int";
  48. string const std_str = "-11";
  49. char const* const c_str = "-123";
  50. char const array_str[] = "3456";
  51. ////////////////////////////////////////////////////////////////////////////
  52. // Testing various kinds of string containers.
  53. // Testing implicit conversion directly to TypeOut (int).
  54. // Testing with the fallback value value provided.
  55. // On failure returns the provided fallback value and DOES NOT THROW.
  56. ////////////////////////////////////////////////////////////////////////////
  57. int const a00 = boost::convert<int>(not_int_str, cnv).value_or(-1);
  58. int const a01 = boost::convert<int>(std_str, cnv).value_or(-1);
  59. int const a02 = boost::convert<int>(c_str, cnv).value_or(-1);
  60. int const a05 = boost::convert<int>(array_str, cnv).value_or(-1);
  61. BOOST_TEST(a00 == -1); // Failed conversion
  62. BOOST_TEST(a01 == -11);
  63. BOOST_TEST(a02 == -123);
  64. BOOST_TEST(a05 == 3456);
  65. ////////////////////////////////////////////////////////////////////////////
  66. // Testing "optional"
  67. ////////////////////////////////////////////////////////////////////////////
  68. boost::optional<int> const r00 = boost::convert<int>(not_int_str, cnv);
  69. boost::optional<int> const r01 = boost::convert<int>(std_str, cnv);
  70. boost::optional<int> const r02 = boost::convert<int>(c_str, cnv);
  71. boost::optional<int> const r05 = boost::convert<int>(array_str, cnv);
  72. BOOST_TEST(!r00); // Failed conversion
  73. BOOST_TEST( r01 && r01.value() == -11);
  74. BOOST_TEST( r02 && r02.value() == -123);
  75. BOOST_TEST( r05 && r05.value() == 3456);
  76. ////////////////////////////////////////////////////////////////////////////
  77. // Testing value() on invalid result.
  78. ////////////////////////////////////////////////////////////////////////////
  79. try
  80. {
  81. boost::convert<int>(not_int_str, cnv).value();
  82. BOOST_TEST(!"failed to throw");
  83. }
  84. catch (std::exception&)
  85. {
  86. // Expected behavior: received 'boost::convert failed' exception. All well.
  87. }
  88. ////////////////////////////////////////////////////////////////////////////
  89. // Testing value() on valid result.
  90. ////////////////////////////////////////////////////////////////////////////
  91. int a21 = boost::convert<int>(std_str, cnv).value();
  92. int a22 = boost::convert<int>(c_str, cnv).value();
  93. int a25 = boost::convert<int>(array_str, cnv).value();
  94. BOOST_TEST(a21 == -11);
  95. BOOST_TEST(a22 == -123);
  96. BOOST_TEST(a25 == 3456);
  97. ////////////////////////////////////////////////////////////////////////////
  98. // Testing empty string.
  99. ////////////////////////////////////////////////////////////////////////////
  100. int a31 = boost::convert<int>(std::string(), cnv).value_or(-1);
  101. int a32 = boost::convert<int>(std::string(""), cnv).value_or(-1);
  102. int a33 = boost::convert<int>("", cnv).value_or(-1);
  103. BOOST_ASSERT(a31 == -1);
  104. BOOST_ASSERT(a32 == -1);
  105. BOOST_ASSERT(a33 == -1);
  106. }
  107. int
  108. main(int, char const* [])
  109. {
  110. boost::cnv::lexical_cast lxcast_cnv;
  111. boost::cnv::cstream stream_cnv;
  112. boost::cnv::strtol strtol_cnv;
  113. boost::cnv::printf printf_cnv;
  114. boost::cnv::spirit spirit_cnv;
  115. str_to_int(lxcast_cnv);
  116. str_to_int(stream_cnv);
  117. str_to_int(strtol_cnv);
  118. str_to_int(printf_cnv);
  119. str_to_int(spirit_cnv);
  120. int_to_str(lxcast_cnv);
  121. int_to_str(stream_cnv);
  122. int_to_str(strtol_cnv);
  123. int_to_str(printf_cnv);
  124. int_to_str(spirit_cnv);
  125. return boost::report_errors();
  126. }
  127. #endif