Merge tag 'hwmon-for-v6.13-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / rtc / lib_test.c
blobc30c759662e39b48e6fedd37073b656e0de1336b
1 // SPDX-License-Identifier: LGPL-2.1+
3 #include <kunit/test.h>
4 #include <linux/rtc.h>
6 /*
7 * Advance a date by one day.
8 */
9 static void advance_date(int *year, int *month, int *mday, int *yday)
11 if (*mday != rtc_month_days(*month - 1, *year)) {
12 ++*mday;
13 ++*yday;
14 return;
17 *mday = 1;
18 if (*month != 12) {
19 ++*month;
20 ++*yday;
21 return;
24 *month = 1;
25 *yday = 1;
26 ++*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;
42 int year = 1970;
43 int month = 1;
44 int mday = 1;
45 int yday = 1;
47 struct rtc_time result;
48 time64_t secs;
49 s64 days;
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");