lib_test.c (1690B)
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 */ 9static 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 * Checks every day in a 160000 years interval starting on 1970-01-01 31 * against the expected result. 32 */ 33static void rtc_time64_to_tm_test_date_range(struct kunit *test) 34{ 35 /* 36 * 160000 years = (160000 / 400) * 400 years 37 * = (160000 / 400) * 146097 days 38 * = (160000 / 400) * 146097 * 86400 seconds 39 */ 40 time64_t total_secs = ((time64_t) 160000) / 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) : %ld", \ 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 69static struct kunit_case rtc_lib_test_cases[] = { 70 KUNIT_CASE(rtc_time64_to_tm_test_date_range), 71 {} 72}; 73 74static struct kunit_suite rtc_lib_test_suite = { 75 .name = "rtc_lib_test_cases", 76 .test_cases = rtc_lib_test_cases, 77}; 78 79kunit_test_suite(rtc_lib_test_suite); 80 81MODULE_LICENSE("GPL");