15 #include "absl/time/civil_time.h"
19 #include <type_traits>
21 #include "absl/base/macros.h"
22 #include "gtest/gtest.h"
26 TEST(CivilTime, DefaultConstruction) {
46 TEST(CivilTime, StructMember) {
54 TEST(CivilTime, FieldsConstruction) {
134 TEST(CivilTime, FieldsConstructionLimits) {
138 1970, 1, 1, 0, 0, kIntMax)));
141 1970, 1, 1, 0, kIntMax, kIntMax)));
144 1970, 1, 1, kIntMax, kIntMax, kIntMax)));
147 1970, 1, kIntMax, kIntMax, kIntMax, kIntMax)));
150 1970, kIntMax, kIntMax, kIntMax, kIntMax, kIntMax)));
155 1970, 1, 1, 0, 0, kIntMin)));
158 1970, 1, 1, 0, kIntMin, kIntMin)));
161 1970, 1, 1, kIntMin, kIntMin, kIntMin)));
164 1970, 1, kIntMin, kIntMin, kIntMin, kIntMin)));
167 1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin)));
170 TEST(CivilTime, RangeLimits) {
202 TEST(CivilTime, ImplicitCrossAlignment) {
282 TEST(CivilTime, ExplicitCrossAlignment) {
326 template <
typename T1,
typename T2>
328 template <
typename U1,
typename U2>
330 template <
typename U1,
typename U2>
335 TEST(CivilTime, DisallowCrossAlignedDifference) {
366 TEST(CivilTime, ValueSemantics) {
375 TEST(CivilTime, Relational) {
381 #define TEST_RELATIONAL(OLDER, YOUNGER) \
383 EXPECT_FALSE(OLDER < OLDER); \
384 EXPECT_FALSE(OLDER > OLDER); \
385 EXPECT_TRUE(OLDER >= OLDER); \
386 EXPECT_TRUE(OLDER <= OLDER); \
387 EXPECT_FALSE(YOUNGER < YOUNGER); \
388 EXPECT_FALSE(YOUNGER > YOUNGER); \
389 EXPECT_TRUE(YOUNGER >= YOUNGER); \
390 EXPECT_TRUE(YOUNGER <= YOUNGER); \
391 EXPECT_EQ(OLDER, OLDER); \
392 EXPECT_NE(OLDER, YOUNGER); \
393 EXPECT_LT(OLDER, YOUNGER); \
394 EXPECT_LE(OLDER, YOUNGER); \
395 EXPECT_GT(YOUNGER, OLDER); \
396 EXPECT_GE(YOUNGER, OLDER); \
420 #undef TEST_RELATIONAL
423 TEST(CivilTime, Arithmetic) {
491 TEST(CivilTime, ArithmeticLimits) {
556 TEST(CivilTime, Difference) {
588 TEST(CivilTime, DifferenceLimits) {
611 TEST(CivilTime, Properties) {
756 TEST(CivilTime, FormatAndParseLenient) {
794 TEST(CivilTime, ParseEdgeCases) {
871 TEST(CivilTime, OutputStream) {
874 std::stringstream ss;
875 ss << std::left << std::setfill(
'.');
876 ss << std::setw(3) <<
'X';
878 ss << std::setw(3) <<
'X';
879 EXPECT_EQ(
"X..2016.................X..", ss.str());
882 std::stringstream ss;
883 ss << std::left << std::setfill(
'.');
884 ss << std::setw(3) <<
'X';
886 ss << std::setw(3) <<
'X';
887 EXPECT_EQ(
"X..2016-02..............X..", ss.str());
890 std::stringstream ss;
891 ss << std::left << std::setfill(
'.');
892 ss << std::setw(3) <<
'X';
894 ss << std::setw(3) <<
'X';
895 EXPECT_EQ(
"X..2016-02-03...........X..", ss.str());
898 std::stringstream ss;
899 ss << std::left << std::setfill(
'.');
900 ss << std::setw(3) <<
'X';
902 ss << std::setw(3) <<
'X';
903 EXPECT_EQ(
"X..2016-02-03T04........X..", ss.str());
906 std::stringstream ss;
907 ss << std::left << std::setfill(
'.');
908 ss << std::setw(3) <<
'X';
910 ss << std::setw(3) <<
'X';
911 EXPECT_EQ(
"X..2016-02-03T04:05.....X..", ss.str());
914 std::stringstream ss;
915 ss << std::left << std::setfill(
'.');
916 ss << std::setw(3) <<
'X';
918 ss << std::setw(3) <<
'X';
919 EXPECT_EQ(
"X..2016-02-03T04:05:06..X..", ss.str());
922 std::stringstream ss;
923 ss << std::left << std::setfill(
'.');
924 ss << std::setw(3) <<
'X';
925 ss << std::setw(21) << absl::Weekday::wednesday;
926 ss << std::setw(3) <<
'X';
927 EXPECT_EQ(
"X..Wednesday............X..", ss.str());
940 TEST(CivilTime, NextPrevWeekday) {
981 TEST(CivilTime, DifferenceWithHugeYear) {
1013 TEST(CivilTime, DifferenceNoIntermediateOverflow) {
1018 EXPECT_EQ(-9223372036854775807 - 1, s1 - s2);
1024 EXPECT_EQ(9223372036854775807, s1 - s2);
1027 TEST(CivilTime, NormalizeSimpleOverflow) {
1041 TEST(CivilTime, NormalizeSimpleUnderflow) {
1055 TEST(CivilTime, NormalizeMultipleOverflow) {
1060 TEST(CivilTime, NormalizeMultipleUnderflow) {
1065 TEST(CivilTime, NormalizeOverflowLimits) {
1077 TEST(CivilTime, NormalizeComplexOverflow) {
1091 TEST(CivilTime, NormalizeComplexUnderflow) {
1107 TEST(CivilTime, NormalizeMishmash) {
1125 TEST(CivilTime, NormalizeAllTheDays) {
1127 for (
int day = 1; day <= 146097; ++day) {
1134 TEST(CivilTime, NormalizeWithHugeYear) {
1146 TEST(CivilTime, LeapYears) {
1164 {1900, 365, {3, 1}},
1165 {1999, 365, {3, 1}},
1166 {2000, 366, {2, 29}},
1167 {2001, 365, {3, 1}},
1168 {2002, 365, {3, 1}},
1169 {2003, 365, {3, 1}},
1170 {2004, 366, {2, 29}},
1171 {2005, 365, {3, 1}},
1172 {2006, 365, {3, 1}},
1173 {2007, 365, {3, 1}},
1174 {2008, 366, {2, 29}},
1175 {2009, 365, {3, 1}},
1176 {2100, 365, {3, 1}},
1180 const int y = kLeapYearTable[
i].year;
1181 const int m = kLeapYearTable[
i].leap_day.month;
1182 const int d = kLeapYearTable[
i].leap_day.day;
1183 const int n = kLeapYearTable[
i].days;
1198 TEST(CivilTime, FirstThursdayInMonth) {
1210 TEST(CivilTime, DocumentationExample) {
1223 int diff = day - june_1;