source: rtems/testsuites/sptests/sp2038/init.c @ 262e250

4.115
Last change on this file since 262e250 was 262e250, checked in by Chirayu Desai <cdesai@…>, on 12/05/13 at 16:27:14

sptest: sp2038: Add a test for leap year

Bug test case for PR 1422

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2011 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
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.com/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/* forward declarations to avoid warnings */
27rtems_task Init(rtems_task_argument argument);
28
29#define TEST_APPLICABLE \
30  (CPU_TIMESTAMP_USE_INT64 == TRUE || CPU_TIMESTAMP_USE_INT64_INLINE == TRUE)
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#if TEST_APPLICABLE
237  rtems_status_code sc = RTEMS_SUCCESSFUL;
238  time_t zero = 0;
239  time_t one = 1;
240  time_t maybe_negative = zero - one;
241  bool time_t_is_32_bit = sizeof(time_t) == 4;
242  bool time_t_is_signed = maybe_negative < zero;
243
244  if (time_t_is_32_bit) {
245    const rtems_time_of_day *nearly_problem = NULL;
246    const rtems_time_of_day *problem = NULL;
247    rtems_time_of_day now;
248
249    if (time_t_is_signed) {
250      nearly_problem = &nearly_problem_2038;
251      problem = &problem_2038;
252    } else {
253      nearly_problem = &nearly_problem_2106;
254      problem = &problem_2106;
255    }
256
257    sc = rtems_clock_set(nearly_problem);
258    ASSERT_SC(sc);
259    sc = rtems_clock_get_tod(&now);
260    ASSERT_SC(sc);
261    rtems_test_assert(memcmp(&now, nearly_problem, sizeof(now)) == 0);
262
263    sc = rtems_clock_set(problem);
264    ASSERT_SC(sc);
265    sc = rtems_clock_get_tod(&now);
266    ASSERT_SC(sc);
267    rtems_test_assert(memcmp(&now, problem, sizeof(now)) == 0);
268  }
269#endif /* TEST_APPLICABLE */
270}
271
272static void test_leap_year(void)
273{
274    bool test_status;
275    const rtems_time_of_day *problem = &problem_2100;
276    const rtems_time_of_day *problem2 = &problem_2100_2;
277    // 2100 is not a leap year, so it should have 28 days
278    test_status = _TOD_Validate(problem);
279    rtems_test_assert(test_status == true);
280    test_status = _TOD_Validate(problem2);
281    rtems_test_assert(test_status == false);
282}
283
284
285rtems_task Init(rtems_task_argument argument)
286{
287  puts("\n\n*** TEST 2038 ***");
288
289  test_tod_to_seconds();
290  test_problem_year();
291  test_leap_year();
292
293  puts("*** END OF TEST 2038 ***");
294
295  rtems_test_exit(0);
296}
297
298#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
299#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
300
301#define CONFIGURE_MAXIMUM_TASKS 1
302
303#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
304
305#define CONFIGURE_INIT
306
307#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.