mirror of
https://github.com/boostorg/conversion.git
synced 2026-08-06 13:34:14 +02:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8f1957c887 | |||
| 0920c53e23 | |||
| 8d373a0c99 | |||
| 733438e072 | |||
| f67347e308 | |||
| 46c6cc911b | |||
| 80d480ca0b | |||
| 675d0bb451 | |||
| 79d6468aeb | |||
| ca9c8d30bd | |||
| b1b5305984 | |||
| eb4ad73caf | |||
| 1e58331baf | |||
| 93ee01ab69 | |||
| a065884c59 |
+3
-3
@@ -6,9 +6,9 @@ using quickbook ;
|
||||
import boostbook : boostbook ;
|
||||
|
||||
xml lexical_cast : lexical_cast.qbk ;
|
||||
boostbook standalone
|
||||
:
|
||||
lexical_cast
|
||||
boostbook standalone
|
||||
:
|
||||
lexical_cast
|
||||
:
|
||||
<xsl:param>boost.root=../../../..
|
||||
<format>pdf:<xsl:param>boost.url.prefix=http://www.boost.org/doc/libs/release/doc/html
|
||||
|
||||
+636
-377
File diff suppressed because it is too large
Load Diff
+831
-521
File diff suppressed because it is too large
Load Diff
+48
-474
@@ -4,7 +4,7 @@
|
||||
//
|
||||
// Copyright Terje Sletteb and Kevlin Henney, 2005.
|
||||
// Copyright Alexander Nasonov, 2006.
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
// Copyright Antony Polukhin, 2011-2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
@@ -57,13 +57,6 @@ struct my_allocator : std::allocator<CharT>
|
||||
{
|
||||
};
|
||||
|
||||
// Test all 65536 values if true:
|
||||
bool const lcast_test_small_integral_types_completely = false;
|
||||
|
||||
// lcast_integral_test_counter: use when testing all values of an integral
|
||||
// types is not possible. Max. portable value is 32767.
|
||||
int const lcast_integral_test_counter=1000;
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_conversion_to_char();
|
||||
@@ -80,18 +73,7 @@ void test_conversion_from_wstring();
|
||||
void test_conversion_to_wstring();
|
||||
void test_bad_lexical_cast();
|
||||
void test_no_whitespace_stripping();
|
||||
void test_conversion_from_to_short();
|
||||
void test_conversion_from_to_ushort();
|
||||
void test_conversion_from_to_int();
|
||||
void test_conversion_from_to_uint();
|
||||
void test_conversion_from_to_long();
|
||||
void test_conversion_from_to_ulong();
|
||||
void test_conversion_from_to_intmax_t();
|
||||
void test_conversion_from_to_uintmax_t();
|
||||
#ifdef LCAST_TEST_LONGLONG
|
||||
void test_conversion_from_to_longlong();
|
||||
void test_conversion_from_to_ulonglong();
|
||||
#endif
|
||||
void test_volatile_types_conversions();
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
void test_traits();
|
||||
void test_wtraits();
|
||||
@@ -100,13 +82,13 @@ void test_wallocator();
|
||||
#endif
|
||||
void test_char_types_conversions();
|
||||
void operators_overload_test();
|
||||
#ifndef BOOST_NO_CHAR16_T
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
void test_char16_conversions();
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR32_T
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
void test_char32_conversions();
|
||||
#endif
|
||||
|
||||
void test_getting_pointer_to_function();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
@@ -128,18 +110,7 @@ unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
#endif
|
||||
suite->add(BOOST_TEST_CASE(test_bad_lexical_cast));
|
||||
suite->add(BOOST_TEST_CASE(test_no_whitespace_stripping));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_short));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ushort));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_int));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_uint));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_long));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulong));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_intmax_t));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_uintmax_t));
|
||||
#ifdef LCAST_TEST_LONGLONG
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_longlong));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulonglong));
|
||||
#endif
|
||||
suite->add(BOOST_TEST_CASE(test_volatile_types_conversions));
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
suite->add(BOOST_TEST_CASE(&test_traits));
|
||||
suite->add(BOOST_TEST_CASE(&test_wtraits));
|
||||
@@ -149,12 +120,13 @@ unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
|
||||
suite->add(BOOST_TEST_CASE(&test_char_types_conversions));
|
||||
suite->add(BOOST_TEST_CASE(&operators_overload_test));
|
||||
#ifndef BOOST_NO_CHAR16_T
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
suite->add(BOOST_TEST_CASE(&test_char16_conversions));
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR32_T
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
suite->add(BOOST_TEST_CASE(&test_char32_conversions));
|
||||
#endif
|
||||
suite->add(BOOST_TEST_CASE(&test_getting_pointer_to_function));
|
||||
|
||||
return suite;
|
||||
}
|
||||
@@ -399,6 +371,7 @@ void test_conversion_to_wchar_t()
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<wchar_t>(std::wstring(L"Test")), bad_lexical_cast);
|
||||
#endif
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void test_conversion_from_wstring()
|
||||
@@ -417,6 +390,7 @@ void test_conversion_from_wstring()
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<bool>(std::wstring(L"Test")), bad_lexical_cast);
|
||||
#endif
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void test_conversion_to_wstring()
|
||||
@@ -427,7 +401,6 @@ void test_conversion_to_wstring()
|
||||
BOOST_CHECK(str == lexical_cast<std::wstring>(str));
|
||||
BOOST_CHECK(L"123" == lexical_cast<std::wstring>(123));
|
||||
BOOST_CHECK(L"1.23" == lexical_cast<std::wstring>(1.23));
|
||||
BOOST_CHECK(L"1.111111111" == lexical_cast<std::wstring>(1.111111111));
|
||||
BOOST_CHECK(L"1" == lexical_cast<std::wstring>(true));
|
||||
BOOST_CHECK(L"0" == lexical_cast<std::wstring>(false));
|
||||
#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
@@ -442,6 +415,7 @@ void test_conversion_to_wstring()
|
||||
BOOST_CHECK(L" " == lexical_cast<std::wstring>(std::wstring(L" ")));
|
||||
BOOST_CHECK(L"" == lexical_cast<std::wstring>(std::wstring(L"")));
|
||||
#endif
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void test_bad_lexical_cast()
|
||||
@@ -465,445 +439,18 @@ void test_no_whitespace_stripping()
|
||||
BOOST_CHECK_THROW(lexical_cast<int>("123 "), bad_lexical_cast);
|
||||
}
|
||||
|
||||
// Replace "-,999" with "-999".
|
||||
template<class CharT>
|
||||
std::basic_string<CharT> to_str_gcc_workaround(std::basic_string<CharT> str)
|
||||
void test_volatile_types_conversions()
|
||||
{
|
||||
std::locale loc;
|
||||
std::numpunct<CharT> const& np = BOOST_USE_FACET(std::numpunct<CharT>, loc);
|
||||
std::ctype<CharT> const& ct = BOOST_USE_FACET(std::ctype<CharT>, loc);
|
||||
volatile int i1 = 100000;
|
||||
BOOST_CHECK_EQUAL("100000", boost::lexical_cast<std::string>(i1));
|
||||
|
||||
if(np.grouping().empty())
|
||||
return str;
|
||||
volatile const int i2 = 100000;
|
||||
BOOST_CHECK_EQUAL("100000", boost::lexical_cast<std::string>(i2));
|
||||
|
||||
CharT prefix[3] = { ct.widen('-'), np.thousands_sep(), CharT() };
|
||||
|
||||
if(str.find(prefix) != 0)
|
||||
return str;
|
||||
|
||||
prefix[1] = CharT();
|
||||
str.replace(0, 2, prefix);
|
||||
return str;
|
||||
volatile const long int i3 = 1000000;
|
||||
BOOST_CHECK_EQUAL("1000000", boost::lexical_cast<std::string>(i3));
|
||||
}
|
||||
|
||||
template<class CharT, class T>
|
||||
std::basic_string<CharT> to_str(T t)
|
||||
{
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << t;
|
||||
return to_str_gcc_workaround(o.str());
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_integral_to_char(CharT zero)
|
||||
{
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(0)) == zero + 0);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(1)) == zero + 1);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(2)) == zero + 2);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(3)) == zero + 3);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(4)) == zero + 4);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(5)) == zero + 5);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(6)) == zero + 6);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(7)) == zero + 7);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(8)) == zero + 8);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(9)) == zero + 9);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(static_cast<T>(10)), bad_lexical_cast);
|
||||
|
||||
T t = (std::numeric_limits<T>::max)();
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(t), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_char_to_integral(CharT zero)
|
||||
{
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 0)) == static_cast<T>(0) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 1)) == static_cast<T>(1) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 2)) == static_cast<T>(2) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 3)) == static_cast<T>(3) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 4)) == static_cast<T>(4) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 5)) == static_cast<T>(5) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 6)) == static_cast<T>(6) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 7)) == static_cast<T>(7) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 8)) == static_cast<T>(8) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 9)) == static_cast<T>(9) );
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero + 10)), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero - 1)), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_integral_to_integral()
|
||||
{
|
||||
T t = 0;
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
|
||||
// Next two variables are used to supress warnings.
|
||||
int st = 32767; unsigned int ut = st;
|
||||
t = st;
|
||||
BOOST_CHECK(lexical_cast<short>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned short>(t) == ut);
|
||||
BOOST_CHECK(lexical_cast<int>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned int>(t) == ut);
|
||||
BOOST_CHECK(lexical_cast<long>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned long>(t) == ut);
|
||||
|
||||
t = (std::numeric_limits<T>::max)();
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
|
||||
t = (std::numeric_limits<T>::min)();
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_integral_to_string(CharT)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
typedef std::basic_string<CharT> string_type;
|
||||
|
||||
T t;
|
||||
|
||||
t = (limits::min)();
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
t = (limits::max)();
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
if(limits::digits <= 16 && lcast_test_small_integral_types_completely)
|
||||
// min and max have already been tested.
|
||||
for(t = 1 + (limits::min)(); t != (limits::max)(); ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
else
|
||||
{
|
||||
T const min_val = (limits::min)();
|
||||
T const max_val = (limits::max)();
|
||||
T const half_max_val = max_val / 2;
|
||||
T const cnt = lcast_integral_test_counter; // to supress warnings
|
||||
unsigned int const counter = cnt < half_max_val ? cnt : half_max_val;
|
||||
|
||||
unsigned int i;
|
||||
|
||||
// Test values around min:
|
||||
t = min_val;
|
||||
for(i = 0; i < counter; ++i, ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around max:
|
||||
t = max_val;
|
||||
for(i = 0; i < counter; ++i, --t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around zero:
|
||||
if(limits::is_signed)
|
||||
for(t = static_cast<T>(-counter); t < static_cast<T>(counter); ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around 100, 1000, 10000, ...
|
||||
T ten_power = 100;
|
||||
for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10)
|
||||
{
|
||||
// ten_power + 100 probably never overflows
|
||||
for(t = ten_power - 100; t != ten_power + 100; ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_string_to_integral(CharT)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
typedef std::basic_string<CharT> string_type;
|
||||
|
||||
string_type s;
|
||||
string_type const zero = to_str<CharT>(0);
|
||||
string_type const nine = to_str<CharT>(9);
|
||||
T const min_val = (limits::min)();
|
||||
T const max_val = (limits::max)();
|
||||
|
||||
s = to_str<CharT>(min_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(s), min_val);
|
||||
if(limits::is_signed)
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
|
||||
}
|
||||
|
||||
s = to_str<CharT>(max_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(s), max_val);
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
|
||||
|
||||
s = to_str<CharT>(max_val);
|
||||
for (int i =1; i <=10; ++i) {
|
||||
s[s.size()-1] += 1;
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( s ), bad_lexical_cast);
|
||||
}
|
||||
|
||||
s = to_str<CharT>(max_val);
|
||||
std::locale loc;
|
||||
typedef std::numpunct<char> numpunct;
|
||||
if ( BOOST_USE_FACET(numpunct, loc).grouping().empty() ) {
|
||||
// Following tests work well for locale C
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+s), max_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+to_str<CharT>(0)+s), max_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+to_str<CharT>(0)+to_str<CharT>(0)+s), max_val);
|
||||
}
|
||||
|
||||
for (int i =1; i <=256; ++i) {
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(i)+s ), bad_lexical_cast);
|
||||
}
|
||||
|
||||
typedef BOOST_DEDUCED_TYPENAME boost::integral_promotion<T>::type promoted;
|
||||
if ( !(boost::is_same<T, promoted>::value) )
|
||||
{
|
||||
promoted prom = max_val;
|
||||
s = to_str<CharT>(max_val);
|
||||
for (int i =1; i <=256; ++i) {
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(prom+i) ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(i)+s ), bad_lexical_cast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(limits::digits <= 16 && lcast_test_small_integral_types_completely)
|
||||
// min and max have already been tested.
|
||||
for(T t = 1 + min_val; t != max_val; ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
else
|
||||
{
|
||||
T const half_max_val = max_val / 2;
|
||||
T const cnt = lcast_integral_test_counter; // to supress warnings
|
||||
unsigned int const counter = cnt < half_max_val ? cnt : half_max_val;
|
||||
|
||||
T t;
|
||||
unsigned int i;
|
||||
|
||||
// Test values around min:
|
||||
t = min_val;
|
||||
for(i = 0; i < counter; ++i, ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around max:
|
||||
t = max_val;
|
||||
for(i = 0; i < counter; ++i, --t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around zero:
|
||||
if(limits::is_signed)
|
||||
for(t = static_cast<T>(-counter); t < static_cast<T>(counter); ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around 100, 1000, 10000, ...
|
||||
T ten_power = 100;
|
||||
for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10)
|
||||
{
|
||||
// ten_power + 100 probably never overflows
|
||||
for(t = ten_power - 100; t != ten_power + 100; ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_integral_for_locale()
|
||||
{
|
||||
std::locale current_locale;
|
||||
typedef std::numpunct<char> numpunct;
|
||||
numpunct const& np = BOOST_USE_FACET(numpunct, current_locale);
|
||||
if ( !np.grouping().empty() )
|
||||
{
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<T>( std::string("100") + np.thousands_sep() + np.thousands_sep() + "0" )
|
||||
, bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( std::string("100") + np.thousands_sep() ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( np.thousands_sep() + std::string("100") ), bad_lexical_cast);
|
||||
|
||||
// Exception must not be thrown, when we are using no separators at all
|
||||
BOOST_CHECK( lexical_cast<T>("30000") == static_cast<T>(30000) );
|
||||
}
|
||||
|
||||
test_conversion_from_integral_to_integral<T>();
|
||||
test_conversion_from_integral_to_string<T>('0');
|
||||
test_conversion_from_string_to_integral<T>('0');
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T)
|
||||
test_conversion_from_integral_to_string<T>(L'0');
|
||||
test_conversion_from_string_to_integral<T>(L'0');
|
||||
#endif
|
||||
}
|
||||
|
||||
struct restore_oldloc
|
||||
{
|
||||
std::locale oldloc;
|
||||
~restore_oldloc() { std::locale::global(oldloc); }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_integral()
|
||||
{
|
||||
char const zero = '0';
|
||||
signed char const szero = '0';
|
||||
unsigned char const uzero = '0';
|
||||
test_conversion_from_integral_to_char<T>(zero);
|
||||
test_conversion_from_char_to_integral<T>(zero);
|
||||
test_conversion_from_integral_to_char<T>(szero);
|
||||
test_conversion_from_char_to_integral<T>(szero);
|
||||
test_conversion_from_integral_to_char<T>(uzero);
|
||||
test_conversion_from_char_to_integral<T>(uzero);
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
wchar_t const wzero = L'0';
|
||||
test_conversion_from_integral_to_char<T>(wzero);
|
||||
test_conversion_from_char_to_integral<T>(wzero);
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR16_T
|
||||
char16_t const u16zero = u'0';
|
||||
test_conversion_from_integral_to_char<T>(u16zero);
|
||||
test_conversion_from_char_to_integral<T>(u16zero);
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR32_T
|
||||
char32_t const u32zero = u'0';
|
||||
test_conversion_from_integral_to_char<T>(u32zero);
|
||||
test_conversion_from_char_to_integral<T>(u32zero);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(lexical_cast<T>("-1") == static_cast<T>(-1));
|
||||
BOOST_CHECK(lexical_cast<T>("-9") == static_cast<T>(-9));
|
||||
BOOST_CHECK(lexical_cast<T>(-1) == static_cast<T>(-1));
|
||||
BOOST_CHECK(lexical_cast<T>(-9) == static_cast<T>(-9));
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("-1.0"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("-9.0"), bad_lexical_cast);
|
||||
BOOST_CHECK(lexical_cast<T>(-1.0) == static_cast<T>(-1));
|
||||
BOOST_CHECK(lexical_cast<T>(-9.0) == static_cast<T>(-9));
|
||||
|
||||
BOOST_CHECK(lexical_cast<T>(static_cast<T>(1)) == static_cast<T>(1));
|
||||
BOOST_CHECK(lexical_cast<T>(static_cast<T>(9)) == static_cast<T>(9));
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.1f), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.1), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.1L), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.0001f), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.0001), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.0001L), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK(lexical_cast<T>("+1") == static_cast<T>(1) );
|
||||
BOOST_CHECK(lexical_cast<T>("+9") == static_cast<T>(9) );
|
||||
BOOST_CHECK(lexical_cast<T>("+10") == static_cast<T>(10) );
|
||||
BOOST_CHECK(lexical_cast<T>("+90") == static_cast<T>(90) );
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("++1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("-+9"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("--1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("+-9"), bad_lexical_cast);
|
||||
// test_conversion_from_to_integral_for_locale
|
||||
|
||||
// Overflow test case from David W. Birdsall
|
||||
std::string must_owerflow_str = "160000000000000000000";
|
||||
std::string must_owerflow_negative_str = "-160000000000000000000";
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(must_owerflow_str), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(must_owerflow_negative_str), bad_lexical_cast);
|
||||
|
||||
must_owerflow_str += '0';
|
||||
must_owerflow_negative_str += '0';
|
||||
}
|
||||
|
||||
typedef std::numpunct<char> numpunct;
|
||||
|
||||
restore_oldloc guard;
|
||||
std::locale const& oldloc = guard.oldloc;
|
||||
|
||||
std::string grouping1 = BOOST_USE_FACET(numpunct, oldloc).grouping();
|
||||
std::string grouping2(grouping1);
|
||||
|
||||
test_conversion_from_to_integral_for_locale<T>();
|
||||
|
||||
try
|
||||
{
|
||||
std::locale newloc("");
|
||||
std::locale::global(newloc);
|
||||
|
||||
grouping2 = BOOST_USE_FACET(numpunct, newloc).grouping();
|
||||
}
|
||||
catch(std::exception const& ex)
|
||||
{
|
||||
std::string msg("Failed to set system locale: ");
|
||||
msg += ex.what();
|
||||
BOOST_TEST_MESSAGE(msg);
|
||||
}
|
||||
|
||||
if(grouping1 != grouping2)
|
||||
test_conversion_from_to_integral_for_locale<T>();
|
||||
|
||||
if(grouping1.empty() && grouping2.empty())
|
||||
BOOST_TEST_MESSAGE("Formatting with thousands_sep has not been tested");
|
||||
}
|
||||
|
||||
void test_conversion_from_to_short()
|
||||
{
|
||||
test_conversion_from_to_integral<short>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ushort()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned short>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_int()
|
||||
{
|
||||
test_conversion_from_to_integral<int>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_uint()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned int>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_long()
|
||||
{
|
||||
test_conversion_from_to_integral<long>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulong()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned long>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_intmax_t()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::intmax_t>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_uintmax_t()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::uintmax_t>();
|
||||
}
|
||||
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
|
||||
void test_conversion_from_to_longlong()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::long_long_type>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulonglong()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::ulong_long_type>();
|
||||
}
|
||||
|
||||
#elif defined(BOOST_HAS_MS_INT64)
|
||||
|
||||
void test_conversion_from_to_longlong()
|
||||
{
|
||||
test_conversion_from_to_integral<__int64>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulonglong()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned __int64>();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
void test_traits()
|
||||
{
|
||||
@@ -929,6 +476,11 @@ void test_wtraits()
|
||||
|
||||
void test_allocator()
|
||||
{
|
||||
// Following test cause compilation error on MSVC2012:
|
||||
// (Reason: cannot convert from 'std::_Wrap_alloc<_Alloc>' to 'const my_allocator<CharT>')
|
||||
//
|
||||
// MSVC developer is notified about this issue
|
||||
#if !defined(_MSC_VER) || (_MSC_VER < 1700)
|
||||
typedef std::basic_string< char
|
||||
, std::char_traits<char>
|
||||
, my_allocator<char>
|
||||
@@ -941,10 +493,16 @@ void test_allocator()
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(1) == "1");
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>("s") == s);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(std::string("s")) == s);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_wallocator()
|
||||
{
|
||||
// Following test cause compilation error on MSVC2012:
|
||||
// (Reason: cannot convert from 'std::_Wrap_alloc<_Alloc>' to 'const my_allocator<CharT>')
|
||||
//
|
||||
// MSVC developer is notified about this issue
|
||||
#if !defined(_MSC_VER) || (_MSC_VER < 1700)
|
||||
typedef std::basic_string< wchar_t
|
||||
, std::char_traits<wchar_t>
|
||||
, my_allocator<wchar_t>
|
||||
@@ -957,10 +515,12 @@ void test_wallocator()
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(1) == L"1");
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(L"s") == s);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(std::wstring(L"s")) == s);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
void test_char_types_conversions()
|
||||
{
|
||||
const char c_arr[] = "Test array of chars";
|
||||
@@ -1025,7 +585,7 @@ void operators_overload_test()
|
||||
}
|
||||
|
||||
|
||||
#ifndef BOOST_NO_CHAR16_T
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
void test_char16_conversions()
|
||||
{
|
||||
BOOST_CHECK(u"100" == lexical_cast<std::u16string>(u"100"));
|
||||
@@ -1033,7 +593,7 @@ void test_char16_conversions()
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_CHAR32_T
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
void test_char32_conversions()
|
||||
{
|
||||
BOOST_CHECK(U"100" == lexical_cast<std::u32string>(U"100"));
|
||||
@@ -1041,3 +601,17 @@ void test_char32_conversions()
|
||||
}
|
||||
#endif
|
||||
|
||||
template <class To, class From, class Func>
|
||||
To try_cast_by_ptr(const From& from, const Func& f) {
|
||||
return f(from);
|
||||
};
|
||||
|
||||
void test_getting_pointer_to_function()
|
||||
{
|
||||
// Just checking that &lexical_cast<To, From> is not ambiguous
|
||||
BOOST_CHECK_EQUAL(100, try_cast_by_ptr<int>("100", &boost::lexical_cast<int, const char[4]>));
|
||||
BOOST_CHECK_EQUAL(100, try_cast_by_ptr<int>("100", &boost::lexical_cast<int, std::string>));
|
||||
BOOST_CHECK_EQUAL(std::string("100"), try_cast_by_ptr<std::string>(100, &boost::lexical_cast<std::string, int>));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ using namespace boost;
|
||||
using std::cout;
|
||||
|
||||
|
||||
int test_main( int argc, char * argv[] )
|
||||
int test_main( int , char * [] )
|
||||
{
|
||||
|
||||
# ifdef NDEBUG
|
||||
@@ -96,5 +96,6 @@ int test_main( int argc, char * argv[] )
|
||||
{ cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
(void)ul; // Supressing GCC warning about set but unused wariable
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#==============================================================================
|
||||
# Copyright (c) 2012 Antony Polukhin
|
||||
#
|
||||
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#==============================================================================
|
||||
|
||||
# performance tests
|
||||
import testing ;
|
||||
import path ;
|
||||
|
||||
path-constant TEST_DIR : . ;
|
||||
|
||||
project performance/test
|
||||
: source-location ./
|
||||
: requirements
|
||||
# <library>/boost/chrono//boost_chrono
|
||||
# <library>/boost/system//boost_system
|
||||
<link>static
|
||||
<target-os>freebsd:<linkflags>"-lrt"
|
||||
<target-os>linux:<linkflags>"-lrt"
|
||||
<toolset>gcc:<cxxflags>-fvisibility=hidden
|
||||
<toolset>intel-linux:<cxxflags>-fvisibility=hidden
|
||||
<toolset>sun:<cxxflags>-xldscope=hidden
|
||||
: default-build release
|
||||
;
|
||||
|
||||
run performance_test.cpp : $(TEST_DIR) ;
|
||||
|
||||
@@ -0,0 +1,369 @@
|
||||
// (C) Copyright Antony Polukhin 2012.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org/libs/config for most recent version.
|
||||
|
||||
//
|
||||
// Testing lexical_cast<> performance
|
||||
//
|
||||
|
||||
#define BOOST_ERROR_CODE_HEADER_ONLY
|
||||
#define BOOST_CHRONO_HEADER_ONLY
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/chrono.hpp>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <boost/container/string.hpp>
|
||||
|
||||
// File to output data
|
||||
std::fstream fout;
|
||||
|
||||
namespace boost {
|
||||
inline std::istream& operator>> (std::istream& in, boost::array<char,50>& res) {
|
||||
in >> res.begin();
|
||||
return in;
|
||||
}
|
||||
}
|
||||
|
||||
template <class OutT, class InT>
|
||||
static inline void test_lexical(const InT& in_val) {
|
||||
OutT out_val = boost::lexical_cast<OutT>(in_val);
|
||||
(void)out_val;
|
||||
}
|
||||
|
||||
template <class OutT, class InT>
|
||||
static inline void test_ss_constr(const InT& in_val) {
|
||||
OutT out_val;
|
||||
std::stringstream ss;
|
||||
ss << in_val;
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
ss >> out_val;
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
}
|
||||
|
||||
template <class OutT, class CharT, std::size_t N>
|
||||
static inline void test_ss_constr(const boost::array<CharT, N>& in_val) {
|
||||
OutT out_val;
|
||||
std::stringstream ss;
|
||||
ss << in_val.begin();
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
ss >> out_val;
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
}
|
||||
|
||||
template <class OutT, class StringStreamT, class CharT, std::size_t N>
|
||||
static inline void test_ss_noconstr(StringStreamT& ss, const boost::array<CharT, N>& in_val) {
|
||||
OutT out_val;
|
||||
ss << in_val.begin(); // ss is an instance of std::stringstream
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
ss >> out_val;
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
/* reseting std::stringstream to use it again */
|
||||
ss.str(std::string());
|
||||
ss.clear();
|
||||
}
|
||||
|
||||
template <class OutT, class StringStreamT, class InT>
|
||||
static inline void test_ss_noconstr(StringStreamT& ss, const InT& in_val) {
|
||||
OutT out_val;
|
||||
ss << in_val; // ss is an instance of std::stringstream
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
ss >> out_val;
|
||||
if (ss.fail()) throw std::logic_error("descr");
|
||||
/* reseting std::stringstream to use it again */
|
||||
ss.str(std::string());
|
||||
ss.clear();
|
||||
}
|
||||
|
||||
struct structure_sprintf {
|
||||
template <class OutT, class BufferT, class InT>
|
||||
static inline void test(BufferT* buffer, const InT& in_val, const char* const conv) {
|
||||
sprintf(buffer, conv, in_val);
|
||||
OutT out_val(buffer);
|
||||
}
|
||||
|
||||
template <class OutT, class BufferT>
|
||||
static inline void test(BufferT* buffer, const std::string& in_val, const char* const conv) {
|
||||
sprintf(buffer, conv, in_val.c_str());
|
||||
OutT out_val(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
struct structure_sscanf {
|
||||
template <class OutT, class BufferT, class CharT, std::size_t N>
|
||||
static inline void test(BufferT* /*buffer*/, const boost::array<CharT, N>& in_val, const char* const conv) {
|
||||
OutT out_val;
|
||||
sscanf(in_val.cbegin(), conv, &out_val);
|
||||
}
|
||||
|
||||
template <class OutT, class BufferT, class InT>
|
||||
static inline void test(BufferT* /*buffer*/, const InT& in_val, const char* const conv) {
|
||||
OutT out_val;
|
||||
sscanf(reinterpret_cast<const char*>(in_val), conv, &out_val);
|
||||
}
|
||||
|
||||
template <class OutT, class BufferT>
|
||||
static inline void test(BufferT* /*buffer*/, const std::string& in_val, const char* const conv) {
|
||||
OutT out_val;
|
||||
sscanf(in_val.c_str(), conv, &out_val);
|
||||
}
|
||||
|
||||
template <class OutT, class BufferT>
|
||||
static inline void test(BufferT* /*buffer*/, const boost::iterator_range<const char*>& in_val, const char* const conv) {
|
||||
OutT out_val;
|
||||
sscanf(in_val.begin(), conv, &out_val);
|
||||
}
|
||||
};
|
||||
|
||||
struct structure_fake {
|
||||
template <class OutT, class BufferT, class InT>
|
||||
static inline void test(BufferT* /*buffer*/, const InT& /*in_val*/, const char* const /*conv*/) {}
|
||||
};
|
||||
|
||||
static const int fake_test_value = 9999;
|
||||
|
||||
template <class T>
|
||||
static inline void min_fancy_output(T v1, T v2, T v3, T v4) {
|
||||
const char beg_mark[] = "!!! *";
|
||||
const char end_mark[] = "* !!!";
|
||||
const char no_mark[] = "";
|
||||
|
||||
unsigned int res = 4;
|
||||
if (v1 < v2 && v1 < v3 && v1 < v4) res = 1;
|
||||
if (v2 < v1 && v2 < v3 && v2 < v4) res = 2;
|
||||
if (v3 < v1 && v3 < v2 && v3 < v4) res = 3;
|
||||
|
||||
fout << "[ "
|
||||
<< (res == 1 ? beg_mark : no_mark)
|
||||
;
|
||||
|
||||
if (v1) fout << v1;
|
||||
else fout << "<1";
|
||||
|
||||
fout << (res == 1 ? end_mark : no_mark)
|
||||
<< " ][ "
|
||||
<< (res == 2 ? beg_mark : no_mark)
|
||||
;
|
||||
|
||||
if (v2) fout << v2;
|
||||
else fout << "<1";
|
||||
|
||||
fout << (res == 2 ? end_mark : no_mark)
|
||||
<< " ][ "
|
||||
<< (res == 3 ? beg_mark : no_mark)
|
||||
;
|
||||
|
||||
if (v3) fout << v3;
|
||||
else fout << "<1";
|
||||
|
||||
fout << (res == 3 ? end_mark : no_mark)
|
||||
<< " ][ "
|
||||
<< (res == 4 ? beg_mark : no_mark)
|
||||
;
|
||||
|
||||
if (!v4) fout << "<1";
|
||||
else if (v4 == fake_test_value) fout << "---";
|
||||
else fout << v4;
|
||||
|
||||
fout
|
||||
<< (res == 4 ? end_mark : no_mark)
|
||||
<< " ]";
|
||||
}
|
||||
|
||||
template <unsigned int IetartionsCountV, class ToT, class SprintfT, class FromT>
|
||||
static inline void perf_test_impl(const FromT& in_val, const char* const conv) {
|
||||
|
||||
typedef boost::chrono::steady_clock test_clock;
|
||||
test_clock::time_point start;
|
||||
typedef boost::chrono::milliseconds duration_t;
|
||||
duration_t lexical_cast_time, ss_constr_time, ss_noconstr_time, printf_time;
|
||||
|
||||
start = test_clock::now();
|
||||
for (unsigned int i = 0; i < IetartionsCountV; ++i) {
|
||||
test_lexical<ToT>(in_val);
|
||||
test_lexical<ToT>(in_val);
|
||||
test_lexical<ToT>(in_val);
|
||||
test_lexical<ToT>(in_val);
|
||||
}
|
||||
lexical_cast_time = boost::chrono::duration_cast<duration_t>(test_clock::now() - start);
|
||||
|
||||
|
||||
start = test_clock::now();
|
||||
for (unsigned int i = 0; i < IetartionsCountV; ++i) {
|
||||
test_ss_constr<ToT>(in_val);
|
||||
test_ss_constr<ToT>(in_val);
|
||||
test_ss_constr<ToT>(in_val);
|
||||
test_ss_constr<ToT>(in_val);
|
||||
}
|
||||
ss_constr_time = boost::chrono::duration_cast<duration_t>(test_clock::now() - start);
|
||||
|
||||
std::stringstream ss;
|
||||
start = test_clock::now();
|
||||
for (unsigned int i = 0; i < IetartionsCountV; ++i) {
|
||||
test_ss_noconstr<ToT>(ss, in_val);
|
||||
test_ss_noconstr<ToT>(ss, in_val);
|
||||
test_ss_noconstr<ToT>(ss, in_val);
|
||||
test_ss_noconstr<ToT>(ss, in_val);
|
||||
}
|
||||
ss_noconstr_time = boost::chrono::duration_cast<duration_t>(test_clock::now() - start);
|
||||
|
||||
|
||||
char buffer[128];
|
||||
start = test_clock::now();
|
||||
for (unsigned int i = 0; i < IetartionsCountV; ++i) {
|
||||
SprintfT::template test<ToT>(buffer, in_val, conv);
|
||||
SprintfT::template test<ToT>(buffer, in_val, conv);
|
||||
SprintfT::template test<ToT>(buffer, in_val, conv);
|
||||
SprintfT::template test<ToT>(buffer, in_val, conv);
|
||||
}
|
||||
printf_time = boost::chrono::duration_cast<duration_t>(test_clock::now() - start);
|
||||
|
||||
min_fancy_output(
|
||||
lexical_cast_time.count(),
|
||||
ss_constr_time.count(),
|
||||
ss_noconstr_time.count(),
|
||||
boost::is_same<SprintfT, structure_fake>::value ? fake_test_value : printf_time.count()
|
||||
);
|
||||
}
|
||||
|
||||
template <class ToT, class SprintfT, class FromT>
|
||||
static inline void perf_test(const std::string& test_name, const FromT& in_val, const char* const conv) {
|
||||
const unsigned int ITERATIONSCOUNT = 100000;
|
||||
fout << " [[ " << test_name << " ]";
|
||||
|
||||
perf_test_impl<ITERATIONSCOUNT/4, ToT, SprintfT>(in_val, conv);
|
||||
|
||||
fout << "]\n";
|
||||
}
|
||||
|
||||
|
||||
template <class ConverterT>
|
||||
void string_like_test_set(const std::string& from) {
|
||||
typedef structure_sscanf ssc_t;
|
||||
ConverterT conv;
|
||||
|
||||
perf_test<char, ssc_t>(from + "->char", conv("c"), "%c");
|
||||
perf_test<signed char, ssc_t>(from + "->signed char", conv("c"), "%hhd");
|
||||
perf_test<unsigned char, ssc_t>(from + "->unsigned char", conv("c"), "%hhu");
|
||||
|
||||
perf_test<int, ssc_t>(from + "->int", conv("100"), "%d");
|
||||
perf_test<short, ssc_t>(from + "->short", conv("100"), "%hd");
|
||||
perf_test<long int, ssc_t>(from + "->long int", conv("100"), "%ld");
|
||||
perf_test<boost::long_long_type, ssc_t>(from + "->long long", conv("100"), "%lld");
|
||||
|
||||
perf_test<unsigned int, ssc_t>(from + "->unsigned int", conv("100"), "%u");
|
||||
perf_test<unsigned short, ssc_t>(from + "->unsigned short", conv("100"), "%hu");
|
||||
perf_test<unsigned long int, ssc_t>(from + "->unsigned long int", conv("100"), "%lu");
|
||||
perf_test<boost::ulong_long_type, ssc_t>(from + "->unsigned long long", conv("100"), "%llu");
|
||||
|
||||
// perf_test<bool, ssc_t>(from + "->bool", conv("1"), "%");
|
||||
|
||||
perf_test<float, ssc_t>(from + "->float", conv("1.123"), "%f");
|
||||
perf_test<double, ssc_t>(from + "->double", conv("1.123"), "%lf");
|
||||
perf_test<long double, ssc_t>(from + "->long double", conv("1.123"), "%Lf");
|
||||
perf_test<boost::array<char, 50>, ssc_t>(from + "->array<char, 50>", conv("1.123"), "%s");
|
||||
|
||||
perf_test<std::string, structure_fake>(from + "->string", conv("string"), "%Lf");
|
||||
perf_test<boost::container::string, structure_fake>(from + "->container::string"
|
||||
, conv("string"), "%Lf");
|
||||
|
||||
}
|
||||
|
||||
struct to_string_conv {
|
||||
std::string operator()(const char* const c) const {
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
struct to_char_conv {
|
||||
const char* operator()(const char* const c) const {
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
struct to_uchar_conv {
|
||||
const unsigned char* operator()(const char* const c) const {
|
||||
return reinterpret_cast<const unsigned char*>(c);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct to_schar_conv {
|
||||
const signed char* operator()(const char* const c) const {
|
||||
return reinterpret_cast<const signed char*>(c);
|
||||
}
|
||||
};
|
||||
|
||||
struct to_iterator_range {
|
||||
boost::iterator_range<const char*> operator()(const char* const c) const {
|
||||
return boost::make_iterator_range(c, c + std::strlen(c));
|
||||
}
|
||||
};
|
||||
|
||||
struct to_array_50 {
|
||||
boost::array<char, 50> operator()(const char* const c) const {
|
||||
boost::array<char, 50> ret;
|
||||
std::strcpy(ret.begin(), c);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
BOOST_ASSERT(argc >= 2);
|
||||
std::string output_path(argv[1]);
|
||||
output_path += "/results.txt";
|
||||
fout.open(output_path.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
|
||||
BOOST_ASSERT(fout);
|
||||
|
||||
fout << "[section " << BOOST_COMPILER << "]\n"
|
||||
<< "[table:id Performance Table ( "<< BOOST_COMPILER << ")\n"
|
||||
<< "[[From->To] [lexical_cast] [std::stringstream with construction] "
|
||||
<< "[std::stringstream without construction][scanf/printf]]\n";
|
||||
|
||||
|
||||
// From std::string to ...
|
||||
string_like_test_set<to_string_conv>("string");
|
||||
|
||||
// From ... to std::string
|
||||
perf_test<std::string, structure_sprintf>("string->char", 'c', "%c");
|
||||
perf_test<std::string, structure_sprintf>("string->signed char", static_cast<signed char>('c'), "%hhd");
|
||||
perf_test<std::string, structure_sprintf>("string->unsigned char", static_cast<unsigned char>('c'), "%hhu");
|
||||
|
||||
perf_test<std::string, structure_sprintf>("int->string", 100, "%d");
|
||||
perf_test<std::string, structure_sprintf>("short->string", static_cast<short>(100), "%hd");
|
||||
perf_test<std::string, structure_sprintf>("long int->string", 100l, "%ld");
|
||||
perf_test<std::string, structure_sprintf>("long long->string", 100ll, "%lld");
|
||||
|
||||
perf_test<std::string, structure_sprintf>("unsigned int->string", static_cast<unsigned short>(100u), "%u");
|
||||
perf_test<std::string, structure_sprintf>("unsigned short->string", 100u, "%hu");
|
||||
perf_test<std::string, structure_sprintf>("unsigned long int->string", 100ul, "%lu");
|
||||
perf_test<std::string, structure_sprintf>("unsigned long long->string", static_cast<boost::ulong_long_type>(100), "%llu");
|
||||
|
||||
// perf_test<bool, structure_sscanf>("bool->string", std::string("1"), "%");
|
||||
|
||||
perf_test<std::string, structure_sprintf>("float->string", 1.123f, "%f");
|
||||
perf_test<std::string, structure_sprintf>("double->string", 1.123, "%lf");
|
||||
perf_test<std::string, structure_sprintf>("long double->string", 1.123L, "%Lf");
|
||||
|
||||
|
||||
string_like_test_set<to_char_conv>("char*");
|
||||
string_like_test_set<to_uchar_conv>("unsigned char*");
|
||||
string_like_test_set<to_schar_conv>("signed char*");
|
||||
string_like_test_set<to_iterator_range>("iterator_range<char*>");
|
||||
string_like_test_set<to_array_50>("array<char, 50>");
|
||||
|
||||
perf_test<int, structure_fake>("int->int", 100, "");
|
||||
perf_test<double, structure_fake>("float->double", 100.0f, "");
|
||||
perf_test<signed char, structure_fake>("char->signed char", 'c', "");
|
||||
|
||||
|
||||
fout << "]\n"
|
||||
<< "[endsect]\n\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+40
-29
@@ -1,42 +1,53 @@
|
||||
# Signals library
|
||||
|
||||
# Copyright (C) 2001-2003 Douglas Gregor
|
||||
# Copyright (C) 2011-2012 Antony Polukhin
|
||||
#
|
||||
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#
|
||||
|
||||
# Permission to copy, use, sell and distribute this software is granted
|
||||
# provided this copyright notice appears in all copies. Permission to modify
|
||||
# the code and to distribute modified code is granted provided this copyright
|
||||
# notice appears in all copies, and a notice that the code was modified is
|
||||
# included with the copyright notice. This software is provided "as is"
|
||||
# without express or implied warranty, and with no claim as to its suitability
|
||||
# for any purpose.
|
||||
|
||||
# For more information, see http://www.boost.org/
|
||||
|
||||
# bring in rules for testing
|
||||
import testing ;
|
||||
import feature ;
|
||||
|
||||
project
|
||||
: requirements
|
||||
<library>/boost/test//boost_unit_test_framework
|
||||
<link>static
|
||||
<toolset>gcc-4.8:<define>BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
|
||||
;
|
||||
|
||||
# Thanks to Steven Watanabe for helping with <nowchar> feature
|
||||
feature.feature nowchar : on :
|
||||
composite optional propagated link-incompatible ;
|
||||
feature.compose <nowchar>on : <cxxflags>/Zc:wchar_t- ;
|
||||
|
||||
feature.compose <nowchar>on : <cxxflags>/Zc:wchar_t- ;
|
||||
|
||||
test-suite conversion
|
||||
: [ run implicit_cast.cpp ]
|
||||
[ compile-fail implicit_cast_fail.cpp ]
|
||||
[ run ../cast_test.cpp ]
|
||||
[ run ../numeric_cast_test.cpp ]
|
||||
[ run ../lexical_cast_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_loopback_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_abstract_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_noncopyable_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_vc8_bug_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_wchars_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_float_types_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_inf_nan_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_pointers_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run ../numeric_cast_test.cpp ]
|
||||
[ run ../lexical_cast_test.cpp ]
|
||||
[ run lexical_cast_loopback_test.cpp ]
|
||||
[ run lexical_cast_abstract_test.cpp ]
|
||||
[ run lexical_cast_noncopyable_test.cpp ]
|
||||
[ run lexical_cast_vc8_bug_test.cpp ]
|
||||
[ run lexical_cast_wchars_test.cpp ]
|
||||
[ run lexical_cast_float_types_test.cpp ]
|
||||
[ run lexical_cast_inf_nan_test.cpp ]
|
||||
[ run lexical_cast_containers_test.cpp ]
|
||||
[ run lexical_cast_empty_input_test.cpp ]
|
||||
[ run lexical_cast_pointers_test.cpp ]
|
||||
[ compile lexical_cast_typedefed_wchar_test.cpp : <toolset>msvc:<nowchar>on ]
|
||||
[ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/<link>static : : : <toolset>msvc:<nowchar>on ]
|
||||
[ run lexical_cast_typedefed_wchar_test_runtime.cpp : : : <toolset>msvc:<nowchar>on <toolset>msvc,<stdlib>stlport:<build>no ]
|
||||
[ run lexical_cast_no_locale_test.cpp : : : <define>BOOST_NO_STD_LOCALE <define>BOOST_LEXICAL_CAST_ASSUME_C_LOCALE ]
|
||||
[ run lexical_cast_no_exceptions_test.cpp : : : <define>BOOST_NO_EXCEPTIONS
|
||||
<toolset>gcc-4.3:<cflags>-fno-exceptions
|
||||
<toolset>gcc-4.4:<cflags>-fno-exceptions
|
||||
<toolset>gcc-4.5:<cflags>-fno-exceptions
|
||||
<toolset>gcc-4.6:<cflags>-fno-exceptions
|
||||
<toolset>gcc-4.7:<cflags>-fno-exceptions
|
||||
]
|
||||
[ run lexical_cast_iterator_range_test.cpp ]
|
||||
[ run lexical_cast_arrays_test.cpp ]
|
||||
[ run lexical_cast_integral_types_test.cpp ]
|
||||
;
|
||||
|
||||
|
||||
|
||||
@@ -28,5 +28,11 @@ int main()
|
||||
|
||||
type<foo> f = check_return(boost::implicit_cast<foo>("hello"));
|
||||
type<long> z = check_return(boost::implicit_cast<long>(foo("hello")));
|
||||
|
||||
// warning supression:
|
||||
(void)x;
|
||||
(void)f;
|
||||
(void)z;
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
@@ -19,4 +19,6 @@ struct foo
|
||||
int test_main(int, char*[])
|
||||
{
|
||||
foo x = implicit_cast<foo>("foobar");
|
||||
(void)x; // warning suppression.
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
// Testing boost::lexical_cast with boost::container::string.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <boost/array.hpp>
|
||||
|
||||
void testing_boost_array_output_conversion();
|
||||
void testing_std_array_output_conversion();
|
||||
|
||||
void testing_boost_array_input_conversion();
|
||||
void testing_std_array_input_conversion();
|
||||
|
||||
using namespace boost;
|
||||
|
||||
boost::unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("Testing boost::lexical_cast with boost::array and std::array");
|
||||
|
||||
suite->add(BOOST_TEST_CASE(testing_boost_array_output_conversion));
|
||||
suite->add(BOOST_TEST_CASE(testing_std_array_output_conversion));
|
||||
suite->add(BOOST_TEST_CASE(testing_boost_array_input_conversion));
|
||||
suite->add(BOOST_TEST_CASE(testing_std_array_input_conversion));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
template <template <class, std::size_t> class ArrayT, class T>
|
||||
static void testing_template_array_output_on_spec_value(T val)
|
||||
{
|
||||
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;
|
||||
|
||||
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, 300> 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);
|
||||
}
|
||||
|
||||
{
|
||||
const warr_type res = lexical_cast<warr_type>(val);
|
||||
BOOST_CHECK(res.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");
|
||||
|
||||
{
|
||||
u16arr_type res = lexical_cast<u16arr_type>(val);
|
||||
BOOST_CHECK(res.begin() == u16ethalon);
|
||||
}
|
||||
|
||||
{
|
||||
const u16arr_type res = lexical_cast<u16arr_type>(val);
|
||||
BOOST_CHECK(res.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");
|
||||
|
||||
{
|
||||
u32arr_type res = lexical_cast<u32arr_type>(val);
|
||||
BOOST_CHECK(res.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);
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<u32short_arr_type>(val), boost::bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void testing_boost_array_output_conversion()
|
||||
{
|
||||
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));
|
||||
testing_template_array_output_on_spec_value<boost::array>(static_cast<unsigned int>(100));
|
||||
}
|
||||
|
||||
void testing_std_array_output_conversion()
|
||||
{
|
||||
#if !defined(BOOST_NO_CXX11_HDR_ARRAY) && defined(BOOST_HAS_TR1_ARRAY)
|
||||
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));
|
||||
testing_template_array_output_on_spec_value<std::array>(static_cast<unsigned int>(100));
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
template <template <class, std::size_t> class ArrayT>
|
||||
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");
|
||||
|
||||
ArrayT<const char, 3> var_none_terminated_const_char = {{ '1', '0', '0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_none_terminated_const_char), "100");
|
||||
|
||||
const ArrayT<char, 4> var_zero_terminated_const_var = {{ '1', '0', '0', '\0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_zero_terminated_const_var), "100");
|
||||
|
||||
const ArrayT<char, 3> var_none_terminated_const_var = {{ '1', '0', '0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_none_terminated_const_var), "100");
|
||||
|
||||
const ArrayT<const char, 4> var_zero_terminated_const_var_const_char = {{ '1', '0', '0', '\0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_zero_terminated_const_var_const_char), "100");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
{
|
||||
const ArrayT<const unsigned char, 4> var_zero_terminated_const_var_const_char = {{ '1', '0', '0', '\0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_zero_terminated_const_var_const_char), "100");
|
||||
|
||||
const ArrayT<const unsigned 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");
|
||||
}
|
||||
|
||||
{
|
||||
const ArrayT<const signed char, 4> var_zero_terminated_const_var_const_char = {{ '1', '0', '0', '\0'}};
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(var_zero_terminated_const_var_const_char), "100");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
#if !defined(BOOST_NO_STRINGSTREAM) && !defined(BOOST_NO_STD_WSTRING)
|
||||
{
|
||||
const ArrayT<const wchar_t, 4> var_zero_terminated_const_var_const_char = {{ L'1', L'0', L'0', L'\0'}};
|
||||
BOOST_CHECK(lexical_cast<std::wstring>(var_zero_terminated_const_var_const_char) == L"100");
|
||||
|
||||
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
|
||||
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
{
|
||||
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");
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
{
|
||||
const ArrayT<const char32_t, 4> var_zero_terminated_const_var_const_char = {{ U'1', U'0', U'0', U'\0'}};
|
||||
BOOST_CHECK(lexical_cast<std::u32string>(var_zero_terminated_const_var_const_char) == U"100");
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
void testing_boost_array_input_conversion()
|
||||
{
|
||||
testing_generic_array_input_conversion<boost::array>();
|
||||
}
|
||||
|
||||
void testing_std_array_input_conversion()
|
||||
{
|
||||
#if !defined(BOOST_NO_CXX11_HDR_ARRAY) && defined(BOOST_HAS_TR1_ARRAY)
|
||||
testing_generic_array_input_conversion<std::array>();
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
#include <boost/container/string.hpp>
|
||||
|
||||
void testing_boost_containers_basic_string();
|
||||
void testing_boost_containers_string_std_string();
|
||||
void testing_boost_containers_string_widening();
|
||||
|
||||
|
||||
using namespace boost;
|
||||
|
||||
@@ -21,12 +24,14 @@ boost::unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("Testing boost::lexical_cast with boost::container::string");
|
||||
suite->add(BOOST_TEST_CASE(testing_boost_containers_basic_string));
|
||||
suite->add(BOOST_TEST_CASE(testing_boost_containers_string_std_string));
|
||||
suite->add(BOOST_TEST_CASE(testing_boost_containers_string_widening));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
void testing_boost_containers_basic_string()
|
||||
{
|
||||
{
|
||||
BOOST_CHECK("100" == lexical_cast<boost::container::string>("100"));
|
||||
BOOST_CHECK(L"100" == lexical_cast<boost::container::wstring>(L"100"));
|
||||
|
||||
@@ -35,4 +40,44 @@ void testing_boost_containers_basic_string()
|
||||
BOOST_CHECK(1000 == lexical_cast<int>(str));
|
||||
}
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
|
||||
#define BOOST_LCAST_NO_WCHAR_T
|
||||
#endif
|
||||
|
||||
void testing_boost_containers_string_std_string()
|
||||
{
|
||||
std::string std_str("std_str");
|
||||
boost::container::string boost_str("boost_str");
|
||||
BOOST_CHECK(boost::lexical_cast<std::string>(boost_str) == "boost_str");
|
||||
BOOST_CHECK(boost::lexical_cast<boost::container::string>(std_str) == "std_str");
|
||||
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
std::wstring std_wstr(L"std_wstr");
|
||||
boost::container::wstring boost_wstr(L"boost_wstr");
|
||||
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(boost_wstr) == L"boost_wstr");
|
||||
BOOST_CHECK(boost::lexical_cast<boost::container::wstring>(std_wstr) == L"std_wstr");
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void testing_boost_containers_string_widening()
|
||||
{
|
||||
const char char_array[] = "Test string";
|
||||
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
const wchar_t wchar_array[] = L"Test string";
|
||||
BOOST_CHECK(boost::lexical_cast<boost::container::wstring>(char_array) == wchar_array);
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
|
||||
const char16_t char16_array[] = u"Test string";
|
||||
BOOST_CHECK(boost::lexical_cast<boost::container::basic_string<char16_t> >(char_array) == char16_array);
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
|
||||
const char32_t char32_array[] = U"Test string";
|
||||
BOOST_CHECK(boost::lexical_cast<boost::container::basic_string<char32_t> >(char_array) == char32_array);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -138,6 +138,19 @@ void test_empty_vector()
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
}
|
||||
|
||||
|
||||
struct my_string {
|
||||
friend std::ostream &operator<<(std::ostream& sout, my_string const&/* st*/) {
|
||||
return sout << "";
|
||||
}
|
||||
};
|
||||
|
||||
void test_empty_zero_terminated_string()
|
||||
{
|
||||
my_string st;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(st), std::string());;
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
@@ -146,6 +159,7 @@ unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_string));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_user_class));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_vector));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_zero_terminated_string));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
@@ -105,13 +105,13 @@ void test_conversion_from_to_float_for_locale()
|
||||
|
||||
|
||||
/*
|
||||
* Converts char* [and wchar_t] to float number type and checks, that generated
|
||||
* number is in interval [base_value-epsilon, base_value+epsilon].
|
||||
* Converts char* [and wchar_t*] to float number type and checks, that generated
|
||||
* number does not exceeds allowed epsilon.
|
||||
*/
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
#define CHECK_CLOSE_ABS_DIFF(VAL,PREFIX) \
|
||||
converted_val = lexical_cast<test_t>(#VAL); \
|
||||
BOOST_CHECK_CLOSE_FRACTION( (VAL ## L? VAL ## L : std::numeric_limits<test_t>::epsilon()), \
|
||||
BOOST_CHECK_CLOSE_FRACTION( (VAL ## L? VAL ## L : std::numeric_limits<test_t>::epsilon()), \
|
||||
(converted_val ? converted_val : std::numeric_limits<test_t>::epsilon()), \
|
||||
std::numeric_limits<test_t>::epsilon() \
|
||||
); \
|
||||
@@ -120,7 +120,7 @@ void test_conversion_from_to_float_for_locale()
|
||||
#else
|
||||
#define CHECK_CLOSE_ABS_DIFF(VAL,TYPE) \
|
||||
converted_val = lexical_cast<test_t>(#VAL); \
|
||||
BOOST_CHECK_CLOSE_FRACTION( (VAL ## L? VAL ## L : std::numeric_limits<test_t>::epsilon()), \
|
||||
BOOST_CHECK_CLOSE_FRACTION( (VAL ## L? VAL ## L : std::numeric_limits<test_t>::epsilon()), \
|
||||
(converted_val ? converted_val : std::numeric_limits<test_t>::epsilon()), \
|
||||
std::numeric_limits<test_t>::epsilon() \
|
||||
);
|
||||
@@ -237,7 +237,7 @@ void test_converion_to_float_types()
|
||||
CHECK_CLOSE_ABS_DIFF(-10101.0E-011, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10101093, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(10101093, test_t);
|
||||
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(-.34, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(.34, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(.34e10, test_t);
|
||||
@@ -261,6 +261,7 @@ void test_converion_to_float_types()
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(".e"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(".11111111111111111111111111111111111111111111111111111111111111111111ee"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(".11111111111111111111111111111111111111111111111111111111111111111111e-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("."), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("-B"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("0xB"), bad_lexical_cast);
|
||||
@@ -276,6 +277,7 @@ void test_converion_to_float_types()
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>('\0'), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>('-'), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>('.'), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -285,7 +287,7 @@ void test_float_typess_for_overflows()
|
||||
test_t minvalue = (std::numeric_limits<test_t>::min)();
|
||||
std::string s_min_value = lexical_cast<std::string>(minvalue);
|
||||
BOOST_CHECK_CLOSE_FRACTION(minvalue, lexical_cast<test_t>(minvalue), (std::numeric_limits<test_t>::epsilon()));
|
||||
BOOST_CHECK_CLOSE_FRACTION(minvalue, lexical_cast<test_t>(s_min_value), (std::numeric_limits<test_t>::epsilon()));
|
||||
BOOST_CHECK_CLOSE_FRACTION(minvalue, lexical_cast<test_t>(s_min_value), (std::numeric_limits<test_t>::epsilon() * 2));
|
||||
|
||||
test_t maxvalue = (std::numeric_limits<test_t>::max)();
|
||||
std::string s_max_value = lexical_cast<std::string>(maxvalue);
|
||||
@@ -324,13 +326,14 @@ void test_float_typess_for_overflows()
|
||||
|
||||
#undef CHECK_CLOSE_ABS_DIFF
|
||||
|
||||
// Epsilon is multiplied by 2 because of two lexical conversions
|
||||
#define TEST_TO_FROM_CAST_AROUND_TYPED(VAL,STRING_TYPE) \
|
||||
test_value = VAL + std::numeric_limits<test_t>::epsilon() * i ; \
|
||||
converted_val = lexical_cast<test_t>( lexical_cast<STRING_TYPE>(test_value) ); \
|
||||
BOOST_CHECK_CLOSE_FRACTION( \
|
||||
BOOST_CHECK_CLOSE_FRACTION( \
|
||||
test_value, \
|
||||
converted_val, \
|
||||
std::numeric_limits<test_t>::epsilon() \
|
||||
std::numeric_limits<test_t>::epsilon() * 2 \
|
||||
);
|
||||
|
||||
/*
|
||||
|
||||
@@ -85,6 +85,12 @@ void test_inf_nan_templated()
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("+infinity") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("+INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("iNfiNity") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("INfinity") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>("-inFINITY") ) );
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>("-INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("NAN") ) );
|
||||
|
||||
@@ -94,6 +100,15 @@ void test_inf_nan_templated()
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("+nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("+NAN") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("nAn") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("NaN") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_nan( lexical_cast<test_t>("-nAn") ) );
|
||||
BOOST_CHECK( is_neg_nan( lexical_cast<test_t>("-NaN") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("+Nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("+nAN") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("nan()") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("NAN(some string)") ) );
|
||||
BOOST_CHECK_THROW( lexical_cast<test_t>("NAN(some string"), bad_lexical_cast );
|
||||
@@ -127,6 +142,12 @@ void test_inf_nan_templated()
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+infinity") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>(L"-infINIty") ) );
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>(L"-INFiniTY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+inFINIty") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+INfinITY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"NAN") ) );
|
||||
|
||||
|
||||
@@ -0,0 +1,539 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Terje Sletteb and Kevlin Henney, 2005.
|
||||
// Copyright Alexander Nasonov, 2006.
|
||||
// Copyright Antony Polukhin, 2011-2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Note: The unit test no longer compile on MSVC 6, but lexical_cast itself works for it.
|
||||
|
||||
//
|
||||
// We need this #define before any #includes: otherwise msvc will emit warnings
|
||||
// deep within std::string, resulting from our (perfectly legal) use of basic_string
|
||||
// with a custom traits class:
|
||||
//
|
||||
#define _SCL_SECURE_NO_WARNINGS
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
|
||||
#include <boost/type_traits/integral_promotion.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#if (defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64)) \
|
||||
&& !(defined(BOOST_MSVC) && BOOST_MSVC < 1300)
|
||||
#define LCAST_TEST_LONGLONG
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
|
||||
#define BOOST_LCAST_NO_WCHAR_T
|
||||
#endif
|
||||
|
||||
// Test all 65536 values if true:
|
||||
bool const lcast_test_small_integral_types_completely = false;
|
||||
|
||||
// lcast_integral_test_counter: use when testing all values of an integral
|
||||
// types is not possible. Max. portable value is 32767.
|
||||
int const lcast_integral_test_counter=500;
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_conversion_from_to_short();
|
||||
void test_conversion_from_to_ushort();
|
||||
void test_conversion_from_to_int();
|
||||
void test_conversion_from_to_uint();
|
||||
void test_conversion_from_to_long();
|
||||
void test_conversion_from_to_ulong();
|
||||
void test_conversion_from_to_intmax_t();
|
||||
void test_conversion_from_to_uintmax_t();
|
||||
#ifdef LCAST_TEST_LONGLONG
|
||||
void test_conversion_from_to_longlong();
|
||||
void test_conversion_from_to_ulonglong();
|
||||
#endif
|
||||
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test on integral types");
|
||||
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_short));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ushort));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_int));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_uint));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_long));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulong));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_intmax_t));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_uintmax_t));
|
||||
#ifdef LCAST_TEST_LONGLONG
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_longlong));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulonglong));
|
||||
#endif
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_integral_to_char(CharT zero)
|
||||
{
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(0)) == zero + 0);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(1)) == zero + 1);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(2)) == zero + 2);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(3)) == zero + 3);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(4)) == zero + 4);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(5)) == zero + 5);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(6)) == zero + 6);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(7)) == zero + 7);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(8)) == zero + 8);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(9)) == zero + 9);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(static_cast<T>(10)), bad_lexical_cast);
|
||||
|
||||
T t = (std::numeric_limits<T>::max)();
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(t), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_char_to_integral(CharT zero)
|
||||
{
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 0)) == static_cast<T>(0) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 1)) == static_cast<T>(1) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 2)) == static_cast<T>(2) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 3)) == static_cast<T>(3) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 4)) == static_cast<T>(4) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 5)) == static_cast<T>(5) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 6)) == static_cast<T>(6) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 7)) == static_cast<T>(7) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 8)) == static_cast<T>(8) );
|
||||
BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 9)) == static_cast<T>(9) );
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero + 10)), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero - 1)), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_integral_to_integral()
|
||||
{
|
||||
T t = 0;
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
|
||||
// Next two variables are used to supress warnings.
|
||||
int st = 32767; unsigned int ut = st;
|
||||
t = st;
|
||||
BOOST_CHECK(lexical_cast<short>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned short>(t) == ut);
|
||||
BOOST_CHECK(lexical_cast<int>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned int>(t) == ut);
|
||||
BOOST_CHECK(lexical_cast<long>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned long>(t) == ut);
|
||||
|
||||
t = (std::numeric_limits<T>::max)();
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
|
||||
t = (std::numeric_limits<T>::min)();
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Replace "-,999" with "-999".
|
||||
template<class CharT>
|
||||
std::basic_string<CharT> to_str_gcc_workaround(std::basic_string<CharT> str)
|
||||
{
|
||||
std::locale loc;
|
||||
std::numpunct<CharT> const& np = BOOST_USE_FACET(std::numpunct<CharT>, loc);
|
||||
std::ctype<CharT> const& ct = BOOST_USE_FACET(std::ctype<CharT>, loc);
|
||||
|
||||
if(np.grouping().empty())
|
||||
return str;
|
||||
|
||||
CharT prefix[3] = { ct.widen('-'), np.thousands_sep(), CharT() };
|
||||
|
||||
if(str.find(prefix) != 0)
|
||||
return str;
|
||||
|
||||
prefix[1] = CharT();
|
||||
str.replace(0, 2, prefix);
|
||||
return str;
|
||||
}
|
||||
|
||||
template<class CharT, class T>
|
||||
std::basic_string<CharT> to_str(T t)
|
||||
{
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << t;
|
||||
return to_str_gcc_workaround(o.str());
|
||||
}
|
||||
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_integral_to_string(CharT)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
typedef std::basic_string<CharT> string_type;
|
||||
|
||||
T t;
|
||||
|
||||
t = (limits::min)();
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
t = (limits::max)();
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
if(limits::digits <= 16 && lcast_test_small_integral_types_completely)
|
||||
// min and max have already been tested.
|
||||
for(t = 1 + (limits::min)(); t != (limits::max)(); ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
else
|
||||
{
|
||||
T const min_val = (limits::min)();
|
||||
T const max_val = (limits::max)();
|
||||
T const half_max_val = max_val / 2;
|
||||
T const cnt = lcast_integral_test_counter; // to supress warnings
|
||||
unsigned int const counter = cnt < half_max_val ? cnt : half_max_val;
|
||||
|
||||
unsigned int i;
|
||||
|
||||
// Test values around min:
|
||||
t = min_val;
|
||||
for(i = 0; i < counter; ++i, ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around max:
|
||||
t = max_val;
|
||||
for(i = 0; i < counter; ++i, --t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around zero:
|
||||
if(limits::is_signed)
|
||||
for(t = static_cast<T>(-counter); t < static_cast<T>(counter); ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around 100, 1000, 10000, ...
|
||||
T ten_power = 100;
|
||||
for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10)
|
||||
{
|
||||
// ten_power + 100 probably never overflows
|
||||
for(t = ten_power - 100; t != ten_power + 100; ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_string_to_integral(CharT)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
typedef std::basic_string<CharT> string_type;
|
||||
|
||||
string_type s;
|
||||
string_type const zero = to_str<CharT>(0);
|
||||
string_type const nine = to_str<CharT>(9);
|
||||
T const min_val = (limits::min)();
|
||||
T const max_val = (limits::max)();
|
||||
|
||||
s = to_str<CharT>(min_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(s), min_val);
|
||||
if(limits::is_signed)
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
|
||||
}
|
||||
|
||||
s = to_str<CharT>(max_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(s), max_val);
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
|
||||
|
||||
s = to_str<CharT>(max_val);
|
||||
for (int i =1; i <=10; ++i) {
|
||||
s[s.size()-1] += 1;
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( s ), bad_lexical_cast);
|
||||
}
|
||||
|
||||
s = to_str<CharT>(max_val);
|
||||
std::locale loc;
|
||||
typedef std::numpunct<char> numpunct;
|
||||
if ( BOOST_USE_FACET(numpunct, loc).grouping().empty() ) {
|
||||
// Following tests work well for locale C
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+s), max_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+to_str<CharT>(0)+s), max_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+to_str<CharT>(0)+to_str<CharT>(0)+s), max_val);
|
||||
}
|
||||
|
||||
for (int i =1; i <=256; ++i) {
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(i)+s ), bad_lexical_cast);
|
||||
}
|
||||
|
||||
typedef BOOST_DEDUCED_TYPENAME boost::integral_promotion<T>::type promoted;
|
||||
if ( !(boost::is_same<T, promoted>::value) )
|
||||
{
|
||||
promoted prom = max_val;
|
||||
s = to_str<CharT>(max_val);
|
||||
for (int i =1; i <=256; ++i) {
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(prom+i) ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(i)+s ), bad_lexical_cast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(limits::digits <= 16 && lcast_test_small_integral_types_completely)
|
||||
// min and max have already been tested.
|
||||
for(T t = 1 + min_val; t != max_val; ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
else
|
||||
{
|
||||
T const half_max_val = max_val / 2;
|
||||
T const cnt = lcast_integral_test_counter; // to supress warnings
|
||||
unsigned int const counter = cnt < half_max_val ? cnt : half_max_val;
|
||||
|
||||
T t;
|
||||
unsigned int i;
|
||||
|
||||
// Test values around min:
|
||||
t = min_val;
|
||||
for(i = 0; i < counter; ++i, ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around max:
|
||||
t = max_val;
|
||||
for(i = 0; i < counter; ++i, --t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around zero:
|
||||
if(limits::is_signed)
|
||||
for(t = static_cast<T>(-counter); t < static_cast<T>(counter); ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around 100, 1000, 10000, ...
|
||||
T ten_power = 100;
|
||||
for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10)
|
||||
{
|
||||
// ten_power + 100 probably never overflows
|
||||
for(t = ten_power - 100; t != ten_power + 100; ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_integral_for_locale()
|
||||
{
|
||||
std::locale current_locale;
|
||||
typedef std::numpunct<char> numpunct;
|
||||
numpunct const& np = BOOST_USE_FACET(numpunct, current_locale);
|
||||
if ( !np.grouping().empty() )
|
||||
{
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<T>( std::string("100") + np.thousands_sep() + np.thousands_sep() + "0" )
|
||||
, bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( std::string("100") + np.thousands_sep() ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( np.thousands_sep() + std::string("100") ), bad_lexical_cast);
|
||||
|
||||
// Exception must not be thrown, when we are using no separators at all
|
||||
BOOST_CHECK( lexical_cast<T>("30000") == static_cast<T>(30000) );
|
||||
}
|
||||
|
||||
test_conversion_from_integral_to_integral<T>();
|
||||
test_conversion_from_integral_to_string<T>('0');
|
||||
test_conversion_from_string_to_integral<T>('0');
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T)
|
||||
test_conversion_from_integral_to_string<T>(L'0');
|
||||
test_conversion_from_string_to_integral<T>(L'0');
|
||||
#endif
|
||||
}
|
||||
|
||||
struct restore_oldloc
|
||||
{
|
||||
std::locale oldloc;
|
||||
~restore_oldloc() { std::locale::global(oldloc); }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_integral()
|
||||
{
|
||||
char const zero = '0';
|
||||
signed char const szero = '0';
|
||||
unsigned char const uzero = '0';
|
||||
test_conversion_from_integral_to_char<T>(zero);
|
||||
test_conversion_from_char_to_integral<T>(zero);
|
||||
test_conversion_from_integral_to_char<T>(szero);
|
||||
test_conversion_from_char_to_integral<T>(szero);
|
||||
test_conversion_from_integral_to_char<T>(uzero);
|
||||
test_conversion_from_char_to_integral<T>(uzero);
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
wchar_t const wzero = L'0';
|
||||
test_conversion_from_integral_to_char<T>(wzero);
|
||||
test_conversion_from_char_to_integral<T>(wzero);
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
char16_t const u16zero = u'0';
|
||||
test_conversion_from_integral_to_char<T>(u16zero);
|
||||
test_conversion_from_char_to_integral<T>(u16zero);
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
char32_t const u32zero = u'0';
|
||||
test_conversion_from_integral_to_char<T>(u32zero);
|
||||
test_conversion_from_char_to_integral<T>(u32zero);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(lexical_cast<T>("-1") == static_cast<T>(-1));
|
||||
BOOST_CHECK(lexical_cast<T>("-9") == static_cast<T>(-9));
|
||||
BOOST_CHECK(lexical_cast<T>(-1) == static_cast<T>(-1));
|
||||
BOOST_CHECK(lexical_cast<T>(-9) == static_cast<T>(-9));
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("-1.0"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("-9.0"), bad_lexical_cast);
|
||||
BOOST_CHECK(lexical_cast<T>(-1.0) == static_cast<T>(-1));
|
||||
BOOST_CHECK(lexical_cast<T>(-9.0) == static_cast<T>(-9));
|
||||
|
||||
BOOST_CHECK(lexical_cast<T>(static_cast<T>(1)) == static_cast<T>(1));
|
||||
BOOST_CHECK(lexical_cast<T>(static_cast<T>(9)) == static_cast<T>(9));
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.1f), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.1), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.1L), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.0001f), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.0001), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(1.0001L), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK(lexical_cast<T>("+1") == static_cast<T>(1) );
|
||||
BOOST_CHECK(lexical_cast<T>("+9") == static_cast<T>(9) );
|
||||
BOOST_CHECK(lexical_cast<T>("+10") == static_cast<T>(10) );
|
||||
BOOST_CHECK(lexical_cast<T>("+90") == static_cast<T>(90) );
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("++1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("-+9"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("--1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("+-9"), bad_lexical_cast);
|
||||
// test_conversion_from_to_integral_for_locale
|
||||
|
||||
// Overflow test case from David W. Birdsall
|
||||
std::string must_owerflow_str = "160000000000000000000";
|
||||
std::string must_owerflow_negative_str = "-160000000000000000000";
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(must_owerflow_str), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(must_owerflow_negative_str), bad_lexical_cast);
|
||||
|
||||
must_owerflow_str += '0';
|
||||
must_owerflow_negative_str += '0';
|
||||
}
|
||||
|
||||
typedef std::numpunct<char> numpunct;
|
||||
|
||||
restore_oldloc guard;
|
||||
std::locale const& oldloc = guard.oldloc;
|
||||
|
||||
std::string grouping1 = BOOST_USE_FACET(numpunct, oldloc).grouping();
|
||||
std::string grouping2(grouping1);
|
||||
|
||||
test_conversion_from_to_integral_for_locale<T>();
|
||||
|
||||
try
|
||||
{
|
||||
std::locale newloc("");
|
||||
std::locale::global(newloc);
|
||||
|
||||
grouping2 = BOOST_USE_FACET(numpunct, newloc).grouping();
|
||||
}
|
||||
catch(std::exception const& ex)
|
||||
{
|
||||
std::string msg("Failed to set system locale: ");
|
||||
msg += ex.what();
|
||||
BOOST_TEST_MESSAGE(msg);
|
||||
}
|
||||
|
||||
if(grouping1 != grouping2)
|
||||
test_conversion_from_to_integral_for_locale<T>();
|
||||
|
||||
if(grouping1.empty() && grouping2.empty())
|
||||
BOOST_TEST_MESSAGE("Formatting with thousands_sep has not been tested");
|
||||
}
|
||||
|
||||
void test_conversion_from_to_short()
|
||||
{
|
||||
test_conversion_from_to_integral<short>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ushort()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned short>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_int()
|
||||
{
|
||||
test_conversion_from_to_integral<int>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_uint()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned int>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_long()
|
||||
{
|
||||
test_conversion_from_to_integral<long>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulong()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned long>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_intmax_t()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::intmax_t>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_uintmax_t()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::uintmax_t>();
|
||||
}
|
||||
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
|
||||
void test_conversion_from_to_longlong()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::long_long_type>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulonglong()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::ulong_long_type>();
|
||||
}
|
||||
|
||||
#elif defined(BOOST_HAS_MS_INT64)
|
||||
|
||||
void test_conversion_from_to_longlong()
|
||||
{
|
||||
test_conversion_from_to_integral<__int64>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulonglong()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned __int64>();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
|
||||
#define BOOST_LCAST_NO_WCHAR_T
|
||||
#endif
|
||||
|
||||
struct class_with_user_defined_sream_operators {
|
||||
int i;
|
||||
|
||||
operator int() const {
|
||||
return i;
|
||||
}
|
||||
};
|
||||
|
||||
template <class CharT>
|
||||
inline std::basic_istream<CharT>& operator >> (std::basic_istream<CharT>& istr, class_with_user_defined_sream_operators& rhs)
|
||||
{
|
||||
return istr >> rhs.i;
|
||||
}
|
||||
|
||||
|
||||
template <class RngT>
|
||||
void do_test_iterator_range_impl(const RngT& rng)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(rng), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(rng.begin(), rng.size()), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(rng), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(rng.begin(), rng.size()), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<short>(rng), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<short>(rng.begin(), rng.size()), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned short>(rng), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned short>(rng.begin(), rng.size()), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<long int>(rng), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<long int>(rng.begin(), rng.size()), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned long int>(rng), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned long int>(rng.begin(), rng.size()), 1u);
|
||||
|
||||
#ifdef BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
|
||||
BOOST_CHECK_EQUAL(lexical_cast<float>(rng), 1.0f);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<float>(rng.begin(), rng.size()), 1.0f);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<double>(rng), 1.0);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<double>(rng.begin(), rng.size()), 1.0);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<long double>(rng), 1.0L);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<long double>(rng.begin(), rng.size()), 1.0L);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<class_with_user_defined_sream_operators>(rng), 1);
|
||||
#endif
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
BOOST_CHECK_EQUAL(lexical_cast<boost::ulong_long_type>(rng), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<boost::ulong_long_type>(rng.begin(), rng.size()), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<boost::long_long_type>(rng), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<boost::long_long_type>(rng.begin(), rng.size()), 1);
|
||||
#elif defined(BOOST_HAS_MS_INT64)
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned __int64>(rng), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned __int64>(rng.begin(), rng.size()), 1u);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<__int64>(rng), 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<__int64>(rng.begin(), rng.size()), 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
void test_it_range_using_any_chars(CharT* one, CharT* eleven)
|
||||
{
|
||||
typedef CharT test_char_type;
|
||||
|
||||
// Zero terminated
|
||||
iterator_range<test_char_type*> rng1(one, one + 1);
|
||||
do_test_iterator_range_impl(rng1);
|
||||
|
||||
iterator_range<const test_char_type*> crng1(one, one + 1);
|
||||
do_test_iterator_range_impl(crng1);
|
||||
|
||||
// Non zero terminated
|
||||
iterator_range<test_char_type*> rng2(eleven, eleven + 1);
|
||||
do_test_iterator_range_impl(rng2);
|
||||
|
||||
iterator_range<const test_char_type*> crng2(eleven, eleven + 1);
|
||||
do_test_iterator_range_impl(crng2);
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
void test_it_range_using_char(CharT* one, CharT* eleven)
|
||||
{
|
||||
typedef CharT test_char_type;
|
||||
|
||||
iterator_range<test_char_type*> rng1(one, one + 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng1), "1");
|
||||
|
||||
iterator_range<const test_char_type*> crng1(one, one + 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng1), "1");
|
||||
|
||||
iterator_range<test_char_type*> rng2(eleven, eleven + 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng2), "1");
|
||||
|
||||
iterator_range<const test_char_type*> crng2(eleven, eleven + 1);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng2), "1");
|
||||
|
||||
BOOST_CHECK_EQUAL(lexical_cast<float>(rng1), 1.0f);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<double>(rng1), 1.0);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<long double>(rng1), 1.0L);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<class_with_user_defined_sream_operators>(rng1), 1);
|
||||
|
||||
BOOST_CHECK_EQUAL(lexical_cast<float>(crng2), 1.0f);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<double>(crng2), 1.0);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<long double>(crng2), 1.0L);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<class_with_user_defined_sream_operators>(crng2), 1);
|
||||
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
BOOST_CHECK(lexical_cast<std::wstring>(rng1) == L"1");
|
||||
BOOST_CHECK(lexical_cast<std::wstring>(crng1) == L"1");
|
||||
BOOST_CHECK(lexical_cast<std::wstring>(rng2) == L"1");
|
||||
BOOST_CHECK(lexical_cast<std::wstring>(crng2) == L"1");
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
|
||||
typedef std::basic_string<char16_t> my_char16_string;
|
||||
BOOST_CHECK(lexical_cast<my_char16_string>(rng1) == u"1");
|
||||
BOOST_CHECK(lexical_cast<my_char16_string>(crng1) == u"1");
|
||||
BOOST_CHECK(lexical_cast<my_char16_string>(rng2) == u"1");
|
||||
BOOST_CHECK(lexical_cast<my_char16_string>(crng2) == u"1");
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
|
||||
typedef std::basic_string<char32_t> my_char32_string;
|
||||
BOOST_CHECK(lexical_cast<my_char32_string>(rng1) == U"1");
|
||||
BOOST_CHECK(lexical_cast<my_char32_string>(crng1) == U"1");
|
||||
BOOST_CHECK(lexical_cast<my_char32_string>(rng2) == U"1");
|
||||
BOOST_CHECK(lexical_cast<my_char32_string>(crng2) == U"1");
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_char_iterator_ranges()
|
||||
{
|
||||
typedef char test_char_type;
|
||||
test_char_type data1[] = "1";
|
||||
test_char_type data2[] = "11";
|
||||
test_it_range_using_any_chars(data1, data2);
|
||||
test_it_range_using_char(data1, data2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void test_unsigned_char_iterator_ranges()
|
||||
{
|
||||
typedef unsigned char test_char_type;
|
||||
test_char_type data1[] = "1";
|
||||
test_char_type data2[] = "11";
|
||||
test_it_range_using_any_chars(data1, data2);
|
||||
test_it_range_using_char(data1, data2);
|
||||
}
|
||||
|
||||
void test_signed_char_iterator_ranges()
|
||||
{
|
||||
typedef signed char test_char_type;
|
||||
test_char_type data1[] = "1";
|
||||
test_char_type data2[] = "11";
|
||||
test_it_range_using_any_chars(data1, data2);
|
||||
test_it_range_using_char(data1, data2);
|
||||
}
|
||||
|
||||
void test_wchar_iterator_ranges()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
typedef wchar_t test_char_type;
|
||||
test_char_type data1[] = L"1";
|
||||
test_char_type data2[] = L"11";
|
||||
test_it_range_using_any_chars(data1, data2);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void test_char16_iterator_ranges()
|
||||
{
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
typedef char16_t test_char_type;
|
||||
test_char_type data1[] = u"1";
|
||||
test_char_type data2[] = u"11";
|
||||
test_it_range_using_any_chars(data1, data2);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void test_char32_iterator_ranges()
|
||||
{
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
|
||||
typedef char32_t test_char_type;
|
||||
test_char_type data1[] = U"1";
|
||||
test_char_type data2[] = U"11";
|
||||
test_it_range_using_any_chars(data1, data2);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite = BOOST_TEST_SUITE("lexical_cast. Testing conversions using iterator_range<>");
|
||||
suite->add(BOOST_TEST_CASE(&test_char_iterator_ranges));
|
||||
suite->add(BOOST_TEST_CASE(&test_unsigned_char_iterator_ranges));
|
||||
suite->add(BOOST_TEST_CASE(&test_signed_char_iterator_ranges));
|
||||
suite->add(BOOST_TEST_CASE(&test_wchar_iterator_ranges));
|
||||
suite->add(BOOST_TEST_CASE(&test_char16_iterator_ranges));
|
||||
suite->add(BOOST_TEST_CASE(&test_char32_iterator_ranges));
|
||||
|
||||
return suite;
|
||||
}
|
||||
Executable
+95
@@ -0,0 +1,95 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
|
||||
#ifndef BOOST_NO_EXCEPTIONS
|
||||
#error "This test must be compiled with -DBOOST_NO_EXCEPTIONS"
|
||||
#endif
|
||||
|
||||
bool g_was_exception = false;
|
||||
|
||||
namespace boost {
|
||||
|
||||
void throw_exception(std::exception const & ) {
|
||||
g_was_exception = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
using namespace boost;
|
||||
|
||||
|
||||
struct Escape
|
||||
{
|
||||
Escape(){}
|
||||
Escape(const std::string& s)
|
||||
: str_(s)
|
||||
{}
|
||||
|
||||
std::string str_;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<< (std::ostream& o, const Escape& rhs)
|
||||
{
|
||||
return o << rhs.str_;
|
||||
}
|
||||
|
||||
inline std::istream& operator>> (std::istream& i, Escape& rhs)
|
||||
{
|
||||
return i >> rhs.str_;
|
||||
}
|
||||
|
||||
void test_exceptions_off()
|
||||
{
|
||||
Escape v("");
|
||||
|
||||
g_was_exception = false;
|
||||
lexical_cast<char>(v);
|
||||
BOOST_CHECK(g_was_exception);
|
||||
|
||||
g_was_exception = false;
|
||||
lexical_cast<unsigned char>(v);
|
||||
BOOST_CHECK(g_was_exception);
|
||||
|
||||
v = lexical_cast<Escape>(100);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(v), 100);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(v), 100u);
|
||||
|
||||
v = lexical_cast<Escape>(0.0);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<double>(v), 0.0);
|
||||
|
||||
BOOST_CHECK_EQUAL(lexical_cast<short>(100), 100);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<float>(0.0), 0.0);
|
||||
|
||||
g_was_exception = false;
|
||||
lexical_cast<short>(700000);
|
||||
BOOST_CHECK(g_was_exception);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast. Testing with BOOST_NO_EXCEPTIONS");
|
||||
suite->add(BOOST_TEST_CASE(&test_exceptions_off));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
Executable
+166
@@ -0,0 +1,166 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
// Testing compilation and some basic usage with BOOST_NO_STD_LOCALE
|
||||
// Tests are mainly copyied from lexical_cast_empty_input_test.cpp (something
|
||||
// new added to test_empty_3)
|
||||
|
||||
#ifndef BOOST_NO_STD_LOCALE
|
||||
#error "This test must be compiled with -DBOOST_NO_STD_LOCALE"
|
||||
#endif
|
||||
|
||||
|
||||
template <class T>
|
||||
void do_test_on_empty_input(T& v)
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
BOOST_CHECK_THROW(lexical_cast<boost::ulong_long_type>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<boost::long_long_type>(v), bad_lexical_cast);
|
||||
#elif defined(BOOST_HAS_MS_INT64)
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned __int64>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_empty_1()
|
||||
{
|
||||
boost::iterator_range<char*> v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
|
||||
boost::iterator_range<const char*> cv;
|
||||
do_test_on_empty_input(cv);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(cv), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(cv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(cv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(cv), bad_lexical_cast);
|
||||
|
||||
const boost::iterator_range<const char*> ccv;
|
||||
do_test_on_empty_input(ccv);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(ccv), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(ccv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(ccv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(ccv), bad_lexical_cast);
|
||||
}
|
||||
|
||||
void test_empty_2()
|
||||
{
|
||||
std::string v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
}
|
||||
|
||||
struct Escape
|
||||
{
|
||||
Escape(){}
|
||||
Escape(const std::string& s)
|
||||
: str_(s)
|
||||
{}
|
||||
|
||||
std::string str_;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<< (std::ostream& o, const Escape& rhs)
|
||||
{
|
||||
return o << rhs.str_;
|
||||
}
|
||||
|
||||
inline std::istream& operator>> (std::istream& i, Escape& rhs)
|
||||
{
|
||||
return i >> rhs.str_;
|
||||
}
|
||||
|
||||
void test_empty_3()
|
||||
{
|
||||
Escape v("");
|
||||
do_test_on_empty_input(v);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
|
||||
v = lexical_cast<Escape>(100);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<int>(v), 100);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(v), 100u);
|
||||
|
||||
v = lexical_cast<Escape>(0.0);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<double>(v), 0.0);
|
||||
}
|
||||
|
||||
namespace std {
|
||||
inline std::ostream & operator<<(std::ostream & out, const std::vector<long> & v)
|
||||
{
|
||||
std::ostream_iterator<long> it(out);
|
||||
std::copy(v.begin(), v.end(), it);
|
||||
assert(out);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
void test_empty_4()
|
||||
{
|
||||
std::vector<long> v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
}
|
||||
|
||||
|
||||
struct my_string {
|
||||
friend std::ostream &operator<<(std::ostream& sout, my_string const&/* st*/) {
|
||||
return sout << "";
|
||||
}
|
||||
};
|
||||
|
||||
void test_empty_5()
|
||||
{
|
||||
my_string st;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(st), std::string());;
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast. Testing with BOOST_NO_STD_LOCALE");
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_1));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_2));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_3));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_4));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_5));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
@@ -13,12 +13,27 @@
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/date_time/gregorian/gregorian.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
void parseDate()
|
||||
{
|
||||
std::locale locale;
|
||||
boost::date_time::format_date_parser<boost::gregorian::date, wchar_t> parser(L"", locale);
|
||||
boost::date_time::special_values_parser<boost::gregorian::date, wchar_t> svp;
|
||||
|
||||
boost::gregorian::date date = parser.parse_date(L"", L"", svp);
|
||||
(void)date;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
#ifdef BOOST_MSVC
|
||||
BOOST_STATIC_ASSERT((boost::is_same<wchar_t, unsigned short>::value));
|
||||
#endif
|
||||
|
||||
parseDate();
|
||||
return ::boost::lexical_cast<int>(L"1000") == 1000;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ void test_typedefed_wchar_t_runtime()
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<int>(L'A'), 65);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<int>(L'B'), 66);
|
||||
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(L"65"), 65);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(L"66"), 66);
|
||||
#endif
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
// Copyright Antony Polukhin, 2011-2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
@@ -17,40 +17,70 @@
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_char_types_conversions()
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
|
||||
#define BOOST_LCAST_NO_WCHAR_T
|
||||
#endif
|
||||
|
||||
template <class CharT>
|
||||
void test_impl(const CharT* wc_arr)
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
typedef CharT wide_char;
|
||||
typedef std::basic_string<CharT> wide_string;
|
||||
const char c_arr[] = "Test array of chars";
|
||||
const unsigned char uc_arr[] = "Test array of chars";
|
||||
const signed char sc_arr[] = "Test array of chars";
|
||||
const wchar_t wc_arr[] =L"Test array of chars";
|
||||
|
||||
// Following tests depend on realization of std::locale
|
||||
// and pass for popular compilers and STL realizations
|
||||
BOOST_CHECK(boost::lexical_cast<wchar_t>(c_arr[0]) == wc_arr[0]);
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(c_arr) == std::wstring(wc_arr));
|
||||
BOOST_CHECK(boost::lexical_cast<wide_char>(c_arr[0]) == wc_arr[0]);
|
||||
BOOST_CHECK(boost::lexical_cast<wide_string>(c_arr) == wide_string(wc_arr));
|
||||
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(sc_arr) == std::wstring(wc_arr) );
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(uc_arr) == std::wstring(wc_arr) );
|
||||
BOOST_CHECK(boost::lexical_cast<wide_string>(sc_arr) == wide_string(wc_arr) );
|
||||
BOOST_CHECK(boost::lexical_cast<wide_string>(uc_arr) == wide_string(wc_arr) );
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(uc_arr[0]), wc_arr[0]);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(sc_arr[0]), wc_arr[0]);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wide_char>(uc_arr[0]), wc_arr[0]);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wide_char>(sc_arr[0]), wc_arr[0]);
|
||||
}
|
||||
|
||||
|
||||
void test_char_types_conversions_wchar_t()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
test_impl(L"Test array of chars");
|
||||
#endif
|
||||
BOOST_CHECK(1);
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void test_char_types_conversions_char16_t()
|
||||
{
|
||||
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
|
||||
test_impl(u"Test array of chars");
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void test_char_types_conversions_char32_t()
|
||||
{
|
||||
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
|
||||
test_impl(U"Test array of chars");
|
||||
#endif
|
||||
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast char<->wchar_t unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_char_types_conversions));
|
||||
BOOST_TEST_SUITE("lexical_cast char => wide characters unit test (widening test)");
|
||||
suite->add(BOOST_TEST_CASE(&test_char_types_conversions_wchar_t));
|
||||
suite->add(BOOST_TEST_CASE(&test_char_types_conversions_char16_t));
|
||||
suite->add(BOOST_TEST_CASE(&test_char_types_conversions_char32_t));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user