From 93f5f4322298b23fc6a1a1ca55187956ce30bfe8 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 25 Dec 2021 19:04:49 +0100 Subject: Test code reorderung and formatting --- src/test-unicode.cpp | 347 +++++++++++++++++++++++++-------------------------- 1 file changed, 172 insertions(+), 175 deletions(-) diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index a30be70..34ae13f 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -68,39 +68,185 @@ std::vector> failure_strings_char32_t { namespace std { #ifdef __cpp_char8_t -std::ostream& operator<<(std::ostream& os, std::basic_string const& s) -{ - os << "["; - for (auto& c: s) - os << " " << std::to_string(static_cast(c)); - os << "]"; + std::ostream& operator<<(std::ostream& os, std::basic_string const& s) + { + os << "["; + for (auto& c: s) + os << " " << std::to_string(static_cast(c)); + os << "]"; - return os; -} + return os; + } #endif -std::ostream& operator<<(std::ostream& os, std::basic_string const& s) + std::ostream& operator<<(std::ostream& os, std::basic_string const& s) + { + os << "["; + for (auto& c: s) + os << " " << std::to_string(static_cast(c)); + os << "]"; + + return os; + } + + std::ostream& operator<<(std::ostream& os, std::basic_string const& s) + { + os << "["; + for (auto& c: s) + os << " " << std::to_string(static_cast(c)); + os << "]"; + + return os; + } + + // utility wrapper to adapt locale-bound facets for wstring/wbuffer convert + template + struct deletable_facet : Facet + { + template + deletable_facet(Args&& ...args) : Facet(std::forward(args)...) {} + ~deletable_facet() {} + }; + + // char8_t instead of char doesn't work w/ clang++-13 + C++20 (yet?) + std::wstring_convert>, char16_t> conv16; + std::wstring_convert>, char32_t> conv32; + + template + std::basic_string std_convert(const std::basic_string& s); + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + std::string a{s.begin(), s.end()}; + a = conv32.to_bytes(conv32.from_bytes(a)); + return std::basic_string{a.begin(), a.end()}; + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + std::string a{s.begin(), s.end()}; + return conv16.from_bytes(a); + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + std::string a{s.begin(), s.end()}; + return conv32.from_bytes(a); + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + auto result{conv16.to_bytes(s)}; + return std::basic_string(result.begin(), result.end()); + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + return conv16.from_bytes(conv16.to_bytes(s)); + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + return conv32.from_bytes(conv16.to_bytes(s)); + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + auto result{conv32.to_bytes(s)}; + return std::basic_string(result.begin(), result.end()); + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + return conv16.from_bytes(conv32.to_bytes(s)); + } + + template<> + std::basic_string std_convert(const std::basic_string& s) + { + return conv32.from_bytes(conv32.to_bytes(s)); + } +} + +// check assumptions about environment +BOOST_AUTO_TEST_CASE(string_u8string) { - os << "["; - for (auto& c: s) - os << " " << std::to_string(static_cast(c)); - os << "]"; + std::string a{"\xc3\xa4"}; + + std::basic_string b{a.begin(), a.end()}; + + BOOST_CHECK(b == std::basic_string{u8"ä"}); + + a = std::string{b.begin(), b.end()}; + + BOOST_CHECK(a == std::string{"\xc3\xa4"}); - return os; + BOOST_CHECK(sizeof(size_t) == 4 || sizeof(size_t) == 8); + + std::cout << "Detected CPU Accu size: " << (sizeof(size_t) * 8) << std::endl; } -std::ostream& operator<<(std::ostream& os, std::basic_string const& s) +// check environment: demonstrate how boost convert u8->u8 throws exception on invalid input +BOOST_AUTO_TEST_CASE(utf_to_utf_failure_boost_u8_u8) { - os << "["; - for (auto& c: s) - os << " " << std::to_string(static_cast(c)); - os << "]"; + for (auto& s: failure_strings_char8_t) { + try { + auto result1{boost::locale::conv::utf_to_utf(s, boost::locale::conv::stop)}; + BOOST_FAIL("Expected boost convert to fail"); + } catch(...) { + // expected + } + } +} - return os; +// check environment: demonstrate how boost convert u8->u16 throws exception on invalid input +BOOST_AUTO_TEST_CASE(utf_to_utf_failure_boost_u8_u16) +{ + for (auto& s: failure_strings_char8_t) { + try { + auto result1{boost::locale::conv::utf_to_utf(s, boost::locale::conv::stop)}; + BOOST_FAIL("Expected boost convert to fail"); + } catch(...) { + // expected + } + } } +// check environment: demonstrate how std u8->u8 throws exception on invalid input +BOOST_AUTO_TEST_CASE(utf_to_utf_failure_std_u8_u8) +{ + for (auto& s: failure_strings_char8_t) { + try { + auto result2{std_convert(s)}; + BOOST_FAIL("Expected std_convert to fail"); + } catch(...) { + // expected + } + + } } +// check environment: demonstrate how std u8->u16 throws exception on invalid input +BOOST_AUTO_TEST_CASE(utf_to_utf_failure_std_u8_u16) +{ + for (auto& s: failure_strings_char8_t) { + try { + auto result2{std_convert(s)}; + BOOST_FAIL("Expected std_convert to fail"); + } catch(...) { + // expected + } + + } +} template void test_utf_to_utf(std::tuple& t) { @@ -365,84 +511,6 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences_invalid, T, types_collection_type } } -// utility wrapper to adapt locale-bound facets for wstring/wbuffer convert -template -struct deletable_facet : Facet -{ - template - deletable_facet(Args&& ...args) : Facet(std::forward(args)...) {} - ~deletable_facet() {} -}; - -namespace { - // char8_t instead of char doesn't work w/ clang++-13 + C++20 (yet?) - std::wstring_convert>, char16_t> conv16; - std::wstring_convert>, char32_t> conv32; - - template - std::basic_string std_convert(const std::basic_string& s); - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - std::string a{s.begin(), s.end()}; - a = conv32.to_bytes(conv32.from_bytes(a)); - return std::basic_string{a.begin(), a.end()}; - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - std::string a{s.begin(), s.end()}; - return conv16.from_bytes(a); - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - std::string a{s.begin(), s.end()}; - return conv32.from_bytes(a); - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - auto result{conv16.to_bytes(s)}; - return std::basic_string(result.begin(), result.end()); - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - return conv16.from_bytes(conv16.to_bytes(s)); - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - return conv32.from_bytes(conv16.to_bytes(s)); - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - auto result{conv32.to_bytes(s)}; - return std::basic_string(result.begin(), result.end()); - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - return conv16.from_bytes(conv32.to_bytes(s)); - } - - template<> - std::basic_string std_convert(const std::basic_string& s) - { - return conv32.from_bytes(conv32.to_bytes(s)); - } -} - class CPUTimer { public: @@ -513,18 +581,18 @@ void test_random_valid(random_context& rc, size_t length, const std::string& des test_random_valid(rc, length, description); } -BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences_valid_all_unicode, T, types_collection_type) +BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences_valid_ascii, T, types_collection_type) { - random_context rc; + random_context rc{127}; - test_random_valid(rc, rc.sequence_length(rc.gen), "All Unicode strings"); + test_random_valid(rc, rc.sequence_length(rc.gen), "ASCII only strings"); } -BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences_valid_ascii, T, types_collection_type) +BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences_valid_all_unicode, T, types_collection_type) { - random_context rc{127}; + random_context rc; - test_random_valid(rc, rc.sequence_length(rc.gen), "ASCII only strings"); + test_random_valid(rc, rc.sequence_length(rc.gen), "All Unicode strings"); } // Test ISO and UTF encodings @@ -616,74 +684,3 @@ BOOST_AUTO_TEST_CASE(is_valid_utf) BOOST_CHECK(unicode::is_valid_utf(u8"äöü")); } -// check assumptions about environment -BOOST_AUTO_TEST_CASE(string_u8string) -{ - std::string a{"\xc3\xa4"}; - - std::basic_string b{a.begin(), a.end()}; - - BOOST_CHECK(b == std::basic_string{u8"ä"}); - - a = std::string{b.begin(), b.end()}; - - BOOST_CHECK(a == std::string{"\xc3\xa4"}); - - BOOST_CHECK(sizeof(size_t) == 4 || sizeof(size_t) == 8); - - std::cout << "Detected CPU Accu size: " << (sizeof(size_t) * 8) << std::endl; -} - -// check environment: demonstrate how boost convert u8->u8 throws exception on invalid input -BOOST_AUTO_TEST_CASE(utf_to_utf_failure_boost_u8_u8) -{ - for (auto& s: failure_strings_char8_t) { - try { - auto result1{boost::locale::conv::utf_to_utf(s, boost::locale::conv::stop)}; - BOOST_FAIL("Expected boost convert to fail"); - } catch(...) { - // expected - } - } -} - -// check environment: demonstrate how boost convert u8->u16 throws exception on invalid input -BOOST_AUTO_TEST_CASE(utf_to_utf_failure_boost_u8_u16) -{ - for (auto& s: failure_strings_char8_t) { - try { - auto result1{boost::locale::conv::utf_to_utf(s, boost::locale::conv::stop)}; - BOOST_FAIL("Expected boost convert to fail"); - } catch(...) { - // expected - } - } -} - -// check environment: demonstrate how std u8->u8 throws exception on invalid input -BOOST_AUTO_TEST_CASE(utf_to_utf_failure_std_u8_u8) -{ - for (auto& s: failure_strings_char8_t) { - try { - auto result2{std_convert(s)}; - BOOST_FAIL("Expected std_convert to fail"); - } catch(...) { - // expected - } - - } -} - -// check environment: demonstrate how std u8->u16 throws exception on invalid input -BOOST_AUTO_TEST_CASE(utf_to_utf_failure_std_u8_u16) -{ - for (auto& s: failure_strings_char8_t) { - try { - auto result2{std_convert(s)}; - BOOST_FAIL("Expected std_convert to fail"); - } catch(...) { - // expected - } - - } -} -- cgit v1.2.3