From dd6cc0e6ac05e9bbec4c736af6a73bbc64fa1c2f Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 14 Mar 2019 18:10:56 -0700 Subject: [PATCH] Merge time-test into chrono-test --- test/CMakeLists.txt | 1 - test/chrono-test.cc | 51 +++++++++++++++++++++++++++++++++++++ test/time-test.cc | 61 --------------------------------------------- 3 files changed, 51 insertions(+), 62 deletions(-) delete mode 100644 test/time-test.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3300ce0a..e44bdcd4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -99,7 +99,6 @@ add_fmt_test(locale-test) add_fmt_test(ostream-test) add_fmt_test(prepare-test) add_fmt_test(printf-test) -add_fmt_test(time-test) add_fmt_test(custom-formatter-test) add_fmt_test(ranges-test) diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 4ef545af..40d34235 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -5,6 +5,10 @@ // // For the license information refer to format.h. +#ifdef WIN32 +# define _CRT_SECURE_NO_WARNINGS +#endif + #include "fmt/chrono.h" #include "gtest-extra.h" @@ -43,6 +47,53 @@ std::string format_tm(const std::tm& time, const char* spec, return os.str(); } +TEST(TimeTest, Format) { + std::tm tm = std::tm(); + tm.tm_year = 116; + tm.tm_mon = 3; + tm.tm_mday = 25; + EXPECT_EQ("The date is 2016-04-25.", + fmt::format("The date is {:%Y-%m-%d}.", tm)); +} + +TEST(TimeTest, GrowBuffer) { + std::string s = "{:"; + for (int i = 0; i < 30; ++i) s += "%c"; + s += "}\n"; + std::time_t t = std::time(FMT_NULL); + fmt::format(s, *std::localtime(&t)); +} + +TEST(TimeTest, FormatToEmptyContainer) { + std::string s; + auto time = std::tm(); + time.tm_sec = 42; + fmt::format_to(std::back_inserter(s), "{:%S}", time); + EXPECT_EQ(s, "42"); +} + +TEST(TimeTest, EmptyResult) { EXPECT_EQ("", fmt::format("{}", std::tm())); } + +static bool EqualTime(const std::tm& lhs, const std::tm& rhs) { + return lhs.tm_sec == rhs.tm_sec && lhs.tm_min == rhs.tm_min && + lhs.tm_hour == rhs.tm_hour && lhs.tm_mday == rhs.tm_mday && + lhs.tm_mon == rhs.tm_mon && lhs.tm_year == rhs.tm_year && + lhs.tm_wday == rhs.tm_wday && lhs.tm_yday == rhs.tm_yday && + lhs.tm_isdst == rhs.tm_isdst; +} + +TEST(TimeTest, LocalTime) { + std::time_t t = std::time(FMT_NULL); + std::tm tm = *std::localtime(&t); + EXPECT_TRUE(EqualTime(tm, fmt::localtime(t))); +} + +TEST(TimeTest, GMTime) { + std::time_t t = std::time(FMT_NULL); + std::tm tm = *std::gmtime(&t); + EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t))); +} + #define EXPECT_TIME(spec, time, duration) \ { \ std::locale loc("ja_JP.utf8"); \ diff --git a/test/time-test.cc b/test/time-test.cc deleted file mode 100644 index d2ee29ec..00000000 --- a/test/time-test.cc +++ /dev/null @@ -1,61 +0,0 @@ -// Formatting library for C++ - time formatting tests -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifdef WIN32 -# define _CRT_SECURE_NO_WARNINGS -#endif - -#include "fmt/chrono.h" -#include "fmt/locale.h" -#include "gmock.h" - -TEST(TimeTest, Format) { - std::tm tm = std::tm(); - tm.tm_year = 116; - tm.tm_mon = 3; - tm.tm_mday = 25; - EXPECT_EQ("The date is 2016-04-25.", - fmt::format("The date is {:%Y-%m-%d}.", tm)); -} - -TEST(TimeTest, GrowBuffer) { - std::string s = "{:"; - for (int i = 0; i < 30; ++i) s += "%c"; - s += "}\n"; - std::time_t t = std::time(FMT_NULL); - fmt::format(s, *std::localtime(&t)); -} - -TEST(TimeTest, FormatToEmptyContainer) { - std::string s; - auto time = std::tm(); - time.tm_sec = 42; - fmt::format_to(std::back_inserter(s), "{:%S}", time); - EXPECT_EQ(s, "42"); -} - -TEST(TimeTest, EmptyResult) { EXPECT_EQ("", fmt::format("{}", std::tm())); } - -static bool EqualTime(const std::tm& lhs, const std::tm& rhs) { - return lhs.tm_sec == rhs.tm_sec && lhs.tm_min == rhs.tm_min && - lhs.tm_hour == rhs.tm_hour && lhs.tm_mday == rhs.tm_mday && - lhs.tm_mon == rhs.tm_mon && lhs.tm_year == rhs.tm_year && - lhs.tm_wday == rhs.tm_wday && lhs.tm_yday == rhs.tm_yday && - lhs.tm_isdst == rhs.tm_isdst; -} - -TEST(TimeTest, LocalTime) { - std::time_t t = std::time(FMT_NULL); - std::tm tm = *std::localtime(&t); - EXPECT_TRUE(EqualTime(tm, fmt::localtime(t))); -} - -TEST(TimeTest, GMTime) { - std::time_t t = std::time(FMT_NULL); - std::tm tm = *std::gmtime(&t); - EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t))); -}