Debian Codegraph @

t/rabend.t

raw
1 #!/usr/bin/perl
2 # Copyright 2022 Jeffrey Kegler
3 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
4 # redistribute it and/or modify it under the terms of the GNU Lesser
5 # General Public License as published by the Free Software Foundation,
6 # either version 3 of the License, or (at your option) any later version.
7 #
8 # Marpa::R2 is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser
14 # General Public License along with Marpa::R2. If not, see
15 # http://www.gnu.org/licenses/.
16
17 use 5.010001;
18 use strict;
19 use warnings;
20
21 use Test::More tests => 6;
22
23 use English qw( -no_match_vars );
24 use Fatal qw( open close );
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28
29 sub catch_problem {
30 my ( $test_name, $test, $expected_result, $expected_error ) = @_;
31 my $result;
32 my $eval_ok = eval {
33 $result = $test->();
34 1;
35 };
36 my $eval_error = $EVAL_ERROR;
37
38 Test::More::is( $result, $expected_result, "Result: $test_name" );
39 if ($eval_ok) {
40 Test::More::fail("Failed to catch problem: $test_name");
41 }
42 elsif ( index( $eval_error, $expected_error ) < 0 ) {
43 my $diag_message =
44 "Failed to find expected message, was expecting:\n";
45 my $temp;
46 $temp = $expected_error;
47 $temp =~ s/^/=== /xmsg;
48 chomp $temp;
49 $diag_message .= "$temp\n";
50 $diag_message .= "This was the message actually received:\n";
51 $temp = $eval_error;
52 $temp =~ s/^/=== /xmsg;
53 chomp $temp;
54 $diag_message .= "$temp\n";
55 Test::More::diag($diag_message);
56 Test::More::fail("Unexpected message: $test_name");
57 } ## end elsif ( index( $eval_error, $expected_error ) < 0 )
58 else {
59 Test::More::pass("Successfully caught problem: $test_name");
60 }
61 return;
62 } ## end sub catch_problem
63
64 my $grammar = Marpa::R2::Grammar->new(
65 { start => 'Top',
66 rules => [
67 { lhs => 'Top', rhs => [qw/Term/], min => 1 },
68 { lhs => 'Term', rhs => [qw/a/] },
69 { lhs => 'Term', rhs => [qw/b/] },
70 { lhs => 'Term', rhs => [qw/c/] },
71 { lhs => 'Term', rhs => [qw/d/] },
72 ],
73 }
74 );
75
76 $grammar->precompute();
77
78 my $test_name;
79 my $trace;
80 my $expected_trace;
81 my $memory;
82 my $recce;
83
84 # First test that duplicates are Detected
85 $test_name = 'duplicate terminal 1';
86 $trace = q{};
87 ## no critic (InputOutput::RequireBriefOpen)
88 open $memory, q{>}, \$trace;
89 $recce = Marpa::R2::Recognizer->new(
90 { grammar => $grammar,
91 trace_terminals => 1,
92 trace_file_handle => $memory
93 }
94 );
95
96 sub duplicate_terminal_1 {
97
98 # Marpa::R2::Display
99 # name: Recognizer alternative Synopsis
100
101 $recce->alternative( 'a', \42, 1 ) or return 'First alternative failed';
102
103 # Marpa::R2::Display::End
104
105 return 1 if $recce->alternative( 'a', \711, 1 );
106 return;
107 } ## end sub duplicate_terminal_1
108
109 catch_problem( $test_name, \&duplicate_terminal_1, undef,
110 q{Duplicate token});
111
112 $expected_trace = q{Accepted "a" at 0-1};
113 if ( index( $trace, $expected_trace ) < 0 ) {
114 my $diag_message =
115 "Failed to get expected trace result, was expecting:\n";
116 $diag_message .= $expected_trace;
117 $diag_message .= "This were the traces actually received:\n";
118 my $temp = $trace;
119 chomp $temp;
120 $diag_message .= "$temp\n";
121 Test::More::diag($diag_message);
122 Test::More::fail("Trace messages are wrong: $test_name");
123 } ## end if ( index( $trace, $expected_trace ) < 0 )
124 else {
125 Test::More::pass("Tracing OK: $test_name");
126 }
127
128 # 2nd test that duplicates are Detected
129 $test_name = 'duplicate terminal 2';
130 $trace = q{};
131 close $memory;
132 open $memory, q{>}, \$trace;
133 $recce = Marpa::R2::Recognizer->new(
134 { grammar => $grammar,
135 trace_terminals => 1,
136 trace_file_handle => $memory
137 }
138 );
139
140 sub duplicate_terminal_2 {
141
142 # Should be OK, because different symbols
143 $recce->alternative( 'a', \11, 1 )
144 or return 'alternative a at 0 failed';
145 $recce->alternative( 'b', \12, 1 )
146 or return 'alternative b at 0 failed';
147
148 # Marpa::R2::Display
149 # name: Recognizer earleme_complete Synopsis
150
151 $recce->earleme_complete();
152
153 # Marpa::R2::Display::End
154
155 # Should be OK, because different lengths
156 $recce->alternative( 'a', \21, 3 )
157 or return 'alternative a at 1 failed';
158 $recce->alternative( 'a', \22, 1 )
159 or return 'alternative b at 1 failed';
160 $recce->earleme_complete();
161 $recce->alternative( 'd', \42, 2 )
162 or return 'first alternative d at 2 failed';
163 $recce->alternative( 'b', \22, 1 )
164 or return 'alternative b at 1 failed';
165
166 # this should cause an abend -- a 2nd d, with the same length
167 return 1 if $recce->alternative( 'd', \711, 2 );
168 return;
169 } ## end sub duplicate_terminal_2
170
171 catch_problem( $test_name, \&duplicate_terminal_2, undef,
172 q{Duplicate token});
173
174 $expected_trace = <<'EOS';
175 Setting trace_terminals option
176 Accepted "a" at 0-1
177 Accepted "b" at 0-1
178 Accepted "a" at 1-4
179 Accepted "a" at 1-2
180 Accepted "d" at 2-4
181 Accepted "b" at 2-3
182 EOS
183
184 if ( index( $trace, $expected_trace ) < 0 ) {
185 my $diag_message =
186 "Failed to get expected trace result, was expecting:\n";
187 $diag_message .= $expected_trace;
188 $diag_message .= "This were the traces actually received:\n";
189 my $temp = $trace;
190 chomp $temp;
191 $diag_message .= "$temp\n";
192 Test::More::diag($diag_message);
193 Test::More::fail("Trace messages are wrong: $test_name");
194 } ## end if ( index( $trace, $expected_trace ) < 0 )
195 else {
196 Test::More::pass("Tracing OK: $test_name");
197 }
198
199 1; # In case used as "do" file
200
201 # Local Variables:
202 # mode: cperl
203 # cperl-indent-level: 4
204 # fill-column: 100
205 # End:
206 # vim: expandtab shiftwidth=4: