1 // SPDX-License-Identifier: LGPL-2.1+
2
3 #include <kunit/test.h>
4 #include <linux/rtc.h>
5
6 /*
7 * Advance a date by one day.
8 */
advance_date(int * year,int * month,int * mday,int * yday)9 static void advance_date(int *year, int *month, int *mday, int *yday)
10 {
11 if (*mday != rtc_month_days(*month - 1, *year)) {
12 ++*mday;
13 ++*yday;
14 return;
15 }
16
17 *mday = 1;
18 if (*month != 12) {
19 ++*month;
20 ++*yday;
21 return;
22 }
23
24 *month = 1;
25 *yday = 1;
26 ++*year;
27 }
28
29 /*
30 * Check every day in specified number of years interval starting on 1970-01-01
31 * against the expected result.
32 */
rtc_time64_to_tm_test_date_range(struct kunit * test,int years)33 static void rtc_time64_to_tm_test_date_range(struct kunit *test, int years)
34 {
35 /*
36 * years = (years / 400) * 400 years
37 * = (years / 400) * 146097 days
38 * = (years / 400) * 146097 * 86400 seconds
39 */
40 time64_t total_secs = ((time64_t)years) / 400 * 146097 * 86400;
41
42 int year = 1970;
43 int month = 1;
44 int mday = 1;
45 int yday = 1;
46
47 struct rtc_time result;
48 time64_t secs;
49 s64 days;
50
51 for (secs = 0; secs <= total_secs; secs += 86400) {
52
53 rtc_time64_to_tm(secs, &result);
54
55 days = div_s64(secs, 86400);
56
57 #define FAIL_MSG "%d/%02d/%02d (%2d) : %lld", \
58 year, month, mday, yday, days
59
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);
64
65 advance_date(&year, &month, &mday, &yday);
66 }
67 }
68
69 /*
70 * Checks every day in a 160000 years interval starting on 1970-01-01
71 * against the expected result.
72 */
rtc_time64_to_tm_test_date_range_160000(struct kunit * test)73 static void rtc_time64_to_tm_test_date_range_160000(struct kunit *test)
74 {
75 rtc_time64_to_tm_test_date_range(test, 160000);
76 }
77
78 /*
79 * Checks every day in a 1000 years interval starting on 1970-01-01
80 * against the expected result.
81 */
rtc_time64_to_tm_test_date_range_1000(struct kunit * test)82 static void rtc_time64_to_tm_test_date_range_1000(struct kunit *test)
83 {
84 rtc_time64_to_tm_test_date_range(test, 1000);
85 }
86
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),
90 {}
91 };
92
93 static struct kunit_suite rtc_lib_test_suite = {
94 .name = "rtc_lib_test_cases",
95 .test_cases = rtc_lib_test_cases,
96 };
97
98 kunit_test_suite(rtc_lib_test_suite);
99
100 MODULE_DESCRIPTION("KUnit test for RTC lib functions");
101 MODULE_LICENSE("GPL");
102