lexical_cast_containers_test.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Testing boost::lexical_cast with boost::container::string.
  2. //
  3. // See http://www.boost.org for most recent version, including documentation.
  4. //
  5. // Copyright Antony Polukhin, 2011-2019.
  6. //
  7. // Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
  10. #include <boost/lexical_cast.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/container/string.hpp>
  13. void testing_boost_containers_basic_string();
  14. void testing_boost_containers_string_std_string();
  15. void testing_boost_containers_string_widening();
  16. using namespace boost;
  17. boost::unit_test::test_suite *init_unit_test_suite(int, char *[])
  18. {
  19. unit_test::test_suite *suite =
  20. BOOST_TEST_SUITE("Testing boost::lexical_cast with boost::container::string");
  21. suite->add(BOOST_TEST_CASE(testing_boost_containers_basic_string));
  22. suite->add(BOOST_TEST_CASE(testing_boost_containers_string_std_string));
  23. suite->add(BOOST_TEST_CASE(testing_boost_containers_string_widening));
  24. return suite;
  25. }
  26. void testing_boost_containers_basic_string()
  27. {
  28. BOOST_CHECK("100" == lexical_cast<boost::container::string>("100"));
  29. BOOST_CHECK(L"100" == lexical_cast<boost::container::wstring>(L"100"));
  30. BOOST_CHECK("100" == lexical_cast<boost::container::string>(100));
  31. boost::container::string str("1000");
  32. BOOST_CHECK(1000 == lexical_cast<int>(str));
  33. }
  34. #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
  35. #define BOOST_LCAST_NO_WCHAR_T
  36. #endif
  37. void testing_boost_containers_string_std_string()
  38. {
  39. std::string std_str("std_str");
  40. boost::container::string boost_str("boost_str");
  41. BOOST_CHECK(boost::lexical_cast<std::string>(boost_str) == "boost_str");
  42. BOOST_CHECK(boost::lexical_cast<boost::container::string>(std_str) == "std_str");
  43. #ifndef BOOST_LCAST_NO_WCHAR_T
  44. std::wstring std_wstr(L"std_wstr");
  45. boost::container::wstring boost_wstr(L"boost_wstr");
  46. BOOST_CHECK(boost::lexical_cast<std::wstring>(boost_wstr) == L"boost_wstr");
  47. BOOST_CHECK(boost::lexical_cast<boost::container::wstring>(std_wstr) == L"std_wstr");
  48. #endif
  49. }
  50. void testing_boost_containers_string_widening()
  51. {
  52. const char char_array[] = "Test string";
  53. #ifndef BOOST_LCAST_NO_WCHAR_T
  54. const wchar_t wchar_array[] = L"Test string";
  55. BOOST_CHECK(boost::lexical_cast<boost::container::wstring>(char_array) == wchar_array);
  56. #endif
  57. #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
  58. const char16_t char16_array[] = u"Test string";
  59. BOOST_CHECK(boost::lexical_cast<boost::container::basic_string<char16_t> >(char_array) == char16_array);
  60. #endif
  61. #if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
  62. const char32_t char32_array[] = U"Test string";
  63. BOOST_CHECK(boost::lexical_cast<boost::container::basic_string<char32_t> >(char_array) == char32_array);
  64. #endif
  65. }