Skip to content

Commit 6a624a6

Browse files
authored
Unit test schedule disabling (speeduino#1406)
* Unit test disabling of schedules * Move setSchedule(Schedule&) tests to test_schedule.cpp * Move test_fuel_schedule_RUNNING_to_RUNNINGWITHNEXT_Disallow() to test_fuel_schedule.cpp * Move test_ignition_schedule_RUNNING_to_RUNNINGWITHNEXT_Disallow() to test_ignition_schedule.cpp * Always output the HTML report - easier local testing
1 parent dc415c6 commit 6a624a6

File tree

10 files changed

+418
-267
lines changed

10 files changed

+418
-267
lines changed

.github/workflows/code-coverage.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ jobs:
5757
print-summary: true
5858
excludes: ${{ env.GCOV_IGNORES }}
5959
github-token: ${{ secrets.GITHUB_TOKEN }}
60-
# html-out: .pio/build/${{ env.PIO_ENV }}/${{ env.FILE_ROOT }}.html
61-
# html-details: true
60+
html-out: .pio/build/${{ env.PIO_ENV }}/${{ env.FILE_ROOT }}.html
61+
html-details: true
6262
# coveralls-out: .pio/build/${{ env.PIO_ENV }}/${{ env.FILE_ROOT }}.coveralls
6363
# cobertura-out: .pio/build/${{ env.PIO_ENV }}/${{ env.COBERTURA_FILE }}
6464

speeduino/scheduler.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ void moveToNextState(IgnitionSchedule &schedule)
450450
movetoNextState(schedule, ignitionPendingToRunning, ignitionRunningToOff, ignitionRunningToPending);
451451
}
452452

453-
static void disableSchedule(Schedule &schedule)
453+
void disableSchedule(Schedule &schedule)
454454
{
455455
ATOMIC() {
456456
if(schedule.Status == PENDING) {
@@ -509,23 +509,13 @@ void disableIgnSchedule(uint8_t channel)
509509

510510
void disableAllFuelSchedules(void)
511511
{
512-
disableFuelSchedule(0);
513-
disableFuelSchedule(1);
514-
disableFuelSchedule(2);
515-
disableFuelSchedule(3);
516-
disableFuelSchedule(4);
517-
disableFuelSchedule(5);
518-
disableFuelSchedule(6);
519-
disableFuelSchedule(7);
512+
for (uint8_t index=0; index<INJ_CHANNELS; ++index) {
513+
disableFuelSchedule(index);
514+
}
520515
}
521516
void disableAllIgnSchedules(void)
522517
{
523-
disableIgnSchedule(0);
524-
disableIgnSchedule(1);
525-
disableIgnSchedule(2);
526-
disableIgnSchedule(3);
527-
disableIgnSchedule(4);
528-
disableIgnSchedule(5);
529-
disableIgnSchedule(6);
530-
disableIgnSchedule(7);
518+
for (uint8_t index=0; index<IGN_CHANNELS; ++index) {
519+
disableIgnSchedule(index);
520+
}
531521
}

speeduino/scheduler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ void setCallbacks(Schedule &schedule, voidVoidCallback pStartCallback, voidVoidC
168168
*/
169169
void setSchedule(Schedule &schedule, uint32_t delay, uint16_t duration, bool allowQueuedSchedule);
170170

171+
/** @brief Disable the schedule */
172+
void disableSchedule(Schedule &schedule);
171173

172174
/** Ignition schedule.
173175
*/

test/test_schedules/main.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ void runAllScheduleTests(void)
1111
extern void test_status_running_to_off(void);
1212
extern void test_accuracy_timeout(void);
1313
extern void test_accuracy_duration(void);
14-
extern void test_setSchedule(void);
1514
extern void testScheduleStateMachine(void);
16-
15+
extern void test_schedule(void);
16+
extern void test_fuel_schedule(void);
17+
extern void test_ignition_schedule(void);
18+
extern void test_ignition_controller();
19+
extern void test_fuel_controller(void);
20+
1721
initialiseAll();
1822

1923
test_status_initial_off();
@@ -23,8 +27,12 @@ void runAllScheduleTests(void)
2327
test_status_running_to_off();
2428
test_accuracy_timeout();
2529
test_accuracy_duration();
26-
test_setSchedule();
2730
testScheduleStateMachine();
31+
test_schedule();
32+
test_fuel_schedule();
33+
test_ignition_schedule();
34+
test_ignition_controller();
35+
test_fuel_controller();
2836
}
2937

3038
TEST_HARNESS(runAllScheduleTests)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <unity.h>
2+
#include "../test_utils.h"
3+
#include "scheduler.h"
4+
#include "channel_test_helpers.h"
5+
6+
static void set_all_statuses(ScheduleStatus status)
7+
{
8+
RUNIF_INJCHANNEL1({ fuelSchedule1.Status=status; }, {});
9+
RUNIF_INJCHANNEL2({ fuelSchedule2.Status=status; }, {});
10+
RUNIF_INJCHANNEL3({ fuelSchedule3.Status=status; }, {});
11+
RUNIF_INJCHANNEL4({ fuelSchedule4.Status=status; }, {});
12+
RUNIF_INJCHANNEL5({ fuelSchedule5.Status=status; }, {});
13+
RUNIF_INJCHANNEL6({ fuelSchedule6.Status=status; }, {});
14+
RUNIF_INJCHANNEL7({ fuelSchedule7.Status=status; }, {});
15+
RUNIF_INJCHANNEL8({ fuelSchedule8.Status=status; }, {});
16+
}
17+
18+
static void assert_all_statuses(ScheduleStatus status)
19+
{
20+
RUNIF_INJCHANNEL1({ TEST_ASSERT_EQUAL(status, fuelSchedule1.Status); }, {});
21+
RUNIF_INJCHANNEL2({ TEST_ASSERT_EQUAL(status, fuelSchedule2.Status); }, {});
22+
RUNIF_INJCHANNEL3({ TEST_ASSERT_EQUAL(status, fuelSchedule3.Status); }, {});
23+
RUNIF_INJCHANNEL4({ TEST_ASSERT_EQUAL(status, fuelSchedule4.Status); }, {});
24+
RUNIF_INJCHANNEL5({ TEST_ASSERT_EQUAL(status, fuelSchedule5.Status); }, {});
25+
RUNIF_INJCHANNEL6({ TEST_ASSERT_EQUAL(status, fuelSchedule6.Status); }, {});
26+
RUNIF_INJCHANNEL7({ TEST_ASSERT_EQUAL(status, fuelSchedule7.Status); }, {});
27+
RUNIF_INJCHANNEL8({ TEST_ASSERT_EQUAL(status, fuelSchedule8.Status); }, {});
28+
}
29+
30+
static void test_disableAllFuelSchedules(void)
31+
{
32+
stopFuelSchedulers();
33+
set_all_statuses(PENDING);
34+
disableAllFuelSchedules();
35+
assert_all_statuses(OFF);
36+
}
37+
38+
void test_fuel_controller(void)
39+
{
40+
SET_UNITY_FILENAME() {
41+
RUN_TEST_P(test_disableAllFuelSchedules);
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#include <Arduino.h>
3+
#include <unity.h>
4+
#include "../test_utils.h"
5+
#include "scheduler.h"
6+
#include "type_traits.h"
7+
8+
using raw_counter_t = type_traits::remove_reference<IgnitionSchedule::counter_t>::type;
9+
using raw_compare_t = type_traits::remove_reference<IgnitionSchedule::compare_t>::type;
10+
11+
static constexpr uint32_t TIMEOUT = 5000U;
12+
static constexpr uint32_t DURATION = 2000U;
13+
static constexpr COMPARE_TYPE INITIAL_COUNTER = 3333U;
14+
15+
static void test_fuel_schedule_RUNNING_to_RUNNINGWITHNEXT_Disallow(void) {
16+
static constexpr uint32_t DURATION_OFFSET = 33;
17+
static constexpr uint32_t TIMEOUT_OFFSET = 77;
18+
19+
raw_counter_t counter = {INITIAL_COUNTER};
20+
raw_compare_t compare = {0};
21+
FuelSchedule schedule(counter, compare);
22+
23+
setFuelSchedule(schedule, TIMEOUT, DURATION);
24+
25+
schedule.Status = RUNNING;
26+
CRANK_ANGLE_MAX_INJ = 360;
27+
28+
// Negative test
29+
// Calculate a revolution time that will result in 360° taking longer than MAX_TIMER_PERIOD
30+
auto revTime = MAX_TIMER_PERIOD+(MAX_TIMER_PERIOD/CRANK_ANGLE_MAX_INJ);
31+
setAngleConverterRevolutionTime(revTime);
32+
TEST_ASSERT_GREATER_THAN(MAX_TIMER_PERIOD, angleToTimeMicroSecPerDegree((uint16_t)CRANK_ANGLE_MAX_INJ));
33+
setFuelSchedule(schedule, TIMEOUT, DURATION);
34+
// Should not have changed
35+
TEST_ASSERT_EQUAL(INITIAL_COUNTER + uS_TO_TIMER_COMPARE(TIMEOUT), schedule._compare);
36+
TEST_ASSERT_EQUAL(RUNNING, schedule.Status);
37+
TEST_ASSERT_EQUAL(uS_TO_TIMER_COMPARE(DURATION), schedule.duration);
38+
TEST_ASSERT_EQUAL(0, schedule.nextStartCompare);
39+
40+
// Positive test
41+
setAngleConverterRevolutionTime(revTime/2U);
42+
TEST_ASSERT_LESS_THAN(MAX_TIMER_PERIOD, angleToTimeMicroSecPerDegree((uint32_t)CRANK_ANGLE_MAX_INJ));
43+
setFuelSchedule(schedule, TIMEOUT+TIMEOUT_OFFSET, DURATION+DURATION_OFFSET);
44+
// Should not have changed
45+
TEST_ASSERT_EQUAL(INITIAL_COUNTER + uS_TO_TIMER_COMPARE(TIMEOUT), schedule._compare);
46+
// These should have changed
47+
TEST_ASSERT_EQUAL(RUNNING_WITHNEXT, schedule.Status);
48+
TEST_ASSERT_EQUAL(uS_TO_TIMER_COMPARE(DURATION+DURATION_OFFSET), schedule.duration);
49+
TEST_ASSERT_EQUAL(INITIAL_COUNTER + uS_TO_TIMER_COMPARE(TIMEOUT+TIMEOUT_OFFSET), schedule.nextStartCompare);
50+
}
51+
52+
53+
void test_fuel_schedule(void)
54+
{
55+
SET_UNITY_FILENAME() {
56+
RUN_TEST_P(test_fuel_schedule_RUNNING_to_RUNNINGWITHNEXT_Disallow);
57+
}
58+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <unity.h>
2+
#include "../test_utils.h"
3+
#include "scheduler.h"
4+
#include "channel_test_helpers.h"
5+
6+
static void set_all_statuses(ScheduleStatus status)
7+
{
8+
RUNIF_IGNCHANNEL1({ ignitionSchedule1.Status=status; }, {});
9+
RUNIF_IGNCHANNEL2({ ignitionSchedule2.Status=status; }, {});
10+
RUNIF_IGNCHANNEL3({ ignitionSchedule3.Status=status; }, {});
11+
RUNIF_IGNCHANNEL4({ ignitionSchedule4.Status=status; }, {});
12+
RUNIF_IGNCHANNEL5({ ignitionSchedule5.Status=status; }, {});
13+
RUNIF_IGNCHANNEL6({ ignitionSchedule6.Status=status; }, {});
14+
RUNIF_IGNCHANNEL7({ ignitionSchedule7.Status=status; }, {});
15+
RUNIF_IGNCHANNEL8({ ignitionSchedule8.Status=status; }, {});
16+
}
17+
18+
static void assert_all_statuses(ScheduleStatus status)
19+
{
20+
RUNIF_IGNCHANNEL1({ TEST_ASSERT_EQUAL(status, ignitionSchedule1.Status); }, {});
21+
RUNIF_IGNCHANNEL2({ TEST_ASSERT_EQUAL(status, ignitionSchedule2.Status); }, {});
22+
RUNIF_IGNCHANNEL3({ TEST_ASSERT_EQUAL(status, ignitionSchedule3.Status); }, {});
23+
RUNIF_IGNCHANNEL4({ TEST_ASSERT_EQUAL(status, ignitionSchedule4.Status); }, {});
24+
RUNIF_IGNCHANNEL5({ TEST_ASSERT_EQUAL(status, ignitionSchedule5.Status); }, {});
25+
RUNIF_IGNCHANNEL6({ TEST_ASSERT_EQUAL(status, ignitionSchedule6.Status); }, {});
26+
RUNIF_IGNCHANNEL7({ TEST_ASSERT_EQUAL(status, ignitionSchedule7.Status); }, {});
27+
RUNIF_IGNCHANNEL8({ TEST_ASSERT_EQUAL(status, ignitionSchedule8.Status); }, {});
28+
}
29+
30+
static void test_disableAllIgnSchedules(void)
31+
{
32+
stopIgnitionSchedulers();
33+
set_all_statuses(PENDING);
34+
disableAllIgnSchedules();
35+
assert_all_statuses(OFF);
36+
}
37+
38+
void test_ignition_controller(void)
39+
{
40+
SET_UNITY_FILENAME() {
41+
RUN_TEST_P(test_disableAllIgnSchedules);
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#include <Arduino.h>
3+
#include <unity.h>
4+
#include "../test_utils.h"
5+
#include "scheduler.h"
6+
#include "type_traits.h"
7+
8+
using raw_counter_t = type_traits::remove_reference<IgnitionSchedule::counter_t>::type;
9+
using raw_compare_t = type_traits::remove_reference<IgnitionSchedule::compare_t>::type;
10+
11+
static constexpr uint32_t TIMEOUT = 5000U;
12+
static constexpr uint32_t DURATION = 2000U;
13+
static constexpr COMPARE_TYPE INITIAL_COUNTER = 3333U;
14+
15+
static void test_ignition_schedule_RUNNING_to_RUNNINGWITHNEXT_Disallow(void) {
16+
static constexpr uint32_t DURATION_OFFSET = 33;
17+
static constexpr uint32_t TIMEOUT_OFFSET = 77;
18+
19+
raw_counter_t counter = {INITIAL_COUNTER};
20+
raw_compare_t compare = {0};
21+
IgnitionSchedule schedule(counter, compare);
22+
23+
setIgnitionSchedule(schedule, TIMEOUT, DURATION);
24+
25+
schedule.Status = RUNNING;
26+
CRANK_ANGLE_MAX_IGN = 360;
27+
28+
// Negative test
29+
// Calculate a revolution time that will result in 360° taking longer than MAX_TIMER_PERIOD
30+
auto revTime = MAX_TIMER_PERIOD+(MAX_TIMER_PERIOD/CRANK_ANGLE_MAX_IGN);
31+
setAngleConverterRevolutionTime(revTime);
32+
TEST_ASSERT_GREATER_THAN_UINT32(MAX_TIMER_PERIOD, angleToTimeMicroSecPerDegree((uint16_t)CRANK_ANGLE_MAX_IGN));
33+
34+
setIgnitionSchedule(schedule, TIMEOUT, DURATION);
35+
// Should not have changed
36+
TEST_ASSERT_EQUAL(INITIAL_COUNTER + uS_TO_TIMER_COMPARE(TIMEOUT), schedule._compare);
37+
TEST_ASSERT_EQUAL(RUNNING, schedule.Status);
38+
TEST_ASSERT_EQUAL(uS_TO_TIMER_COMPARE(DURATION), schedule.duration);
39+
TEST_ASSERT_EQUAL(0, schedule.nextStartCompare);
40+
41+
// Positive test
42+
setAngleConverterRevolutionTime(revTime/2U);
43+
TEST_ASSERT_LESS_THAN(MAX_TIMER_PERIOD, angleToTimeMicroSecPerDegree((uint32_t)CRANK_ANGLE_MAX_INJ));
44+
setIgnitionSchedule(schedule, TIMEOUT+TIMEOUT_OFFSET, DURATION+DURATION_OFFSET);
45+
// Should not have changed
46+
TEST_ASSERT_EQUAL(INITIAL_COUNTER + uS_TO_TIMER_COMPARE(TIMEOUT), schedule._compare);
47+
// These should have changed
48+
TEST_ASSERT_EQUAL(RUNNING_WITHNEXT, schedule.Status);
49+
TEST_ASSERT_EQUAL(uS_TO_TIMER_COMPARE(DURATION+DURATION_OFFSET), schedule.duration);
50+
TEST_ASSERT_EQUAL(INITIAL_COUNTER + uS_TO_TIMER_COMPARE(TIMEOUT+TIMEOUT_OFFSET), schedule.nextStartCompare);
51+
}
52+
53+
void test_ignition_schedule(void)
54+
{
55+
SET_UNITY_FILENAME() {
56+
RUN_TEST_P(test_ignition_schedule_RUNNING_to_RUNNINGWITHNEXT_Disallow);
57+
}
58+
}

0 commit comments

Comments
 (0)