From 4965d1eac5a38389169d4d935c56ab468a685354 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 18 Aug 2023 06:19:37 +0200 Subject: [PATCH] unicode: add unit tests Signed-off-by: Simon Rozman --- UnitTests/UnitTests.vcxproj | 1 + UnitTests/UnitTests.vcxproj.filters | 3 ++ UnitTests/pch.h | 1 + UnitTests/unicode.cpp | 48 +++++++++++++++++++++++++++++ include/stdex/unicode.hpp | 2 ++ 5 files changed, 55 insertions(+) create mode 100644 UnitTests/unicode.cpp diff --git a/UnitTests/UnitTests.vcxproj b/UnitTests/UnitTests.vcxproj index 2f6f6bcd6..dc1f70e7a 100644 --- a/UnitTests/UnitTests.vcxproj +++ b/UnitTests/UnitTests.vcxproj @@ -123,6 +123,7 @@ + diff --git a/UnitTests/UnitTests.vcxproj.filters b/UnitTests/UnitTests.vcxproj.filters index 29bb1fd0d..a42e11a19 100644 --- a/UnitTests/UnitTests.vcxproj.filters +++ b/UnitTests/UnitTests.vcxproj.filters @@ -33,6 +33,9 @@ Source Files + + Source Files + diff --git a/UnitTests/pch.h b/UnitTests/pch.h index 30152cedc..7e2d87e1c 100644 --- a/UnitTests/pch.h +++ b/UnitTests/pch.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include diff --git a/UnitTests/unicode.cpp b/UnitTests/unicode.cpp new file mode 100644 index 000000000..b740d2b07 --- /dev/null +++ b/UnitTests/unicode.cpp @@ -0,0 +1,48 @@ +/* + SPDX-License-Identifier: MIT + Copyright © 2023 Amebis +*/ + +#include "pch.h" + +using namespace std; +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace UnitTests +{ + TEST_CLASS(unicode) + { + public: + TEST_METHOD(str2wstr) + { + Assert::AreEqual( + L"This is a test.", + stdex::str2wstr("This is a test.", stdex::charset_id::utf8).c_str()); + Assert::AreEqual( + L"Th\u00ed\u0161 i\u22c5 a te\u0073\u0304t. 😀😅", + stdex::str2wstr("Thíš i⋅ a tes̄t. 😀😅", stdex::charset_id::utf8).c_str()); + Assert::AreEqual( + L"", + stdex::str2wstr("test", 0, stdex::charset_id::utf8).c_str()); + Assert::AreEqual( + L"", + stdex::str2wstr(nullptr, 0, stdex::charset_id::utf8).c_str()); + } + + TEST_METHOD(wstr2str) + { + Assert::AreEqual( + "This is a test.", + stdex::wstr2str(L"This is a test.", stdex::charset_id::utf8).c_str()); + Assert::AreEqual( + "Th\xc3\xad\xc5\xa1 i\xe2\x8b\x85 a tes\xcc\x84t. \xf0\x9f\x98\x80\xf0\x9f\x98\x85", + stdex::wstr2str(L"Thíš i⋅ a tes̄t. 😀😅", stdex::charset_id::utf8).c_str()); + Assert::AreEqual( + "", + stdex::wstr2str(L"test", 0, stdex::charset_id::utf8).c_str()); + Assert::AreEqual( + "", + stdex::wstr2str(nullptr, 0, stdex::charset_id::utf8).c_str()); + } + }; +} diff --git a/include/stdex/unicode.hpp b/include/stdex/unicode.hpp index 0fcfbf6eb..289fdc336 100644 --- a/include/stdex/unicode.hpp +++ b/include/stdex/unicode.hpp @@ -46,6 +46,7 @@ namespace stdex // Try to convert to stack buffer first. WCHAR szStackBuffer[1024/sizeof(WCHAR)]; +#pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpMultiByteStr parameter wrong? int cch = MultiByteToWideChar(static_cast(charset), dwFlags, src, static_cast(count_src), szStackBuffer, _countof(szStackBuffer)); if (cch) { // Append from stack. @@ -134,6 +135,7 @@ namespace stdex // Try to convert to stack buffer first. CHAR szStackBuffer[1024/sizeof(CHAR)]; +#pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpWideCharStr parameter wrong? int cch = WideCharToMultiByte(static_cast(charset), dwFlags, src, static_cast(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL); if (cch) { // Copy from stack. Be careful not to include zero terminator.