summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/tests/libntp/caljulian.c
blob: b25f8acac8e7f0e84ed93512ac9a1418f2fe4038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "config.h"

#include "ntp_calendar.h"
#include "ntp_stdlib.h"

#include "unity.h"
#include "test-libntp.h"

#include <string.h>


char * CalendarToString(const struct calendar cal);
int IsEqual(const struct calendar expected, const struct calendar actual);
void setUp(void);
void tearDown(void);
void test_RegularTime(void);
void test_LeapYear(void);
void test_uLongBoundary(void);
void test_uLongWrapped(void);


char *
CalendarToString(const struct calendar cal)
{
	char * str = emalloc (sizeof (char) * 100);
	char buffer[100] ="";

	*str = '\0';
	snprintf(buffer, 100, "%u", cal.year);
	strcat(str, buffer);
	strcat(str, "-");
	snprintf(buffer, 100, "%u", (u_int)cal.month);
	strcat(str, buffer);
	strcat(str, "-");
	snprintf(buffer, 100, "%u", (u_int)cal.monthday);
	strcat(str, buffer);
	strcat(str, " (");
	snprintf(buffer, 100, "%u", (u_int) cal.yearday);
	strcat(str, buffer);
	strcat(str, ") ");
	snprintf(buffer, 100, "%u", (u_int)cal.hour);
	strcat(str, buffer);
	strcat(str, ":");
	snprintf(buffer, 100, "%u", (u_int)cal.minute);
	strcat(str, buffer);
	strcat(str, ":");
	snprintf(buffer, 100, "%u", (u_int)cal.second);
	strcat(str, buffer);
	return str;
}

int // technically boolean
IsEqual(const struct calendar expected, const struct calendar actual)
{
	if (   expected.year == actual.year
	    && (   expected.yearday == actual.yearday
		|| (   expected.month == actual.month
		    && expected.monthday == actual.monthday))
	    && expected.hour == actual.hour
	    && expected.minute == actual.minute
	    && expected.second == actual.second) {
		return TRUE;
	} else {
		char *p_exp, *p_act;

		p_exp = CalendarToString(expected);
		p_act = CalendarToString(actual);
		printf("expected: %s but was %s", p_exp, p_act);
		free(p_exp);
		free(p_act);
		return FALSE;
	}
}


void
setUp()
{
    ntpcal_set_timefunc(timefunc);
    settime(1970, 1, 1, 0, 0, 0);
    init_lib();

    return;
}

void
tearDown()
{
    ntpcal_set_timefunc(NULL);

    return;
}


void
test_RegularTime(void)
{
	u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
	struct calendar expected = {2010,160,6,9,14,0,0};

	struct calendar actual;

	caljulian(testDate, &actual);

	TEST_ASSERT_TRUE(IsEqual(expected, actual));

	return;
}

void
test_LeapYear(void)
{
	u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
	struct calendar expected = {2012, 179, 6, 28, 20, 0, 0};

	struct calendar actual;

	caljulian(input, &actual);

	TEST_ASSERT_TRUE(IsEqual(expected, actual));

	return;
}

void
test_uLongBoundary(void)
{
	u_long enc_time = 4294967295UL; // 2036-02-07 6:28:15
	struct calendar expected = {2036,0,2,7,6,28,15};

	struct calendar actual;

	caljulian(enc_time, &actual);

	TEST_ASSERT_TRUE(IsEqual(expected, actual));

	return;
}

void
test_uLongWrapped(void)
{
	u_long enc_time = 0;
	struct calendar expected = {2036,0,2,7,6,28,16};

	struct calendar actual;

	caljulian(enc_time, &actual);

	TEST_ASSERT_TRUE(IsEqual(expected, actual));

	return;
}
OpenPOWER on IntegriCloud