unicode: add unit tests

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-08-18 06:19:37 +02:00
parent d7950abe42
commit 4965d1eac5
5 changed files with 55 additions and 0 deletions

View File

@ -123,6 +123,7 @@
</ClCompile> </ClCompile>
<ClCompile Include="ring.cpp" /> <ClCompile Include="ring.cpp" />
<ClCompile Include="sgml.cpp" /> <ClCompile Include="sgml.cpp" />
<ClCompile Include="unicode.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="pch.h" /> <ClInclude Include="pch.h" />

View File

@ -33,6 +33,9 @@
<ClCompile Include="ring.cpp"> <ClCompile Include="ring.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="unicode.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="pch.h"> <ClInclude Include="pch.h">

View File

@ -23,6 +23,7 @@
#include <stdex/sal.hpp> #include <stdex/sal.hpp>
#include <stdex/sgml.hpp> #include <stdex/sgml.hpp>
#include <stdex/string.hpp> #include <stdex/string.hpp>
#include <stdex/unicode.hpp>
#include <stdex/vector_queue.hpp> #include <stdex/vector_queue.hpp>
#include <CppUnitTest.h> #include <CppUnitTest.h>

48
UnitTests/unicode.cpp Normal file
View File

@ -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());
}
};
}

View File

@ -46,6 +46,7 @@ namespace stdex
// Try to convert to stack buffer first. // Try to convert to stack buffer first.
WCHAR szStackBuffer[1024/sizeof(WCHAR)]; 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<UINT>(charset), dwFlags, src, static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer)); int cch = MultiByteToWideChar(static_cast<UINT>(charset), dwFlags, src, static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer));
if (cch) { if (cch) {
// Append from stack. // Append from stack.
@ -134,6 +135,7 @@ namespace stdex
// Try to convert to stack buffer first. // Try to convert to stack buffer first.
CHAR szStackBuffer[1024/sizeof(CHAR)]; 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<UINT>(charset), dwFlags, src, static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL); int cch = WideCharToMultiByte(static_cast<UINT>(charset), dwFlags, src, static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL);
if (cch) { if (cch) {
// Copy from stack. Be careful not to include zero terminator. // Copy from stack. Be careful not to include zero terminator.