1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00
esp8266/tests/host/core/test_PolledTimeout.cpp
david gauchard 9a2ed274f3 polledTimeout: add option to use CPU count instead of millis() (#5870)
* polledTimeout: add option to use CPU count instead of millis()

* use more "using" alias

* more c++/clear code, using typename (thanks @devyte)

* rename class name to include unit, introduce timeMax() and check it with assert()

* remove useless defines

* improve api readability, add micro-second unit

* update example

* mock: emulate getCycleCount, add/fix polledTimeout CI test

* + nano-seconds, assert -> message, comments, host test

* allow 0 for timeout (enables immediate timeout, fix division by 0)

* typo, set member instead of local variable

* unify error message

* slight change on checkExpired() allows "never expired"
also removed printed message, add YieldAndDelay, simplify calculations

* remove traces of debug.h/cpp in this PR

* include missing <limits> header

* back to original expired test, introduce boolean _neverExpires, fix reset(), getTimeout() is invalid

* fix expiredOneShot with _timeout==0 check

* reenable getTimeout()

* expose checkExpired with unit conversion

* fix timing comments, move critical code to iram

* add member ::neverExpires and use it where relevant

* improve clarity

* remove exposed checkExpired(), adapt LEAmDNS with equivalent

* add API ::resetToNeverExpires(), use it in LEAmDNS

* remove offending constness from ::flagged() LEAmDNS (due do API fix in PolledTimeout)

* simplify "Fast" base classes

* minor variable rename

* Fix examples

* compliance with good c++ manners

* minor changes for consistency

* add missing const

* expired() and bool() moved to iram

* constexpr compensation computing

* add/update comments

* move neverExpires and alwaysExpired
2019-04-05 10:50:53 -03:00

247 lines
5.0 KiB
C++

#include <catch.hpp>
#include "PolledTimeout.h"
#define mockverbose printf
#include "common/MockEsp.cpp" // getCycleCount
//This won't work for
template<typename argT>
inline bool
fuzzycomp(argT a, argT b)
{
const argT epsilon = 10;
return (std::max(a,b) - std::min(a,b) <= epsilon);
}
TEST_CASE("OneShot Timeout 500000000ns (0.5s)", "[polledTimeout]")
{
using esp8266::polledTimeout::oneShotFastNs;
using timeType = oneShotFastNs::timeType;
timeType before, after, delta;
Serial.println("OneShot Timeout 500000000ns (0.5s)");
oneShotFastNs timeout(500000000);
before = micros();
while(!timeout.expired())
yield();
after = micros();
delta = after - before;
Serial.printf("delta = %u\n", delta);
REQUIRE(fuzzycomp(delta/1000, (timeType)500));
Serial.print("reset\n");
timeout.reset();
before = micros();
while(!timeout)
yield();
after = micros();
delta = after - before;
Serial.printf("delta = %u\n", delta);
REQUIRE(fuzzycomp(delta/1000, (timeType)500));
}
TEST_CASE("OneShot Timeout 3000000us", "[polledTimeout]")
{
using esp8266::polledTimeout::oneShotFastUs;
using timeType = oneShotFastUs::timeType;
timeType before, after, delta;
Serial.println("OneShot Timeout 3000000us");
oneShotFastUs timeout(3000000);
before = micros();
while(!timeout.expired())
yield();
after = micros();
delta = after - before;
Serial.printf("delta = %u\n", delta);
REQUIRE(fuzzycomp(delta/1000, (timeType)3000));
Serial.print("reset\n");
timeout.reset();
before = micros();
while(!timeout)
yield();
after = micros();
delta = after - before;
Serial.printf("delta = %u\n", delta);
REQUIRE(fuzzycomp(delta/1000, (timeType)3000));
}
TEST_CASE("OneShot Timeout 3000ms", "[polledTimeout]")
{
using esp8266::polledTimeout::oneShotMs;
using timeType = oneShotMs::timeType;
timeType before, after, delta;
Serial.println("OneShot Timeout 3000ms");
oneShotMs timeout(3000);
before = millis();
while(!timeout.expired())
yield();
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)3000));
Serial.print("reset\n");
timeout.reset();
before = millis();
while(!timeout)
yield();
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)3000));
}
TEST_CASE("OneShot Timeout 3000ms reset to 1000ms", "[polledTimeout]")
{
using esp8266::polledTimeout::oneShotMs;
using timeType = oneShotMs::timeType;
timeType before, after, delta;
Serial.println("OneShot Timeout 3000ms");
oneShotMs timeout(3000);
before = millis();
while(!timeout.expired())
yield();
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)3000));
Serial.print("reset\n");
timeout.reset(1000);
before = millis();
while(!timeout)
yield();
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)1000));
}
TEST_CASE("Periodic Timeout 1T 3000ms", "[polledTimeout]")
{
using esp8266::polledTimeout::periodicMs;
using timeType = periodicMs::timeType;
timeType before, after, delta;
Serial.println("Periodic Timeout 1T 3000ms");
periodicMs timeout(3000);
before = millis();
while(!timeout)
yield();
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)3000));
Serial.print("no reset needed\n");
before = millis();
while(!timeout)
yield();
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)3000));
}
TEST_CASE("Periodic Timeout 10T 1000ms", "[polledTimeout]")
{
using esp8266::polledTimeout::periodicMs;
using timeType = periodicMs::timeType;
timeType before, after, delta;
Serial.println("Periodic 10T Timeout 1000ms");
int counter = 10;
periodicMs timeout(1000);
before = millis();
while(1)
{
if(timeout)
{
Serial.print("*");
if(!--counter)
break;
yield();
}
}
after = millis();
delta = after - before;
Serial.printf("\ndelta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)10000));
}
TEST_CASE("OneShot Timeout 3000ms reset to 1000ms custom yield", "[polledTimeout]")
{
using YieldOrSkipPolicy = esp8266::polledTimeout::YieldPolicy::YieldOrSkip;
using oneShotMsYield = esp8266::polledTimeout::timeoutTemplate<false, YieldOrSkipPolicy>;
using timeType = oneShotMsYield::timeType;
timeType before, after, delta;
Serial.println("OneShot Timeout 3000ms");
oneShotMsYield timeout(3000);
before = millis();
while(!timeout.expired());
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)3000));
Serial.print("reset\n");
timeout.reset(1000);
before = millis();
while(!timeout);
after = millis();
delta = after - before;
Serial.printf("delta = %lu\n", delta);
REQUIRE(fuzzycomp(delta, (timeType)1000));
}