Skip to content

Commit 0de5ab2

Browse files
committed
Add a protected HAL set_time() test
When DEVICE_LPTICKER is defined set_time() only works correctly on the first call. This test calls set_time() twice and ensures the time set by both calls is correct. This test only runs if DEVICE_RTC or DEVICE_LPTICKER is defined.
1 parent d82990c commit 0de5ab2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

TESTS/mbed_hal/rtc_time/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,41 @@ void test_local_time_invalid_param()
169169
TEST_ASSERT_EQUAL(false, _rtc_localtime(1, NULL, RTC_4_YEAR_LEAP_YEAR_SUPPORT));
170170
}
171171

172+
/* Test set_time() function called a few seconds apart.
173+
*
174+
* Given is set_time() function.
175+
* When set_time() is used to set the system time two times.
176+
* Then if the value returned from time() is always correct return true, otherwise return false.
177+
*/
178+
#define NEW_TIME 15
179+
void test_set_time_twice()
180+
{
181+
time_t current_time;
182+
183+
/* Set the time to NEW_TIME and check it */
184+
set_time(NEW_TIME);
185+
current_time = time(NULL);
186+
TEST_ASSERT_EQUAL (true, (current_time == NEW_TIME));
187+
188+
/* Wait 2 seconds */
189+
wait_ms(2000);
190+
191+
/* set the time to NEW_TIME again and check it */
192+
set_time(NEW_TIME);
193+
current_time = time(NULL);
194+
TEST_ASSERT_EQUAL (true, (current_time == NEW_TIME));
195+
}
196+
172197
Case cases[] = {
173198
Case("test is leap year - RTC leap years full support", test_is_leap_year<RTC_FULL_LEAP_YEAR_SUPPORT>),
174199
Case("test is leap year - RTC leap years partial support", test_is_leap_year<RTC_4_YEAR_LEAP_YEAR_SUPPORT>),
175200
Case("test make time boundary values - RTC leap years full support", test_mk_time_boundary<RTC_FULL_LEAP_YEAR_SUPPORT>),
176201
Case("test make time boundary values - RTC leap years partial support", test_mk_time_boundary<RTC_4_YEAR_LEAP_YEAR_SUPPORT>),
177202
Case("test make time - invalid param", test_mk_time_invalid_param),
178203
Case("test local time - invalid param", test_local_time_invalid_param),
204+
#if DEVICE_RTC || DEVICE_LPTICKER
205+
Case("test set_time twice", test_set_time_twice),
206+
#endif
179207
};
180208

181209
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)

0 commit comments

Comments
 (0)