main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <string.h>
  5. #include "Timer.h"
  6. #include "DummyTimer.h"
  7. #include "Test_ExpressionStatemachine.h"
  8. #include "Test_ExpressionEvent.h"
  9. /*@DescTest: Expression Statechart Test (Test_Expression.sct) */
  10. #define MAXEVENTSPERTYPE 4
  11. const char* stateName[6] = {"State1", "State2", "State3", "State4", "State5", "State6"};
  12. void setupStatemachine(Timer* dummyTimer, EventPool* eventPool)
  13. {
  14. /* set up dummy Timer */
  15. dummyTimer_init(dummyTimer);
  16. /* Set up Event Pool */
  17. test_Expression_eventPool_init_heap(eventPool, MAXEVENTSPERTYPE);
  18. /* initialize state machine */
  19. test_ExpressionStatemachine_init(dummyTimer, eventPool);
  20. /* call all necessary enter functions */
  21. test_ExpressionStatemachine_enter();
  22. }
  23. void teardownStatemachine(Timer* dummyTimer, EventPool* eventPool)
  24. {
  25. /* call all exit actions for this state machine */
  26. test_ExpressionStatemachine_exit();
  27. /* free all internal memory for this state machine */
  28. test_ExpressionStatemachine_destruct();
  29. /* free the timer */
  30. timer_exit(dummyTimer);
  31. /* free all events in the event pool */
  32. eventPool_exit(eventPool);
  33. }
  34. /*@Test: test_default_var1 test behavior of var1 in default interface */
  35. int test_initialization_and_first_entry()
  36. {
  37. // Test_ExpressionStatemachine* machine;
  38. Timer dummyTimer;
  39. EventPool eventPool;
  40. real testvalue1u = (19.4 + 19.4*123 + 0.1);
  41. real testvalue1l = (19.4 + 19.4*123 - 0.1);
  42. /*@Desc: setup initial statemachine */
  43. setupStatemachine(&dummyTimer, &eventPool);
  44. /* get the handle only for getting the state name */
  45. // machine = test_Expression_get_handle();
  46. /*@Desc: check whether var1 from default interface is initially set to 6 */
  47. assert( test_Expression_if_get_var1() == 6);
  48. /*@Desc: check whether var2 from default is initially set to 123 */
  49. assert( test_Expression_if_get_var2() == 123);
  50. /*@Desc: check, wether var3 from default interface is set correct after state1 entry */
  51. printf("Var3 is set to %f\n", test_Expression_if_get_var3());
  52. assert( (test_Expression_if_get_var3() > testvalue1l) &&
  53. (test_Expression_if_get_var3() < testvalue1u) );
  54. /*@Desc: check, whether var4 from default interface is between 43.3 and 43.5 */
  55. printf("Var4 is set to %f\n", test_Expression_if_get_var4());
  56. assert( (test_Expression_if_get_var4() > 43.2) &&
  57. (test_Expression_if_get_var4() < 43.4) );
  58. /*@Desc: check whether var5 from default interface is initially set to false */
  59. assert( test_Expression_if_get_var5() == bool_false);
  60. /*@Desc: teardown statemachine */
  61. teardownStatemachine(&dummyTimer, &eventPool);
  62. return 0;
  63. }
  64. /*@Test: test_default_var1 test behavior of var1 in default and other interface */
  65. int test_default_other_var1()
  66. {
  67. Test_ExpressionStatemachine* machine;
  68. Timer dummyTimer;
  69. EventPool eventPool;
  70. /*@Desc: setup initial statemachine */
  71. setupStatemachine(&dummyTimer, &eventPool);
  72. /* get the handle only for getting the state name */
  73. machine = test_Expression_get_handle();
  74. /*@Desc: check initial value for var1 ( == 6) */
  75. assert( test_Expression_if_get_var1() == 6 );
  76. /*@Desc: run an explicit cycle - without any waiting event */
  77. test_ExpressionStatemachine_runCycle();
  78. /*@Desc: check the initial state */
  79. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)machine, 0)]);
  80. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)machine, 0)], "State1") == 0);
  81. /*@Desc: check value for var1 after state1 entry / var1 is not changed during entry into state1 */
  82. assert( test_Expression_if_get_var1() == 6 );
  83. /*@Desc: raise event1 on default interface with value 5 (actually unused) */
  84. test_Expression_if_raise_event1(5);
  85. /*@Desc: set other.var1 to "true" to let transition take place */
  86. test_Expression_if_other_set_var1(bool_true);
  87. /*@Desc: run an explicit cycle */
  88. test_ExpressionStatemachine_runCycle();
  89. /*@Desc: check whether the state is set to "State2" */
  90. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)machine, 0)]);
  91. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)machine, 0)], "State2") == 0);
  92. /*@Desc: check whether the transition action was executed */
  93. assert( test_Expression_if_other_get_var1() == bool_false);
  94. /*@Desc: check whether var1 has been increased by 1 (==7) */
  95. assert( test_Expression_if_get_var1() == 7 );
  96. /*@Desc: run an explicit cycle */
  97. test_ExpressionStatemachine_runCycle();
  98. /*@Desc: check whether the state is still set to "State2" */
  99. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)machine, 0)]);
  100. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)machine, 0)], "State2") == 0);
  101. /*@Desc: teardown statemachine */
  102. teardownStatemachine(&dummyTimer, &eventPool);
  103. return 0;
  104. }
  105. int test_default_other_var2_var3()
  106. {
  107. Test_ExpressionStatemachine* machine;
  108. Timer dummyTimer;
  109. EventPool eventPool;
  110. real testvalue1u = (19.4 + 19.4*123 + 0.1);
  111. real testvalue1l = (19.4 + 19.4*123 - 0.1);
  112. real testvalue2u = (19.4 + 19.4*123)/5.0 + 0.1;
  113. real testvalue2l = (19.4 + 19.4*123)/5.0 - 0.1;
  114. /*@Desc: setup initial statemachine */
  115. setupStatemachine(&dummyTimer, &eventPool);
  116. /* get the handle only for getting the state name */
  117. machine = test_Expression_get_handle();
  118. /*@Desc: check initial value for var2 ( == 123) */
  119. assert( test_Expression_if_get_var2() == 123 );
  120. /*@Desc: check initial value for var3 (19.4) */
  121. printf("Var3 is set to %f\n", test_Expression_if_get_var3());
  122. assert( (test_Expression_if_get_var3() > testvalue1l) &&
  123. (test_Expression_if_get_var3() < testvalue1u));
  124. /*@Desc: run an explicit cycle - without any waiting event */
  125. test_ExpressionStatemachine_runCycle();
  126. /*@Desc: check the initial state */
  127. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)machine, 0)]);
  128. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)machine, 0)], "State1") == 0);
  129. /*@Desc: raise event1 on default interface with value 5 (actually unused) */
  130. test_Expression_if_raise_event1(5);
  131. /*@Desc: run an explicit cycle - without any waiting event */
  132. test_ExpressionStatemachine_runCycle();
  133. /*@Desc: check the initial state */
  134. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)machine, 0)]);
  135. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)machine, 0)], "State2") == 0);
  136. /*@Desc: check, wether var3 on default interface is set correct after state1 entry */
  137. assert( test_Expression_if_get_var2() == 1 );
  138. /*@Desc: set var5 to "false" to let transition take place */
  139. test_Expression_if_set_var5(bool_false);
  140. /*@Desc: raise event1 on default interface with value 5 (actually unused) */
  141. test_Expression_if_raise_event1(5);
  142. /*@Desc: check the initial state */
  143. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)machine, 0)]);
  144. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)machine, 0)], "State2") == 0);
  145. /*@Desc: check, wether var3 on default interface is set correct after entry */
  146. printf("Var3 is set to %f\n", test_Expression_if_get_var3());
  147. assert( (test_Expression_if_get_var3() > testvalue2l) &&
  148. (test_Expression_if_get_var3() < testvalue2u) );
  149. /*@Desc: set event1 with variable 5 (acutally unused) */
  150. test_Expression_if_raise_event1(5);
  151. /*@Desc: run an explicit cycle */
  152. test_ExpressionStatemachine_runCycle();
  153. /*@Desc: check the transition to state1 */
  154. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)machine, 0)]);
  155. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)machine, 0)], "State1") == 0);
  156. /*@Desc-Info: I will not calculate the complete for var3 in this scenario */
  157. /*@Desc: teardown statemachine */
  158. teardownStatemachine(&dummyTimer, &eventPool);
  159. return 0;
  160. }
  161. int test_onCycle()
  162. {
  163. // Test_ExpressionStatemachine* machine;
  164. Timer dummyTimer;
  165. EventPool eventPool;
  166. /*@Desc: setup initial statemachine */
  167. setupStatemachine(&dummyTimer, &eventPool);
  168. /* get the handle only for getting the state name */
  169. // machine = test_Expression_get_handle();
  170. /*@Desc: run an explicit cycle - without any waiting event */
  171. test_ExpressionStatemachine_runCycle();
  172. /*@Desc: raise event1 on default interface with value 5 (actually unused) */
  173. test_Expression_if_raise_event1(5);
  174. /*@Desc: set other.var1 to "true" to let transition take place */
  175. test_Expression_if_other_set_var1(bool_true);
  176. /*@Desc: run an explicit cycle */
  177. test_ExpressionStatemachine_runCycle();
  178. /*@Desc: check whether var1 has been increased by one (==7) */
  179. assert( test_Expression_if_get_var1() == 7 );
  180. /*@Desc: run an explicit cycle - without any change */
  181. test_ExpressionStatemachine_runCycle();
  182. /*@Desc: check whether var1 has been increased by 1 on onCycle (==8) */
  183. assert( test_Expression_if_get_var1() == 8 );
  184. /*@Desc: run an explicit cycle - without any change */
  185. test_ExpressionStatemachine_runCycle();
  186. /*@Desc: check whether var1 has been increased by 1 on onCycle (==9) */
  187. assert( test_Expression_if_get_var1() == 9 );
  188. /*@Desc: teardown statemachine */
  189. teardownStatemachine(&dummyTimer, &eventPool);
  190. return 0;
  191. }
  192. int main(int argc, char** argv)
  193. {
  194. if (argc != 2)
  195. return -1;
  196. switch (atoi(argv[1])) {
  197. case 1:
  198. return test_initialization_and_first_entry();
  199. case 2:
  200. return test_default_other_var1();
  201. case 3:
  202. return test_default_other_var2_var3();
  203. case 4:
  204. return test_onCycle();
  205. }
  206. return 0;
  207. }