string: add unit test for sprintf

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
2024-01-09 19:31:07 +01:00
parent 38c6b40b21
commit c5feba2ed0
4 changed files with 41 additions and 2 deletions

View File

@@ -128,6 +128,7 @@
<ClCompile Include="ring.cpp" />
<ClCompile Include="sgml.cpp" />
<ClCompile Include="stream.cpp" />
<ClCompile Include="string.cpp" />
<ClCompile Include="unicode.cpp" />
<ClCompile Include="watchdog.cpp" />
</ItemGroup>

View File

@@ -45,6 +45,9 @@
<ClCompile Include="pool.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="string.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.hpp">

37
UnitTests/string.cpp Normal file
View File

@@ -0,0 +1,37 @@
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
#include "pch.hpp"
using namespace std;
#ifdef _WIN32
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#endif
namespace UnitTests
{
TEST_CLASS(string)
{
public:
TEST_METHOD(sprintf)
{
Assert::AreEqual(L"This is a test.", stdex::sprintf(L"This is %s.", stdex::locale_default, L"a test").c_str());
Assert::AreEqual<size_t>(15, stdex::sprintf(L"This is %s.", stdex::locale_default, L"a test").size());
Assert::AreEqual("This is a test.", stdex::sprintf("This is %s.", stdex::locale_default, "a test").c_str());
Assert::AreEqual<size_t>(15, stdex::sprintf("This is %s.", stdex::locale_default, "a test").size());
wstring wstr;
std::string str;
for (size_t i = 0; i < 2000; i++) {
wstr += L"🐔Test🐮\r\n";
str += "🐔Test🐮\r\n";
}
Assert::AreEqual(wstr.c_str(), stdex::sprintf(L"%s", stdex::locale_default, wstr.data()).c_str());
Assert::AreEqual(wstr.size(), stdex::sprintf(L"%s", stdex::locale_default, wstr.data()).size());
Assert::AreEqual(str.c_str(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).c_str());
Assert::AreEqual(str.size(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).size());
}
};
}