Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1+
0002 
0003 #include <kunit/test.h>
0004 #include <linux/rtc.h>
0005 
0006 /*
0007  * Advance a date by one day.
0008  */
0009 static void advance_date(int *year, int *month, int *mday, int *yday)
0010 {
0011     if (*mday != rtc_month_days(*month - 1, *year)) {
0012         ++*mday;
0013         ++*yday;
0014         return;
0015     }
0016 
0017     *mday = 1;
0018     if (*month != 12) {
0019         ++*month;
0020         ++*yday;
0021         return;
0022     }
0023 
0024     *month = 1;
0025     *yday  = 1;
0026     ++*year;
0027 }
0028 
0029 /*
0030  * Checks every day in a 160000 years interval starting on 1970-01-01
0031  * against the expected result.
0032  */
0033 static void rtc_time64_to_tm_test_date_range(struct kunit *test)
0034 {
0035     /*
0036      * 160000 years = (160000 / 400) * 400 years
0037      *      = (160000 / 400) * 146097 days
0038      *      = (160000 / 400) * 146097 * 86400 seconds
0039      */
0040     time64_t total_secs = ((time64_t) 160000) / 400 * 146097 * 86400;
0041 
0042     int year    = 1970;
0043     int month   = 1;
0044     int mday    = 1;
0045     int yday    = 1;
0046 
0047     struct rtc_time result;
0048     time64_t secs;
0049     s64 days;
0050 
0051     for (secs = 0; secs <= total_secs; secs += 86400) {
0052 
0053         rtc_time64_to_tm(secs, &result);
0054 
0055         days = div_s64(secs, 86400);
0056 
0057         #define FAIL_MSG "%d/%02d/%02d (%2d) : %ld", \
0058             year, month, mday, yday, days
0059 
0060         KUNIT_ASSERT_EQ_MSG(test, year - 1900, result.tm_year, FAIL_MSG);
0061         KUNIT_ASSERT_EQ_MSG(test, month - 1, result.tm_mon, FAIL_MSG);
0062         KUNIT_ASSERT_EQ_MSG(test, mday, result.tm_mday, FAIL_MSG);
0063         KUNIT_ASSERT_EQ_MSG(test, yday, result.tm_yday, FAIL_MSG);
0064 
0065         advance_date(&year, &month, &mday, &yday);
0066     }
0067 }
0068 
0069 static struct kunit_case rtc_lib_test_cases[] = {
0070     KUNIT_CASE(rtc_time64_to_tm_test_date_range),
0071     {}
0072 };
0073 
0074 static struct kunit_suite rtc_lib_test_suite = {
0075     .name = "rtc_lib_test_cases",
0076     .test_cases = rtc_lib_test_cases,
0077 };
0078 
0079 kunit_test_suite(rtc_lib_test_suite);
0080 
0081 MODULE_LICENSE("GPL");