xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/caljulian.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: caljulian.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/
2 
3 #include "config.h"
4 
5 #include "ntp_calendar.h"
6 #include "ntp_stdlib.h"
7 
8 #include "unity.h"
9 #include "test-libntp.h"
10 
11 #include <string.h>
12 
13 
14 char * CalendarToString(const struct calendar cal);
15 int IsEqual(const struct calendar expected, const struct calendar actual);
16 void setUp(void);
17 void tearDown(void);
18 void test_RegularTime(void);
19 void test_LeapYear(void);
20 void test_uLongBoundary(void);
21 void test_uLongWrapped(void);
22 
23 
24 char *
25 CalendarToString(const struct calendar cal)
26 {
27 	char * str = emalloc (sizeof (char) * 100);
28 	char buffer[100] ="";
29 
30 	*str = '\0';
31 	snprintf(buffer, 100, "%u", cal.year);
32 	strcat(str, buffer);
33 	strcat(str, "-");
34 	snprintf(buffer, 100, "%u", (u_int)cal.month);
35 	strcat(str, buffer);
36 	strcat(str, "-");
37 	snprintf(buffer, 100, "%u", (u_int)cal.monthday);
38 	strcat(str, buffer);
39 	strcat(str, " (");
40 	snprintf(buffer, 100, "%u", (u_int) cal.yearday);
41 	strcat(str, buffer);
42 	strcat(str, ") ");
43 	snprintf(buffer, 100, "%u", (u_int)cal.hour);
44 	strcat(str, buffer);
45 	strcat(str, ":");
46 	snprintf(buffer, 100, "%u", (u_int)cal.minute);
47 	strcat(str, buffer);
48 	strcat(str, ":");
49 	snprintf(buffer, 100, "%u", (u_int)cal.second);
50 	strcat(str, buffer);
51 	return str;
52 }
53 
54 int // technically boolean
55 IsEqual(const struct calendar expected, const struct calendar actual)
56 {
57 	if (   expected.year == actual.year
58 	    && (   expected.yearday == actual.yearday
59 		|| (   expected.month == actual.month
60 		    && expected.monthday == actual.monthday))
61 	    && expected.hour == actual.hour
62 	    && expected.minute == actual.minute
63 	    && expected.second == actual.second) {
64 		return TRUE;
65 	} else {
66 		char *p_exp, *p_act;
67 
68 		p_exp = CalendarToString(expected);
69 		p_act = CalendarToString(actual);
70 		printf("expected: %s but was %s", p_exp, p_act);
71 		free(p_exp);
72 		free(p_act);
73 		return FALSE;
74 	}
75 }
76 
77 
78 void
79 setUp()
80 {
81     ntpcal_set_timefunc(timefunc);
82     settime(1970, 1, 1, 0, 0, 0);
83     init_lib();
84 
85     return;
86 }
87 
88 void
89 tearDown()
90 {
91     ntpcal_set_timefunc(NULL);
92 
93     return;
94 }
95 
96 
97 void
98 test_RegularTime(void)
99 {
100 	u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
101 	struct calendar expected = {2010,160,6,9,14,0,0};
102 
103 	struct calendar actual;
104 
105 	caljulian(testDate, &actual);
106 
107 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
108 
109 	return;
110 }
111 
112 void
113 test_LeapYear(void)
114 {
115 	u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
116 	struct calendar expected = {2012, 179, 6, 28, 20, 0, 0};
117 
118 	struct calendar actual;
119 
120 	caljulian(input, &actual);
121 
122 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
123 
124 	return;
125 }
126 
127 void
128 test_uLongBoundary(void)
129 {
130 	u_long enc_time = 4294967295UL; // 2036-02-07 6:28:15
131 	struct calendar expected = {2036,0,2,7,6,28,15};
132 
133 	struct calendar actual;
134 
135 	caljulian(enc_time, &actual);
136 
137 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
138 
139 	return;
140 }
141 
142 void
143 test_uLongWrapped(void)
144 {
145 	u_long enc_time = 0;
146 	struct calendar expected = {2036,0,2,7,6,28,16};
147 
148 	struct calendar actual;
149 
150 	caljulian(enc_time, &actual);
151 
152 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
153 
154 	return;
155 }
156