unicode: add unit tests
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
d7950abe42
commit
4965d1eac5
@ -123,6 +123,7 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="ring.cpp" />
|
||||
<ClCompile Include="sgml.cpp" />
|
||||
<ClCompile Include="unicode.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="pch.h" />
|
||||
|
@ -33,6 +33,9 @@
|
||||
<ClCompile Include="ring.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="unicode.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="pch.h">
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <stdex/sal.hpp>
|
||||
#include <stdex/sgml.hpp>
|
||||
#include <stdex/string.hpp>
|
||||
#include <stdex/unicode.hpp>
|
||||
#include <stdex/vector_queue.hpp>
|
||||
|
||||
#include <CppUnitTest.h>
|
||||
|
48
UnitTests/unicode.cpp
Normal file
48
UnitTests/unicode.cpp
Normal 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());
|
||||
}
|
||||
};
|
||||
}
|
@ -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<UINT>(charset), dwFlags, src, static_cast<int>(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<UINT>(charset), dwFlags, src, static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL);
|
||||
if (cch) {
|
||||
// Copy from stack. Be careful not to include zero terminator.
|
||||
|
Loading…
x
Reference in New Issue
Block a user