From cd7e832e2f47fa35d36794808582118cb34eca3f Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 22 Dec 2021 14:17:56 +0100 Subject: Added tests --- src/test-unicode.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index c325f6c..59d55b9 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -384,7 +384,9 @@ namespace { template<> std::basic_string std_convert(const std::basic_string& s) { - return 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<> @@ -411,7 +413,7 @@ namespace { template<> std::basic_string std_convert(const std::basic_string& s) { - return s; + return conv16.from_bytes(conv16.to_bytes(s)); } template<> @@ -436,7 +438,7 @@ namespace { template<> std::basic_string std_convert(const std::basic_string& s) { - return s; + return conv32.from_bytes(conv32.to_bytes(s)); } } @@ -459,6 +461,8 @@ void test_random_valid(random_context& rc, size_t length, const std::string& des BOOST_CHECK(list[i].size() >= u32list[i].size()); To result{unicode::convert::Facet,typename unicode::Encoding::Facet>(list[i])}; BOOST_CHECK(result.size() >= u32list[i].size()); + auto boost_result{boost::locale::conv::utf_to_utf(list[i])}; + BOOST_CHECK_EQUAL(result, boost_result); } { @@ -609,3 +613,57 @@ BOOST_AUTO_TEST_CASE(string_u8string) BOOST_CHECK(a == std::string{"\xc3\xa4"}); } + +// 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