1 | /* |
---|
2 | * Copyright (c) 2011, 2016 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <tmacros.h> |
---|
20 | |
---|
21 | #include <time.h> |
---|
22 | #include <string.h> |
---|
23 | |
---|
24 | #include <rtems.h> |
---|
25 | |
---|
26 | const char rtems_test_name[] = "SP 2038"; |
---|
27 | |
---|
28 | /* forward declarations to avoid warnings */ |
---|
29 | rtems_task Init(rtems_task_argument argument); |
---|
30 | |
---|
31 | #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL) |
---|
32 | |
---|
33 | static const uint32_t sample_seconds [] = { |
---|
34 | 571213695UL, |
---|
35 | 602836095UL, |
---|
36 | 634372095UL, |
---|
37 | 665908095UL, |
---|
38 | 697444095UL, |
---|
39 | 729066495UL, |
---|
40 | 760602495UL, |
---|
41 | 792138495UL, |
---|
42 | 823674495UL, |
---|
43 | 855296895UL, |
---|
44 | 886832895UL, |
---|
45 | 918368895UL, |
---|
46 | 949904895UL, |
---|
47 | 981527295UL, |
---|
48 | 1013063295UL, |
---|
49 | 1044599295UL, |
---|
50 | 1076135295UL, |
---|
51 | 1107757695UL, |
---|
52 | 1139293695UL, |
---|
53 | 1170829695UL, |
---|
54 | 1202365695UL, |
---|
55 | 1233988095UL, |
---|
56 | 1265524095UL, |
---|
57 | 1297060095UL, |
---|
58 | 1328596095UL, |
---|
59 | 1360218495UL, |
---|
60 | 1391754495UL, |
---|
61 | 1423290495UL, |
---|
62 | 1454826495UL, |
---|
63 | 1486448895UL, |
---|
64 | 1517984895UL, |
---|
65 | 1549520895UL, |
---|
66 | 1581056895UL, |
---|
67 | 1612679295UL, |
---|
68 | 1644215295UL, |
---|
69 | 1675751295UL, |
---|
70 | 1707287295UL, |
---|
71 | 1738909695UL, |
---|
72 | 1770445695UL, |
---|
73 | 1801981695UL, |
---|
74 | 1833517695UL, |
---|
75 | 1865140095UL, |
---|
76 | 1896676095UL, |
---|
77 | 1928212095UL, |
---|
78 | 1959748095UL, |
---|
79 | 1991370495UL, |
---|
80 | 2022906495UL, |
---|
81 | 2054442495UL, |
---|
82 | 2085978495UL, |
---|
83 | 2117600895UL, |
---|
84 | 2149136895UL, |
---|
85 | 2180672895UL, |
---|
86 | 2212208895UL, |
---|
87 | 2243831295UL, |
---|
88 | 2275367295UL, |
---|
89 | 2306903295UL, |
---|
90 | 2338439295UL, |
---|
91 | 2370061695UL, |
---|
92 | 2401597695UL, |
---|
93 | 2433133695UL, |
---|
94 | 2464669695UL, |
---|
95 | 2496292095UL, |
---|
96 | 2527828095UL, |
---|
97 | 2559364095UL, |
---|
98 | 2590900095UL, |
---|
99 | 2622522495UL, |
---|
100 | 2654058495UL, |
---|
101 | 2685594495UL, |
---|
102 | 2717130495UL, |
---|
103 | 2748752895UL, |
---|
104 | 2780288895UL, |
---|
105 | 2811824895UL, |
---|
106 | 2843360895UL, |
---|
107 | 2874983295UL, |
---|
108 | 2906519295UL, |
---|
109 | 2938055295UL, |
---|
110 | 2969591295UL, |
---|
111 | 3001213695UL, |
---|
112 | 3032749695UL, |
---|
113 | 3064285695UL, |
---|
114 | 3095821695UL, |
---|
115 | 3127444095UL, |
---|
116 | 3158980095UL, |
---|
117 | 3190516095UL, |
---|
118 | 3222052095UL, |
---|
119 | 3253674495UL, |
---|
120 | 3285210495UL, |
---|
121 | 3316746495UL, |
---|
122 | 3348282495UL, |
---|
123 | 3379904895UL, |
---|
124 | 3411440895UL, |
---|
125 | 3442976895UL, |
---|
126 | 3474512895UL, |
---|
127 | 3506135295UL, |
---|
128 | 3537671295UL, |
---|
129 | 3569207295UL, |
---|
130 | 3600743295UL, |
---|
131 | 3632365695UL, |
---|
132 | 3663901695UL, |
---|
133 | 3695437695UL, |
---|
134 | 3726973695UL, |
---|
135 | 3758596095UL, |
---|
136 | 3790132095UL, |
---|
137 | 3821668095UL, |
---|
138 | 3853204095UL, |
---|
139 | 3884826495UL, |
---|
140 | 3916362495UL, |
---|
141 | 3947898495UL, |
---|
142 | 3979434495UL, |
---|
143 | 4011056895UL, |
---|
144 | 4042592895UL, |
---|
145 | 4074128895UL, |
---|
146 | 4105664895UL, |
---|
147 | 4137200895UL, |
---|
148 | 4168736895UL, |
---|
149 | 4200272895UL, |
---|
150 | 4231808895UL, |
---|
151 | 4263431295UL, |
---|
152 | 4294967295UL |
---|
153 | }; |
---|
154 | |
---|
155 | static const rtems_time_of_day nearly_problem_2038 = { |
---|
156 | .year = 2038, |
---|
157 | .month = 1, |
---|
158 | .day = 19, |
---|
159 | .hour = 3, |
---|
160 | .minute = 14, |
---|
161 | .second = 7 |
---|
162 | }; |
---|
163 | |
---|
164 | static const rtems_time_of_day problem_2038 = { |
---|
165 | .year = 2038, |
---|
166 | .month = 1, |
---|
167 | .day = 19, |
---|
168 | .hour = 3, |
---|
169 | .minute = 14, |
---|
170 | .second = 8 |
---|
171 | }; |
---|
172 | |
---|
173 | static const rtems_time_of_day nearly_problem_2106 = { |
---|
174 | .year = 2106, |
---|
175 | .month = 2, |
---|
176 | .day = 7, |
---|
177 | .hour = 6, |
---|
178 | .minute = 28, |
---|
179 | .second = 15 |
---|
180 | }; |
---|
181 | |
---|
182 | static const rtems_time_of_day problem_2106 = { |
---|
183 | .year = 2106, |
---|
184 | .month = 2, |
---|
185 | .day = 7, |
---|
186 | .hour = 6, |
---|
187 | .minute = 28, |
---|
188 | .second = 16 |
---|
189 | }; |
---|
190 | |
---|
191 | static const rtems_time_of_day problem_2100 = { |
---|
192 | .year = 2100, |
---|
193 | .month = 2, |
---|
194 | .day = 28, |
---|
195 | .hour = 0, |
---|
196 | .minute = 0, |
---|
197 | .second = 0 |
---|
198 | }; |
---|
199 | |
---|
200 | static const rtems_time_of_day problem_2100_2 = { |
---|
201 | .year = 2100, |
---|
202 | .month = 2, |
---|
203 | .day = 29, |
---|
204 | .hour = 0, |
---|
205 | .minute = 0, |
---|
206 | .second = 0 |
---|
207 | }; |
---|
208 | |
---|
209 | static void test_tod_to_seconds(void) |
---|
210 | { |
---|
211 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
212 | size_t i = 0; |
---|
213 | size_t n = sizeof(sample_seconds) / sizeof(sample_seconds [0]); |
---|
214 | |
---|
215 | for (i = 0; i < n; ++i) { |
---|
216 | rtems_time_of_day tod = nearly_problem_2106; |
---|
217 | uint32_t seconds = 0; |
---|
218 | rtems_interval seconds_as_interval = 0; |
---|
219 | |
---|
220 | tod.year = 1988 + i; |
---|
221 | seconds = _TOD_To_seconds(&tod); |
---|
222 | rtems_test_assert(seconds == sample_seconds [i]); |
---|
223 | |
---|
224 | sc = rtems_clock_set(&tod); |
---|
225 | ASSERT_SC(sc); |
---|
226 | |
---|
227 | sc = rtems_clock_get_seconds_since_epoch(&seconds_as_interval); |
---|
228 | ASSERT_SC(sc); |
---|
229 | rtems_test_assert(seconds_as_interval == sample_seconds [i]); |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | static void test_problem_year(void) |
---|
234 | { |
---|
235 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
236 | time_t zero = 0; |
---|
237 | time_t one = 1; |
---|
238 | time_t maybe_negative = zero - one; |
---|
239 | bool time_t_is_32_bit = sizeof(time_t) == 4; |
---|
240 | bool time_t_is_signed = maybe_negative < zero; |
---|
241 | |
---|
242 | if (time_t_is_32_bit) { |
---|
243 | const rtems_time_of_day *nearly_problem = NULL; |
---|
244 | const rtems_time_of_day *problem = NULL; |
---|
245 | rtems_time_of_day now; |
---|
246 | |
---|
247 | if (time_t_is_signed) { |
---|
248 | nearly_problem = &nearly_problem_2038; |
---|
249 | problem = &problem_2038; |
---|
250 | } else { |
---|
251 | nearly_problem = &nearly_problem_2106; |
---|
252 | problem = &problem_2106; |
---|
253 | } |
---|
254 | |
---|
255 | sc = rtems_clock_set(nearly_problem); |
---|
256 | ASSERT_SC(sc); |
---|
257 | sc = rtems_clock_get_tod(&now); |
---|
258 | ASSERT_SC(sc); |
---|
259 | rtems_test_assert(memcmp(&now, nearly_problem, sizeof(now)) == 0); |
---|
260 | |
---|
261 | sc = rtems_clock_set(problem); |
---|
262 | ASSERT_SC(sc); |
---|
263 | sc = rtems_clock_get_tod(&now); |
---|
264 | ASSERT_SC(sc); |
---|
265 | rtems_test_assert(memcmp(&now, problem, sizeof(now)) == 0); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | static void test_leap_year(void) |
---|
270 | { |
---|
271 | bool test_status; |
---|
272 | const rtems_time_of_day *problem = &problem_2100; |
---|
273 | const rtems_time_of_day *problem2 = &problem_2100_2; |
---|
274 | // 2100 is not a leap year, so it should have 28 days |
---|
275 | test_status = _TOD_Validate(problem); |
---|
276 | rtems_test_assert(test_status == true); |
---|
277 | test_status = _TOD_Validate(problem2); |
---|
278 | rtems_test_assert(test_status == false); |
---|
279 | } |
---|
280 | |
---|
281 | static bool test_year_is_leap_year(uint32_t year) |
---|
282 | { |
---|
283 | return (((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0); |
---|
284 | } |
---|
285 | |
---|
286 | static void test_every_day(void) |
---|
287 | { |
---|
288 | rtems_time_of_day every_day = { |
---|
289 | .year = 1970, |
---|
290 | .month = 1, |
---|
291 | .day = 1, |
---|
292 | .hour = 0, |
---|
293 | .minute = 1, |
---|
294 | .second = 2 |
---|
295 | }; |
---|
296 | const int days_per_month[2][12] = { |
---|
297 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
---|
298 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
---|
299 | }; |
---|
300 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
301 | rtems_time_of_day now; |
---|
302 | |
---|
303 | for (every_day.year = 1988; every_day.year <= 2100; ++every_day.year) { |
---|
304 | int leap_year = test_year_is_leap_year(every_day.year) ? 1 : 0; |
---|
305 | for (every_day.month = 1; every_day.month <= 12; ++every_day.month) { |
---|
306 | int days = days_per_month[leap_year][every_day.month - 1]; |
---|
307 | for (every_day.day = 1; every_day.day <= days; ++every_day.day) { |
---|
308 | sc = rtems_clock_set(&every_day); |
---|
309 | ASSERT_SC(sc); |
---|
310 | sc = rtems_clock_get_tod(&now); |
---|
311 | ASSERT_SC(sc); |
---|
312 | rtems_test_assert(memcmp(&now, &every_day, sizeof(now)) == 0); |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | rtems_task Init(rtems_task_argument argument) |
---|
319 | { |
---|
320 | TEST_BEGIN(); |
---|
321 | |
---|
322 | test_tod_to_seconds(); |
---|
323 | test_every_day(); |
---|
324 | test_problem_year(); |
---|
325 | test_leap_year(); |
---|
326 | |
---|
327 | TEST_END(); |
---|
328 | |
---|
329 | rtems_test_exit(0); |
---|
330 | } |
---|
331 | |
---|
332 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
333 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
334 | |
---|
335 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
336 | |
---|
337 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
338 | |
---|
339 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
340 | |
---|
341 | #define CONFIGURE_INIT |
---|
342 | |
---|
343 | #include <rtems/confdefs.h> |
---|