Added Mitchell Foral.
[ragel.git] / test / call1.rl
blobddd552a70e4ebdd53f748fa460c5b1884635ff56
1 /*
2  * @LANG: c
3  */
5 #include <stdio.h>
6 #include <string.h>
8 int num = 0;
10 struct test
12         int cs, top, stack[32];
15 %%{ 
16         machine test;
17         access fsm->;
19         action check_num {
20                 if ( num & 1 )
21                         fcall *fentry(odd);
22                 else
23                         fcall even;
24         }
26         # Test call and return functionality.
27         even := 'even' any @{fhold; fret;};
28         odd := 'odd' any @{fhold; fret;};
29         num = [0-9]+ ${ num = num * 10 + (fc - '0'); };
30         even_odd = num ' ' @check_num "\n";
32         # Test calls in out actions.
33         fail := !(any*);
34         out_acts = 'OA ok\n' | 
35                 'OA error1\n' |
36                 'OA error2\n';
38         main := even_odd | out_acts;
39 }%%
41 %% write data;
43 void test_init( struct test *fsm )
45         num = 0;
46         %% write init;
49 void test_execute( struct test *fsm, const char *data, int len )
51         const char *p = data;
52         const char *pe = data+len;
54         %% write exec;
57 int test_finish( struct test *fsm )
59         if ( fsm->cs == test_error )
60                 return -1;
61         if ( fsm->cs >= test_first_final )
62                 return 1;
63         return 0;
66 #define BUFSIZE 1024
68 void test( char *buf )
69 {   
70         struct test test;
71         test_init( &test );
72         test_execute( &test, buf, strlen(buf) );
73         if ( test_finish( &test ) > 0 )
74                 printf( "ACCEPT\n" );
75         else
76                 printf( "FAIL\n" );
79 int main()
81         test( "78 even\n" );
82         test( "89 odd\n" );
83         test( "1 even\n" );
84         test( "0 odd\n" );
85         test( "OA ok\n" );
86         test( "OA error1\n" );
87         test( "OA error2\n" );
88         
89         return 0;
93 #ifdef _____OUTPUT_____
94 ACCEPT
95 ACCEPT
96 FAIL
97 FAIL
98 ACCEPT
99 ACCEPT
100 ACCEPT
101 #endif