Debian Codegraph @

t/limits.t

raw
1 #!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 use English qw( -no_match_vars );
21 use Test::More tests => 3;
22 use Fatal qw(open close);
23
24 use lib 'inc';
25 use Marpa::R2::Test;
26 use Marpa::R2;
27
28 ## no critic (Subroutines::RequireArgUnpacking)
29
30 sub default_action {
31 shift;
32 my $v_count = scalar @_;
33 return q{} if $v_count <= 0;
34 return $_[0] if $v_count == 1;
35 return '(' . join( q{;}, @_ ) . ')';
36 } ## end sub default_action
37
38 ## use critic
39
40 sub test_grammar {
41 my ( $grammar_args, $tokens ) = @_;
42
43 my $grammar;
44 my $eval_ok =
45 eval { $grammar = Marpa::R2::Grammar->new($grammar_args); 1; };
46 die "Exception while creating Grammar:\n$EVAL_ERROR"
47 if not $eval_ok;
48 die "Grammar not created\n" if not $grammar;
49 $grammar->precompute();
50
51 my $recce;
52 $eval_ok = eval {
53 $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
54 1;
55 };
56
57 die "Exception while creating Recognizer:\n$EVAL_ERROR"
58 if not $eval_ok;
59 die "Recognizer not created\n" if not $recce;
60
61 for my $token ( @{$tokens} ) {
62 my $earleme_result;
63 $eval_ok = eval {
64 $recce->alternative( @{$token} );
65 $earleme_result = $recce->earleme_complete();
66 1;
67 };
68 die "Exception while recognizing earleme:\n$EVAL_ERROR"
69 if not $eval_ok;
70 die "Parsing exhausted\n"
71 if not defined $earleme_result;
72 } ## end for my $token ( @{$tokens} )
73
74 $eval_ok = eval { $recce->end_input(); 1; };
75 die "Exception while recognizing end of input:\n$EVAL_ERROR"
76 if not $eval_ok;
77
78 my $value_ref = $recce->value();
79 die "No parse\n" if not $value_ref;
80 return ${$value_ref};
81 } ## end sub test_grammar
82
83 # RHS too long is not testable
84 # Perl runs out of memory first
85
86 # test a grammar with no limit problems
87 my $result_on_success = '(a;a)';
88
89 my $placebo = {
90 start => 'S',
91 rules => [
92 #<<< no perltidy
93 [ 'S', [ qw(A A) ] ],
94 [ 'A', [qw/a/] ]
95 #>>>
96 ],
97 default_action => 'main::default_action',
98 };
99
100 sub gen_tokens {
101 my ($earleme_length) = @_;
102 return [ [ 'a', \'a', 1 ], [ 'a', \'a', $earleme_length ] ];
103 }
104
105 my $value;
106 my $eval_ok = eval { $value = test_grammar( $placebo, gen_tokens(1) ); 1; };
107 if ( not defined $eval_ok ) {
108 Test::More::diag($EVAL_ERROR);
109 Test::More::fail('Placebo grammar');
110 }
111 else { Test::More::is( $value, $result_on_success, 'Placebo grammar' ) }
112
113 ## lots of test values in the following, some of them pretty
114 ## arbitrary
115
116 $eval_ok = eval { $value = test_grammar( $placebo, gen_tokens(20_031) ); 1; };
117 if ( not defined $eval_ok ) { Test::More::fail('Earleme very long') }
118 else {
119 Test::More::is( $value, $result_on_success,
120 'Earleme very long, but still OK' );
121 }
122
123 $eval_ok =
124 eval { $value = test_grammar( $placebo, gen_tokens( 2**31 ) ); 1; };
125 REPORT_RESULT: {
126 if ( defined $eval_ok ) {
127 Test::More::diag("Earleme too long test returned value: $value");
128 Test::More::fail('Did not catch problem with earleme too long');
129 last REPORT_RESULT;
130 }
131 if ( $EVAL_ERROR
132 =~ / \A Exception \s while \s recognizing \s earleme /xms )
133 {
134 Test::More::pass('Caught over-long earleme');
135 last REPORT_RESULT;
136 } ## end if ( $EVAL_ERROR =~ ...)
137 Test::More::is( $EVAL_ERROR, q{}, 'Grammar with earleme too long' );
138 } ## end REPORT_RESULT:
139
140 1; # In case used as "do" file
141
142 # Local Variables:
143 # mode: cperl
144 # cperl-indent-level: 4
145 # fill-column: 100
146 # End:
147 # vim: expandtab shiftwidth=4: