]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/caljulian.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / caljulian.c
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         char * str = emalloc (sizeof (char) * 100);
25         
26         char buffer[100] ="";
27         snprintf(buffer, 100, "%u", cal.year);
28         strcat(str, buffer);
29         strcat(str, "-");
30         snprintf(buffer, 100, "%u", (u_int)cal.month);
31         strcat(str, buffer);
32         strcat(str, "-");
33         snprintf(buffer, 100, "%u", (u_int)cal.monthday);
34         strcat(str, buffer);
35         strcat(str, " (");
36         snprintf(buffer, 100, "%u", (u_int) cal.yearday);
37         strcat(str, buffer);
38         strcat(str, ") ");
39         snprintf(buffer, 100, "%u", (u_int)cal.hour);
40         strcat(str, buffer);
41         strcat(str, ":");
42         snprintf(buffer, 100, "%u", (u_int)cal.minute);
43         strcat(str, buffer);
44         strcat(str, ":");
45         snprintf(buffer, 100, "%u", (u_int)cal.second);
46         strcat(str, buffer);
47         return str;
48 }
49
50 int // technically boolean
51 IsEqual(const struct calendar expected, const struct calendar actual) {
52         if (expected.year == actual.year &&
53                 (expected.yearday == actual.yearday ||
54                  (expected.month == actual.month &&
55                   expected.monthday == actual.monthday)) &&
56                 expected.hour == actual.hour &&
57                 expected.minute == actual.minute &&
58                 expected.second == actual.second) {
59                 return TRUE;
60         } else {
61                 printf("expected: %s but was %s", CalendarToString(expected) ,CalendarToString(actual));
62                 return FALSE;
63                         
64         }
65 }
66
67
68 void
69 setUp()
70 {
71     ntpcal_set_timefunc(timefunc);
72     settime(1970, 1, 1, 0, 0, 0);
73 }
74
75 void
76 tearDown()
77 {
78     ntpcal_set_timefunc(NULL);
79 }
80
81
82 void
83 test_RegularTime(void) {
84         u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
85         struct calendar expected = {2010,160,6,9,14,0,0};
86
87         struct calendar actual;
88
89         caljulian(testDate, &actual);
90
91         TEST_ASSERT_TRUE(IsEqual(expected, actual));
92 }
93
94 void
95 test_LeapYear(void) {
96         u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
97         struct calendar expected = {2012, 179, 6, 28, 20, 0, 0};
98
99         struct calendar actual;
100
101         caljulian(input, &actual);
102
103         TEST_ASSERT_TRUE(IsEqual(expected, actual));
104 }
105
106 void
107 test_uLongBoundary(void) {
108         u_long time = 4294967295UL; // 2036-02-07 6:28:15
109         struct calendar expected = {2036,0,2,7,6,28,15};
110
111         struct calendar actual;
112
113         caljulian(time, &actual);
114
115         TEST_ASSERT_TRUE(IsEqual(expected, actual));
116 }
117
118 void
119 test_uLongWrapped(void) {
120         u_long time = 0;
121         struct calendar expected = {2036,0,2,7,6,28,16};
122
123         struct calendar actual;
124
125         caljulian(time, &actual);
126
127         TEST_ASSERT_TRUE(IsEqual(expected, actual));
128 }