From 268b7845af166c68b1c226f0be9ba5cf983ae91c Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 14 Feb 2021 17:50:51 +0100 Subject: Support different std containers, support different basic types --- src/test-unicode.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index 5f5ebbf..fbd4749 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -5,9 +5,12 @@ #include #include +#include #include +#include #include #include +#include #include #include #include @@ -98,14 +101,14 @@ void test_utf_to_utf(std::tuple& t) // test base type interface To result { unicode::convert(std::get(t)) }; + BOOST_CHECK_MESSAGE(std::get(t) == result, "Base: From " << typeid(typename From::value_type).name() << "(" << i << ", " << std::get(t) << ") to " << typeid(typename To::value_type).name() << "(" << j << ", " << std::get(t) << "), got " << result); - BOOST_CHECK_MESSAGE(std::get(t) == result, "Base: From " << typeid(From).name() << "(" << i << ", " << std::get(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get(t) << "), got " << result); + // test container interface + result = unicode::convert(std::get(t)); + BOOST_CHECK_MESSAGE(std::get(t) == result, "Container: From " << typeid(From).name() << "(" << i << ", " << std::get(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get(t) << "), got " << result); - //std::cout << std::to_string(std::tuple_size::type>::value) << "," << std::to_string(i) << "," << std::to_string(j) << std::endl; - // test facet interface result = unicode::convert::Facet, typename unicode::Encoding::Facet>(std::get(t)); - BOOST_CHECK_MESSAGE(std::get(t) == result, "Facet: From " << typeid(From).name() << "(" << i << ", " << std::get(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get(t) << "), got " << result); // iterate over other combinations @@ -132,6 +135,10 @@ void test_is_valid_utf(std::tuple& t) // test via basic type bool result { unicode::is_valid_utf(std::get(t)) }; + BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(typename T::value_type).name() << "(" << i << ", " << std::get(t) << "), got " << result); + + // test via container type + 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); // test via Facet @@ -158,7 +165,17 @@ void test_utf_to_utf_failure(std::basic_string& s) // via base type try { (void) unicode::convert(s); - BOOST_ERROR("Base: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); + BOOST_ERROR("Base type: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); + } catch (const std::invalid_argument&) { + // OK: this is an expected exception for convert() on bad input + } catch (const std::exception& ex) { + BOOST_ERROR("Unexpected error on convert(): " << ex.what()); + }; + + // via container + try { + (void) unicode::convert::Facet::string_type, typename unicode::Encoding::Facet::string_type>(s); + BOOST_ERROR("Container type: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); } catch (const std::invalid_argument&) { // OK: this is an expected exception for convert() on bad input } catch (const std::exception& ex) { @@ -198,6 +215,8 @@ 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()); + BOOST_CHECK_MESSAGE(unicode::is_valid_utf>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(T).name()); + BOOST_CHECK_MESSAGE(unicode::is_valid_utf::Facet>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(typename unicode::Encoding::Facet).name()); // iterate over remaining types @@ -275,6 +294,21 @@ void test_random(random_context& rc, size_t length) BOOST_ERROR("Unexpected error on convert(): " << ex.what()); } + // container type interface + try { + To result{unicode::convert(r)}; + + if (r.empty()) { + BOOST_CHECK(result.empty()); + } else { + BOOST_CHECK(!result.empty()); + } + } catch (const std::invalid_argument&) { + // OK: this is an expected exception for convert() on bad input + } catch (const std::exception& ex) { + BOOST_ERROR("Unexpected error on convert(): " << ex.what()); + } + // facet interface try { To result{unicode::convert::Facet,typename unicode::Encoding::Facet>(r)}; @@ -331,7 +365,7 @@ BOOST_AUTO_TEST_CASE(convert) BOOST_CHECK((unicode::convert("abc")) == std::u16string{u"abc"}); BOOST_CHECK((unicode::convert(U"abc")) == std::u16string{u"abc"}); - + BOOST_CHECK((unicode::convert("abc")) == std::u16string{u"abc"}); BOOST_CHECK((unicode::convert(U"abc")) == std::u16string{u"abc"}); @@ -354,7 +388,37 @@ BOOST_AUTO_TEST_CASE(convert) BOOST_CHECK((unicode::convert(std::string{"äöü"})) == std::wstring{L"äöü"}); - //BOOST_CHECK((unicode::convert, std::vector>(std::vector{})) == std::vector{}); + BOOST_CHECK((unicode::convert, std::vector>(std::vector{})) == std::vector{}); + BOOST_CHECK((unicode::convert, std::vector>(std::vector{'\xc3', '\xa4', '\xc3', '\xb6', '\xc3', '\xbc'})) == (std::vector{L'ä', L'ö', L'ü'})); + + // deque + BOOST_CHECK((unicode::convert, std::deque>(std::deque{})) == std::deque{}); + BOOST_CHECK((unicode::convert, std::deque>(std::deque{'\xc3', '\xa4', '\xc3', '\xb6', '\xc3', '\xbc'})) == (std::deque{L'ä', L'ö', L'ü'})); + + // deque with uint8_t, uint16_t + BOOST_CHECK((unicode::convert, std::deque>(std::deque{})) == std::deque{}); + BOOST_CHECK((unicode::convert, std::deque>(std::deque{0xc3, 0xa4, 0xc3, 0xb6, 0xc3, 0xbc})) == (std::deque{L'ä', L'ö', L'ü'})); + + // deque with int8_t, int16_t + BOOST_CHECK((unicode::convert, std::deque>(std::deque{ + static_cast(0xc3), + static_cast(0xa4), + static_cast(0xc3), + static_cast(0xb6), + static_cast(0xc3), + static_cast(0xbc)})) == (std::deque{L'ä', L'ö', L'ü'})); + + // list + BOOST_CHECK((unicode::convert, std::list>(std::list{})) == std::list{}); + BOOST_CHECK((unicode::convert, std::list>(std::list{0xc3, 0xa4, 0xc3, 0xb6, 0xc3, 0xbc})) == (std::list{L'ä', L'ö', L'ü'})); + + // list -> deque + BOOST_CHECK((unicode::convert, std::deque>(std::list{})) == std::deque{}); + BOOST_CHECK((unicode::convert, std::deque>(std::list{0xc3, 0xa4, 0xc3, 0xb6, 0xc3, 0xbc})) == (std::deque{L'ä', L'ö', L'ü'})); + + // array + BOOST_CHECK((unicode::convert, std::list>(std::array{})) == std::list{}); + BOOST_CHECK((unicode::convert, std::list>(std::array{0xc3, 0xa4, 0xc3, 0xb6, 0xc3, 0xbc})) == (std::list{L'ä', L'ö', L'ü'})); } BOOST_AUTO_TEST_CASE(is_valid_utf) @@ -376,8 +440,3 @@ BOOST_AUTO_TEST_CASE(string_u8string) BOOST_CHECK(a == std::string{"\xc3\xa4"}); } - -// TODO: -// -// string, vector? -// uint8_t, uint16_t, uint32_t? -- cgit v1.2.3