Skip to content

Commit 0fec4e1

Browse files
authored
fix(tests): add calendar invariants for *_ms boundaries
1 parent e68787d commit 0fec4e1

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

tests/time_conversions_coverage_test.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ int main() {
102102
assert(sec_of_hour(SEC_PER_HOUR + 15) == 15);
103103

104104
// date_conversions and aliases
105+
{
106+
const ts_ms_t points[] = {
107+
static_cast<ts_ms_t>(-2001), static_cast<ts_ms_t>(-2000), static_cast<ts_ms_t>(-1999),
108+
static_cast<ts_ms_t>(-1001), static_cast<ts_ms_t>(-1000), static_cast<ts_ms_t>(-999),
109+
static_cast<ts_ms_t>(-2), static_cast<ts_ms_t>(-1), static_cast<ts_ms_t>(0),
110+
static_cast<ts_ms_t>(1), static_cast<ts_ms_t>(999), static_cast<ts_ms_t>(1000),
111+
static_cast<ts_ms_t>(1001)
112+
};
113+
114+
for (size_t i = 0; i < sizeof(points)/sizeof(points[0]); ++i) {
115+
const ts_ms_t t = points[i];
116+
117+
// Day boundaries in ms:
118+
const ts_ms_t d0 = start_of_day_ms(t);
119+
const ts_ms_t d1 = end_of_day_ms(t);
120+
assert(d0 <= t && t <= d1);
121+
assert((d0 % MS_PER_SEC) == 0);
122+
assert((d1 % MS_PER_SEC) == (MS_PER_SEC - 1));
123+
124+
// Year boundaries in ms:
125+
const ts_ms_t y0 = start_of_year_ms(t);
126+
const ts_ms_t y1 = end_of_year_ms(t);
127+
assert(y0 <= t && t <= y1);
128+
assert((y0 % MS_PER_SEC) == 0);
129+
assert((y1 % MS_PER_SEC) == (MS_PER_SEC - 1));
130+
}
131+
}
105132
const ts_t sample_ts = to_timestamp(2024, 6, 30, 12, 0, 0);
106133
DateStruct sample_date{2024, 6, 30};
107134
assert(to_date_time(sample_ts).year == 2024);
@@ -131,7 +158,16 @@ int main() {
131158
assert(start_of_year_date_ms(2024) == sec_to_ms(to_timestamp(2024, 1, 1)));
132159
assert(end_of_year(sample_ts) == to_timestamp(2024, 12, 31, 23, 59, 59));
133160
assert(end_of_year_ms(sec_to_ms(sample_ts)) == sec_to_ms(to_timestamp(2024, 12, 31, 23, 59, 59)) + (MS_PER_SEC - 1));
134-
assert(start_of_year_ms(-1) == sec_to_ms(start_of_year(-1)));
161+
{
162+
const ts_ms_t t = -1;
163+
const ts_ms_t y0 = start_of_year_ms(t);
164+
const ts_ms_t y1 = end_of_year_ms(t);
165+
166+
// Calendar invariants for ms boundaries:
167+
assert(y0 <= t && t <= y1);
168+
assert((y0 % MS_PER_SEC) == 0);
169+
assert((y1 % MS_PER_SEC) == (MS_PER_SEC - 1));
170+
}
135171
const ts_t before_epoch = to_timestamp(1969, 12, 31, 23, 59, 59);
136172
assert(start_of_year(before_epoch) == to_timestamp(1969, 1, 1));
137173
assert(end_of_year(before_epoch) == to_timestamp(1969, 12, 31, 23, 59, 59));
@@ -208,6 +244,13 @@ int main() {
208244
assert(end_of_day(day_start) == to_timestamp(2024, 6, 30, 23, 59, 59));
209245
assert(end_of_day_sec(sec_to_ms(day_start)) == to_timestamp(2024, 6, 30, 23, 59, 59));
210246
assert(end_of_day_ms(sec_to_ms(day_start)) == sec_to_ms(to_timestamp(2024, 6, 30, 23, 59, 59)) + 999);
247+
{
248+
const ts_ms_t t = sec_to_ms(day_start);
249+
const ts_ms_t d0 = start_of_day_ms(t);
250+
const ts_ms_t d1 = end_of_day_ms(t);
251+
assert(d0 == t);
252+
assert(d1 == t + MS_PER_DAY - 1);
253+
}
211254

212255
assert(day_of_year(day_start) == 182);
213256
assert(month_of_year<int>(day_start) == 6);

0 commit comments

Comments
 (0)