3 #include "ntp_calendar.h"
4 #include "ntp_stdlib.h"
7 #include "test-libntp.h"
10 // ---------------------------------------------------------------------
13 // The clocktimeTest uses the NTP calendar feature to use a mockup
14 // function for getting the current system time, so the tests are not
15 // dependent on the actual system time.
20 ntpcal_set_timefunc(timefunc);
21 settime(2000, 1, 1, 0, 0, 0);
26 ntpcal_set_timefunc(NULL);
29 // ---------------------------------------------------------------------
32 void test_CurrentYear() {
33 // Timestamp: 2010-06-24 12:50:00Z
34 const u_int32 timestamp = 3486372600UL;
35 const u_int32 expected = timestamp; // exactly the same.
37 const int yday=175, hour=12, minute=50, second=0, tzoff=0;
42 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
43 &yearstart, &actual));
44 TEST_ASSERT_EQUAL(expected, actual);
47 void test_CurrentYearFuzz() {
49 * Timestamp (rec_ui) is: 2010-06-24 12:50:00
50 * Time sent into function is 12:00:00.
52 * Since the fuzz is rather small, we should get a NTP
53 * timestamp for the 12:00:00 time.
56 const u_int32 timestamp = 3486372600UL; // 2010-06-24 12:50:00Z
57 const u_int32 expected = 3486369600UL; // 2010-06-24 12:00:00Z
59 const int yday=175, hour=12, minute=0, second=0, tzoff=0;
64 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
65 &yearstart, &actual));
66 TEST_ASSERT_EQUAL(expected, actual);
69 void test_TimeZoneOffset() {
71 * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
72 * (which is 2010-06-24 04:00:00Z)
74 * Time sent into function is 04:00:00 +0800
76 const u_int32 timestamp = 3486369600UL;
77 const u_int32 expected = timestamp;
79 const int yday=175, hour=4, minute=0, second=0, tzoff=8;
84 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
85 &yearstart, &actual));
86 TEST_ASSERT_EQUAL(expected, actual);
89 void test_WrongYearStart() {
91 * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z
92 * Time sent into function is 11:00:00.
93 * Yearstart sent into function is the yearstart of 2009!
95 const u_int32 timestamp = 3471418800UL;
96 const u_int32 expected = timestamp;
98 const int yday=2, hour=11, minute=0, second=0, tzoff=0;
100 u_long yearstart = 302024100UL; // Yearstart of 2009.
103 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
104 &yearstart, &actual));
105 TEST_ASSERT_EQUAL(expected, actual);
108 void test_PreviousYear() {
110 * Timestamp is: 2010-01-01 01:00:00Z
111 * Time sent into function is 23:00:00
112 * (which is meant to be 2009-12-31 23:00:00Z)
114 const u_int32 timestamp = 3471296400UL;
115 const u_int32 expected = 3471289200UL;
117 const int yday=365, hour=23, minute=0, second=0, tzoff=0;
119 u_long yearstart = 0;
122 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
123 &yearstart, &actual));
124 TEST_ASSERT_EQUAL(expected, actual);
127 void test_NextYear() {
129 * Timestamp is: 2009-12-31 23:00:00Z
130 * Time sent into function is 01:00:00
131 * (which is meant to be 2010-01-01 01:00:00Z)
133 const u_int32 timestamp = 3471289200UL;
134 const u_int32 expected = 3471296400UL;
136 const int yday=1, hour=1, minute=0, second=0, tzoff=0;
137 u_long yearstart = 0;
140 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
141 &yearstart, &actual));
142 TEST_ASSERT_EQUAL(expected, actual);
145 void test_NoReasonableConversion() {
146 /* Timestamp is: 2010-01-02 11:00:00Z */
147 const u_int32 timestamp = 3471418800UL;
149 const int yday=100, hour=12, minute=0, second=0, tzoff=0;
150 u_long yearstart = 0;
153 TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
154 &yearstart, &actual));
157 // *** FUNCTION isLE, to simulate gtest's ASSERT_LE using Unity's TEST_ASSERT_TRUE
159 int isLE(u_int32 diff,u_int32 actual){
167 void test_AlwaysInLimit() {
168 /* Timestamp is: 2010-01-02 11:00:00Z */
169 const u_int32 timestamp = 3471418800UL;
170 const u_short prime_incs[] = { 127, 151, 163, 179 };
183 for (cyc = 0; cyc < 5; cyc++) {
184 settime(1900 + cyc * 65, 1, 1, 0, 0, 0);
185 for (yday = -26000; yday < 26000; yday += ydayinc) {
186 whichprime = abs(yday) % COUNTOF(prime_incs);
187 ydayinc = prime_incs[whichprime];
188 for (hour = -204; hour < 204; hour += 2) {
189 for (minute = -60; minute < 60; minute++) {
190 clocktime(yday, hour, minute, 30, 0,
191 timestamp, &yearstart, &actual);
192 diff = actual - timestamp;
193 if (diff >= 0x80000000UL)
195 TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY))); // adding new function to return TRUE if first number is less or equal the second
196 //TEST_ASSERT_LE(diff, (183u * SECSPERDAY));