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