From d3d30a46f0017078b69ea97e24089974ef9f2ae5 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Sat, 9 Oct 2021 23:17:51 +0500 Subject: [PATCH] New tests --- test/chrono-test.cc | 7 +++++++ test/xchar-test.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 8cf95a1f..a33dee47 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -51,6 +51,7 @@ TEST(chrono_test, format_tm) { EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm), "The date is 2016-04-25 11:22:33."); EXPECT_EQ(fmt::format("{:%Y}", tm), "2016"); + EXPECT_EQ(fmt::format("{:%C}", tm), "20"); EXPECT_EQ(fmt::format("{:%e}", tm), "25"); EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/16"); EXPECT_EQ(fmt::format("{:%F}", tm), "2016-04-25"); @@ -72,6 +73,7 @@ TEST(chrono_test, format_tm_future) { EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm), "The date is 12345-04-25 11:22:33."); EXPECT_EQ(fmt::format("{:%Y}", tm), "12345"); + EXPECT_EQ(fmt::format("{:%C}", tm), "123"); EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/45"); EXPECT_EQ(fmt::format("{:%F}", tm), "12345-04-25"); EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33"); @@ -89,6 +91,10 @@ TEST(chrono_test, format_tm_past) { "The date is -101-04-25 11:22:33."); EXPECT_EQ(fmt::format("{:%Y}", tm), "-101"); + // macOS %C - "-1" + // Linux %C - "-2" + // EXPECT_EQ(fmt::format("{:%C}", tm), "-1"); + // macOS %D - "04/25/01" (%y) // Linux %D - "04/25/99" (%y) // EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/01"); @@ -160,6 +166,7 @@ TEST(chrono_test, time_point) { "%w", "%Ow", "%u", "%Ou", "%H", "%OH", "%I", "%OI", "%M", "%OM", "%S", "%OS", "%c", "%Ec", "%x", "%Ex", "%X", "%EX", "%D", "%F", "%r", "%R", "%T", "%p", "%z", "%Z"}; + spec_list.push_back("%Y-%m-%d %H:%M:%S"); for (const auto& spec : spec_list) { auto t = std::chrono::system_clock::to_time_t(t1); auto tm = *std::localtime(&t); diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 751aeba7..e9b4d5e2 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -8,6 +8,8 @@ #include "fmt/xchar.h" #include +#include +#include #include "fmt/chrono.h" #include "fmt/color.h" @@ -265,6 +267,30 @@ TEST(xchar_test, chrono) { EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33"); } +TEST(chrono_test, time_point) { + auto t1 = std::chrono::system_clock::now(); + + std::vector spec_list = { + L"%%", L"%n", L"%t", L"%Y", L"%EY", L"%y", L"%Oy", L"%Ey", + L"%C", L"%EC", L"%G", L"%g", L"%b", L"%h", L"%B", L"%m", + L"%Om", L"%U", L"%OU", L"%W", L"%OW", L"%V", L"%OV", L"%j", + L"%d", L"%Od", L"%e", L"%Oe", L"%a", L"%A", L"%w", L"%Ow", + L"%u", L"%Ou", L"%H", L"%OH", L"%I", L"%OI", L"%M", L"%OM", + L"%S", L"%OS", L"%c", L"%Ec", L"%x", L"%Ex", L"%X", L"%EX", + L"%D", L"%F", L"%r", L"%R", L"%T", L"%p", L"%z", L"%Z"}; + spec_list.push_back(L"%Y-%m-%d %H:%M:%S"); + 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 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)); + } +} + TEST(xchar_test, color) { EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), L"rgb(255,20,30) wide"), L"\x1b[38;2;255;020;030mrgb(255,20,30) wide\x1b[0m");