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