main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <time.h>
  12. #include "Timer.h"
  13. #include "DummyTimer.h"
  14. #include "Test_ParallelRegionsStatemachine.h"
  15. #include "definition.h"
  16. //typedef long int int64_t;
  17. /*@DTestSuite: ParallelRegions Statechart Test (Test_ParallelRegions.sct) */
  18. #define MAXEVENTSPERTYPE 4
  19. const char* stateName[10] = {"State1", "State2", "State3", "State4", "State5", "State6", "State7", "State8", "State9", "noState"};
  20. const int EnumTostateStr[10] = { _State1, _State2, _State3, _State4, _State5, _State6, _State7, _State8, _State9, last_state };
  21. int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
  22. {
  23. return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
  24. ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
  25. }
  26. const char* getStateString(uint32_t index)
  27. {
  28. int i;
  29. for (i=0; i<10; ++i)
  30. if (EnumTostateStr[i] == index)
  31. return stateName[i];
  32. return stateName[last_state];
  33. }
  34. void setupStatemachine(Test_ParallelRegionsStatemachine* machine, Timer* dummyTimer, EventPool* eventPool)
  35. {
  36. /* set up dummy Timer */
  37. dummyTimer_init(dummyTimer);
  38. /* Set up Event Pool */
  39. test_ParallelRegions_eventPool_init_heap(eventPool, MAXEVENTSPERTYPE);
  40. /* initialize state machine */
  41. test_ParallelRegionsStatemachine_init(machine, dummyTimer, eventPool);
  42. /* call all necessary enter functions */
  43. test_ParallelRegionsStatemachine_enter(machine);
  44. }
  45. void teardownStatemachine(Test_ParallelRegionsStatemachine* machine, Timer* dummyTimer, EventPool* eventPool)
  46. {
  47. /* call all exit actions for this state machine */
  48. test_ParallelRegionsStatemachine_exit(machine);
  49. /* free all internal memory for this state machine */
  50. test_ParallelRegionsStatemachine_destruct(machine);
  51. /* free the timer */
  52. timer_exit(dummyTimer);
  53. /* free all events in the event pool */
  54. eventPool_exit(eventPool);
  55. }
  56. int main(int argc, char** argv)
  57. {
  58. Test_ParallelRegionsStatemachine machine;
  59. Timer dummyTimer;
  60. EventPool eventPool;
  61. struct timespec start, end;
  62. int i;
  63. int max;
  64. setupStatemachine(&machine, &dummyTimer, &eventPool);
  65. /* create some Events */
  66. _Event* ev;
  67. max = atoi(argv[1]);
  68. clock_gettime(CLOCK_MONOTONIC, &start);
  69. for (i=0; i<max; ++i) {
  70. int random = rand()%14;
  71. ev = eventPool_createEvent(machine.base.eventPool, random);
  72. if (!ev)
  73. abort();
  74. statemachine_cy_setEvent(&machine.base, ev);
  75. test_ParallelRegionsStatemachine_runCycle(&machine);
  76. }
  77. clock_gettime(CLOCK_MONOTONIC, &end);
  78. int64_t timeElapsed = timespecDiff(&end, &start);
  79. printf("Time is %lld per cycle %lld\n", timeElapsed, timeElapsed/max);
  80. return 0;
  81. }