1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-24 08:45:10 +03:00
esp8266/tests/device/test_schedule/test_schedule.ino
Ivan Grokhotkov 5eb6a7f449 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
2016-06-08 11:22:48 +08:00

78 lines
1.7 KiB
C++

#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(){}