main.c 5.4 KB

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