summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2021-01-31 19:00:34 +0100
committerRoland Reichwein <mail@reichwein.it>2021-01-31 19:00:34 +0100
commit611601ec36a5603bc9c94cdac9a307c4bb07c929 (patch)
tree0b1c27d5958a2a3bdfe3c421a27f6ab528fbc3e1 /src
parent2ef9f51df48b14556e236d14213233e1bd7f829a (diff)
Add facet based interface
Diffstat (limited to 'src')
-rw-r--r--src/test-unicode.cpp72
-rw-r--r--src/validate.cpp4
2 files changed, 69 insertions, 7 deletions
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<Ts...>& t)
typedef typename std::tuple_element<i,typename std::remove_reference<decltype(t)>::type>::type From;
typedef typename std::tuple_element<j,typename std::remove_reference<decltype(t)>::type>::type To;
- // test
- To result { unicode::utf_to_utf<typename From::value_type, typename To::value_type>(std::get<i>(t)) };
+ // test base type interface
+ To result { unicode::convert<typename From::value_type, typename To::value_type>(std::get<i>(t)) };
- BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);
+ BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "Base: From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);
//std::cout << std::to_string(std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value) << "," << std::to_string(i) << "," << std::to_string(j) << std::endl;
+
+ // test facet interface
+ result = unicode::convert<typename unicode::Encoding<typename From::value_type>::Facet, typename unicode::Encoding<typename To::value_type>::Facet>(std::get<i>(t));
+ BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "Facet: From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);
+
// iterate over other combinations
if constexpr (i + 1 < std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value)
test_utf_to_utf<i + 1, j>(t);
@@ -147,9 +152,18 @@ void test_utf_to_utf_failure(std::basic_string<From>& s)
{
typedef typename std::tuple_element<index, Collection>::type::value_type To;
+ // via base type
+ try {
+ (void) unicode::convert<From,To>(s);
+ BOOST_ERROR("Base: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name());
+ } catch (...) {
+ // OK
+ };
+
+ // via facet
try {
- unicode::utf_to_utf<From,To>(s);
- BOOST_ERROR("Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name());
+ (void) unicode::convert<typename unicode::Encoding<From>::Facet,typename unicode::Encoding<To>::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<From>(rc, length)};
+ // base type interface
try {
- To result{unicode::utf_to_utf<typename From::value_type,typename To::value_type>(r)};
+ To result{unicode::convert<typename From::value_type,typename To::value_type>(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<typename unicode::Encoding<typename From::value_type>::Facet,typename unicode::Encoding<typename To::value_type>::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<T,types_collection_type>(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<unicode::ISO_8859_1,unicode::ISO_8859_1>({})}) == std::string{});
+ BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_1,unicode::ISO_8859_1>("abc")}) == std::string{"abc"});
+ BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_1,unicode::ISO_8859_1>("äöü")}) == std::string{"äöü"});
+ BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_1,unicode::ISO_8859_1>("\xa4")}) == std::string{"\xa4"}); // €
+
+ BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_15,unicode::ISO_8859_15>("\xa4")}) == std::string{"\xa4"}); // €
+
+ BOOST_CHECK_THROW(((void)std::string{unicode::convert<unicode::ISO_8859_15,unicode::ISO_8859_1>("\xa4")}), std::invalid_argument); // € not available in ISO-8859-1
+
+ BOOST_CHECK((unicode::convert<unicode::UTF_8,unicode::UTF_16>("abc")) == std::u16string{u"abc"});
+ BOOST_CHECK((unicode::convert<unicode::UTF_32,unicode::UTF_16>(U"abc")) == std::u16string{u"abc"});
+
+ BOOST_CHECK((unicode::convert<utf8_t,char16_t>("abc")) == std::u16string{u"abc"});
+ BOOST_CHECK((unicode::convert<char32_t,char16_t>(U"abc")) == std::u16string{u"abc"});
}
// TODO:
diff --git a/src/validate.cpp b/src/validate.cpp
new file mode 100644
index 0000000..8927fe4
--- /dev/null
+++ b/src/validate.cpp
@@ -0,0 +1,4 @@
+int main(int argc, char* argv[])
+{
+ return 0;
+}