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