/
input.t
282 lines (247 loc) · 8.11 KB
/
input.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl
# bc.t - test for script bc
# Author: Gary Puckering, gary.puckering@outlook.com
# Date written: 2023-06-16
# Test cases based on the GNU bc documentation
# See https://www.gnu.org/software/bc/manual/text/bc.txt
use strict;
use warnings;
use Test::More;
use File::Temp qw/tempfile/;
require './t/lib/common.pl';
# we expect to run this from the PerlPowerTools directory
my $Script = program_name();
run_tests();
sub run_tests {
sanity_test($Script);
my @tables = (
operator_table(),
precedence_table(),
special_expr_table(),
statement_table(),
);
run_table($_) foreach @tables;
return;
}
done_testing();
sub operator_table {
my $table = [
'operators',
[ '-1', '-1', 'negation' ],
[ 'var=12', undef, 'variable assignment' ],
[ 'var=12;var', '12', 'variable assignment and print' ],
[ 'v=3; ++v', undef, 'prefix increment' ],
[ 'v=3; --v', undef, 'prefix increment' ],
[ 'v=3; v++; v', "4", 'postfix increment and print' ],
[ 'v=3; v--; v', "2", 'postfix increment and print' ],
[ 'v=3; v+=5; v', "8", 'postfix increment and print' ],
[ 'v=5; v-=3; v', "2", 'postfix decrement and print' ],
[ '1+2', '3', 'addition' ],
[ '5-3', '2', 'subtraction' ],
[ '3*5', '15', 'multiplication' ],
[ '15/3', '5', 'division' ],
[ '15%3', '0', 'remainder zero' ],
[ '29%5', '4', 'remainder four' ],
[ '2^15', '32768', 'exponentiation' ],
[ '3*2+5', '11', 'expr without parens' ],
[ '3*(2+5)', '21', 'expr with parens' ],
[ 'sqrt(16)', '4', 'sqrt' ],
[ '!0', '1', 'boolean not' ],
[ '!1', '0', 'boolean not' ],
[ '1 && 1', '1', 'boolean and' ],
[ '1 && 0', '0', 'boolean and' ],
[ '0 && 1', '0', 'boolean and' ],
[ '0 && 0', '0', 'boolean and' ],
[ '1 || 1', '1', 'boolean or' ],
[ '1 || 0', '1', 'boolean or' ],
[ '0 || 1', '1', 'boolean or' ],
[ '0 || 0', '0', 'boolean or' ],
[ '9 == 9', '1', 'equals (true)' ],
[ '9 == 8', '0', 'equals (false)' ],
[ '9 != 8', '1', 'not equals (true)' ],
[ '9 != 9', '0', 'not equals (false)' ],
[ '5 < 7', '1', 'less than' ],
[ '5 <= 5', '1', 'less than or equal' ],
[ '7 > 5', '1', 'greater than' ],
[ '7 >= 7', '1', 'greater than or equal' ],
[ '7 >= 7', '1', 'greater than or equal' ],
[
"x = 3\nx *= 4\nx",
"12",
'binary assignment (multiplication)'
],
[
"x = 16\nx /= 4\nx",
"4",
'binary assignment (division)'
],
[
"x = 16\nx /= 0.5\nx",
"32",
'binary assignment (division by fraction)',
],
[
"x = 16\nx /= 0\nx",
"16",
'binary assignment (division)',
'Runtime error (func=(main), adr=4): Divide by zero'
],
[
"x = 56\nx %= 17\nx",
"5",
'binary assignment (modulo)'
],
[
"x = 56\nx %= 0.5\nx",
"56",
'binary assignment (modulo fraction 0.5)',
'Runtime error (func=(main), adr=4): Modulo by zero',
],
[
"x = 56\nx %= 1.9\nx",
"56",
'binary assignment (modulo fraction 1.9)',
'Runtime error (func=(main), adr=4): Modulo by zero',
'Need to investigate fractional modulo'
],
[
"x = 56\nx %= 0\nx",
"56",
'binary assignment (modulo zero)',
'Runtime error (func=(main), adr=4): Modulo by zero',
],
[
"x = 56\nx %= -2\nx",
"56",
'binary assignment (modulo negative number)',
'Runtime error (func=(main), adr=4): Modulo by zero',
'todo'
],
[
"x = 5\nx ^= 4\nx",
"625",
'binary assignment (exponentiation)'
],
[
"x = 5\nx ^= 0\nx",
"1",
'binary assignment (exponentiation - 0 power)'
],
[
"x = 5\nx ^= -1\nx",
"0.2",
'binary assignment (exponentiation - negative power)'
],
[
"x = 5\nx ^= 0.5\nx",
qr/2\.23606/,
'binary assignment (exponentiation - fractional power)'
],
[
"x = 5\ny = 2\nx ^= y\nx",
"25",
'binary assignment (exponentiation - two variables)'
],
[
"x = -1\ny = 0.5\nx ^= y\nx",
'NaN', # NaN might be -nan
'binary assignment (exponentiation - square root of -1)'
],
];
}
sub precedence_table {
my $table = [
'precedence',
[ "v = (3 < 5)\nv", '1', 'just like gnu bc' ],
# according to the GNU bc documentation, their implementation
# will assign 3 to v and then do the relational test
[ "v = 3 < 5\nv", '1', ' not like gnu bc' ],
[ "v = 4+5*2^3\nv", '44', 'PEDMAS' ],
[ "v = (4+5)*2\nv", '18', 'PEDMAS' ],
[ "v = -(4+5)+8\nv", '-1', 'PEDMAS' ],
];
}
sub special_expr_table {
# 1.41421 35623 73095 04880 16887 24209 7 USE_QUADMATH
# 1.41421 35623 73095 04 USE_LONG_DOUBLE
# 1.41421 35623 731 -
my $perl_sqrt_2 = sqrt(2);
diag( "Perl sqrt(2) is <$perl_sqrt_2>" );
my $table = [
'special expressions',
[ 'sqrt(2)', $perl_sqrt_2, 'sqrt(2) returns appropriate value' ],
[ 'length(sqrt(2))', length($perl_sqrt_2) - 1, 'length(sqrt(2)) has the expected value' ],
[ 'scale(sqrt(2))', length($perl_sqrt_2) - 2, 'precision digits' ],
];
}
sub statement_table {
## no critic [ValuesAndExpressions::RequireInterpolationOfMetachars]
my $table = [
'statements',
[
q("a string"),
'a string',
'string literal',
],
[
# need to print the newline here so that the test harness
# recognizes the end of the test output
'print "1+2", " is ", 1+2, "\n"',
'1+2 is 3',
'print statement',
],
[
'{ print "a"; print "b"; print "\n" }',
'ab',
'compound statement',
],
[
'v=5; while (v--) { print v }; print "\n"',
"43210",
'while statement',
],
[
'for (v=0;v<5;v++) { print v }; print "\n"',
"01234",
'for statement',
],
[
'for ( v=0; v<5; v++) { print v; if (v>2) break }; print "\n"',
"0123\n",
'for with break statement',
],
# The following aren't being tested because they are extensions
# that aren't support in the PerlPowerTools implementation of bc
# - read()
# - if ... else
# - continue
# - halt
# - warranty
# - define (because I can't even get it to work interactively)
# Not testing this, for obvious reasons
# - quit
];
}
sub run_table {
my( $table ) = @_;
my $label = shift @$table;
subtest $label => sub {
foreach my $tuple (@$table) {
my( $input, $expected, $description, $error, $todo ) = @$tuple;
$expected .= "\n" unless( ! defined $expected or ref $expected );
my ( $fh, $temp_filename ) = tempfile();
print {$fh} $input, "\n";
my $output = `"$^X" $Script $temp_filename`;
TODO: {
local $TODO = $todo;
if( ! ref $expected ) {
is $output, $expected // '', $description;
}
elsif( ref $expected eq ref qr// ) {
like $output, $expected, $description;
}
}
}
};
return;
}