1*cdfa2a7eSchristos /* $NetBSD: caltontp.c,v 1.2 2020/05/25 20:47:36 christos Exp $ */
2067f5680Schristos
3f17b710fSchristos #include "config.h"
4f17b710fSchristos #include "ntp_calendar.h"
5a6f3f22fSchristos #include "unity.h"
6a6f3f22fSchristos
7a6f3f22fSchristos void test_DateGivenMonthDay(void);
8a6f3f22fSchristos void test_DateGivenYearDay(void);
9a6f3f22fSchristos void test_DateLeapYear(void);
10a6f3f22fSchristos void test_WraparoundDateIn2036(void);
11f17b710fSchristos
12f17b710fSchristos void
test_DateGivenMonthDay(void)13f17b710fSchristos test_DateGivenMonthDay(void) {
14f17b710fSchristos // 2010-06-24 12:50:00
15f17b710fSchristos struct calendar input = {2010, 0, 6, 24, 12, 50, 0};
16f17b710fSchristos
17f17b710fSchristos u_long expected = 3486372600UL; // This is the timestamp above.
18f17b710fSchristos
19f17b710fSchristos TEST_ASSERT_EQUAL_UINT(expected, caltontp(&input));
20f17b710fSchristos }
21f17b710fSchristos
22f17b710fSchristos void
test_DateGivenYearDay(void)23f17b710fSchristos test_DateGivenYearDay(void) {
24f17b710fSchristos // 2010-06-24 12:50:00
25f17b710fSchristos // This is the 175th day of 2010.
26f17b710fSchristos struct calendar input = {2010, 175, 0, 0, 12, 50, 0};
27f17b710fSchristos
28f17b710fSchristos u_long expected = 3486372600UL; // This is the timestamp above.
29f17b710fSchristos
30f17b710fSchristos TEST_ASSERT_EQUAL_UINT(expected, caltontp(&input));
31f17b710fSchristos }
32f17b710fSchristos
33f17b710fSchristos void
test_DateLeapYear(void)34f17b710fSchristos test_DateLeapYear(void) {
35f17b710fSchristos // 2012-06-24 12:00:00
36f17b710fSchristos // This is the 176th day of 2012 (since 2012 is a leap year).
37f17b710fSchristos struct calendar inputYd = {2012, 176, 0, 0, 12, 00, 00};
38f17b710fSchristos struct calendar inputMd = {2012, 0, 6, 24, 12, 00, 00};
39f17b710fSchristos
40f17b710fSchristos u_long expected = 3549528000UL;
41f17b710fSchristos
42f17b710fSchristos TEST_ASSERT_EQUAL_UINT(expected, caltontp(&inputYd));
43f17b710fSchristos TEST_ASSERT_EQUAL_UINT(expected, caltontp(&inputMd));
44f17b710fSchristos }
45f17b710fSchristos
46f17b710fSchristos void
test_WraparoundDateIn2036(void)47f17b710fSchristos test_WraparoundDateIn2036(void) {
48f17b710fSchristos // 2036-02-07 06:28:16
49f17b710fSchristos // This is (one) wrapping boundary where we go from ULONG_MAX to 0.
50f17b710fSchristos struct calendar input = {2036, 0, 2, 7, 6, 28, 16};
51f17b710fSchristos
52f17b710fSchristos u_long expected = 0UL;
53f17b710fSchristos
54f17b710fSchristos TEST_ASSERT_EQUAL_UINT(expected, caltontp(&input));
55f17b710fSchristos }
56