From f03838fac8ae3e256fa9a67dd0291a2dfdbed280 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 22 Jun 2022 16:09:21 +0200 Subject: Tests: Add tests for `wcsftime` --- Tests/LibC/TestWchar.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'Tests') diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp index d8b7c0c236..86a3e61692 100644 --- a/Tests/LibC/TestWchar.cpp +++ b/Tests/LibC/TestWchar.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021-2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,6 +9,7 @@ #include #include #include +#include #include TEST_CASE(wcspbrk) @@ -614,3 +615,46 @@ TEST_CASE(mblen) EXPECT_EQ(ret, -1); EXPECT_EQ(errno, EILSEQ); } + +TEST_CASE(wcsftime) +{ + // FIXME: Test actual wide char inputs once those are implemented. + + auto* buf = static_cast(malloc(32 * sizeof(wchar_t))); + if (!buf) { + FAIL("Could not allocate space for copy target"); + return; + } + + struct tm time = { + .tm_sec = 54, + .tm_min = 44, + .tm_hour = 12, + .tm_mday = 27, + .tm_mon = 4, + .tm_year = 121, + .tm_wday = 4, + .tm_yday = 0, + .tm_isdst = 0, + }; + + size_t ret; + + // Normal behavior. + ret = wcsftime(buf, 32, L"%a, %d %b %Y %H:%M:%S", &time); + EXPECT_EQ(ret, 25ul); + EXPECT_EQ(wcscmp(buf, L"Thu, 27 May 2021 12:44:54"), 0); + + // String fits exactly. + ret = wcsftime(buf, 26, L"%a, %d %b %Y %H:%M:%S", &time); + EXPECT_EQ(ret, 25ul); + EXPECT_EQ(wcscmp(buf, L"Thu, 27 May 2021 12:44:54"), 0); + + // Buffer is too small. + ret = wcsftime(buf, 25, L"%a, %d %b %Y %H:%M:%S", &time); + EXPECT_EQ(ret, 0ul); + ret = wcsftime(buf, 1, L"%a, %d %b %Y %H:%M:%S", &time); + EXPECT_EQ(ret, 0ul); + ret = wcsftime(nullptr, 0, L"%a, %d %b %Y %H:%M:%S", &time); + EXPECT_EQ(ret, 0ul); +} -- cgit v1.2.3