forked from boostorg/conversion
Updated documentaion, fixed bug and refactored tests (refs #7065)
[SVN r79266]
This commit is contained in:
@ -112,6 +112,7 @@ The character type of the underlying stream is assumed to be `char` unless eithe
|
||||
* Arrays of characters: `wchar_t *`, `char16_t *`, `char32_t *`, `const wchar_t *`, `const char16_t *`, `const char32_t *`
|
||||
* Strings: `std::basic_string`, `boost::containers::basic_string`
|
||||
* `boost::iterator_range<WideCharPtr>`, where `WideCharPtr` is a pointer to wide-character or pointer to const wide-character
|
||||
* `boost::array<CharT, N>` and `std::array<CharT, N>`, `boost::array<const CharT, N>` and `std::array<const CharT, N>`
|
||||
|
||||
[important Many compilers and runtime libraries fail to make conversions using new Unicode characters. Make shure that the following code compiles and outputs nonzero values, before using new types:
|
||||
``
|
||||
|
@ -272,12 +272,24 @@ namespace boost {
|
||||
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
|
||||
};
|
||||
|
||||
template<typename CharT, std::size_t N>
|
||||
struct stream_char<boost::array<const CharT, N> >
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
|
||||
};
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_ARRAY) && defined(BOOST_HAS_TR1_ARRAY)
|
||||
template <typename CharT, std::size_t N>
|
||||
struct stream_char<std::array<CharT, N> >
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
|
||||
};
|
||||
|
||||
template <typename CharT, std::size_t N>
|
||||
struct stream_char<std::array<const CharT, N> >
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
|
||||
};
|
||||
#endif // !defined(BOOST_NO_CXX11_HDR_ARRAY) && defined(BOOST_HAS_TR1_ARRAY)
|
||||
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
|
@ -11,7 +11,7 @@
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
|
||||
#include <boost/array.hpp>
|
||||
|
||||
void testing_boost_array_output_conversion();
|
||||
@ -80,15 +80,11 @@ static void testing_template_array_output_on_spec_value(T val)
|
||||
{
|
||||
warr_type res = lexical_cast<warr_type>(val);
|
||||
BOOST_CHECK(res.begin() == wethalon);
|
||||
warr_type res3 = lexical_cast<warr_type>(wethalon);
|
||||
BOOST_CHECK(res.begin() == wethalon);
|
||||
}
|
||||
|
||||
{
|
||||
const warr_type res = lexical_cast<warr_type>(val);
|
||||
BOOST_CHECK(res.begin() == wethalon);
|
||||
const warr_type res3 = lexical_cast<warr_type>(wethalon);
|
||||
BOOST_CHECK(res.begin() == wethalon);
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<wshort_arr_type>(val), boost::bad_lexical_cast);
|
||||
@ -103,17 +99,11 @@ static void testing_template_array_output_on_spec_value(T val)
|
||||
{
|
||||
u16arr_type res = lexical_cast<u16arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u16ethalon);
|
||||
|
||||
u16arr_type res3 = lexical_cast<u16arr_type>(u16ethalon);
|
||||
BOOST_CHECK(res3.begin() == u16ethalon);
|
||||
}
|
||||
|
||||
{
|
||||
const u16arr_type res = lexical_cast<u16arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u16ethalon);
|
||||
|
||||
const u16arr_type res3 = lexical_cast<u16arr_type>(u16ethalon);
|
||||
BOOST_CHECK(res3.begin() == u16ethalon);
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<u16short_arr_type>(val), boost::bad_lexical_cast);
|
||||
@ -127,15 +117,125 @@ static void testing_template_array_output_on_spec_value(T val)
|
||||
{
|
||||
u32arr_type res = lexical_cast<u32arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u32ethalon);
|
||||
|
||||
u32arr_type res3 = lexical_cast<u32arr_type>(u32ethalon);
|
||||
BOOST_CHECK(res3.begin() == u32ethalon);
|
||||
}
|
||||
|
||||
{
|
||||
const u32arr_type res = lexical_cast<u32arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u32ethalon);
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<u32short_arr_type>(val), boost::bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
template <template <class, std::size_t> class ArrayT>
|
||||
static void testing_template_array_output_on_char_value()
|
||||
{
|
||||
typedef ArrayT<char, 300> arr_type;
|
||||
typedef ArrayT<char, 1> short_arr_type;
|
||||
typedef ArrayT<unsigned char, 300> uarr_type;
|
||||
typedef ArrayT<unsigned char, 1> ushort_arr_type;
|
||||
typedef ArrayT<signed char, 4> sarr_type;
|
||||
typedef ArrayT<signed char, 3> sshort_arr_type;
|
||||
|
||||
const char val[] = "100";
|
||||
std::string ethalon("100");
|
||||
using namespace std;
|
||||
|
||||
{
|
||||
arr_type res1 = lexical_cast<arr_type>(val);
|
||||
BOOST_CHECK_EQUAL(res1.begin(), ethalon);
|
||||
const arr_type res2 = lexical_cast<arr_type>(val);
|
||||
BOOST_CHECK_EQUAL(res2.begin(), ethalon);
|
||||
BOOST_CHECK_THROW(lexical_cast<short_arr_type>(val), boost::bad_lexical_cast);
|
||||
}
|
||||
|
||||
{
|
||||
uarr_type res1 = lexical_cast<uarr_type>(val);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<char*>(res1.begin()), ethalon);
|
||||
const uarr_type res2 = lexical_cast<uarr_type>(val);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<const char*>(res2.begin()), ethalon);
|
||||
BOOST_CHECK_THROW(lexical_cast<ushort_arr_type>(val), boost::bad_lexical_cast);
|
||||
}
|
||||
|
||||
{
|
||||
sarr_type res1 = lexical_cast<sarr_type>(val);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<char*>(res1.begin()), ethalon);
|
||||
const sarr_type res2 = lexical_cast<sarr_type>(val);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<const char*>(res2.begin()), ethalon);
|
||||
BOOST_CHECK_THROW(lexical_cast<sshort_arr_type>(val), boost::bad_lexical_cast);
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_STRINGSTREAM) && !defined(BOOST_NO_STD_WSTRING)
|
||||
typedef ArrayT<wchar_t, 4> warr_type;
|
||||
typedef ArrayT<wchar_t, 3> wshort_arr_type;
|
||||
std::wstring wethalon(L"100");
|
||||
|
||||
{
|
||||
warr_type res = lexical_cast<warr_type>(val);
|
||||
BOOST_CHECK(res.begin() == wethalon);
|
||||
warr_type res3 = lexical_cast<warr_type>(wethalon);
|
||||
BOOST_CHECK(res3.begin() == wethalon);
|
||||
}
|
||||
|
||||
{
|
||||
const warr_type res = lexical_cast<warr_type>(val);
|
||||
BOOST_CHECK(res.begin() == wethalon);
|
||||
const warr_type res3 = lexical_cast<warr_type>(wethalon);
|
||||
BOOST_CHECK(res3.begin() == wethalon);
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<wshort_arr_type>(val), boost::bad_lexical_cast);
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
typedef ArrayT<char16_t, 300> u16arr_type;
|
||||
typedef ArrayT<char16_t, 3> u16short_arr_type;
|
||||
std::u16string u16ethalon(u"100");
|
||||
|
||||
{
|
||||
#ifdef BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
|
||||
u16arr_type res = lexical_cast<u16arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u16ethalon);
|
||||
#endif
|
||||
|
||||
u16arr_type res3 = lexical_cast<u16arr_type>(u16ethalon);
|
||||
BOOST_CHECK(res3.begin() == u16ethalon);
|
||||
}
|
||||
|
||||
{
|
||||
#ifdef BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
|
||||
const u16arr_type res = lexical_cast<u16arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u16ethalon);
|
||||
#endif
|
||||
const u16arr_type res3 = lexical_cast<u16arr_type>(u16ethalon);
|
||||
BOOST_CHECK(res3.begin() == u16ethalon);
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<u16short_arr_type>(val), boost::bad_lexical_cast);
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
typedef ArrayT<char32_t, 300> u32arr_type;
|
||||
typedef ArrayT<char32_t, 3> u32short_arr_type;
|
||||
std::u32string u32ethalon(U"100");
|
||||
|
||||
{
|
||||
#ifdef BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
|
||||
u32arr_type res = lexical_cast<u32arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u32ethalon);
|
||||
#endif
|
||||
u32arr_type res3 = lexical_cast<u32arr_type>(u32ethalon);
|
||||
BOOST_CHECK(res3.begin() == u32ethalon);
|
||||
}
|
||||
|
||||
{
|
||||
#ifdef BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
|
||||
const u32arr_type res = lexical_cast<u32arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u32ethalon);
|
||||
#endif
|
||||
const u32arr_type res3 = lexical_cast<u32arr_type>(u32ethalon);
|
||||
BOOST_CHECK(res3.begin() == u32ethalon);
|
||||
}
|
||||
@ -147,7 +247,7 @@ static void testing_template_array_output_on_spec_value(T val)
|
||||
|
||||
void testing_boost_array_output_conversion()
|
||||
{
|
||||
testing_template_array_output_on_spec_value<boost::array>("100");
|
||||
testing_template_array_output_on_char_value<boost::array>();
|
||||
testing_template_array_output_on_spec_value<boost::array>(100);
|
||||
testing_template_array_output_on_spec_value<boost::array>(static_cast<short>(100));
|
||||
testing_template_array_output_on_spec_value<boost::array>(static_cast<unsigned short>(100));
|
||||
@ -157,7 +257,7 @@ void testing_boost_array_output_conversion()
|
||||
void testing_std_array_output_conversion()
|
||||
{
|
||||
#if !defined(BOOST_NO_CXX11_HDR_ARRAY) && defined(BOOST_HAS_TR1_ARRAY)
|
||||
testing_template_array_output_on_spec_value<std::array>("100");
|
||||
testing_template_array_output_on_char_value<std::array>();
|
||||
testing_template_array_output_on_spec_value<std::array>(100);
|
||||
testing_template_array_output_on_spec_value<std::array>(static_cast<short>(100));
|
||||
testing_template_array_output_on_spec_value<std::array>(static_cast<unsigned short>(100));
|
||||
@ -173,9 +273,11 @@ static void testing_generic_array_input_conversion()
|
||||
{
|
||||
ArrayT<char, 4> var_zero_terminated = {{ '1', '0', '0', '\0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_zero_terminated), "100");
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(var_zero_terminated), 100);
|
||||
|
||||
ArrayT<char, 3> var_none_terminated = {{ '1', '0', '0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_none_terminated), "100");
|
||||
BOOST_CHECK_EQUAL(lexical_cast<short>(var_none_terminated), static_cast<short>(100));
|
||||
|
||||
ArrayT<const char, 4> var_zero_terminated_const_char = {{ '1', '0', '0', '\0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_zero_terminated_const_char), "100");
|
||||
@ -194,6 +296,7 @@ static void testing_generic_array_input_conversion()
|
||||
|
||||
const ArrayT<const char, 3> var_none_terminated_const_var_const_char = {{ '1', '0', '0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_none_terminated_const_var_const_char), "100");
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(var_none_terminated_const_var_const_char), 100);
|
||||
}
|
||||
|
||||
{
|
||||
@ -210,6 +313,7 @@ static void testing_generic_array_input_conversion()
|
||||
|
||||
const ArrayT<const signed char, 3> var_none_terminated_const_var_const_char = {{ '1', '0', '0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_none_terminated_const_var_const_char), "100");
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(var_none_terminated_const_var_const_char), 100u);
|
||||
}
|
||||
|
||||
|
||||
@ -220,6 +324,7 @@ static void testing_generic_array_input_conversion()
|
||||
|
||||
const ArrayT<const wchar_t, 3> var_none_terminated_const_var_const_char = {{ L'1', L'0', L'0'}};
|
||||
BOOST_CHECK(lexical_cast<std::wstring>(var_none_terminated_const_var_const_char) == L"100");
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(var_none_terminated_const_var_const_char), 100);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -227,6 +332,7 @@ static void testing_generic_array_input_conversion()
|
||||
{
|
||||
const ArrayT<const char16_t, 4> var_zero_terminated_const_var_const_char = {{ u'1', u'0', u'0', u'\0'}};
|
||||
BOOST_CHECK(lexical_cast<std::u16string>(var_zero_terminated_const_var_const_char) == u"100");
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned short>(var_zero_terminated_const_var_const_char), static_cast<unsigned short>(100));
|
||||
|
||||
const ArrayT<const char16_t, 3> var_none_terminated_const_var_const_char = {{ u'1', u'0', u'0'}};
|
||||
BOOST_CHECK(lexical_cast<std::u16string>(var_none_terminated_const_var_const_char) == u"100");
|
||||
@ -240,6 +346,7 @@ static void testing_generic_array_input_conversion()
|
||||
|
||||
const ArrayT<const char32_t, 3> var_none_terminated_const_var_const_char = {{ U'1', U'0', U'0'}};
|
||||
BOOST_CHECK(lexical_cast<std::u32string>(var_none_terminated_const_var_const_char) == U"100");
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(var_none_terminated_const_var_const_char), 100);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user