Initialize `flag' member variable in state structure
[eleutheria.git] / fsm / test_stress.c
blob99297e0c5dc01e649b68a4c503064f5c094091a3
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h> /* for time() in srand() */
5 #include "fsm.h"
6 #include "states.h"
7 #include "types.h"
9 #define NSTATES 20
10 #define NEVENTS 20
11 #define NEPOCHS 100000
13 /* Function prototypes */
14 void foo1(void *data);
15 void foo2(void *data);
17 void foo1(void *data)
21 void foo2(void *data)
25 int main(int argc, char *argv[])
27 state_t *state[NSTATES];
28 void (*pf[])(void *) = { foo1, foo2, NULL };
29 fsm_t *fsm;
30 unsigned int i, j, k;
32 /* Initialize fsm */
33 printf("Initializing fsm\n");
34 fsm_init(&fsm, 2<<11, 5, 0);
36 /* Initialize random number generator */
37 srand(time(NULL));
39 /* Initialize states */
40 printf("Initializing states (%u)\n", NSTATES);
41 for (i = 0; i < NSTATES; i++) {
42 if (state_init(&state[i], 2<<12, 2) == ST_NOMEM) {
43 fprintf(stderr, "error: state_init(): ST_NOMEM\n");
44 for (j = 0; j < i; j++)
45 state_free(state[j]);
46 fsm_free(fsm);
47 exit(EXIT_FAILURE);
51 /* Populate states with events they can handle */
52 printf("Populating states with events (events/state = %u)\n", NEVENTS);
53 for (i = 0; i < NSTATES; i++) {
54 for (j = 0; j < NEVENTS; j++) {
55 if (state_add_evt(state[i], j, "", pf[rand() % 3], state[i]) == ST_NOMEM) {
56 fprintf(stderr, "error: state_add_evt(): ST_NOMEM\n");
57 for (k = 0; k < i; k++)
58 state_free(state[k]);
59 fsm_free(fsm);
60 exit(EXIT_FAILURE);
65 /* Add states to fsm */
66 printf("Adding states to fsm (%u)\n", NSTATES);
67 for (i = 0; i < NSTATES; i++)
68 fsm_add_state(fsm, i, state[i]);
70 /* Set initial state */
71 fsm_set_state(fsm, 0);
73 /* Process events */
74 printf("Simulating (events = %u)\n", NEPOCHS);
75 for (i = 0; i < NEPOCHS; i++)
76 fsm_process_event(fsm, rand() % NEVENTS, NULL);
78 /* Free memory */
79 printf("Destroying FSM\n");
80 fsm_free(fsm);
82 return EXIT_SUCCESS;