adding a test case
[IRC.git] / Robust / src / Tests / setjmpTest / sj.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <setjmp.h>
5
6 #include "Queue.h"
7
8 struct SESEstruct {
9   int     id;
10   jmp_buf buf;
11   void*   parent;
12   Q*      childQ;
13
14   // "local" variables for use in macros
15   void* child;
16 };
17 typedef struct SESEstruct SESE;
18   
19
20 SESE* createSESE( SESE* parent, int id ) {
21   SESE* sese   = (SESE*) malloc( sizeof( SESE ) );
22   sese->id     = id;
23   sese->parent = (void*) parent;
24   sese->childQ = createQueue();
25 }
26
27 void freeSESE( SESE* sese ) {
28   freeQueue( sese->childQ );
29   free( sese );
30   sese = NULL;
31 }
32
33 // macros set this return value for inspection
34 int mlpRet;
35
36 // currently running SESE
37 SESE* current;
38
39
40 int mlpTime = 0;
41 void mlpLog( char* point ) {
42   printf( "  time = %d, id = %d, point = %s\n", mlpTime, current->id, point );
43   mlpTime++;
44 }
45
46
47 #define mlpInit( id ) \
48   current = createSESE( NULL, id );
49
50
51 void mlpBlock( int id ) {
52 }
53
54
55 #define mlpEnqueue( childid )                                   \
56   current->child = (void*) createSESE( current, childid );      \
57   addNewItem( current->childQ, current->child );                \
58   if( setjmp( ((SESE*)current->child)->buf ) ) {                \
59     mlpRet = 1;                                                 \
60   } else {                                                      \
61     mlpRet = 0;                                                 \
62   }                                             
63
64
65 #define mlpNotifyExit()                                         \
66   while( !isEmpty( current->childQ ) ) {                        \
67     current->child = getItem( current->childQ );                \
68     current = (SESE*) current->child;                           \
69     if( setjmp( ((SESE*)current->parent)->buf ) ) {             \
70     } else {                                                    \
71       longjmp( current->buf, 1 );                               \
72     }                                                           \
73   }                                                             \
74   if( current->parent != NULL ) {                               \
75     current = (SESE*) current->parent;                          \
76     longjmp( current->buf, 1 );                                 \
77   }
78
79
80
81 void foo() {
82   mlpLog( "f" );
83
84   mlpEnqueue( 20 );
85   if( mlpRet ) {
86     mlpLog( "fc" );
87     mlpNotifyExit();
88
89   } else {
90     mlpLog( "fp" );
91     mlpNotifyExit();
92   }  
93 }
94
95
96
97 int main() {
98   int i;
99   char lname[10];
100
101   printf( "Beginning setjump/longjump test.\n" );
102
103   mlpInit( 0 );
104   mlpLog( "a" );
105
106   mlpEnqueue( 1 );
107   if( mlpRet ) {
108
109     mlpLog( "w" );
110
111     for( i = 0; i < 2; ++i ) {
112
113       mlpEnqueue( 10 + i );
114       if( mlpRet ) {
115         
116         sprintf( lname, "Ls%d", 10 + i );
117         mlpLog( lname );
118         mlpNotifyExit();
119
120       } else {
121         sprintf( lname, "%d", i );
122         mlpLog( lname );
123       }
124     }
125
126     mlpLog( "x" );
127     mlpNotifyExit();
128
129   } else {
130     mlpLog( "W" );
131
132     //foo();
133
134     mlpLog( "X" );
135     mlpNotifyExit();
136   }
137
138   printf( "End test.\n" );
139
140   return 0;
141 }