char16_t is not exactly the wchar_t on Windows. char32_t is not exactly the wchar_t on POSIX. Rather than selecting the appropriate variant, polymorphism picked the template implementation of strncmp, strcpy and strncpy. The one that does not convert UTF16 surrogate pairs against their UTF32 representation. Signed-off-by: Simon Rozman <simon@rozman.si>
73 lines
2.4 KiB
C++
73 lines
2.4 KiB
C++
/*
|
|
SPDX-License-Identifier: MIT
|
|
Copyright © 2023-2025 Amebis
|
|
*/
|
|
|
|
#include "pch.hpp"
|
|
|
|
using namespace std;
|
|
#ifdef _WIN32
|
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
|
#endif
|
|
|
|
namespace UnitTests
|
|
{
|
|
void string::strncpy()
|
|
{
|
|
stdex::utf32_t tmp[0x100];
|
|
stdex::strncpy(tmp, u"This is a 🐔Test🐮.");
|
|
Assert::IsTrue(stdex::strcmp(U"This is a 🐔Test🐮.", tmp) == 0);
|
|
}
|
|
|
|
void string::sprintf()
|
|
{
|
|
stdex::locale locale(stdex::create_locale(LC_ALL, "en_US.UTF-8"));
|
|
|
|
Assert::AreEqual(L"This is a test.", stdex::sprintf(L"This is %ls.", locale, L"a test").c_str());
|
|
Assert::AreEqual<size_t>(15, stdex::sprintf(L"This is %ls.", locale, L"a test").size());
|
|
Assert::AreEqual("This is a test.", stdex::sprintf("This is %s.", locale, "a test").c_str());
|
|
Assert::AreEqual<size_t>(15, stdex::sprintf("This is %s.", locale, "a test").size());
|
|
|
|
Assert::AreEqual(L"This is a 🐔Test🐮.", stdex::sprintf(L"This is %ls.", locale, L"a 🐔Test🐮").c_str());
|
|
Assert::AreEqual("This is a 🐔Test🐮.", stdex::sprintf("This is %s.", locale, "a 🐔Test🐮").c_str());
|
|
|
|
wstring wstr;
|
|
std::string str;
|
|
for (size_t i = 0; i < 200; i++) {
|
|
wstr += L"🐔Test🐮\r\n";
|
|
str += "🐔Test🐮\r\n";
|
|
}
|
|
Assert::AreEqual(wstr.c_str(), stdex::sprintf(L"%ls", locale, wstr.data()).c_str());
|
|
Assert::AreEqual(wstr.size(), stdex::sprintf(L"%ls", locale, wstr.data()).size());
|
|
Assert::AreEqual(str.c_str(), stdex::sprintf("%s", locale, str.data()).c_str());
|
|
Assert::AreEqual(str.size(), stdex::sprintf("%s", locale, str.data()).size());
|
|
}
|
|
|
|
void string::snprintf()
|
|
{
|
|
stdex::locale locale(stdex::create_locale(LC_ALL, "en_US.UTF-8"));
|
|
|
|
{
|
|
wchar_t buf[0x100];
|
|
Assert::IsTrue(stdex::snprintf(buf, _countof(buf), L"This is %ls.", locale, L"a test") == 15);
|
|
Assert::AreEqual(L"This is a test.", buf);
|
|
}
|
|
{
|
|
char buf[0x100];
|
|
Assert::IsTrue(stdex::snprintf(buf, _countof(buf), "This is %s.", locale, "a test") == 15);
|
|
Assert::AreEqual("This is a test.", buf);
|
|
}
|
|
|
|
{
|
|
wchar_t buf[8];
|
|
Assert::IsTrue(stdex::snprintf(buf, _countof(buf), L"This is %ls.", locale, L"a test") == 8);
|
|
Assert::IsTrue(stdex::strncmp(L"This is a test.", buf, _countof(buf)) == 0);
|
|
}
|
|
{
|
|
char buf[8];
|
|
Assert::IsTrue(stdex::snprintf(buf, _countof(buf), "This is %s.", locale, "a test") == 8);
|
|
Assert::IsTrue(stdex::strncmp("This is a test.", buf, _countof(buf)) == 0);
|
|
}
|
|
}
|
|
}
|