string: add snprintf

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
2025-01-13 09:54:50 +01:00
parent db413bb5ce
commit a7543cf9ab
6 changed files with 73 additions and 5 deletions

View File

@@ -28,6 +28,7 @@ int main(int, const char *[])
UnitTests::stream::replicator();
UnitTests::string::strncpy();
UnitTests::string::sprintf();
UnitTests::string::snprintf();
UnitTests::unicode::charset_encoder();
UnitTests::unicode::normalize();
UnitTests::unicode::str2wstr();

View File

@@ -109,6 +109,7 @@ namespace UnitTests
public:
TEST_METHOD(strncpy);
TEST_METHOD(sprintf);
TEST_METHOD(snprintf);
};
TEST_CLASS(unicode)

View File

@@ -42,4 +42,31 @@ namespace UnitTests
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);
}
}
}