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