| 1 | // Copyright Antony Polukhin, 2012-2024. |
| 2 | // |
| 3 | // Distributed under the Boost |
| 4 | // Software License, Version 1.0. (See accompanying file |
| 5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). |
| 6 | |
| 7 | #include <boost/lexical_cast/detail/converter_lexical.hpp> |
| 8 | |
| 9 | #include <boost/core/lightweight_test.hpp> |
| 10 | |
| 11 | #include <boost/array.hpp> |
| 12 | #include <boost/container/string.hpp> |
| 13 | #include <boost/range/iterator_range.hpp> |
| 14 | #include <boost/utility/string_view.hpp> |
| 15 | |
| 16 | template <class T> |
| 17 | struct is_optimized_stream : boost::false_type {}; |
| 18 | |
| 19 | template <class CharT, class Traits, std::size_t CharacterBufferSize> |
| 20 | struct is_optimized_stream< boost::detail::lcast::optimized_src_stream<CharT, Traits, CharacterBufferSize> > : boost::true_type {}; |
| 21 | |
| 22 | template <class Source, class Target> |
| 23 | static void assert_optimized_stream() |
| 24 | { |
| 25 | BOOST_TEST((is_optimized_stream< |
| 26 | typename boost::detail::lexical_converter_impl<Source, Target>::from_src_stream |
| 27 | >::value)); |
| 28 | } |
| 29 | |
| 30 | template <class T> |
| 31 | static void test_optimized_types_to_string_const() |
| 32 | { |
| 33 | namespace de = boost::detail; |
| 34 | typedef de::lexical_cast_stream_traits<T, std::string> trait_1; |
| 35 | BOOST_TEST((boost::is_same<typename trait_1::src_char_t, char>::value)); |
| 36 | BOOST_TEST((boost::is_same<typename trait_1::target_char_t, char>::value)); |
| 37 | BOOST_TEST((boost::is_same<typename trait_1::char_type, char>::value)); |
| 38 | assert_optimized_stream<T, std::string>(); |
| 39 | assert_optimized_stream<T, boost::container::string>(); |
| 40 | |
| 41 | typedef de::lexical_cast_stream_traits<const T, std::string> trait_2; |
| 42 | BOOST_TEST((boost::is_same<typename trait_2::src_char_t, char>::value)); |
| 43 | BOOST_TEST((boost::is_same<typename trait_2::target_char_t, char>::value)); |
| 44 | BOOST_TEST((boost::is_same<typename trait_2::char_type, char>::value)); |
| 45 | |
| 46 | typedef de::lexical_cast_stream_traits<T, std::wstring> trait_3; |
| 47 | BOOST_TEST((boost::is_same<typename trait_3::src_char_t, char>::value)); |
| 48 | BOOST_TEST((boost::is_same<typename trait_3::target_char_t, wchar_t>::value)); |
| 49 | BOOST_TEST((boost::is_same<typename trait_3::char_type, wchar_t>::value)); |
| 50 | assert_optimized_stream<T, std::wstring>(); |
| 51 | assert_optimized_stream<T, boost::container::wstring>(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | template <class T> |
| 56 | static void test_optimized_types_to_string() |
| 57 | { |
| 58 | test_optimized_types_to_string_const<T>(); |
| 59 | |
| 60 | namespace de = boost::detail; |
| 61 | typedef de::lexical_cast_stream_traits<std::string, T> trait_4; |
| 62 | BOOST_TEST((boost::is_same<typename trait_4::src_char_t, char>::value)); |
| 63 | BOOST_TEST((boost::is_same<typename trait_4::target_char_t, char>::value)); |
| 64 | BOOST_TEST((boost::is_same<typename trait_4::char_type, char>::value)); |
| 65 | assert_optimized_stream<std::string, T>(); |
| 66 | |
| 67 | typedef de::lexical_cast_stream_traits<const std::string, T> trait_5; |
| 68 | BOOST_TEST((boost::is_same<typename trait_5::src_char_t, char>::value)); |
| 69 | BOOST_TEST((boost::is_same<typename trait_5::target_char_t, char>::value)); |
| 70 | BOOST_TEST((boost::is_same<typename trait_5::char_type, char>::value)); |
| 71 | |
| 72 | typedef de::lexical_cast_stream_traits<const std::wstring, T> trait_6; |
| 73 | BOOST_TEST((boost::is_same<typename trait_6::src_char_t, wchar_t>::value)); |
| 74 | BOOST_TEST((boost::is_same<typename trait_6::target_char_t, char>::value)); |
| 75 | BOOST_TEST((boost::is_same<typename trait_6::char_type, wchar_t>::value)); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | template <class T> |
| 80 | static void test_optimized_types_to_x_string() |
| 81 | { |
| 82 | test_optimized_types_to_string<T>(); |
| 83 | assert_optimized_stream<std::wstring, T>(); |
| 84 | assert_optimized_stream<boost::container::wstring, T>(); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | template <class T> |
| 89 | static void test_optimized_types_to_wstring() |
| 90 | { |
| 91 | assert_optimized_stream<std::wstring, T>(); |
| 92 | assert_optimized_stream<T, std::wstring>(); |
| 93 | assert_optimized_stream<boost::container::wstring, T>(); |
| 94 | assert_optimized_stream<T, boost::container::wstring>(); |
| 95 | } |
| 96 | |
| 97 | void test_metafunctions() |
| 98 | { |
| 99 | test_optimized_types_to_x_string<bool>(); |
| 100 | test_optimized_types_to_x_string<char>(); |
| 101 | test_optimized_types_to_x_string<unsigned char>(); |
| 102 | test_optimized_types_to_x_string<signed char>(); |
| 103 | test_optimized_types_to_x_string<short>(); |
| 104 | test_optimized_types_to_x_string<unsigned short>(); |
| 105 | test_optimized_types_to_x_string<int>(); |
| 106 | test_optimized_types_to_x_string<unsigned int>(); |
| 107 | test_optimized_types_to_x_string<long>(); |
| 108 | test_optimized_types_to_x_string<unsigned long>(); |
| 109 | |
| 110 | #if defined(BOOST_HAS_LONG_LONG) |
| 111 | test_optimized_types_to_x_string<boost::ulong_long_type>(); |
| 112 | test_optimized_types_to_x_string<boost::long_long_type>(); |
| 113 | #elif defined(BOOST_HAS_MS_INT64) |
| 114 | test_optimized_types_to_x_string<unsigned __int64>(); |
| 115 | test_optimized_types_to_x_string<__int64>(); |
| 116 | #endif |
| 117 | |
| 118 | test_optimized_types_to_string<float>(); |
| 119 | test_optimized_types_to_string<double>(); |
| 120 | test_optimized_types_to_string<long double>(); |
| 121 | test_optimized_types_to_string<std::string>(); |
| 122 | test_optimized_types_to_string<char*>(); |
| 123 | //test_optimized_types_to_string<char[5]>(); |
| 124 | //test_optimized_types_to_string<char[1]>(); |
| 125 | test_optimized_types_to_string<unsigned char*>(); |
| 126 | //test_optimized_types_to_string<unsigned char[5]>(); |
| 127 | //test_optimized_types_to_string<unsigned char[1]>(); |
| 128 | test_optimized_types_to_string<signed char*>(); |
| 129 | //test_optimized_types_to_string<signed char[5]>(); |
| 130 | //test_optimized_types_to_string<signed char[1]>(); |
| 131 | test_optimized_types_to_string<boost::array<char, 1> >(); |
| 132 | test_optimized_types_to_string<boost::array<char, 5> >(); |
| 133 | test_optimized_types_to_string<boost::array<unsigned char, 1> >(); |
| 134 | test_optimized_types_to_string<boost::array<unsigned char, 5> >(); |
| 135 | test_optimized_types_to_string<boost::array<signed char, 1> >(); |
| 136 | test_optimized_types_to_string<boost::array<signed char, 5> >(); |
| 137 | test_optimized_types_to_string<boost::iterator_range<char*> >(); |
| 138 | test_optimized_types_to_string<boost::iterator_range<unsigned char*> >(); |
| 139 | test_optimized_types_to_string<boost::iterator_range<signed char*> >(); |
| 140 | |
| 141 | test_optimized_types_to_string_const<boost::array<const char, 1> >(); |
| 142 | test_optimized_types_to_string_const<boost::array<const char, 5> >(); |
| 143 | test_optimized_types_to_string_const<boost::array<const unsigned char, 1> >(); |
| 144 | test_optimized_types_to_string_const<boost::array<const unsigned char, 5> >(); |
| 145 | test_optimized_types_to_string_const<boost::array<const signed char, 1> >(); |
| 146 | test_optimized_types_to_string_const<boost::array<const signed char, 5> >(); |
| 147 | test_optimized_types_to_string_const<boost::iterator_range<const char*> >(); |
| 148 | test_optimized_types_to_string_const<boost::iterator_range<const unsigned char*> >(); |
| 149 | test_optimized_types_to_string_const<boost::iterator_range<const signed char*> >(); |
| 150 | |
| 151 | #ifndef BOOST_NO_CXX11_HDR_ARRAY |
| 152 | test_optimized_types_to_string<std::array<char, 1> >(); |
| 153 | test_optimized_types_to_string<std::array<char, 5> >(); |
| 154 | test_optimized_types_to_string<std::array<unsigned char, 1> >(); |
| 155 | test_optimized_types_to_string<std::array<unsigned char, 5> >(); |
| 156 | test_optimized_types_to_string<std::array<signed char, 1> >(); |
| 157 | test_optimized_types_to_string<std::array<signed char, 5> >(); |
| 158 | |
| 159 | test_optimized_types_to_string_const<std::array<const char, 1> >(); |
| 160 | test_optimized_types_to_string_const<std::array<const char, 5> >(); |
| 161 | test_optimized_types_to_string_const<std::array<const unsigned char, 1> >(); |
| 162 | test_optimized_types_to_string_const<std::array<const unsigned char, 5> >(); |
| 163 | test_optimized_types_to_string_const<std::array<const signed char, 1> >(); |
| 164 | test_optimized_types_to_string_const<std::array<const signed char, 5> >(); |
| 165 | |
| 166 | test_optimized_types_to_wstring<std::array<wchar_t, 42>>(); |
| 167 | test_optimized_types_to_wstring<std::array<const wchar_t, 42>>(); |
| 168 | #endif |
| 169 | |
| 170 | test_optimized_types_to_wstring<wchar_t*>(); |
| 171 | test_optimized_types_to_wstring<const wchar_t*>(); |
| 172 | test_optimized_types_to_wstring<boost::iterator_range<const wchar_t*>>(); |
| 173 | test_optimized_types_to_wstring<boost::iterator_range<wchar_t*>>(); |
| 174 | |
| 175 | test_optimized_types_to_string<boost::string_view>(); |
| 176 | test_optimized_types_to_wstring<boost::basic_string_view<wchar_t>>(); |
| 177 | |
| 178 | #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW |
| 179 | test_optimized_types_to_string<std::string_view>(); |
| 180 | test_optimized_types_to_wstring<std::basic_string_view<wchar_t>>(); |
| 181 | #endif |
| 182 | } |
| 183 | |
| 184 | int main() |
| 185 | { |
| 186 | test_metafunctions(); |
| 187 | |
| 188 | return boost::report_errors(); |
| 189 | } |
| 190 | |
| 191 | |