Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:44

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2011, 2016 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include <tmacros.h>
0033 
0034 #include <time.h>
0035 #include <string.h>
0036 
0037 #include <rtems.h>
0038 #include <rtems/rtems/clockimpl.h>
0039 
0040 const char rtems_test_name[] = "SP 2038";
0041 
0042 /* forward declarations to avoid warnings */
0043 rtems_task Init(rtems_task_argument argument);
0044 
0045 #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL)
0046 
0047 static const uint32_t sample_seconds [] = {
0048   571213695UL,
0049   602836095UL,
0050   634372095UL,
0051   665908095UL,
0052   697444095UL,
0053   729066495UL,
0054   760602495UL,
0055   792138495UL,
0056   823674495UL,
0057   855296895UL,
0058   886832895UL,
0059   918368895UL,
0060   949904895UL,
0061   981527295UL,
0062   1013063295UL,
0063   1044599295UL,
0064   1076135295UL,
0065   1107757695UL,
0066   1139293695UL,
0067   1170829695UL,
0068   1202365695UL,
0069   1233988095UL,
0070   1265524095UL,
0071   1297060095UL,
0072   1328596095UL,
0073   1360218495UL,
0074   1391754495UL,
0075   1423290495UL,
0076   1454826495UL,
0077   1486448895UL,
0078   1517984895UL,
0079   1549520895UL,
0080   1581056895UL,
0081   1612679295UL,
0082   1644215295UL,
0083   1675751295UL,
0084   1707287295UL,
0085   1738909695UL,
0086   1770445695UL,
0087   1801981695UL,
0088   1833517695UL,
0089   1865140095UL,
0090   1896676095UL,
0091   1928212095UL,
0092   1959748095UL,
0093   1991370495UL,
0094   2022906495UL,
0095   2054442495UL,
0096   2085978495UL,
0097   2117600895UL,
0098   2149136895UL,
0099   2180672895UL,
0100   2212208895UL,
0101   2243831295UL,
0102   2275367295UL,
0103   2306903295UL,
0104   2338439295UL,
0105   2370061695UL,
0106   2401597695UL,
0107   2433133695UL,
0108   2464669695UL,
0109   2496292095UL,
0110   2527828095UL,
0111   2559364095UL,
0112   2590900095UL,
0113   2622522495UL,
0114   2654058495UL,
0115   2685594495UL,
0116   2717130495UL,
0117   2748752895UL,
0118   2780288895UL,
0119   2811824895UL,
0120   2843360895UL,
0121   2874983295UL,
0122   2906519295UL,
0123   2938055295UL,
0124   2969591295UL,
0125   3001213695UL,
0126   3032749695UL,
0127   3064285695UL,
0128   3095821695UL,
0129   3127444095UL,
0130   3158980095UL,
0131   3190516095UL,
0132   3222052095UL,
0133   3253674495UL,
0134   3285210495UL,
0135   3316746495UL,
0136   3348282495UL,
0137   3379904895UL,
0138   3411440895UL,
0139   3442976895UL,
0140   3474512895UL,
0141   3506135295UL,
0142   3537671295UL,
0143   3569207295UL,
0144   3600743295UL,
0145   3632365695UL,
0146   3663901695UL,
0147   3695437695UL,
0148   3726973695UL,
0149   3758596095UL,
0150   3790132095UL,
0151   3821668095UL,
0152   3853204095UL,
0153   3884826495UL,
0154   3916362495UL,
0155   3947898495UL,
0156   3979434495UL,
0157   4011056895UL,
0158   4042592895UL,
0159   4074128895UL
0160 };
0161 
0162 static const rtems_time_of_day nearly_problem_2038 = {
0163   .year = 2038,
0164   .month = 1,
0165   .day = 19,
0166   .hour = 3,
0167   .minute = 14,
0168   .second = 7
0169 };
0170 
0171 static const rtems_time_of_day problem_2038 = {
0172   .year = 2038,
0173   .month = 1,
0174   .day = 19,
0175   .hour = 3,
0176   .minute = 14,
0177   .second = 8
0178 };
0179 
0180 static const rtems_time_of_day tod_to_seconds_base = {
0181   .year = 0,
0182   .month = 2,
0183   .day = 7,
0184   .hour = 6,
0185   .minute = 28,
0186   .second = 15
0187 };
0188 
0189 static const rtems_time_of_day problem_2100 = {
0190   .year = 2100,
0191   .month = 2,
0192   .day = 28,
0193   .hour = 0,
0194   .minute = 0,
0195   .second = 0
0196 };
0197 
0198 static const rtems_time_of_day problem_2100_2 = {
0199   .year = 2100,
0200   .month = 2,
0201   .day = 29,
0202   .hour = 0,
0203   .minute = 0,
0204   .second = 0
0205 };
0206 
0207 static void test_tod_to_seconds(void)
0208 {
0209   rtems_status_code sc = RTEMS_SUCCESSFUL;
0210   size_t i = 0;
0211   size_t n = sizeof(sample_seconds) / sizeof(sample_seconds [0]);
0212 
0213   for (i = 0; i < n; ++i) {
0214     rtems_time_of_day tod = tod_to_seconds_base;
0215     uint32_t seconds = 0;
0216     rtems_interval seconds_as_interval = 0;
0217 
0218     tod.year = 1988 + i;
0219     seconds = _TOD_To_seconds(&tod);
0220     rtems_test_assert(seconds == sample_seconds [i]);
0221 
0222     sc = rtems_clock_set(&tod);
0223     ASSERT_SC(sc);
0224 
0225     sc = rtems_clock_get_seconds_since_epoch(&seconds_as_interval);
0226     ASSERT_SC(sc);
0227     rtems_test_assert(seconds_as_interval == sample_seconds [i]);
0228   }
0229 }
0230 
0231 static void test_problem_year(void)
0232 {
0233   rtems_status_code sc = RTEMS_SUCCESSFUL;
0234   bool time_t_is_32_bit = sizeof(time_t) == 4;
0235 
0236   if (time_t_is_32_bit) {
0237     const rtems_time_of_day *nearly_problem = NULL;
0238     const rtems_time_of_day *problem = NULL;
0239     rtems_time_of_day now;
0240 
0241     nearly_problem = &nearly_problem_2038;
0242     problem = &problem_2038;
0243 
0244     sc = rtems_clock_set(nearly_problem);
0245     ASSERT_SC(sc);
0246     sc = rtems_clock_get_tod(&now);
0247     ASSERT_SC(sc);
0248     rtems_test_assert(memcmp(&now, nearly_problem, sizeof(now)) == 0);
0249 
0250     sc = rtems_clock_set(problem);
0251     ASSERT_SC(sc);
0252     sc = rtems_clock_get_tod(&now);
0253     ASSERT_SC(sc);
0254     rtems_test_assert(memcmp(&now, problem, sizeof(now)) == 0);
0255   }
0256 }
0257 
0258 static void test_leap_year(void)
0259 {
0260     rtems_status_code test_status;
0261     const rtems_time_of_day *problem = &problem_2100;
0262     const rtems_time_of_day *problem2 = &problem_2100_2;
0263     // 2100 is not a leap year, so it should have 28 days
0264     test_status = _TOD_Validate(problem, TOD_ENABLE_TICKS_VALIDATION);
0265     rtems_test_assert(test_status == RTEMS_INVALID_CLOCK);
0266     test_status = _TOD_Validate(problem2, TOD_ENABLE_TICKS_VALIDATION);
0267     rtems_test_assert(test_status == RTEMS_INVALID_CLOCK);
0268 }
0269 
0270 static bool test_year_is_leap_year(uint32_t year)
0271 {
0272     return (((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0);
0273 }
0274 
0275 static void test_every_day(void)
0276 {
0277     rtems_time_of_day every_day = {
0278         .year = 1970,
0279         .month = 1,
0280         .day = 1,
0281         .hour = 0,
0282         .minute = 1,
0283         .second = 2
0284     };
0285     const int days_per_month[2][12] = {
0286         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
0287         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
0288     };
0289     rtems_status_code sc = RTEMS_SUCCESSFUL;
0290     rtems_time_of_day now;
0291 
0292     for (every_day.year = 1988; every_day.year <= 2099; ++every_day.year) {
0293         int leap_year = test_year_is_leap_year(every_day.year) ? 1 : 0;
0294         for (every_day.month = 1; every_day.month <= 12; ++every_day.month) {
0295             int days = days_per_month[leap_year][every_day.month - 1];
0296             for (every_day.day = 1; every_day.day <= days; ++every_day.day) {
0297                 sc = rtems_clock_set(&every_day);
0298                 ASSERT_SC(sc);
0299                 sc = rtems_clock_get_tod(&now);
0300                 ASSERT_SC(sc);
0301                 rtems_test_assert(memcmp(&now, &every_day, sizeof(now)) == 0);
0302             }
0303         }
0304     }
0305 }
0306 
0307 rtems_task Init(rtems_task_argument argument)
0308 {
0309   TEST_BEGIN();
0310 
0311   test_tod_to_seconds();
0312   test_every_day();
0313   test_problem_year();
0314   test_leap_year();
0315 
0316   TEST_END();
0317 
0318   rtems_test_exit(0);
0319 }
0320 
0321 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0322 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0323 
0324 #define CONFIGURE_MAXIMUM_TASKS 1
0325 
0326 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0327 
0328 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0329 
0330 #define CONFIGURE_INIT
0331 
0332 #include <rtems/confdefs.h>