Added a ruby test: atoi3.rl and adapted the test harness to build and run ruby
[external/ragel.git] / test / atoi3.rl
1 #
2 # @LANG: ruby
3 # @ALLOW_GENFLAGS: -T0
4 #
5
6 neg = false
7 val = 0
8
9 %%{
10     machine atoi1_java;
11     alphtype byte;
12     action begin {
13         neg = false;
14         val = 0;
15     }
16     action see_neg {
17         neg = true;
18     }
19     action add_digit {
20                 val = val * 10 + (fc - "0"[0]);
21     }
22     action finish {
23                 val = -1 * val if neg
24     }
25     action print {
26         puts val;
27     }
28     atoi = (('-' @ see_neg | '+') ? (digit @ add_digit) +) > begin % finish;
29     main := atoi '\n' @ print;
30 }%%
31
32 %% write data;
33
34 def run_machine( data )
35         p = 0;
36         pe = data.length
37         cs = 0
38         cs = 0
39         val = 0;
40         neg = false;
41
42         %% write init;
43         %% write exec;
44         %% write eof;
45         if  cs >= atoi1_java_first_final
46                 puts "ACCEPT"
47         else
48                 puts "FAIL"
49         end
50 end
51
52 inp = [
53                 "1\n",
54                 "12\n",
55                 "222222\n",
56                 "+2123\n",
57                 "213 3213\n",
58                 "-12321\n",
59                 "--123\n",
60                 "-99\n",
61                 " -3000\n",
62 ]
63
64 inp.each { |str| run_machine(str) }
65
66 =begin _____OUTPUT_____
67 1
68 ACCEPT
69 12
70 ACCEPT
71 222222
72 ACCEPT
73 2123
74 ACCEPT
75 FAIL
76 -12321
77 ACCEPT
78 FAIL
79 -99
80 ACCEPT
81 FAIL
82 =end _____OUTPUT_____