]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Unit test for boost::lexical_cast. |
2 | // | |
3 | // See http://www.boost.org for most recent version, including documentation. | |
4 | // | |
92f5a8d4 | 5 | // Copyright Antony Polukhin, 2011-2019. |
7c673cae FG |
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/config.hpp> | |
12 | ||
13 | #if defined(__INTEL_COMPILER) | |
14 | #pragma warning(disable: 193 383 488 981 1418 1419) | |
15 | #elif defined(BOOST_MSVC) | |
16 | #pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) | |
17 | #endif | |
18 | ||
19 | #include <boost/lexical_cast.hpp> | |
20 | #include <boost/test/unit_test.hpp> | |
21 | ||
22 | using namespace boost; | |
23 | ||
24 | #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) | |
25 | #define BOOST_LCAST_NO_WCHAR_T | |
26 | #endif | |
27 | ||
28 | template <class CharT> | |
29 | void test_impl(const CharT* wc_arr) | |
30 | { | |
31 | typedef CharT wide_char; | |
32 | typedef std::basic_string<CharT> wide_string; | |
33 | const char c_arr[] = "Test array of chars"; | |
34 | const unsigned char uc_arr[] = "Test array of chars"; | |
35 | const signed char sc_arr[] = "Test array of chars"; | |
36 | ||
37 | // Following tests depend on realization of std::locale | |
38 | // and pass for popular compilers and STL realizations | |
39 | BOOST_CHECK(boost::lexical_cast<wide_char>(c_arr[0]) == wc_arr[0]); | |
40 | BOOST_CHECK(boost::lexical_cast<wide_string>(c_arr) == wide_string(wc_arr)); | |
41 | ||
42 | BOOST_CHECK(boost::lexical_cast<wide_string>(sc_arr) == wide_string(wc_arr) ); | |
43 | BOOST_CHECK(boost::lexical_cast<wide_string>(uc_arr) == wide_string(wc_arr) ); | |
44 | ||
45 | BOOST_CHECK_EQUAL(boost::lexical_cast<wide_char>(uc_arr[0]), wc_arr[0]); | |
46 | BOOST_CHECK_EQUAL(boost::lexical_cast<wide_char>(sc_arr[0]), wc_arr[0]); | |
47 | } | |
48 | ||
49 | ||
50 | void test_char_types_conversions_wchar_t() | |
51 | { | |
52 | #ifndef BOOST_LCAST_NO_WCHAR_T | |
53 | test_impl(L"Test array of chars"); | |
54 | wchar_t c = boost::detail::lcast_char_constants<wchar_t>::zero; | |
55 | BOOST_CHECK_EQUAL(L'0', c); | |
56 | ||
57 | c = boost::detail::lcast_char_constants<wchar_t>::minus; | |
58 | BOOST_CHECK_EQUAL(L'-', c); | |
59 | ||
60 | c = boost::detail::lcast_char_constants<wchar_t>::plus; | |
61 | BOOST_CHECK_EQUAL(L'+', c); | |
62 | ||
63 | c = boost::detail::lcast_char_constants<wchar_t>::lowercase_e; | |
64 | BOOST_CHECK_EQUAL(L'e', c); | |
65 | ||
66 | c = boost::detail::lcast_char_constants<wchar_t>::capital_e; | |
67 | BOOST_CHECK_EQUAL(L'E', c); | |
68 | ||
69 | c = boost::detail::lcast_char_constants<wchar_t>::c_decimal_separator; | |
70 | BOOST_CHECK_EQUAL(L'.', c); | |
71 | #endif | |
72 | ||
73 | BOOST_CHECK(true); | |
74 | } | |
75 | ||
76 | void test_char_types_conversions_char16_t() | |
77 | { | |
78 | #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES) | |
79 | test_impl(u"Test array of chars"); | |
80 | char16_t c = boost::detail::lcast_char_constants<char16_t>::zero; | |
81 | BOOST_CHECK_EQUAL(u'0', c); | |
82 | ||
83 | c = boost::detail::lcast_char_constants<char16_t>::minus; | |
84 | BOOST_CHECK_EQUAL(u'-', c); | |
85 | ||
86 | c = boost::detail::lcast_char_constants<char16_t>::plus; | |
87 | BOOST_CHECK_EQUAL(u'+', c); | |
88 | ||
89 | c = boost::detail::lcast_char_constants<char16_t>::lowercase_e; | |
90 | BOOST_CHECK_EQUAL(u'e', c); | |
91 | ||
92 | c = boost::detail::lcast_char_constants<char16_t>::capital_e; | |
93 | BOOST_CHECK_EQUAL(u'E', c); | |
94 | ||
95 | c = boost::detail::lcast_char_constants<char16_t>::c_decimal_separator; | |
96 | BOOST_CHECK_EQUAL(u'.', c); | |
97 | #endif | |
98 | ||
99 | BOOST_CHECK(true); | |
100 | } | |
101 | ||
102 | void test_char_types_conversions_char32_t() | |
103 | { | |
104 | #if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES) | |
105 | test_impl(U"Test array of chars"); | |
106 | char32_t c = boost::detail::lcast_char_constants<char32_t>::zero; | |
107 | BOOST_CHECK_EQUAL(U'0', c); | |
108 | ||
109 | c = boost::detail::lcast_char_constants<char32_t>::minus; | |
110 | BOOST_CHECK_EQUAL(U'-', c); | |
111 | ||
112 | c = boost::detail::lcast_char_constants<char32_t>::plus; | |
113 | BOOST_CHECK_EQUAL(U'+', c); | |
114 | ||
115 | c = boost::detail::lcast_char_constants<char32_t>::lowercase_e; | |
116 | BOOST_CHECK_EQUAL(U'e', c); | |
117 | ||
118 | c = boost::detail::lcast_char_constants<char32_t>::capital_e; | |
119 | BOOST_CHECK_EQUAL(U'E', c); | |
120 | ||
121 | c = boost::detail::lcast_char_constants<char32_t>::c_decimal_separator; | |
122 | BOOST_CHECK_EQUAL(U'.', c); | |
123 | #endif | |
124 | ||
125 | BOOST_CHECK(true); | |
126 | } | |
127 | ||
128 | unit_test::test_suite *init_unit_test_suite(int, char *[]) | |
129 | { | |
130 | unit_test::test_suite *suite = | |
131 | BOOST_TEST_SUITE("lexical_cast char => wide characters unit test (widening test)"); | |
132 | suite->add(BOOST_TEST_CASE(&test_char_types_conversions_wchar_t)); | |
133 | suite->add(BOOST_TEST_CASE(&test_char_types_conversions_char16_t)); | |
134 | suite->add(BOOST_TEST_CASE(&test_char_types_conversions_char32_t)); | |
135 | ||
136 | return suite; | |
137 | } |