From 90a8a9666c93cf583d9742afdd4e7546323e025f Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 14 Aug 2019 13:30:52 +0200 Subject: [PATCH] unittests: add tests for rtc_tm_normalize() --- tests/unittests/tests-rtc/Makefile | 1 + tests/unittests/tests-rtc/Makefile.include | 3 + tests/unittests/tests-rtc/tests-rtc.c | 193 +++++++++++++++++++++ tests/unittests/tests-rtc/tests-rtc.h | 37 ++++ 4 files changed, 234 insertions(+) create mode 100644 tests/unittests/tests-rtc/Makefile create mode 100644 tests/unittests/tests-rtc/Makefile.include create mode 100644 tests/unittests/tests-rtc/tests-rtc.c create mode 100644 tests/unittests/tests-rtc/tests-rtc.h diff --git a/tests/unittests/tests-rtc/Makefile b/tests/unittests/tests-rtc/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-rtc/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-rtc/Makefile.include b/tests/unittests/tests-rtc/Makefile.include new file mode 100644 index 0000000000..050dcfda2b --- /dev/null +++ b/tests/unittests/tests-rtc/Makefile.include @@ -0,0 +1,3 @@ +CFLAGS += -DRTC_NORMALIZE_COMPAT=1 + +USEMODULE += periph_rtc_common diff --git a/tests/unittests/tests-rtc/tests-rtc.c b/tests/unittests/tests-rtc/tests-rtc.c new file mode 100644 index 0000000000..053eec7151 --- /dev/null +++ b/tests/unittests/tests-rtc/tests-rtc.c @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2019 ML!PA Consulting GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + */ +#include +#include + +#include "embUnit.h" + +#include "periph/rtc.h" + +static void _test_equal_tm(const struct tm *a, const struct tm *b) +{ + TEST_ASSERT_EQUAL_INT((a)->tm_sec, (b)->tm_sec); + TEST_ASSERT_EQUAL_INT((a)->tm_min, (b)->tm_min); + TEST_ASSERT_EQUAL_INT((a)->tm_hour, (b)->tm_hour); + TEST_ASSERT_EQUAL_INT((a)->tm_mday, (b)->tm_mday); + TEST_ASSERT_EQUAL_INT((a)->tm_mon, (b)->tm_mon); + TEST_ASSERT_EQUAL_INT((a)->tm_year, (b)->tm_year); + TEST_ASSERT_EQUAL_INT((a)->tm_wday, (b)->tm_wday); + TEST_ASSERT_EQUAL_INT((a)->tm_yday, (b)->tm_yday); +} + +static void test_rtc_compat(void) +{ + struct tm t1 = { + .tm_sec = 42, + .tm_min = 37, + .tm_hour = 13, + .tm_mday = 23, + .tm_mon = 5, + .tm_year = 84, + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst=1 + }; + + struct tm t2 = t1; + + mktime(&t1); + rtc_tm_normalize(&t2); + _test_equal_tm(&t1, &t2); +} + +static void test_rtc_sec_wrap(void) +{ + struct tm t1 = { + .tm_sec = 360, + .tm_min = 58, + .tm_hour = 23, + .tm_mday = 30, + .tm_mon = 5, + .tm_year = 100, + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst= 1 + }; + + struct tm t2 = t1; + + mktime(&t1); + rtc_tm_normalize(&t2); + _test_equal_tm(&t1, &t2); +} + +static void test_rtc_lyear(void) +{ + struct tm t1 = { + .tm_sec = 360, + .tm_min = 58, + .tm_hour = 23, + .tm_mday = 28, + .tm_mon = 1, + .tm_year = 100, + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst= 0 + }; + + struct tm t2 = t1; + + mktime(&t1); + rtc_tm_normalize(&t2); + _test_equal_tm(&t1, &t2); +} + +static void test_rtc_nyear(void) +{ + struct tm t1 = { + .tm_sec = 360, + .tm_min = 58, + .tm_hour = 23, + .tm_mday = 28, + .tm_mon = 1, + .tm_year = 101, + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst= 0 + }; + + struct tm t2 = t1; + + mktime(&t1); + rtc_tm_normalize(&t2); + _test_equal_tm(&t1, &t2); +} + +static void test_rtc_ywrap(void) +{ + struct tm t1 = { + .tm_sec = 360, + .tm_min = 58, + .tm_hour = 23, + .tm_mday = 32, + .tm_mon = 11, + .tm_year = 101, + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst= 0 + }; + + struct tm t2 = t1; + + mktime(&t1); + rtc_tm_normalize(&t2); + _test_equal_tm(&t1, &t2); +} + +static void test_rtc_year(void) +{ + struct tm t1 = { + .tm_sec = 360, + .tm_min = 58, + .tm_hour = 23, + .tm_mday = 32, + .tm_mon = 11, + .tm_year = 100, + .tm_wday = 0, + .tm_yday = 0, + }; + + struct tm t2 = t1; + + for (int i = 0; i <= 2*366; ++i) { + t1.tm_mday++; + t2.tm_mday++; + + mktime(&t1); + + /* rtc_tm_normalize does not handle DST */ + if (t1.tm_isdst && !t2.tm_isdst) { + t2.tm_hour++; + t2.tm_isdst = 1; + } else if (!t1.tm_isdst && t2.tm_isdst) { + t2.tm_hour--; + t2.tm_isdst = 0; + } + + rtc_tm_normalize(&t2); + _test_equal_tm(&t1, &t2); + } +} + +Test *tests_rtc_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_rtc_compat), + new_TestFixture(test_rtc_sec_wrap), + new_TestFixture(test_rtc_lyear), + new_TestFixture(test_rtc_nyear), + new_TestFixture(test_rtc_ywrap), + new_TestFixture(test_rtc_year), + }; + + EMB_UNIT_TESTCALLER(rtc_tests, NULL, NULL, fixtures); + + return (Test *)&rtc_tests; +} + +void tests_rtc(void) +{ + TESTS_RUN(tests_rtc_tests()); +} +/** @} */ diff --git a/tests/unittests/tests-rtc/tests-rtc.h b/tests/unittests/tests-rtc/tests-rtc.h new file mode 100644 index 0000000000..13a1a8ad36 --- /dev/null +++ b/tests/unittests/tests-rtc/tests-rtc.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2019 ML!PA Consulting GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @addtogroup unittests + * @{ + * + * @file + * @brief Unittests for the ``rtc`` module + * + * @author Benjamin Valentin + */ +#ifndef TESTS_RTC_H +#define TESTS_RTC_H + +#include "embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + */ +void tests_rtc(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_RTC_H */ +/** @} */