From 2ef9f51df48b14556e236d14213233e1bd7f829a Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 30 Jan 2021 19:20:15 +0100 Subject: Added Support for Debian and Ubuntu, add is_valid_utf() --- src/test-unicode.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index 05370c7..3d67124 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -37,7 +37,7 @@ std::vector success_sets { std::vector> failure_strings_char8_t { u8"\x80", // utf-8 continuation byte u8"\x81", // utf-8 continuation byte - u8"\xc3ä", // initial byte of utf-8 "ä", followed by valid utf-8 "ä" + u8"\xc3\xc3\xa4", // initial byte of utf-8 "ä", followed by valid utf-8 "ä" u8"\xF8\x80\x80\x80\x80", // overlong encoding u8"\xF7\xBF\xBF\xBF", // valid encoding of invalid code point }; @@ -56,6 +56,7 @@ std::vector> failure_strings_char32_t { // output operators must be in same namespace as the type itself namespace std { +#ifdef __cpp_char8_t std::ostream& operator<<(std::ostream& os, std::basic_string const& s) { os << "["; @@ -65,6 +66,7 @@ std::ostream& operator<<(std::ostream& os, std::basic_string const& s) return os; } +#endif std::ostream& operator<<(std::ostream& os, std::basic_string const& s) { @@ -118,6 +120,27 @@ BOOST_AUTO_TEST_CASE(utf_to_utf_success) test_utf_to_utf(t); } +template +void test_is_valid_utf(std::tuple& t) +{ + typedef typename std::tuple_element::type>::type T; + + // test + bool result { unicode::is_valid_utf(std::get(t)) }; + + BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(T).name() << "(" << i << ", " << std::get(t) << "), got " << result); + + // iterate over other combinations + if constexpr (i + 1 < std::tuple_size::type>::value) + test_is_valid_utf(t); +} + +BOOST_AUTO_TEST_CASE(is_valid_utf_success) +{ + for (auto& t: success_sets) + test_is_valid_utf(t); +} + // iterate over std::tuple T types template void test_utf_to_utf_failure(std::basic_string& s) @@ -126,7 +149,7 @@ void test_utf_to_utf_failure(std::basic_string& s) try { unicode::utf_to_utf(s); - BOOST_FAIL("Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); + BOOST_ERROR("Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); } catch (...) { // OK }; @@ -148,6 +171,29 @@ BOOST_AUTO_TEST_CASE(utf_to_utf_failure) test_utf_to_utf_failure::type::value_type, types_collection_type>(s); } +// iterate over std::tuple T types +template +void test_is_valid_utf_failure(std::basic_string& s) +{ + BOOST_CHECK_MESSAGE(unicode::is_valid_utf(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(T).name()); + + // iterate over remaining types + if constexpr (index + 1 < std::tuple_size::value) + test_is_valid_utf_failure(s); +} + +BOOST_AUTO_TEST_CASE(is_valid_utf_failure) +{ + for (auto& s: failure_strings_char8_t) + test_is_valid_utf_failure::type::value_type, types_collection_type>(s); + + for (auto& s: failure_strings_char16_t) + test_is_valid_utf_failure::type::value_type, types_collection_type>(s); + + for (auto& s: failure_strings_char32_t) + test_is_valid_utf_failure::type::value_type, types_collection_type>(s); +} + BOOST_AUTO_TEST_CASE(is_valid_unicode) { BOOST_CHECK(unicode::is_valid_unicode('\0')); -- cgit v1.2.3