From 4e1d3fade083c4a1e747913206ea28c61cfd9504 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Thu, 14 Oct 2021 22:06:49 +0500 Subject: [PATCH] Generalization of strftime/wcsftime function calls in tests --- test/chrono-test.cc | 38 +++++++++++++++++++++----------------- test/xchar-test.cc | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/test/chrono-test.cc b/test/chrono-test.cc index a25265b8e4f6..11362ff78e8e 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -42,6 +42,14 @@ auto make_second(int s) -> std::tm { return time; } +std::string system_strftime(const std::string& format, const std::tm* timeptr, + size_t maxsize = 1024) { + std::vector output(maxsize); + auto size = + std::strftime(output.data(), output.size(), format.c_str(), timeptr); + return std::string(output.data(), size); +} + TEST(chrono_test, format_tm) { auto tm = std::tm(); tm.tm_year = 116; @@ -102,10 +110,9 @@ TEST(chrono_test, format_tm) { std::time_t t = std::mktime(&tm); tm = *std::localtime(&t); - char output[256] = {}; - std::strftime(output, sizeof(output), iso_week_spec.c_str(), &tm); auto fmt_spec = std::string("{:").append(iso_week_spec).append("}"); - EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm)); + EXPECT_EQ(system_strftime(iso_week_spec, &tm), + fmt::format(fmt::runtime(fmt_spec), tm)); } // Every day from 1970-01-01 @@ -113,10 +120,9 @@ TEST(chrono_test, format_tm) { for (std::time_t t = 6 * 3600; t < time_now; t += 86400) { tm = *std::localtime(&t); - char output[256] = {}; - std::strftime(output, sizeof(output), iso_week_spec.c_str(), &tm); auto fmt_spec = std::string("{:").append(iso_week_spec).append("}"); - EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm)); + EXPECT_EQ(system_strftime(iso_week_spec, &tm), + fmt::format(fmt::runtime(fmt_spec), tm)); } } @@ -208,22 +214,20 @@ TEST(chrono_test, gmtime) { EXPECT_TRUE(equal(tm, fmt::gmtime(t))); } -template auto strftime(TimePoint tp) -> std::string { +template auto strftime_full(TimePoint tp) -> std::string { auto t = std::chrono::system_clock::to_time_t(tp); auto tm = *std::localtime(&t); - char output[256] = {}; - std::strftime(output, sizeof(output), "%Y-%m-%d %H:%M:%S", &tm); - return output; + return system_strftime("%Y-%m-%d %H:%M:%S", &tm); } TEST(chrono_test, time_point) { auto t1 = std::chrono::system_clock::now(); - EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1)); - EXPECT_EQ(strftime(t1), fmt::format("{}", t1)); + EXPECT_EQ(strftime_full(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1)); + EXPECT_EQ(strftime_full(t1), fmt::format("{}", t1)); using time_point = std::chrono::time_point; auto t2 = time_point(std::chrono::seconds(42)); - EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2)); + EXPECT_EQ(strftime_full(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2)); std::vector spec_list = { "%%", "%n", "%t", "%Y", "%EY", "%y", "%Oy", "%Ey", "%C", "%EC", @@ -236,12 +240,12 @@ TEST(chrono_test, time_point) { for (const auto& spec : spec_list) { auto t = std::chrono::system_clock::to_time_t(t1); auto tm = *std::localtime(&t); - char output[256] = {}; - std::strftime(output, sizeof(output), spec.c_str(), &tm); + + auto sys_output = system_strftime(spec, &tm); auto fmt_spec = std::string("{:").append(spec).append("}"); - EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), t1)); - EXPECT_EQ(output, fmt::format(fmt::runtime(fmt_spec), tm)); + EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1)); + EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm)); } } diff --git a/test/xchar-test.cc b/test/xchar-test.cc index e9b4d5e25d92..1d04eb0ef117 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -267,6 +267,14 @@ TEST(xchar_test, chrono) { EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33"); } +std::wstring system_wcsftime(const std::wstring& format, const std::tm* timeptr, + size_t maxsize = 1024) { + std::vector output(maxsize); + auto size = + std::wcsftime(output.data(), output.size(), format.c_str(), timeptr); + return std::wstring(output.data(), size); +} + TEST(chrono_test, time_point) { auto t1 = std::chrono::system_clock::now(); @@ -282,12 +290,12 @@ TEST(chrono_test, time_point) { for (const auto& spec : spec_list) { auto t = std::chrono::system_clock::to_time_t(t1); auto tm = *std::localtime(&t); - wchar_t output[1024] = {}; - std::wcsftime(output, sizeof(output) / sizeof(wchar_t), spec.c_str(), &tm); + + auto sys_output = system_wcsftime(spec, &tm); auto fmt_spec = std::wstring(L"{:").append(spec).append(L"}"); - EXPECT_EQ(output, fmt::format(fmt_spec, t1)); - EXPECT_EQ(output, fmt::format(fmt_spec, tm)); + EXPECT_EQ(sys_output, fmt::format(fmt_spec, t1)); + EXPECT_EQ(sys_output, fmt::format(fmt_spec, tm)); } }