source: rtems/testsuites/sptests/sp2038/init.c @ 9c20b987

Last change on this file since 9c20b987 was 9c20b987, checked in by Sebastian Huber <sebastian.huber@…>, on 11/26/20 at 18:53:25

rtems: Add <rtems/rtems/clockimpl.h>

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