]> git.saurik.com Git - bison.git/blob - tests/torture.at
* src/muscle_tab.h (MUSCLE_INSERT_LONG_INT): New.
[bison.git] / tests / torture.at
1 # Torturing Bison. -*- Autotest -*-
2 # Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 # 02111-1307, USA.
18
19 AT_BANNER([[Torture Tests.]])
20
21
22 ## ------------------------------------- ##
23 ## Creating a large artificial grammar. ##
24 ## ------------------------------------- ##
25
26 # AT_DATA_TRIANGULAR_GRAMMAR(FILE-NAME, SIZE)
27 # -------------------------------------------
28 # Create FILE-NAME, containing a self checking parser for a huge
29 # triangular grammar.
30 # FIXME: The `10 *' below are there to avoid clashes with predefined
31 # tokens. These clashes should be exercised, I'm afraid something
32 # is broken wrt previous Bisons.
33 m4_define([AT_DATA_TRIANGULAR_GRAMMAR],
34 [AT_DATA([[gengram.pl]],
35 [[#! /usr/bin/perl -w
36
37 use strict;
38 my $max = $ARGV[0] || 10;
39
40 print <<EOF;
41 %{
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <assert.h>
45
46 #define YYERROR_VERBOSE 1
47 #define YYDEBUG 1
48
49 static int yylex (void);
50 static void yyerror (const char *msg);
51 %}
52 %union
53 {
54 int val;
55 };
56
57 %token END "end"
58 %type <val> exp input
59 EOF
60
61 for my $size (1 .. $max)
62 {
63 print "%token \"$size\" ", $size * 10, "\n";
64 };
65
66 print <<EOF;
67 %%
68 input:
69 exp { assert (\@S|@1 == 0); \$\$ = \@S|@1; }
70 | input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; }
71 ;
72
73 exp:
74 END
75 { \$\$ = 0; }
76 EOF
77
78 for my $size (1 .. $max)
79 {
80 use Text::Wrap;
81 print wrap ("| ", " ",
82 (map { "\"$_\"" } (1 .. $size)),
83 " END \n"),
84 " { \$\$ = $size; }\n";
85 };
86 print ";\n";
87
88 print <<EOF;
89 %%
90 static int
91 yylex (void)
92 {
93 static int inner = 1;
94 static int outer = 0;
95 if (outer > $max)
96 return 0;
97 else if (inner > outer)
98 {
99 inner = 1;
100 ++outer;
101 return END;
102 }
103 return inner++ * 10;
104 }
105
106 static void
107 yyerror (const char *msg)
108 {
109 fprintf (stderr, "%s\\n", msg);
110 }
111
112 int
113 main (void)
114 {
115 yydebug = !!getenv ("YYDEBUG");
116 return yyparse ();
117 }
118 EOF
119 ]])
120
121 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
122 mv stdout $1
123 ])
124
125
126 ## -------------- ##
127 ## Big triangle. ##
128 ## -------------- ##
129
130 AT_SETUP([Big triangle])
131
132 AT_DATA_TRIANGULAR_GRAMMAR([input.y], [253])
133 AT_CHECK([bison input.y -v -o input.c])
134 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
135 AT_CHECK([./input])
136
137 AT_CLEANUP
138
139
140
141 # AT_DATA_STACK_TORTURE(C-PROLOGUE)
142 # ---------------------------------
143 # A parser specialized in torturing the stack size.
144 m4_define([AT_DATA_STACK_TORTURE],
145 [# A grammar of parens growing the stack thanks to right recursion.
146 # exp:
147 AT_DATA([input.y],
148 [[%{
149 #include <stdio.h>
150 #include <stdlib.h>
151 #include <assert.h>
152 ]$1[
153 static int yylex (void);
154 static void yyerror (const char *msg);
155 #define YYPRINT(File, Type, Value) \
156 fprintf (File, " (%d, stack size = %d, max = %d)", \
157 Value, yyssp - yyss + 1, yystacksize);
158 %}
159 %error-verbose
160 %debug
161 %token WAIT_FOR_EOF
162 %%
163 exp: WAIT_FOR_EOF exp | ;
164 %%
165 static void
166 yyerror (const char *msg)
167 {
168 fprintf (stderr, "%s\n", msg);
169 exit (1);
170 }
171
172 /* There are YYLVAL_MAX of WAIT_FOR_EOFs. */
173 unsigned int yylval_max;
174
175 static int
176 yylex (void)
177 {
178 if (yylval--)
179 return WAIT_FOR_EOF;
180 else
181 return EOF;
182 }
183
184 int
185 main (int argc, const char **argv)
186 {
187 assert (argc == 2);
188 yylval = atoi (argv[1]);
189 yydebug = 1;
190 return yyparse ();
191 }
192 ]])
193 AT_CHECK([bison input.y -o input.c])
194 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
195 ])
196
197
198 ## -------------------------------------- ##
199 ## Exploding the Stack Size with Alloca. ##
200 ## -------------------------------------- ##
201
202 AT_SETUP([Exploding the Stack Size with Alloca])
203
204 AT_DATA_STACK_TORTURE
205
206 # Below the limit of 200.
207 AT_CHECK([./input 20], 0, [], [ignore])
208 # Two enlargements: 2 * 2 * 200.
209 AT_CHECK([./input 900], 0, [], [ignore])
210 # Fails: beyond the limit of 10,000 (which we don't reach anyway since we
211 # multiply by two starting at 200 => 5120 is the last possible).
212 AT_CHECK([./input 10000], 1, [], [ignore])
213
214 AT_CLEANUP
215
216
217
218
219 ## -------------------------------------- ##
220 ## Exploding the Stack Size with Malloc. ##
221 ## -------------------------------------- ##
222
223 AT_SETUP([Exploding the Stack Size with Malloc])
224
225 AT_DATA_STACK_TORTURE([[#define YYSTACK_USE_ALLOCA 0]])
226
227 # Below the limit of 200.
228 AT_CHECK([./input 20], 0, [], [ignore])
229 # Two enlargements: 2 * 2 * 200.
230 AT_CHECK([./input 900], 0, [], [ignore])
231 # Fails: beyond the limit of 10,000 (which we don't reach anyway since we
232 # multiply by two starting at 200 => 5120 is the possible).
233 AT_CHECK([./input 10000], 1, [], [ignore])
234
235 AT_CLEANUP