build: ensure make-prime-list doesn't access out of bounds memory
[platform/upstream/coreutils.git] / tests / misc / expr.pl
1 #!/usr/bin/perl
2 # Basic tests for "expr".
3
4 # Copyright (C) 2001-2013 Free Software Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 use strict;
20
21 (my $program_name = $0) =~ s|.*/||;
22 my $prog = 'expr';
23
24 # Turn off localization of executable's output.
25 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
26
27 my $big =      '98782897298723498732987928734';
28 my $big_p1 =   '98782897298723498732987928735';
29 my $big_sum = '197565794597446997465975857469';
30 my $big_prod = '9758060798730154302876482828124348356960410232492450771490';
31
32 my @Tests =
33     (
34      ['a', '5 + 6', {OUT => '11'}],
35      ['b', '5 - 6', {OUT => '-1'}],
36      ['c', '5 \* 6', {OUT => '30'}],
37      ['d', '100 / 6', {OUT => '16'}],
38      ['e', '100 % 6', {OUT => '4'}],
39      ['f', '3 + -2', {OUT => '1'}],
40      ['g', '-2 + -2', {OUT => '-4'}],
41
42      # Verify option processing.
43      # Added when option processing broke in the 7.0 beta release
44      ['opt1', '-- -11 + 12', {OUT => '1'}],
45      ['opt2', '-11 + 12', {OUT => '1'}],
46      ['opt3', '-- -1 + 2', {OUT => '1'}],
47      ['opt4', '-1 + 2', {OUT => '1'}],
48      # This evoked a syntax error diagnostic before 2.0.12.
49      ['opt5', '-- 2 + 2', {OUT => '4'}],
50
51      ['paren1', '\( 100 % 6 \)', {OUT => '4'}],
52      ['paren2', '\( 100 % 6 \) - 8', {OUT => '-4'}],
53      ['paren3', '9 / \( 100 % 6 \) - 8', {OUT => '-6'}],
54      ['paren4', '9 / \( \( 100 % 6 \) - 8 \)', {OUT => '-2'}],
55      ['paren5', '9 + \( 100 % 6 \)', {OUT => '13'}],
56
57      # Before 2.0.12, this would output '1'.
58      ['0bang', '00 \< 0!', {OUT => '0'}, {EXIT => 1}],
59
60      # In 5.1.3 and earlier, these would exit with status 0.
61      ['00', '00', {OUT => '00'}, {EXIT => 1}],
62      ['minus0', '-0', {OUT => '-0'}, {EXIT => 1}],
63
64      # In 5.1.3 and earlier, these would report errors.
65      ['andand', '0 \& 1 / 0', {OUT => '0'}, {EXIT => 1}],
66      ['oror', '1 \| 1 / 0', {OUT => '1'}, {EXIT => 0}],
67
68      # In 5.1.3 and earlier, this would output the empty string.
69      ['orempty', '"" \| ""', {OUT => '0'}, {EXIT => 1}],
70
71
72      # This erroneously succeeded and output '3' before 2.0.12.
73      ['fail-a', '3 + -', {ERR => "$prog: non-integer argument\n"},
74       {EXIT => 2}],
75
76      # This erroneously succeeded before 5.3.1.
77      ['bigcmp', '-- -2417851639229258349412352 \< 2417851639229258349412352',
78       {OUT => '1'}, {EXIT => 0}],
79
80      # In 5.94 and earlier, anchors incorrectly matched newlines.
81      ['anchor', "'a\nb' : 'a\$'", {OUT => '0'}, {EXIT => 1}],
82
83      # These tests are taken from grep/tests/bre.tests.
84      ['bre1', '"abc" : "a\\(b\\)c"', {OUT => 'b'}],
85      ['bre2', '"a(" : "a("', {OUT => '2'}],
86      ['bre3', '_ : "a\\("',
87       {ERR => "$prog: Unmatched ( or \\(\n"}, {EXIT => 2}],
88      ['bre4', '_ : "a\\(b"',
89       {ERR => "$prog: Unmatched ( or \\(\n"}, {EXIT => 2}],
90      ['bre5', '"a(b" : "a(b"', {OUT => '3'}],
91      ['bre6', '"a)" : "a)"', {OUT => '2'}],
92      ['bre7', '_ : "a\\)"',
93       {ERR => "$prog: Unmatched ) or \\)\n"}, {EXIT => 2}],
94      ['bre8', '_ : "\\)"',
95       {ERR => "$prog: Unmatched ) or \\)\n"}, {EXIT => 2}],
96      ['bre9', '"ab" : "a\\(\\)b"', {OUT => ''}, {EXIT => 1}],
97      ['bre10', '"a^b" : "a^b"', {OUT => '3'}],
98      ['bre11', '"a\$b" : "a\$b"', {OUT => '3'}],
99      ['bre12', '"" : "\\($\\)\\(^\\)"', {OUT => ''}, {EXIT => 1}],
100      ['bre13', '"b" : "a*\\(^b\$\\)c*"', {OUT => 'b'}],
101      ['bre14', '"X|" : "X\\(|\\)" : "(" "X|" : "X\\(|\\)" ")"', {OUT => '1'}],
102      ['bre15', '"X*" : "X\\(*\\)" : "(" "X*" : "X\\(*\\)" ")"', {OUT => '1'}],
103      ['bre16', '"abc" : "\\(\\)"', {OUT => ''}, {EXIT => 1}],
104      ['bre17', '"{1}a" : "\\(\\{1\\}a\\)"', {OUT => '{1}a'}],
105      ['bre18', '"X*" : "X\\(*\\)" : "^*"', {OUT => '1'}],
106      ['bre19', '"{1}" : "^\\{1\\}"', {OUT => '3'}],
107      ['bre20', '"{" : "{"', {OUT => '1'}],
108      ['bre21', '"abbcbd" : "a\\(b*\\)c\\1d"', {OUT => ''}, {EXIT => 1}],
109      ['bre22', '"abbcbbbd" : "a\\(b*\\)c\\1d"', {OUT => ''}, {EXIT => 1}],
110      ['bre23', '"abc" : "\\(.\\)\\1"', {OUT => ''}, {EXIT => 1}],
111      ['bre24', '"abbccd" : "a\\(\\([bc]\\)\\2\\)*d"', {OUT => 'cc'}],
112      ['bre25', '"abbcbd" : "a\\(\\([bc]\\)\\2\\)*d"',
113       {OUT => ''}, {EXIT => 1}],
114      ['bre26', '"abbbd" : "a\\(\\(b\\)*\\2\\)*d"', {OUT => 'bbb'}],
115      ['bre27', '"aabcd" : "\\(a\\)\\1bcd"', {OUT => 'a'}],
116      ['bre28', '"aabcd" : "\\(a\\)\\1bc*d"', {OUT => 'a'}],
117      ['bre29', '"aabd" : "\\(a\\)\\1bc*d"', {OUT => 'a'}],
118      ['bre30', '"aabcccd" : "\\(a\\)\\1bc*d"', {OUT => 'a'}],
119      ['bre31', '"aabcccd" : "\\(a\\)\\1bc*[ce]d"', {OUT => 'a'}],
120      ['bre32', '"aabcccd" : "\\(a\\)\\1b\\(c\\)*cd\$"', {OUT => 'a'}],
121      ['bre33', '"a*b" : "a\\(*\\)b"', {OUT => '*'}],
122      ['bre34', '"ab" : "a\\(**\\)b"', {OUT => ''}, {EXIT => 1}],
123      ['bre35', '"ab" : "a\\(***\\)b"', {OUT => ''}, {EXIT => 1}],
124      ['bre36', '"*a" : "*a"', {OUT => '2'}],
125      ['bre37', '"a" : "**a"', {OUT => '1'}],
126      ['bre38', '"a" : "***a"', {OUT => '1'}],
127      ['bre39', '"ab" : "a\\{1\\}b"', {OUT => '2'}],
128      ['bre40', '"ab" : "a\\{1,\\}b"', {OUT => '2'}],
129      ['bre41', '"aab" : "a\\{1,2\\}b"', {OUT => '3'}],
130      ['bre42', '_ : "a\\{1"',
131       {ERR => "$prog: Unmatched \\{\n"}, {EXIT => 2}],
132      ['bre43', '_ : "a\\{1a"',
133       {ERR => "$prog: Unmatched \\{\n"}, {EXIT => 2}],
134      ['bre44', '_ : "a\\{1a\\}"',
135       {ERR => "$prog: Invalid content of \\{\\}\n"}, {EXIT => 2}],
136      ['bre45', '"a" : "a\\{,2\\}"', {OUT => '1'}],
137      ['bre46', '"a" : "a\\{,\\}"', {OUT => '1'}],
138      ['bre47', '_ : "a\\{1,x\\}"',
139       {ERR => "$prog: Invalid content of \\{\\}\n"}, {EXIT => 2}],
140      ['bre48', '_ : "a\\{1,x"',
141       {ERR => "$prog: Unmatched \\{\n"}, {EXIT => 2}],
142      ['bre49', '_ : "a\\{32768\\}"',
143       {ERR => "$prog: Invalid content of \\{\\}\n"}, {EXIT => 2},
144       # Map AIX-6's different diagnostic to the one we expect:
145       {ERR_SUBST =>
146        's,Regular expression too big,Invalid content of \\\\{\\\\},'},
147       ],
148      ['bre50', '_ : "a\\{1,0\\}"',
149       {ERR => "$prog: Invalid content of \\{\\}\n"}, {EXIT => 2}],
150      ['bre51', '"acabc" : ".*ab\\{0,0\\}c"', {OUT => '2'}],
151      ['bre52', '"abcac" : "ab\\{0,1\\}c"', {OUT => '3'}],
152      ['bre53', '"abbcac" : "ab\\{0,3\\}c"', {OUT => '4'}],
153      ['bre54', '"abcac" : ".*ab\\{1,1\\}c"', {OUT => '3'}],
154      ['bre55', '"abcac" : ".*ab\\{1,3\\}c"', {OUT => '3'}],
155      ['bre56', '"abbcabc" : ".*ab\{2,2\}c"', {OUT => '4'}],
156      ['bre57', '"abbcabc" : ".*ab\{2,4\}c"', {OUT => '4'}],
157      ['bre58', '"aa" : "a\\{1\\}\\{1\\}"', {OUT => '1'}],
158      ['bre59', '"aa" : "a*\\{1\\}"', {OUT => '2'}],
159      ['bre60', '"aa" : "a\\{1\\}*"', {OUT => '2'}],
160      ['bre61', '"acd" : "a\\(b\\)?c\\1d"', {OUT => ''}, {EXIT => 1}],
161      ['bre62', '-- "-5" : "-\\{0,1\\}[0-9]*\$"', {OUT => '2'}],
162
163      ['fail-b', '9 9', {ERR => "$prog: syntax error\n"},
164       {EXIT => 2}],
165      ['fail-c', {ERR => "$prog: missing operand\n"
166                  . "Try '$prog --help' for more information.\n"},
167       {EXIT => 2}],
168
169      ['bignum-add', "$big + 1", {OUT => $big_p1}],
170      ['bignum-add2', "$big + $big_p1", {OUT => $big_sum}],
171      ['bignum-sub', "$big_p1 - 1", {OUT => $big}],
172      ['bignum-sub2', "$big_sum - $big", {OUT => $big_p1}],
173      ['bignum-mul', "$big_p1 '*' $big", {OUT => $big_prod}],
174      ['bignum-div', "$big_prod / $big", {OUT => $big_p1}],
175     );
176
177 # If using big numbers fails, remove all /^bignum-/ tests
178 qx!expr $big_prod '*' $big_prod '*' $big_prod!
179   or @Tests = grep {$_->[0] !~ /^bignum-/} @Tests;
180
181 # Append a newline to end of each expected 'OUT' string.
182 my $t;
183 foreach $t (@Tests)
184   {
185     my $arg1 = $t->[1];
186     my $e;
187     foreach $e (@$t)
188       {
189         $e->{OUT} .= "\n"
190           if ref $e eq 'HASH' and exists $e->{OUT};
191       }
192   }
193
194 my $save_temps = $ENV{SAVE_TEMPS};
195 my $verbose = $ENV{VERBOSE};
196
197 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
198 exit $fail;