mirror of
https://github.com/boostorg/locale.git
synced 2026-01-19 04:22:08 +00:00
Enhance test output for test_encoding
This commit is contained in:
@@ -88,6 +88,28 @@ std::ostream& operator<<(std::ostream& s, boost::locale::conv::detail::conv_back
|
||||
return s; // LCOV_EXCL_LINE
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
std::string char_name()
|
||||
{
|
||||
if(std::is_same<Char, char>::value)
|
||||
return "char";
|
||||
else if(std::is_same<Char, wchar_t>::value)
|
||||
return "wchar_t";
|
||||
#ifdef __cpp_lib_char8_t
|
||||
else if(std::is_same<Char, char8_t>::value)
|
||||
return "char8_t";
|
||||
#endif
|
||||
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
|
||||
else if(std::is_same<Char, char16_t>::value)
|
||||
return "char16_t";
|
||||
#endif
|
||||
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
|
||||
else if(std::is_same<Char, char32_t>::value)
|
||||
return "char32_t";
|
||||
#endif
|
||||
return "unknown char type"; // LCOV_EXCL_LINE
|
||||
}
|
||||
|
||||
#define TEST_FAIL_CONVERSION(X) TEST_THROWS(X, boost::locale::conv::conversion_error)
|
||||
|
||||
template<typename Char>
|
||||
@@ -97,12 +119,13 @@ void test_to_utf_for_impls(const std::string& source,
|
||||
const bool expectSuccess = true,
|
||||
const bool test_default = true)
|
||||
{
|
||||
TEST_CONTEXT(encoding << '/' << char_name<Char>());
|
||||
if(test_default) {
|
||||
boost::locale::conv::utf_encoder<Char> conv(encoding);
|
||||
TEST_EQ(conv(source), target);
|
||||
}
|
||||
for(const auto impl : all_conv_backends) {
|
||||
std::cout << "----- " << impl << '\n';
|
||||
std::cout << "----- Convert to UTF w/ " << impl << '\n';
|
||||
using boost::locale::conv::invalid_charset_error;
|
||||
try {
|
||||
auto convPtr =
|
||||
@@ -135,12 +158,13 @@ void test_from_utf_for_impls(const std::basic_string<Char>& source,
|
||||
const bool expectSuccess = true,
|
||||
const bool test_default = true)
|
||||
{
|
||||
TEST_CONTEXT(encoding << '/' << char_name<Char>());
|
||||
if(test_default) {
|
||||
boost::locale::conv::utf_decoder<Char> conv(encoding);
|
||||
TEST_EQ(conv(source), target);
|
||||
}
|
||||
for(const auto impl : all_conv_backends) {
|
||||
std::cout << "----- " << impl << '\n';
|
||||
std::cout << "----- Convert from UTF w/ " << impl << '\n';
|
||||
using boost::locale::conv::invalid_charset_error;
|
||||
try {
|
||||
auto convPtr =
|
||||
@@ -172,6 +196,7 @@ void test_to_from_utf(const std::string& source,
|
||||
const std::string& encoding,
|
||||
const bool test_default = true)
|
||||
{
|
||||
TEST_CONTEXT(__func__ << ':' << encoding << '/' << char_name<Char>());
|
||||
std::cout << "-- " << encoding << std::endl;
|
||||
|
||||
if(test_default) {
|
||||
@@ -185,6 +210,7 @@ void test_to_from_utf(const std::string& source,
|
||||
template<typename Char>
|
||||
void test_error_to_utf(const std::string& source, const std::basic_string<Char>& target, const std::string& encoding)
|
||||
{
|
||||
TEST_CONTEXT(__func__ << ':' << encoding << '/' << char_name<Char>());
|
||||
using boost::locale::conv::to_utf;
|
||||
using boost::locale::conv::stop;
|
||||
|
||||
@@ -206,6 +232,7 @@ void test_error_to_utf(const std::string& source, const std::basic_string<Char>&
|
||||
template<typename Char>
|
||||
void test_error_from_utf(const std::basic_string<Char>& source, const std::string& target, const std::string& encoding)
|
||||
{
|
||||
TEST_CONTEXT(__func__ << ':' << encoding << '/' << char_name<Char>());
|
||||
using boost::locale::conv::from_utf;
|
||||
using boost::locale::conv::stop;
|
||||
|
||||
@@ -359,6 +386,7 @@ void test_all_combinations()
|
||||
template<typename Char>
|
||||
void test_utf_for()
|
||||
{
|
||||
std::cout << "- Testing to/from UTF for " << char_name<Char>() << '\n';
|
||||
using boost::locale::conv::invalid_charset_error;
|
||||
|
||||
{
|
||||
@@ -389,7 +417,7 @@ void test_utf_for()
|
||||
if(iconvIssue != MacOSIconvIssue::No_CN_Support)
|
||||
test_to_from_utf<Char>("\x1b\x24\x29\x41\x0e\x4a\x35\xf", utf<Char>("实"), "ISO-2022-CN", false);
|
||||
|
||||
std::cout << "- Testing correct invalid bytes skipping\n";
|
||||
std::cout << "- Testing correct invalid bytes skipping for " << char_name<Char>() << '\n';
|
||||
{
|
||||
std::cout << "-- UTF-8" << std::endl;
|
||||
|
||||
@@ -428,7 +456,7 @@ void test_utf_for()
|
||||
}
|
||||
std::cout << "-- Error for encoding at start" << std::endl;
|
||||
test_error_from_utf<Char>(utf<Char>("שלום hello"), " hello", "ISO8859-1");
|
||||
std::cout << "-- Error for encoding at middle and end" << std::endl;
|
||||
std::cout << "-- Error for encoding at middle" << std::endl;
|
||||
test_error_from_utf<Char>(utf<Char>("hello שלום world"), "hello world", "ISO8859-1");
|
||||
std::cout << "-- Error for encoding at end" << std::endl;
|
||||
test_error_from_utf<Char>(utf<Char>("hello שלום"), "hello ", "ISO8859-1");
|
||||
@@ -461,6 +489,7 @@ void test_utf_for()
|
||||
template<typename Char1, typename Char2>
|
||||
void test_utf_to_utf_for(const std::string& utf8_string)
|
||||
{
|
||||
std::cout << "---- " << char_name<Char1>() << "<->" << char_name<Char1>() << "\n";
|
||||
const auto utf_string1 = utf<Char1>(utf8_string);
|
||||
const auto utf_string2 = utf<Char2>(utf8_string);
|
||||
using boost::locale::conv::utf_to_utf;
|
||||
@@ -474,22 +503,17 @@ template<typename Char>
|
||||
void test_utf_to_utf_for()
|
||||
{
|
||||
const std::string& utf8_string = "A-Za-z0-9grüße'\xf0\xa0\x82\x8a'\xf4\x8f\xbf\xbf";
|
||||
std::cout << "---- char\n";
|
||||
test_utf_to_utf_for<Char, char>(utf8_string);
|
||||
test_to_utf_for_impls(utf8_string, utf<Char>(utf8_string), "UTF-8");
|
||||
test_from_utf_for_impls(utf<Char>(utf8_string), utf8_string, "UTF-8");
|
||||
std::cout << "---- wchar_t\n";
|
||||
test_utf_to_utf_for<Char, wchar_t>(utf8_string);
|
||||
#ifdef __cpp_lib_char8_t
|
||||
std::cout << "---- char8_t\n";
|
||||
test_utf_to_utf_for<Char, char8_t>(utf8_string);
|
||||
#endif
|
||||
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
|
||||
std::cout << "---- char16_t\n";
|
||||
test_utf_to_utf_for<Char, char16_t>(utf8_string);
|
||||
#endif
|
||||
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
|
||||
std::cout << "---- char32_t\n";
|
||||
test_utf_to_utf_for<Char, char32_t>(utf8_string);
|
||||
#endif
|
||||
}
|
||||
@@ -832,20 +856,15 @@ void test_main(int /*argc*/, char** /*argv*/)
|
||||
test_utf_to_utf_allocator_support();
|
||||
|
||||
std::cout << "Testing charset to/from UTF conversion functions\n";
|
||||
std::cout << " char" << std::endl;
|
||||
test_utf_for<char>();
|
||||
std::cout << " wchar_t" << std::endl;
|
||||
test_utf_for<wchar_t>();
|
||||
#ifdef __cpp_lib_char8_t
|
||||
std::cout << " char8_t" << std::endl;
|
||||
test_utf_for<char8_t>();
|
||||
#endif
|
||||
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
|
||||
std::cout << " char16_t" << std::endl;
|
||||
test_utf_for<char16_t>();
|
||||
#endif
|
||||
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
|
||||
std::cout << " char32_t" << std::endl;
|
||||
test_utf_for<char32_t>();
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user