test_rtc_lib.c 2.6 KB

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