Skip to content

Commit ec07f61

Browse files
test(newlib): Added unit tests for usleep and sleep_for functions
This commit adds unit tests to verify the basic functionality of usleep() and this_thread::sleep_for() std functions.
1 parent d5d7bb6 commit ec07f61

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
idf_component_register(SRCS "test_cxx_general.cpp"
2-
PRIV_REQUIRES unity driver)
2+
PRIV_REQUIRES unity driver esp_timer)

components/cxx/test_apps/general/main/test_cxx_general.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -14,6 +14,9 @@
1414
#include "unity.h"
1515
#include "unity_test_utils.h"
1616
#include "soc/soc.h"
17+
#include <chrono>
18+
#include <thread>
19+
#include "esp_timer.h"
1720

1821
extern "C" void setUp()
1922
{
@@ -319,6 +322,29 @@ TEST_CASE("stack smashing protection CXX", "[stack_smash]")
319322
recur_and_smash_cxx();
320323
}
321324

325+
TEST_CASE("test std::this_thread::sleep_for basic functionality", "[misc]")
326+
{
327+
const int us_per_tick = portTICK_PERIOD_MS * 1000;
328+
329+
// Test sub-tick sleep
330+
const auto short_sleep = std::chrono::microseconds(us_per_tick / 4);
331+
int64_t start = esp_timer_get_time();
332+
std::this_thread::sleep_for(short_sleep);
333+
int64_t end = esp_timer_get_time();
334+
int64_t elapsed_us = end - start;
335+
printf("short sleep: %lld us\n", elapsed_us);
336+
TEST_ASSERT_GREATER_OR_EQUAL(short_sleep.count(), elapsed_us);
337+
338+
// Test multi-tick sleep
339+
const auto long_sleep = std::chrono::microseconds(us_per_tick * 2);
340+
start = esp_timer_get_time();
341+
std::this_thread::sleep_for(long_sleep);
342+
end = esp_timer_get_time();
343+
elapsed_us = end - start;
344+
printf("long sleep: %lld us\n", elapsed_us);
345+
TEST_ASSERT_GREATER_OR_EQUAL(long_sleep.count(), elapsed_us);
346+
}
347+
322348
extern "C" void app_main(void)
323349
{
324350
s_testTLS.foo(); /* allocates memory that will be reused */

components/newlib/test_apps/newlib/main/test_time.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <stdio.h>
77
#include <string.h>
88
#include <math.h>
9+
#include <unistd.h>
910
#include "unity.h"
1011
#include <time.h>
1112
#include <sys/time.h>
@@ -77,6 +78,27 @@ TEST_CASE("Reading RTC registers on APP CPU doesn't affect clock", "[newlib]")
7778

7879
#endif // (CONFIG_FREERTOS_NUMBER_OF_CORES == 2) && CONFIG_IDF_TARGET_ARCH_XTENSA
7980

81+
TEST_CASE("test usleep basic functionality", "[newlib]")
82+
{
83+
const int us_per_tick = portTICK_PERIOD_MS * 1000;
84+
85+
// Test sub-tick sleep such that usleep() uses ROM delay path
86+
const int short_sleep_us = us_per_tick / 4;
87+
int64_t start = esp_timer_get_time();
88+
TEST_ASSERT_EQUAL(0, usleep(short_sleep_us));
89+
int64_t end = esp_timer_get_time();
90+
printf("short sleep: %lld us\n", end - start);
91+
TEST_ASSERT_GREATER_OR_EQUAL(short_sleep_us, end - start);
92+
93+
// Test multi-tick sleep using vTaskDelay path
94+
const int long_sleep_us = us_per_tick * 2;
95+
start = esp_timer_get_time();
96+
TEST_ASSERT_EQUAL(0, usleep(long_sleep_us));
97+
end = esp_timer_get_time();
98+
printf("long sleep: %lld us\n", end - start);
99+
TEST_ASSERT_GREATER_OR_EQUAL(long_sleep_us, end - start);
100+
}
101+
80102
TEST_CASE("test adjtime function", "[newlib]")
81103
{
82104
struct timeval tv_time;

0 commit comments

Comments
 (0)