mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +03:00
Add mechanism for posting functions to the main loop (#2082)
* Add mechanism for posting functions to the main loop (#1064) * Fix indentation, add note that API is not stable
This commit is contained in:
77
tests/device/test_schedule/test_schedule.ino
Normal file
77
tests/device/test_schedule/test_schedule.ino
Normal file
@ -0,0 +1,77 @@
|
||||
#include <BSTest.h>
|
||||
#include <test_config.h>
|
||||
#include <Schedule.h>
|
||||
|
||||
BS_ENV_DECLARE();
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
BS_RUN(Serial);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("scheduled functions are executed", "[schedule]")
|
||||
{
|
||||
bool executed = false;
|
||||
CHECK(schedule_function([&](){
|
||||
executed = true;
|
||||
}));
|
||||
run_scheduled_functions();
|
||||
CHECK(executed);
|
||||
}
|
||||
|
||||
TEST_CASE("scheduled functions are executed in correct order", "[schedule]")
|
||||
{
|
||||
int counter = 0;
|
||||
auto fn = [&](int id) {
|
||||
CHECK(id == counter);
|
||||
++counter;
|
||||
};
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
schedule_function(std::bind(fn, i));
|
||||
}
|
||||
run_scheduled_functions();
|
||||
}
|
||||
|
||||
TEST_CASE("functions are only executed once", "[schedule]")
|
||||
{
|
||||
int counter = 0;
|
||||
auto fn = [&](){
|
||||
++counter;
|
||||
};
|
||||
schedule_function(fn);
|
||||
schedule_function(fn);
|
||||
schedule_function(fn);
|
||||
run_scheduled_functions();
|
||||
CHECK(counter == 3);
|
||||
counter = 0;
|
||||
run_scheduled_functions();
|
||||
CHECK(counter == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("can schedule SCHEDULED_FN_MAX_COUNT functions", "[schedule]")
|
||||
{
|
||||
int counter = 0;
|
||||
auto fn = [&](int id) {
|
||||
CHECK(id == counter);
|
||||
CHECK(id < SCHEDULED_FN_MAX_COUNT);
|
||||
++counter;
|
||||
};
|
||||
int i;
|
||||
for (i = 0; i < SCHEDULED_FN_MAX_COUNT; ++i) {
|
||||
CHECK(schedule_function(std::bind(fn, i)));
|
||||
}
|
||||
CHECK(!schedule_function(std::bind(fn, i)));
|
||||
run_scheduled_functions();
|
||||
CHECK(counter == SCHEDULED_FN_MAX_COUNT);
|
||||
counter = 0;
|
||||
for (i = 0; i < SCHEDULED_FN_MAX_COUNT; ++i) {
|
||||
CHECK(schedule_function(std::bind(fn, i)));
|
||||
}
|
||||
CHECK(!schedule_function(std::bind(fn, i)));
|
||||
run_scheduled_functions();
|
||||
CHECK(counter == SCHEDULED_FN_MAX_COUNT);
|
||||
}
|
||||
|
||||
void loop(){}
|
Reference in New Issue
Block a user