main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_ExpressionStatemachine.h"
  14. #include "Test_Hierarchy.h"
  15. /*@DTestSuite: Hierachy Statechart Test (Test_Hierarchy.sct) */
  16. #define MAXEVENTSPERTYPE 4
  17. const char* stateName[6] = {"State1", "State2", "State3", "State4", "State5", "State6"};
  18. void setupStatemachine(Test_HierarchyStatemachine* machine, Timer* dummyTimer, EventPool* eventPool)
  19. {
  20. /* set up dummy Timer */
  21. dummyTimer_init(dummyTimer);
  22. /* Set up Event Pool */
  23. eventPool_init_heap(eventPool, event_last, MAXEVENTSPERTYPE);
  24. /* initialize state machine */
  25. test_HierarchyStatemachine_init(machine, dummyTimer, eventPool);
  26. }
  27. void teardownStatemachine(Test_HierarchyStatemachine* machine, Timer* dummyTimer, EventPool* eventPool)
  28. {
  29. test_HierarchyStatemachine_exit(machine);
  30. timer_exit(dummyTimer);
  31. eventPool_exit(eventPool);
  32. }
  33. /*@Test: test_default_var1 test behavior of var1 in default interface */
  34. int test_initialization()
  35. {
  36. return 0;
  37. }
  38. /*@Test: test_state9_state10_transition test behavior of var1 in default and other interface */
  39. int test_state9_state10_transition()
  40. {
  41. Test_HierarchyStatemachine machine;
  42. Timer dummyTimer;
  43. EventPool eventPool;
  44. /*@Desc: setup initial statemachine */
  45. setupStatemachine(&machine, &dummyTimer, &eventPool);
  46. /*@Desc: teardown statemachine */
  47. teardownStatemachine(&machine, &dummyTimer, &eventPool);
  48. return 0;
  49. }
  50. /*@Test: test_default_var1 test behavior of var1 in default and other interface */
  51. int test_state1_state2_back_transition()
  52. {
  53. Test_HierarchyStatemachine machine;
  54. Timer dummyTimer;
  55. EventPool eventPool;
  56. /*@Desc: setup initial statemachine */
  57. setupStatemachine(&machine, &dummyTimer, &eventPool);
  58. /*@Desc: run an explicit cycle - without any waiting event (for initialization) */
  59. test_ExpressionStatemachine_runCycle(&machine);
  60. /*@Desc: */
  61. /*@Desc: teardown statemachine */
  62. teardownStatemachine(&machine, &dummyTimer, &eventPool);
  63. return 0;
  64. }
  65. int test_default_other_var2_var3()
  66. {
  67. Test_ExpressionStatemachine machine;
  68. Timer dummyTimer;
  69. EventPool eventPool;
  70. real testvalue1u = (19.4 + 19.4*123 + 0.1);
  71. real testvalue1l = (19.4 + 19.4*123 - 0.1);
  72. real testvalue2u = 19.4 + 19.4*123/5.0 + 0.1;
  73. real testvalue2l = 19.4 + 19.4*123/5.0 - 0.1;
  74. /*@Desc: setup initial statemachine */
  75. setupStatemachine(&machine, &dummyTimer, &eventPool);
  76. /*@Desc: check initial value for var2 ( == 123) */
  77. assert( test_Expression_if_get_var2(&machine.interface) == 123 );
  78. /*@Desc: check initial value for var3 (19.4) */
  79. assert( (test_Expression_if_get_var3(&machine.interface) > 19.3) &&
  80. (test_Expression_if_get_var3(&machine.interface) < 19.5));
  81. /*@Desc: run an explicit cycle - without any waiting event */
  82. test_ExpressionStatemachine_runCycle(&machine);
  83. /*@Desc: check the initial state */
  84. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)&machine, 0)]);
  85. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)&machine, 0)], "State1") == 0);
  86. /*@Desc: check, wether var3 on default interface is set correct after state1 entry */
  87. assert( (test_Expression_if_get_var3(&machine.interface) > testvalue1l) &&
  88. (test_Expression_if_get_var3(&machine.interface) < testvalue1u) );
  89. /*@Desc: check, wether var3 on default interface is set correct after state1 entry */
  90. assert( test_Expression_if_get_var2(&machine.interface) == 1 );
  91. /*@Desc: set var5 to "false" to let transition take place */
  92. test_Expression_if_set_var5(&machine.interface, bool_false);
  93. /*@Desc: raise event1 on default interface with value 5 (actually unused) */
  94. test_Expression_if_raise_event1(&machine.interface, 5);
  95. /*@Desc: check the initial state */
  96. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)&machine, 0)]);
  97. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)&machine, 0)], "State2") == 0);
  98. /*@Desc: check, wether var3 on default interface is set correct after entry */
  99. assert( (test_Expression_if_get_var3(&machine.interface) > testvalue2l) &&
  100. (test_Expression_if_get_var3(&machine.interface) < testvalue2u) );
  101. /*@Desc: set event1 with variable 5 (acutally unused) */
  102. test_Expression_if_raise_event1(&machine.interface, 5);
  103. /*@Desc: run an explicit cycle */
  104. test_ExpressionStatemachine_runCycle(&machine);
  105. /*@Desc: check the transition to state1 */
  106. printf("%s\n", stateName[statemachineBase_getState((StatemachineBase*)&machine, 0)]);
  107. assert( strcmp(stateName[statemachineBase_getState((StatemachineBase*)&machine, 0)], "State1") == 0);
  108. /*@Desc-Info: I will not calculate the complete for var3 in this scenario */
  109. /*@Desc: teardown statemachine */
  110. teardownStatemachine(&machine, &dummyTimer, &eventPool);
  111. return 0;
  112. }
  113. int main(int argc, char** argv)
  114. {
  115. if (argc != 2)
  116. return -1;
  117. switch (atoi(argv[1])) {
  118. case 1:
  119. return test_default_other_var1();
  120. case 2:
  121. return test_default_other_var2_var3();
  122. }
  123. return 0;
  124. }