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"
|
2018-12-08 15:11:28 -08:00
|
|
|
#include <time.h>
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "test_signal_executor.h"
|
|
|
|
#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
|
|
|
|
2018-12-08 15:11:28 -08:00
|
|
|
// this instance is used by some unit tests here which reference it directly
|
2015-07-10 06:01:56 -07:00
|
|
|
static EventQueue eq;
|
|
|
|
|
|
|
|
static int callbackCounter = 0;
|
|
|
|
|
|
|
|
static void callback(void *a) {
|
|
|
|
callbackCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int complexTestNow;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
scheduling_s s;
|
|
|
|
int period;
|
|
|
|
} TestPwm;
|
|
|
|
|
|
|
|
static void complexCallback(TestPwm *testPwm) {
|
|
|
|
callbackCounter++;
|
|
|
|
|
|
|
|
eq.insertTask(&testPwm->s, complexTestNow + testPwm->period,
|
2020-01-07 15:10:31 -08:00
|
|
|
{ complexCallback, testPwm });
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void testSignalExecutor2(void) {
|
|
|
|
print("*************************************** testSignalExecutor2\r\n");
|
|
|
|
eq.clear();
|
|
|
|
TestPwm p1;
|
|
|
|
TestPwm p2;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void testSignalExecutor3(void) {
|
|
|
|
print("*************************************** testSignalExecutor3\r\n");
|
|
|
|
eq.clear();
|
|
|
|
|
|
|
|
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) {
|
2016-09-03 18:03:51 -07:00
|
|
|
testSignalExecutor3();
|
2015-07-10 06:01:56 -07:00
|
|
|
print("*************************************** testSignalExecutor\r\n");
|
|
|
|
|
2016-09-03 18:03:51 -07:00
|
|
|
eq.clear();
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(EMPTY_QUEUE, eq.getNextEventTime(0));
|
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);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(10, eq.getNextEventTime(0));
|
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
|
|
|
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(EMPTY_QUEUE, eq.getNextEventTime(0));
|
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);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(10, eq.getNextEventTime(0));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
eq.executeAll(1);
|
2019-01-14 12:45:35 -08:00
|
|
|
ASSERT_EQ(10, eq.getNextEventTime(0));
|
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
|
|
|
testSignalExecutor2();
|
|
|
|
}
|