From aa2edab739e3daac42cd5dbb44d10234ad880626 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 2 Jan 2022 19:02:38 +0100 Subject: Separated unit tests and performance tests --- src/test-helper.cpp | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/test-helper.cpp (limited to 'src/test-helper.cpp') diff --git a/src/test-helper.cpp b/src/test-helper.cpp new file mode 100644 index 0000000..d441895 --- /dev/null +++ b/src/test-helper.cpp @@ -0,0 +1,131 @@ +#include "test-helper.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// 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 const& s) + { + os << "["; + for (auto& c: s) + os << " " << std::to_string(static_cast(c)); + os << "]"; + + return os; + } +#endif + + std::ostream& operator<<(std::ostream& os, std::basic_string const& s) + { + os << "["; + for (auto& c: s) + os << " " << std::to_string(static_cast(c)); + os << "]"; + + return os; + } + + std::ostream& operator<<(std::ostream& os, std::basic_string const& s) + { + os << "["; + for (auto& c: s) + os << " " << std::to_string(static_cast(c)); + os << "]"; + + return os; + } + +} // namespace std + +namespace { + + // utility wrapper to adapt locale-bound facets for wstring/wbuffer convert + template + struct deletable_facet : Facet + { + template + deletable_facet(Args&& ...args) : Facet(std::forward(args)...) {} + ~deletable_facet() {} + }; + + // char8_t instead of char doesn't work w/ clang++-13 + C++20 (yet?) + std::wstring_convert>, char16_t> conv16; + std::wstring_convert>, char32_t> conv32; + +} // namespace + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + std::string a{s.begin(), s.end()}; + a = conv32.to_bytes(conv32.from_bytes(a)); + return std::basic_string{a.begin(), a.end()}; +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + std::string a{s.begin(), s.end()}; + return conv16.from_bytes(a); +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + std::string a{s.begin(), s.end()}; + return conv32.from_bytes(a); +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + auto result{conv16.to_bytes(s)}; + return std::basic_string(result.begin(), result.end()); +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + return conv16.from_bytes(conv16.to_bytes(s)); +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + return conv32.from_bytes(conv16.to_bytes(s)); +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + auto result{conv32.to_bytes(s)}; + return std::basic_string(result.begin(), result.end()); +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + return conv16.from_bytes(conv32.to_bytes(s)); +} + +template<> +std::basic_string std_convert(const std::basic_string& s) +{ + return conv32.from_bytes(conv32.to_bytes(s)); +} + -- cgit v1.2.3