summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2021-01-30 19:20:15 +0100
committerRoland Reichwein <mail@reichwein.it>2021-01-30 19:20:15 +0100
commit2ef9f51df48b14556e236d14213233e1bd7f829a (patch)
treef5230e326501ebaec1baca9615dc69b7538f1911 /src
parentee9dfd30e8c02e9a7457385786218d2b4b935720 (diff)
Added Support for Debian and Ubuntu, add is_valid_utf()
Diffstat (limited to 'src')
-rw-r--r--src/test-unicode.cpp50
1 files changed, 48 insertions, 2 deletions
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<types_collection_type> success_sets {
std::vector<std::basic_string<utf8_t>> 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<std::basic_string<char32_t>> 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<utf8_t> const& s)
{
os << "[";
@@ -65,6 +66,7 @@ std::ostream& operator<<(std::ostream& os, std::basic_string<utf8_t> const& s)
return os;
}
+#endif
std::ostream& operator<<(std::ostream& os, std::basic_string<char16_t> const& s)
{
@@ -118,6 +120,27 @@ BOOST_AUTO_TEST_CASE(utf_to_utf_success)
test_utf_to_utf(t);
}
+template<size_t i = 0, typename... Ts>
+void test_is_valid_utf(std::tuple<Ts...>& t)
+{
+ typedef typename std::tuple_element<i,typename std::remove_reference<decltype(t)>::type>::type T;
+
+ // test
+ bool result { unicode::is_valid_utf<typename T::value_type>(std::get<i>(t)) };
+
+ BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(T).name() << "(" << i << ", " << std::get<i>(t) << "), got " << result);
+
+ // iterate over other combinations
+ if constexpr (i + 1 < std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value)
+ test_is_valid_utf<i + 1>(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<typename From, typename Collection, size_t index = 0>
void test_utf_to_utf_failure(std::basic_string<From>& s)
@@ -126,7 +149,7 @@ void test_utf_to_utf_failure(std::basic_string<From>& s)
try {
unicode::utf_to_utf<From,To>(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<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
}
+// iterate over std::tuple T types
+template<typename T, typename Collection, size_t index = 0>
+void test_is_valid_utf_failure(std::basic_string<T>& s)
+{
+ BOOST_CHECK_MESSAGE(unicode::is_valid_utf<T>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(T).name());
+
+ // iterate over remaining types
+ if constexpr (index + 1 < std::tuple_size<Collection>::value)
+ test_is_valid_utf_failure<T, Collection, index + 1>(s);
+}
+
+BOOST_AUTO_TEST_CASE(is_valid_utf_failure)
+{
+ for (auto& s: failure_strings_char8_t)
+ test_is_valid_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
+
+ for (auto& s: failure_strings_char16_t)
+ test_is_valid_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
+
+ for (auto& s: failure_strings_char32_t)
+ test_is_valid_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
+}
+
BOOST_AUTO_TEST_CASE(is_valid_unicode)
{
BOOST_CHECK(unicode::is_valid_unicode('\0'));