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  */
9 static void advance_date(int *year, int *month, int *mday, int *yday, int *wday)
10 {
11 	*wday = (*wday + 1) % 7;
12 
13 	if (*mday != rtc_month_days(*month - 1, *year)) {
14 		++*mday;
15 		++*yday;
16 		return;
17 	}
18 
19 	*mday = 1;
20 	if (*month != 12) {
21 		++*month;
22 		++*yday;
23 		return;
24 	}
25 
26 	*month = 1;
27 	*yday  = 1;
28 	++*year;
29 }
30 
31 /*
32  * Check every day in specified number of years interval starting on 1970-01-01
33  * against the expected result.
34  */
35 static void rtc_time64_to_tm_test_date_range(struct kunit *test, int years)
36 {
37 	/*
38 	 * years	= (years / 400) * 400 years
39 	 *		= (years / 400) * 146097 days
40 	 *		= (years / 400) * 146097 * 86400 seconds
41 	 */
42 	time64_t total_secs = ((time64_t)years) / 400 * 146097 * 86400;
43 
44 	int year	= 1900;
45 	int month	= 1;
46 	int mday	= 1;
47 	int yday	= 1;
48 	int wday	= 1; /* Jan 1st 1900 was a Monday */
49 
50 	struct rtc_time result;
51 	time64_t secs;
52 	const time64_t sec_offset = RTC_TIMESTAMP_BEGIN_1900 + ((1 * 60) + 2) * 60 + 3;
53 
54 	for (secs = 0; secs <= total_secs; secs += 86400) {
55 
56 		rtc_time64_to_tm(secs + sec_offset, &result);
57 
58 		#define FAIL_MSG "%d/%02d/%02d (%2d, %d) : %lld", \
59 			year, month, mday, yday, wday, secs + sec_offset
60 
61 		KUNIT_ASSERT_EQ_MSG(test, year - 1900, result.tm_year, FAIL_MSG);
62 		KUNIT_ASSERT_EQ_MSG(test, month - 1, result.tm_mon, FAIL_MSG);
63 		KUNIT_ASSERT_EQ_MSG(test, mday, result.tm_mday, FAIL_MSG);
64 		KUNIT_ASSERT_EQ_MSG(test, yday, result.tm_yday, FAIL_MSG);
65 		KUNIT_ASSERT_EQ_MSG(test, 1, result.tm_hour, FAIL_MSG);
66 		KUNIT_ASSERT_EQ_MSG(test, 2, result.tm_min, FAIL_MSG);
67 		KUNIT_ASSERT_EQ_MSG(test, 3, result.tm_sec, FAIL_MSG);
68 		KUNIT_ASSERT_EQ_MSG(test, wday, result.tm_wday, FAIL_MSG);
69 
70 		advance_date(&year, &month, &mday, &yday, &wday);
71 	}
72 }
73 
74 /*
75  * Checks every day in a 160000 years interval starting on 1900-01-01
76  * against the expected result.
77  */
78 static void rtc_time64_to_tm_test_date_range_160000(struct kunit *test)
79 {
80 	rtc_time64_to_tm_test_date_range(test, 160000);
81 }
82 
83 /*
84  * Checks every day in a 1000 years interval starting on 1900-01-01
85  * against the expected result.
86  */
87 static void rtc_time64_to_tm_test_date_range_1000(struct kunit *test)
88 {
89 	rtc_time64_to_tm_test_date_range(test, 1000);
90 }
91 
92 static struct kunit_case rtc_lib_test_cases[] = {
93 	KUNIT_CASE(rtc_time64_to_tm_test_date_range_1000),
94 	KUNIT_CASE_SLOW(rtc_time64_to_tm_test_date_range_160000),
95 	{}
96 };
97 
98 static struct kunit_suite rtc_lib_test_suite = {
99 	.name = "rtc_lib_test_cases",
100 	.test_cases = rtc_lib_test_cases,
101 };
102 
103 kunit_test_suite(rtc_lib_test_suite);
104 
105 MODULE_DESCRIPTION("KUnit test for RTC lib functions");
106 MODULE_LICENSE("GPL");
107