From 611601ec36a5603bc9c94cdac9a307c4bb07c929 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 31 Jan 2021 19:00:34 +0100 Subject: Add facet based interface --- src/test-unicode.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 7 deletions(-) (limited to 'src/test-unicode.cpp') diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index 3d67124..e1aa23d 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -96,13 +96,18 @@ void test_utf_to_utf(std::tuple& t) typedef typename std::tuple_element::type>::type From; typedef typename std::tuple_element::type>::type To; - // test - To result { unicode::utf_to_utf(std::get(t)) }; + // test base type interface + To result { unicode::convert(std::get(t)) }; - BOOST_CHECK_MESSAGE(std::get(t) == result, "From " << typeid(From).name() << "(" << i << ", " << std::get(t) << ") to " << typeid(To).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); //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 if constexpr (i + 1 < std::tuple_size::type>::value) test_utf_to_utf(t); @@ -147,9 +152,18 @@ void test_utf_to_utf_failure(std::basic_string& s) { typedef typename std::tuple_element::type::value_type To; + // via base type + try { + (void) unicode::convert(s); + BOOST_ERROR("Base: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); + } catch (...) { + // OK + }; + + // via facet try { - unicode::utf_to_utf(s); - BOOST_ERROR("Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); + (void) unicode::convert::Facet,typename unicode::Encoding::Facet>(s); + BOOST_ERROR("Facet: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); } catch (...) { // OK }; @@ -236,14 +250,35 @@ void test_random(random_context& rc, size_t length) From r {generate_random(rc, length)}; + // base type interface try { - To result{unicode::utf_to_utf(r)}; + To result{unicode::convert(r)}; + + if (r.empty()) { + BOOST_CHECK(result.empty()); + } else { + BOOST_CHECK(!result.empty()); + } } catch (const std::runtime_error&) { // OK: this is an expected exception for utf_to_utf on bad input } catch (const std::invalid_argument&) { // OK: this is an expected exception for utf_to_utf on bad input } + // facet interface + try { + To result{unicode::convert::Facet,typename unicode::Encoding::Facet>(r)}; + + if (r.empty()) { + BOOST_CHECK(result.empty()); + } else { + BOOST_CHECK(!result.empty()); + } + } catch (const std::runtime_error&) { + // OK: this is an expected exception for utf_to_utf on bad input + } catch (const std::invalid_argument&) { + // OK: this is an expected exception for utf_to_utf on bad input + } //std::cerr << "DEBUG: " << typeid(From).name() << std::endl; //std::cerr << " DEBUG2: " << typeid(To).name() << std::endl; @@ -255,8 +290,9 @@ void test_random(random_context& rc, size_t length) BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences, T, types_collection_type) { random_context rc; + int i{}; - // run for 1s (debug) 10s (release) + // run for 1s (debug) 10s (release) = total time for all random_sequences types! #ifdef _DEBUG const auto timeout{1.0s}; #else @@ -267,7 +303,29 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences, T, types_collection_type) while (!(std::chrono::steady_clock::now() > timeout_stamp)) { test_random(rc, rc.sequence_length(rc.gen)); + i++; } + + BOOST_CHECK_MESSAGE(i > 1, "Not enough iterations done!"); +} + +// Test ISO and UTF encodings +BOOST_AUTO_TEST_CASE(convert) +{ + BOOST_CHECK((std::string{unicode::convert({})}) == std::string{}); + BOOST_CHECK((std::string{unicode::convert("abc")}) == std::string{"abc"}); + BOOST_CHECK((std::string{unicode::convert("äöü")}) == std::string{"äöü"}); + BOOST_CHECK((std::string{unicode::convert("\xa4")}) == std::string{"\xa4"}); // € + + BOOST_CHECK((std::string{unicode::convert("\xa4")}) == std::string{"\xa4"}); // € + + BOOST_CHECK_THROW(((void)std::string{unicode::convert("\xa4")}), std::invalid_argument); // € not available in ISO-8859-1 + + 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"}); } // TODO: -- cgit v1.2.3