From 5572e23e8e2109abd73b916f4f0d278e1aa21f34 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 3 Feb 2021 13:18:25 +0100 Subject: Add msbuild files --- src/test-unicode.cpp | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'src/test-unicode.cpp') diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index e1aa23d..c169fc9 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -35,11 +35,13 @@ std::vector success_sets { // Error cases: throwing upon convert to all other types std::vector> failure_strings_char8_t { - u8"\x80", // utf-8 continuation byte - u8"\x81", // utf-8 continuation byte - 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 + // Note: don't encode this as u8"" since MSVC will interpret \x80 as \u0080, +// yet to be encoded to UTF-8 for execution encoding + "\x80", // utf-8 continuation byte + "\x81", // utf-8 continuation byte + "\xc3\xc3\xa4", // initial byte of utf-8 "ä", followed by valid utf-8 "ä" + "\xF8\x80\x80\x80\x80", // overlong encoding + "\xF7\xBF\xBF\xBF", // valid encoding of invalid code point }; std::vector> failure_strings_char16_t { @@ -156,16 +158,20 @@ void test_utf_to_utf_failure(std::basic_string& s) try { (void) unicode::convert(s); BOOST_ERROR("Base: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name()); - } catch (...) { - // OK + } 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 facet try { (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 + } 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()); }; // iterate over remaining types @@ -229,15 +235,16 @@ BOOST_AUTO_TEST_CASE(is_valid_unicode) struct random_context { std::random_device rd; // OS random number engine to seed RNG (below) std::mt19937 gen{rd()}; - std::uniform_int_distribution<> sequence_length{0, 100000}; // length of sequence: 0 ... 100000 code units + std::uniform_int_distribution sequence_length{0, 100000}; // length of sequence: 0 ... 100000 code units }; template T generate_random(random_context& rc, size_t length) { - std::uniform_int_distribution<> code_unit(0, std::numeric_limits::max()); // code unit value + // Using unsigned long for std::uniform_int_distribution<> because it needs to be basic type according to MSVC + std::uniform_int_distribution code_unit(std::numeric_limits::max()); // code unit value T result; - std::generate_n(std::back_inserter(result), length, [&](){return code_unit(rc.gen);}); + std::generate_n(std::back_inserter(result), length, [&](){return static_cast(code_unit(rc.gen));}); return result; } @@ -248,7 +255,7 @@ void test_random(random_context& rc, size_t length) //std::cerr << "LENGTH: " << length << std::endl; typedef typename std::tuple_element::type To; - From r {generate_random(rc, length)}; + From r {static_cast(generate_random(rc, length))}; // base type interface try { @@ -259,10 +266,10 @@ void test_random(random_context& rc, size_t length) } 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 + // 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 @@ -274,13 +281,11 @@ void test_random(random_context& rc, size_t length) } 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 + // OK: this is an expected exception for convert() on bad input + } catch (const std::exception& ex) { + BOOST_ERROR("Unexpected error on convert(): " << ex.what()); } - //std::cerr << "DEBUG: " << typeid(From).name() << std::endl; - //std::cerr << " DEBUG2: " << typeid(To).name() << std::endl; // iterate over remaining To types if constexpr (i + 1 < std::tuple_size::value) -- cgit v1.2.3