2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file test_signal_executor.cpp
|
|
|
|
*
|
|
|
|
* @date Nov 28, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:39:46 -07:00
|
|
|
#include "global.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "io_pins.h"
|
|
|
|
#include "event_queue.h"
|
2018-12-08 12:08:02 -08:00
|
|
|
#include "pwm_generator_logic.h"
|
2018-12-08 15:11:28 -08:00
|
|
|
#include "unit_test_framework.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static int callbackCounter = 0;
|
|
|
|
|
|
|
|
static void callback(void *a) {
|
|
|
|
callbackCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int complexTestNow;
|
|
|
|
|
2020-06-14 21:11:02 -07:00
|
|
|
struct TestPwm {
|
|
|
|
TestPwm(EventQueue *eventQueue) {
|
|
|
|
this->eventQueue = eventQueue;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduling_s s;
|
|
|
|
int period;
|
2020-06-14 21:11:02 -07:00
|
|
|
EventQueue *eventQueue;
|
|
|
|
};
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static void complexCallback(TestPwm *testPwm) {
|
|
|
|
callbackCounter++;
|
|
|
|
|
2020-06-14 21:11:02 -07:00
|
|
|
testPwm->eventQueue->insertTask(&testPwm->s, complexTestNow + testPwm->period,
|
2020-01-07 15:10:31 -08:00
|
|
|
{ complexCallback, testPwm });
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2020-06-17 05:42:37 -07:00
|
|
|
TEST(misc, testSignalExecutor2) {
|
2020-06-14 21:11:02 -07:00
|
|
|
EventQueue eq;
|
|
|
|
TestPwm p1(&eq);
|
|
|
|
TestPwm p2(&eq);
|
2015-07-10 06:01:56 -07:00
|
|
|
p1.period = 2;
|
|
|
|
p2.period = 3;
|
|
|
|
|
|
|
|
complexTestNow = 0;
|
|
|
|
callbackCounter = 0;
|
2020-01-07 15:10:31 -08:00
|
|
|
eq.insertTask(&p1.s, 0, { complexCallback, &p1 });
|
|
|
|
eq.insertTask(&p2.s, 0, { complexCallback, &p2 });
|
2015-07-10 06:01:56 -07:00
|
|
|
eq.executeAll(complexTestNow);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 2, callbackCounter) << "callbackCounter #1";
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(2, eq.size());
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(complexTestNow = 2);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 3, callbackCounter) << "callbackCounter #2";
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(2, eq.size());
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(complexTestNow = 3);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 4, callbackCounter) << "callbackCounter #3";
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(2, eq.size());
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-03 19:01:51 -07:00
|
|
|
static long prevValue = -1;
|
2016-09-03 18:03:51 -07:00
|
|
|
|
|
|
|
static void orderCallback(void *a) {
|
2016-09-03 19:01:51 -07:00
|
|
|
long value = (long)a;
|
2016-09-03 18:03:51 -07:00
|
|
|
|
|
|
|
printf("value=%d prevValue=%d\r\n", value, prevValue);
|
2019-01-14 14:44:03 -08:00
|
|
|
ASSERT_TRUE(value > prevValue) << "orderCallback";
|
2016-09-03 18:03:51 -07:00
|
|
|
|
|
|
|
prevValue = value;
|
|
|
|
}
|
|
|
|
|
2020-06-14 21:11:02 -07:00
|
|
|
TEST(misc, testSignalExecutor3) {
|
|
|
|
EventQueue eq;
|
2016-09-03 18:03:51 -07:00
|
|
|
|
|
|
|
scheduling_s s1;
|
|
|
|
scheduling_s s2;
|
|
|
|
scheduling_s s3;
|
|
|
|
|
2020-01-06 21:41:18 -08:00
|
|
|
eq.insertTask(&s1, 10, { orderCallback, (void*)1 });
|
|
|
|
eq.insertTask(&s2, 11, { orderCallback, (void*)2 });
|
|
|
|
eq.insertTask(&s3, 12, { orderCallback, (void*)3 });
|
2016-09-03 18:03:51 -07:00
|
|
|
eq.executeAll(100);
|
|
|
|
}
|
|
|
|
|
2019-01-14 12:31:56 -08:00
|
|
|
TEST(misc, testSignalExecutor) {
|
2020-06-14 21:11:02 -07:00
|
|
|
EventQueue eq;
|
2020-10-05 10:53:34 -07:00
|
|
|
ASSERT_EQ(eq.getNextEventTime(0), unexpected);
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduling_s s1;
|
|
|
|
scheduling_s s2;
|
|
|
|
scheduling_s s3;
|
|
|
|
scheduling_s s4;
|
|
|
|
|
2020-01-06 21:41:18 -08:00
|
|
|
eq.insertTask(&s1, 10, callback);
|
|
|
|
eq.insertTask(&s4, 10, callback);
|
|
|
|
eq.insertTask(&s3, 12, callback);
|
|
|
|
eq.insertTask(&s2, 11, callback);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(4, eq.size());
|
|
|
|
ASSERT_EQ(10, eq.getHead()->momentX);
|
2019-11-23 14:04:51 -08:00
|
|
|
ASSERT_EQ(10, eq.getHead()->nextScheduling_s->momentX);
|
|
|
|
ASSERT_EQ(11, eq.getHead()->nextScheduling_s->nextScheduling_s->momentX);
|
|
|
|
ASSERT_EQ(12, eq.getHead()->nextScheduling_s->nextScheduling_s->nextScheduling_s->momentX);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
callbackCounter = 0;
|
|
|
|
eq.executeAll(10);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 2, callbackCounter) << "callbackCounter/2";
|
2015-07-10 06:01:56 -07:00
|
|
|
callbackCounter = 0;
|
|
|
|
eq.executeAll(11);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 1, callbackCounter) << "callbackCounter/1#1";
|
2015-07-10 06:01:56 -07:00
|
|
|
eq.executeAll(100);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(0, eq.size());
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-01-06 21:41:18 -08:00
|
|
|
eq.insertTask(&s1, 12, callback);
|
|
|
|
eq.insertTask(&s2, 11, callback);
|
|
|
|
eq.insertTask(&s3, 10, callback);
|
2015-07-10 06:01:56 -07:00
|
|
|
callbackCounter = 0;
|
|
|
|
eq.executeAll(10);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 1, callbackCounter) << "callbackCounter/1#2";
|
2015-07-10 06:01:56 -07:00
|
|
|
callbackCounter = 0;
|
|
|
|
eq.executeAll(11);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(1, callbackCounter);
|
2015-07-10 06:01:56 -07:00
|
|
|
eq.executeAll(100);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(0, eq.size());
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
callbackCounter = 0;
|
2020-01-06 21:41:18 -08:00
|
|
|
eq.insertTask(&s1, 10, callback);
|
2020-10-05 10:53:34 -07:00
|
|
|
ASSERT_EQ(10, eq.getNextEventTime(0).value_or(-1));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(1);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 0, callbackCounter) << "callbacks not expected";
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(11);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(1, callbackCounter);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-10-05 10:53:34 -07:00
|
|
|
ASSERT_EQ(eq.getNextEventTime(0), unexpected);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-01-06 21:41:18 -08:00
|
|
|
eq.insertTask(&s1, 10, callback);
|
|
|
|
eq.insertTask(&s2, 13, callback);
|
2020-10-05 10:53:34 -07:00
|
|
|
ASSERT_EQ(10, eq.getNextEventTime(0).value_or(-1));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(1);
|
2020-10-05 10:53:34 -07:00
|
|
|
ASSERT_EQ(10, eq.getNextEventTime(0).value_or(-1));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(100);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(0, eq.size());
|
2015-07-10 06:01:56 -07:00
|
|
|
callbackCounter = 0;
|
|
|
|
// both events are scheduled for the same time
|
2020-01-06 21:41:18 -08:00
|
|
|
eq.insertTask(&s1, 10, callback);
|
|
|
|
eq.insertTask(&s2, 10, callback);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(11);
|
|
|
|
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(2, callbackCounter);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|