Debian Codegraph @

t/sequence.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 # Basic tests of sequences.
18 # The matrix is separation (none/perl5/proper);
19 # and minimium count (0, 1, 3);
20 # keep vs. no-keep.
21
22 use 5.010001;
23 use strict;
24 use warnings;
25
26 use Test::More tests => 70;
27 use lib 'inc';
28 use Marpa::R2::Test;
29 use Marpa::R2;
30
31 ## no critic (Subroutines::RequireArgUnpacking)
32
33 sub default_action {
34 shift;
35 my $v_count = scalar @_;
36 return q{} if $v_count <= 0;
37 return $_[0] if $v_count == 1;
38 return '(' . join( q{;}, @_ ) . ')';
39 } ## end sub default_action
40
41 ## use critic
42
43 sub run_sequence_test {
44 my ( $minimum, $separation, $keep ) = @_;
45
46 my @terminals = ('A');
47 my @separation_args = ();
48 if ( $separation ne 'none' ) {
49 push @separation_args, separator => 'sep';
50 push @terminals, 'sep';
51 }
52 if ( $separation eq 'proper' ) {
53 push @separation_args, proper => 1;
54 }
55
56 my $grammar = Marpa::R2::Grammar->new(
57 { start => 'TOP',
58 rules => [
59 { lhs => 'TOP',
60 rhs => [qw/A/],
61 min => $minimum,
62 keep => $keep,
63 @separation_args
64 },
65 ],
66 default_action => 'main::default_action',
67 }
68 );
69
70 $grammar->set( { terminals => \@terminals } );
71
72 $grammar->precompute();
73
74 # Number of symbols to test at the higher numbers is
75 # more or less arbitrary. You really need to test 0 .. 3.
76 # And you ought to test a couple of higher values,
77 # say 5 and 10.
78 SYMBOL_COUNT: for my $symbol_count ( 0, 1, 2, 3, 5, 10 ) {
79
80 next SYMBOL_COUNT if $symbol_count < $minimum;
81 my $test_name =
82 "min=$minimum;"
83 . ( $keep ? 'keep;' : q{} )
84 . ( $separation ne 'none' ? "$separation;" : q{} )
85 . ";count=$symbol_count";
86 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
87
88 my @expected = ();
89 my $last_symbol_ix = $symbol_count - 1;
90 SYMBOL_IX: for my $symbol_ix ( 0 .. $last_symbol_ix ) {
91 push @expected, 'a';
92 $recce->read( 'A', 'a' )
93 and $recce->exhausted()
94 and die 'Parsing exhausted';
95 next SYMBOL_IX if $separation eq 'none';
96 next SYMBOL_IX
97 if $symbol_ix >= $last_symbol_ix
98 and $separation ne 'perl5';
99 if ($keep) {
100 push @expected, q{!};
101 }
102 $recce->read( 'sep', q{!} )
103 and $recce->exhausted()
104 and die 'Parsing exhausted';
105 } ## end for my $symbol_ix ( 0 .. $last_symbol_ix )
106
107 $recce->end_input();
108
109 my $value_ref = $recce->value();
110 my $value = $value_ref ? ${$value_ref} : 'No parse';
111
112 my $expected = join q{;}, @expected;
113 if ( @expected > 1 ) {
114 $expected = "($expected)";
115 }
116 Test::More::is( $value, $expected, $test_name );
117
118 } ## end for my $symbol_count ( 0, 1, 2, 3, 5, 10 )
119
120 return;
121 } ## end sub run_sequence_test
122
123 for my $minimum ( 0, 1, 3 ) {
124 run_sequence_test( $minimum, 'none', 0 );
125 for my $separation (qw(proper perl5)) {
126 for my $keep ( 0, 1 ) {
127 run_sequence_test( $minimum, $separation, $keep );
128 }
129 }
130 } ## end for my $minimum ( 0, 1, 3 )
131
132 1; # In case used as "do" file
133
134 # Local Variables:
135 # mode: cperl
136 # cperl-indent-level: 4
137 # fill-column: 100
138 # End:
139 # vim: expandtab shiftwidth=4: