// Copyright (c) 2020 Egor Tensin // This file is part of the "winapi-utf8" project. // For details, see https://github.com/egor-tensin/winapi-utf8. // Distributed under the MIT License. #include #include // clang-format off // The order matters for older Boost versions. #include #include #include // clang-format on #include #include #include namespace std { ostream& operator<<(ostream& os, unsigned char c) { return os << boost::format("%|1$02x|") % static_cast(c); } ostream& operator<<(ostream& os, const vector& cs) { for (auto c : cs) { os << c; } return os; } } // namespace std namespace { std::vector from(std::initializer_list xs) { return {xs}; } template std::vector from(const std::basic_string& s) { const auto buf = reinterpret_cast(s.c_str()); const auto size = s.size() * sizeof(CharT); return {buf, buf + size}; } std::vector> utf16 = { // Hello from({0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00}), // Привет from({0x1f, 0x04, 0x40, 0x04, 0x38, 0x04, 0x32, 0x04, 0x35, 0x04, 0x42, 0x04}), }; std::vector> utf8 = { // Hello from({0x48, 0x65, 0x6c, 0x6c, 0x6f}), // Привет from({0xd0, 0x9f, 0xd1, 0x80, 0xd0, 0xb8, 0xd0, 0xb2, 0xd0, 0xb5, 0xd1, 0x82}), }; } // namespace BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(std::vector); BOOST_AUTO_TEST_SUITE(convert_tests) BOOST_DATA_TEST_CASE(test_narrow, boost::unit_test::data::make(utf16) ^ utf8, input, expected) { auto actual = from(winapi::narrow(input)); BOOST_TEST(actual == expected, "Expected: " << expected << ", actual: " << actual); } BOOST_DATA_TEST_CASE(test_widen, boost::unit_test::data::make(utf8) ^ utf16, input, expected) { auto actual = from(winapi::widen(input)); BOOST_TEST(actual == expected, "Expected: " << expected << ", actual: " << actual); } BOOST_AUTO_TEST_SUITE_END()