| 1 | // Unit test for boost::lexical_cast. |
| 2 | // |
| 3 | // See http://www.boost.org for most recent version, including documentation. |
| 4 | // |
| 5 | // Copyright Antony Polukhin, 2011-2024. |
| 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 | |
| 11 | #include <boost/lexical_cast.hpp> |
| 12 | |
| 13 | #include <boost/core/lightweight_test.hpp> |
| 14 | |
| 15 | #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) |
| 16 | #define BOOST_LCAST_NO_WCHAR_T |
| 17 | #endif |
| 18 | |
| 19 | template <class CharT> |
| 20 | void test_impl(const CharT* wc_arr) |
| 21 | { |
| 22 | typedef CharT wide_char; |
| 23 | typedef std::basic_string<CharT> wide_string; |
| 24 | const char c_arr[] = "Test array of chars" ; |
| 25 | const unsigned char uc_arr[] = "Test array of chars" ; |
| 26 | const signed char sc_arr[] = "Test array of chars" ; |
| 27 | |
| 28 | // Following tests depend on realization of std::locale |
| 29 | // and pass for popular compilers and STL realizations |
| 30 | BOOST_TEST(boost::lexical_cast<wide_char>(c_arr[0]) == wc_arr[0]); |
| 31 | BOOST_TEST(boost::lexical_cast<wide_string>(c_arr) == wide_string(wc_arr)); |
| 32 | |
| 33 | BOOST_TEST(boost::lexical_cast<wide_string>(sc_arr) == wide_string(wc_arr) ); |
| 34 | BOOST_TEST(boost::lexical_cast<wide_string>(uc_arr) == wide_string(wc_arr) ); |
| 35 | |
| 36 | BOOST_TEST(boost::lexical_cast<wide_char>(uc_arr[0]) == wc_arr[0]); |
| 37 | BOOST_TEST(boost::lexical_cast<wide_char>(sc_arr[0]) == wc_arr[0]); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void test_char_types_conversions_wchar_t() |
| 42 | { |
| 43 | #ifndef BOOST_LCAST_NO_WCHAR_T |
| 44 | test_impl(wc_arr: L"Test array of chars" ); |
| 45 | wchar_t c = boost::detail::lcast_char_constants<wchar_t>::zero; |
| 46 | BOOST_TEST(L'0' == c); |
| 47 | |
| 48 | c = boost::detail::lcast_char_constants<wchar_t>::minus; |
| 49 | BOOST_TEST(L'-' == c); |
| 50 | |
| 51 | c = boost::detail::lcast_char_constants<wchar_t>::plus; |
| 52 | BOOST_TEST(L'+' == c); |
| 53 | |
| 54 | c = boost::detail::lcast_char_constants<wchar_t>::lowercase_e; |
| 55 | BOOST_TEST(L'e' == c); |
| 56 | |
| 57 | c = boost::detail::lcast_char_constants<wchar_t>::capital_e; |
| 58 | BOOST_TEST(L'E' == c); |
| 59 | |
| 60 | c = boost::detail::lcast_char_constants<wchar_t>::c_decimal_separator; |
| 61 | BOOST_TEST(L'.' == c); |
| 62 | #endif |
| 63 | |
| 64 | BOOST_TEST(true); |
| 65 | } |
| 66 | |
| 67 | void test_char_types_conversions_char16_t() |
| 68 | { |
| 69 | #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES) |
| 70 | test_impl(u"Test array of chars" ); |
| 71 | char16_t c = boost::detail::lcast_char_constants<char16_t>::zero; |
| 72 | BOOST_TEST(u'0' == c); |
| 73 | |
| 74 | c = boost::detail::lcast_char_constants<char16_t>::minus; |
| 75 | BOOST_TEST(u'-' == c); |
| 76 | |
| 77 | c = boost::detail::lcast_char_constants<char16_t>::plus; |
| 78 | BOOST_TEST(u'+' == c); |
| 79 | |
| 80 | c = boost::detail::lcast_char_constants<char16_t>::lowercase_e; |
| 81 | BOOST_TEST(u'e' == c); |
| 82 | |
| 83 | c = boost::detail::lcast_char_constants<char16_t>::capital_e; |
| 84 | BOOST_TEST(u'E' == c); |
| 85 | |
| 86 | c = boost::detail::lcast_char_constants<char16_t>::c_decimal_separator; |
| 87 | BOOST_TEST(u'.' == c); |
| 88 | #endif |
| 89 | |
| 90 | BOOST_TEST(true); |
| 91 | } |
| 92 | |
| 93 | void test_char_types_conversions_char32_t() |
| 94 | { |
| 95 | #if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES) |
| 96 | test_impl(U"Test array of chars" ); |
| 97 | char32_t c = boost::detail::lcast_char_constants<char32_t>::zero; |
| 98 | BOOST_TEST(U'0' == c); |
| 99 | |
| 100 | c = boost::detail::lcast_char_constants<char32_t>::minus; |
| 101 | BOOST_TEST(U'-' == c); |
| 102 | |
| 103 | c = boost::detail::lcast_char_constants<char32_t>::plus; |
| 104 | BOOST_TEST(U'+' == c); |
| 105 | |
| 106 | c = boost::detail::lcast_char_constants<char32_t>::lowercase_e; |
| 107 | BOOST_TEST(U'e' == c); |
| 108 | |
| 109 | c = boost::detail::lcast_char_constants<char32_t>::capital_e; |
| 110 | BOOST_TEST(U'E', == c); |
| 111 | |
| 112 | c = boost::detail::lcast_char_constants<char32_t>::c_decimal_separator; |
| 113 | BOOST_TEST(U'.' == c); |
| 114 | #endif |
| 115 | |
| 116 | BOOST_TEST(true); |
| 117 | } |
| 118 | |
| 119 | int main() |
| 120 | { |
| 121 | test_char_types_conversions_wchar_t(); |
| 122 | test_char_types_conversions_char16_t(); |
| 123 | test_char_types_conversions_char32_t(); |
| 124 | |
| 125 | return boost::report_errors(); |
| 126 | } |
| 127 | |