main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * main.c
  3. *
  4. * Created on: 16.11.2011
  5. * Author: showcase
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include "Timer.h"
  12. #include "DummyTimer.h"
  13. #include "Test_LocalActionsStatemachine.h"
  14. /*@DTestSuite: LocalActions Statechart Test (Test_LocalActions.sct) */
  15. #define MAXEVENTSPERTYPE 4
  16. const char* stateName[3] = {"State1", "State2", "noState"};
  17. const int EnumTostateStr[3] = {
  18. _Test_LocalActions_mainRegion_State1,
  19. _Test_LocalActions_mainRegion_State2,
  20. last_state };
  21. const char* getStateString(uint32_t index)
  22. {
  23. int i;
  24. for (i=0; i<10; ++i)
  25. if (EnumTostateStr[i] == index)
  26. return stateName[i];
  27. return stateName[last_state];
  28. }
  29. /* Timer Test Environment START */
  30. int myTimerSet = 0;
  31. uint32_t timerValue;
  32. sc_boolean timerPeriodic;
  33. void setMyTimer(const uint32_t evid, const uint32_t time_ms, sc_boolean periodic)
  34. {
  35. myTimerSet = 1;
  36. timerValue = time_ms;
  37. timerPeriodic = periodic;
  38. printf("myTimer: set event <%d> with time %dms\n", evid, time_ms);
  39. }
  40. void unsetMyTimer(const uint32_t evid)
  41. {
  42. myTimerSet = -1;
  43. timerValue = 0;
  44. timerPeriodic = bool_false;
  45. printf("myTimer: unset event <%d>\n", evid);
  46. }
  47. void myTimer_init(Timer* handle)
  48. {
  49. timer_setFPtr(handle, &setMyTimer, &unsetMyTimer);
  50. }
  51. /* Timer Test Environment END */
  52. void setupStatemachine(Test_LocalActionsStatemachine* machine, Timer* dummyTimer, EventPool* eventPool)
  53. {
  54. /* set up dummy Timer */
  55. dummyTimer_init(dummyTimer);
  56. /* Set up Event Pool */
  57. test_LocalActions_eventPool_init_heap(eventPool, MAXEVENTSPERTYPE);
  58. /* initialize state machine */
  59. test_LocalActionsStatemachine_init(machine, dummyTimer, eventPool);
  60. /* call all necessary enter functions */
  61. test_LocalActionsStatemachine_enter(machine);
  62. }
  63. void setupStatemachineMyTimer(Test_LocalActionsStatemachine* machine, Timer* dummyTimer, EventPool* eventPool)
  64. {
  65. /* set up dummy Timer */
  66. myTimer_init(dummyTimer);
  67. /* Set up Event Pool */
  68. test_LocalActions_eventPool_init_heap(eventPool, MAXEVENTSPERTYPE);
  69. /* initialize state machine */
  70. test_LocalActionsStatemachine_init(machine, dummyTimer, eventPool);
  71. /* call all necessary enter functions */
  72. test_LocalActionsStatemachine_enter(machine);
  73. }
  74. void teardownStatemachine(Test_LocalActionsStatemachine* machine, Timer* dummyTimer, EventPool* eventPool)
  75. {
  76. /* call all exit actions for this state machine */
  77. test_LocalActionsStatemachine_exit(machine);
  78. /* free all internal memory for this state machine */
  79. test_LocalActionsStatemachine_destruct(machine);
  80. /* free the timer */
  81. timer_exit(dummyTimer);
  82. /* free all events in the event pool */
  83. eventPool_exit(eventPool);
  84. }
  85. /*@Test: localActions_check_initial_entry test whether the entry action is called within initialization */
  86. int localActions_check_initial_entry()
  87. {
  88. Test_LocalActionsStatemachine machine;
  89. Timer dummyTimer;
  90. EventPool eventPool;
  91. /*@Desc: setup initial statemachine */
  92. setupStatemachine(&machine, &dummyTimer, &eventPool);
  93. /*@Desc: check whether entry works on initialisation*/
  94. assert(test_LocalActions_if_get_i(test_LocalActionsStatemachine_get_iface(&machine)) == 1);
  95. /*@Desc: teardown statemachine */
  96. teardownStatemachine(&machine, &dummyTimer, &eventPool);
  97. return 0;
  98. }
  99. /*@Test: localActions_check_transition_entry test whether the entry action is called after the Event1 transition*/
  100. int localActions_check_transition_entry()
  101. {
  102. Test_LocalActionsStatemachine machine;
  103. Timer dummyTimer;
  104. EventPool eventPool;
  105. /*@Desc: setup initial statemachine */
  106. setupStatemachine(&machine, &dummyTimer, &eventPool);
  107. /*@Desc: raise event1 on default Interface */
  108. test_LocalActions_if_raise_Event1(test_LocalActionsStatemachine_get_iface(&machine));
  109. /*@Desc: run an explicit cycle */
  110. test_LocalActionsStatemachine_runCycle(&machine);
  111. /*@Desc: check whether entry works on initialisation*/
  112. assert(test_LocalActions_if_get_j(test_LocalActionsStatemachine_get_iface(&machine)) == 1);
  113. /*@Desc: teardown statemachine */
  114. teardownStatemachine(&machine, &dummyTimer, &eventPool);
  115. return 0;
  116. }
  117. /*@Test: localActions_check_transition_exit test whether exit action in state 1 and 2 is called after the transitions */
  118. int localActions_check_transition_exit()
  119. {
  120. Test_LocalActionsStatemachine machine;
  121. Timer dummyTimer;
  122. EventPool eventPool;
  123. /*@Desc: setup initial statemachine */
  124. setupStatemachine(&machine, &dummyTimer, &eventPool);
  125. /*@Desc: raise event1 on default Interface */
  126. test_LocalActions_if_raise_Event1(test_LocalActionsStatemachine_get_iface(&machine));
  127. /*@Desc: run an explicit cycle */
  128. test_LocalActionsStatemachine_runCycle(&machine);
  129. /*@Desc: check whether entry works on initialisation*/
  130. assert(test_LocalActions_if_get_i(test_LocalActionsStatemachine_get_iface(&machine)) == 0);
  131. /*@Desc: raise event1 on default Interface */
  132. test_LocalActions_if_raise_Event3(test_LocalActionsStatemachine_get_iface(&machine));
  133. /*@Desc: run an explicit cycle */
  134. test_LocalActionsStatemachine_runCycle(&machine);
  135. /*@Desc: check whether entry works on initialisation*/
  136. assert(test_LocalActions_if_get_j(test_LocalActionsStatemachine_get_iface(&machine)) == 0);
  137. /*@Desc: teardown statemachine */
  138. teardownStatemachine(&machine, &dummyTimer, &eventPool);
  139. return 0;
  140. }
  141. int main(int argc, char** argv)
  142. {
  143. if (argc != 2)
  144. return -1;
  145. switch (atoi(argv[1])) {
  146. case 1:
  147. return localActions_check_initial_entry();
  148. case 2:
  149. return localActions_check_transition_entry();
  150. case 3:
  151. return localActions_check_transition_exit();
  152. }
  153. return -1;
  154. }