1 // SPDX-License-Identifier: LGPL-2.1+
3 #include <kunit/test.h>
7 * Advance a date by one day.
9 static void advance_date(int *year
, int *month
, int *mday
, int *yday
)
11 if (*mday
!= rtc_month_days(*month
- 1, *year
)) {
30 * Check every day in specified number of years interval starting on 1970-01-01
31 * against the expected result.
33 static void rtc_time64_to_tm_test_date_range(struct kunit
*test
, int years
)
36 * years = (years / 400) * 400 years
37 * = (years / 400) * 146097 days
38 * = (years / 400) * 146097 * 86400 seconds
40 time64_t total_secs
= ((time64_t
)years
) / 400 * 146097 * 86400;
47 struct rtc_time result
;
51 for (secs
= 0; secs
<= total_secs
; secs
+= 86400) {
53 rtc_time64_to_tm(secs
, &result
);
55 days
= div_s64(secs
, 86400);
57 #define FAIL_MSG "%d/%02d/%02d (%2d) : %lld", \
58 year, month, mday, yday, days
60 KUNIT_ASSERT_EQ_MSG(test
, year
- 1900, result
.tm_year
, FAIL_MSG
);
61 KUNIT_ASSERT_EQ_MSG(test
, month
- 1, result
.tm_mon
, FAIL_MSG
);
62 KUNIT_ASSERT_EQ_MSG(test
, mday
, result
.tm_mday
, FAIL_MSG
);
63 KUNIT_ASSERT_EQ_MSG(test
, yday
, result
.tm_yday
, FAIL_MSG
);
65 advance_date(&year
, &month
, &mday
, &yday
);
70 * Checks every day in a 160000 years interval starting on 1970-01-01
71 * against the expected result.
73 static void rtc_time64_to_tm_test_date_range_160000(struct kunit
*test
)
75 rtc_time64_to_tm_test_date_range(test
, 160000);
79 * Checks every day in a 1000 years interval starting on 1970-01-01
80 * against the expected result.
82 static void rtc_time64_to_tm_test_date_range_1000(struct kunit
*test
)
84 rtc_time64_to_tm_test_date_range(test
, 1000);
87 static struct kunit_case rtc_lib_test_cases
[] = {
88 KUNIT_CASE(rtc_time64_to_tm_test_date_range_1000
),
89 KUNIT_CASE_SLOW(rtc_time64_to_tm_test_date_range_160000
),
93 static struct kunit_suite rtc_lib_test_suite
= {
94 .name
= "rtc_lib_test_cases",
95 .test_cases
= rtc_lib_test_cases
,
98 kunit_test_suite(rtc_lib_test_suite
);
100 MODULE_DESCRIPTION("KUnit test for RTC lib functions");
101 MODULE_LICENSE("GPL");