blob: 0a8222be62b547b2967daa3973b3b68a4bb04cb8 [file] [log] [blame]
Denys Vlasenko9a23b072018-12-12 21:41:40 +01001#!/bin/sh
2# Copyright 2018 by Denys Vlasenko
3# Licensed under GPLv2 or later, see file LICENSE in this source tree.
4
5. ./testing.sh
6
Denys Vlasenko766f6722018-12-13 17:23:24 +01007# testing "test name" "command" "expected result" "file input" "stdin"
8
Denys Vlasenko94576d22018-12-25 23:45:57 +01009testing "bc comment" \
Denys Vlasenko766f6722018-12-13 17:23:24 +010010 "bc" \
11 "3\n" \
12 "" "1 /* comment */ + 2"
13
Denys Vlasenko94576d22018-12-25 23:45:57 +010014testing "bc /*/ is not a closed comment" \
Denys Vlasenko766f6722018-12-13 17:23:24 +010015 "bc" \
16 "4\n" \
17 "" "1 /*/ + 2 */ + 3"
18
Denys Vlasenko94576d22018-12-25 23:45:57 +010019# this needs interactive testing
20testing "bc comment with \"" \
21 "bc" \
22 "3\n" \
23 "" "1 /* \" */ + 2"
24
25# this needs interactive testing
26testing "bc \"string/*\" is not a comment" \
27 "bc" \
28 "string/*9\n" \
29 "" "\"string/*\";9"
30
Denys Vlasenko55f3cab2018-12-18 14:37:16 +010031testing "bc comment 3: unterminated #comment" \
32 "bc" \
33 "" \
34 "" "#foo" # no trailing newline
35
Denys Vlasenko818b6022018-12-13 17:56:35 +010036testing "bc backslash 1" \
37 "bc" \
38 "3\n" \
39 "" "1 \\\\\n + 2"
40
41testing "bc string 1" \
42 "bc" \
43 "STR\n" \
44 "" "\"STR\n\""
45
Denys Vlasenko8c1e7232018-12-22 01:34:10 +010046testing "bc read() 4<EOF>" \
47 "bc input" \
48 "4\n" \
49 "read();halt" "4"
50
51testing "bc read()^2" \
52 "bc input" \
53 "16\n" \
54 "read()^2;halt" "4\n"
55
56testing "bc read()*read()" \
57 "bc input" \
58 "20\n" \
59 "read()*read();halt" "4\n5"
60
Denys Vlasenko99b37622018-12-15 20:06:59 +010061testing "bc if 0 else" \
62 "bc" \
63 "2\n9\n" \
64 "" "if (0) 1 else 2; 9"
65
66testing "bc if 1 else" \
67 "bc" \
68 "1\n9\n" \
69 "" "if (1) 1 else 2; 9"
70
71testing "bc if 1 if 1 else else" \
72 "bc" \
73 "1\n9\n" \
74 "" "if (1) if (1) 1 else 2 else 3; 9"
75
76testing "bc if 0 else if 1" \
77 "bc" \
78 "2\n9\n" \
79 "" "if (0) 1 else if (1) 2; 9"
80
Denys Vlasenko5fa74b92018-12-25 17:07:51 +010081testing "bc for (;;)" \
Denys Vlasenko52caa002018-12-21 00:35:22 +010082 "bc" \
83 "2\n3\n2\n9\n" \
84 "" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9"
85
Denys Vlasenko19eee8e2018-12-21 20:29:34 +010086testing "bc for (;cond;)" \
87 "bc" \
88 "1\n2\n3\n9\n" \
89 "" "i=0; for(;i<3;)++i; 9"
90
91testing "bc for (;cond;upd)" \
92 "bc" \
93 "1\n2\n3\n9\n" \
94 "" "i=1; for(;i<4;i++)i; 9"
95
96testing "bc for (init;cond;upd)" \
97 "bc" \
98 "1\n2\n3\n9\n" \
99 "" "for(i=1;i<4;i++)i; 9"
100
Denys Vlasenko5fa74b92018-12-25 17:07:51 +0100101testing "bc for (;;) {break}" \
102 "bc" \
103 "2\n9\n" \
104 "" "for (;;) {2;break}; 9"
105
Denys Vlasenko96b5ec12019-01-03 23:34:36 +0100106testing "bc define {return}" \
107 "bc" \
108 "0\n9\n" \
109 "" "define w() {return}\nw();9"
110
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100111testing "bc define auto" \
112 "bc" \
113 "8\n9\n" \
114 "" "define w() { auto z; return 8; }; w(); 9"
115
Denys Vlasenko22314682019-01-01 21:50:14 +0100116testing "bc define auto array same name" \
117 "bc" \
118 "8\n9\n" \
119 "" "define w(x) { auto x[]; return x; }; w(8); 9"
120
Denys Vlasenkoe9519e42018-12-16 17:06:07 +0100121testing "bc define with body on next line" \
122 "bc" \
123 "8\n9\n" \
124 "" "define w()\n{ auto z; return 8; }\nw()\n9"
125
Denys Vlasenko202dd192018-12-16 17:30:35 +0100126testing "bc if(cond)<NL>" \
127 "bc" \
128 "9\n" \
129 "" "if(0)\n3\n9"
130
Denys Vlasenkoa50576a2018-12-16 19:21:57 +0100131testing "bc if(cond) stmt else<NL>" \
132 "bc" \
133 "4\n9\n" \
134 "" "if(0)3 else\n4\n9"
135
Denys Vlasenko202dd192018-12-16 17:30:35 +0100136testing "bc while(cond)<NL>" \
137 "bc" \
138 "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \
139 "" "i=9;while(--i)\ni\n9"
140
Denys Vlasenko5acd14b2018-12-20 16:48:50 +0100141testing "bc ifz does not match if keyword" \
142 "bc" \
143 "1\n2\n2\n3\n" \
144 "" "ifz=1;ifz\n++ifz;ifz++\nifz"
145
Denys Vlasenko22314682019-01-01 21:50:14 +0100146# had parse error on "f()-N"
147testing "bc -l 'e(0)-2'" \
148 "bc -l" \
149 "-1.00000000000000000000\n" \
150 "" "e(0)-2"
151
152testing "bc (!a&&b)" \
153 "bc" \
154 "0\n" \
155 "" "(!a&&b)"
156
Denys Vlasenko266bec82019-01-02 05:03:53 +0100157# check that dc code is not messing this up (no NUL printing!)
158testing "bc print \"\"" \
159 "bc" \
160 "" \
161 "" "print \"\""
162
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +0100163testing "bc print 1,2,3" \
164 "bc" \
165 "123" \
166 "" "print 1,2,3"
167
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100168testing "bc { print 1 }" \
169 "bc" \
170 "1" \
171 "" "{ print 1 }"
172
Denys Vlasenko06ade772018-12-16 22:44:51 +0100173testing "bc nested loops and breaks" \
174 "bc" \
175 "\
17611
17721
17831
17922
18012
18199
182" \
183 "" "\
184if(1) {
185 11
186 while(1) {
187 21
188 while(1) {
189 31
190 break
191 32
192 }
193 22
194 break
195 23
196 }
197 12
198} else {
199 88
200}
20199
202"
203
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100204testing "bc continue in if" \
205 "bc" \
206 "\
20711
20821
20911
21031
21199
212" \
213 "" "\
214i=2
215while(i--) {
Denys Vlasenkocb7c9552019-01-02 14:00:20 +0100216 11
217 if(i) {
218 21
219 continue
220 22
221 } else {
222 31
223 continue
224 32
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100225 }
Denys Vlasenkocb7c9552019-01-02 14:00:20 +0100226 12
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100227}
22899
229"
230
Denys Vlasenko266aa002018-12-16 23:24:25 +0100231testing "bc continue in for" \
232 "bc" \
233 "\
2341
23577
2362
23799
238" \
239 "" "\
240for(i=1; i<3; i++) {
241 i
242 if(i==2) continue
243 77
244}
24599
246"
247
Denys Vlasenko680ccd32018-12-31 19:42:13 +0100248testing "bc ibase" \
249 "bc" \
250 "99\n1295\n1224\n" \
251 "" "a=ZZ;a;ibase=36;a=ZZ;a;ibase=Z;a=ZZ;a"
252
Denys Vlasenkocb7c9552019-01-02 14:00:20 +0100253testing "bc parsing of numbers" \
254 "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
255 "465d8c01308d0863b6f5669e8a1c69fb -\n" \
256 "" '
257for (b = 2; b <= 16; ++b) {
258 if (b == 10) continue
259 obase = 10
260 print "ibase = A; ibase = ", b, "\n"
261 obase = b
262 for (i = 0; i <= 65536; ++i) {
263 i
264 print "0.", i, "\n"
265 print "1.", i, "\n"
266 print i, ".", i, "\n"
267 }
268}'
269
270testing "bc printing of numbers" \
271 "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
272 "d884b35d251ca096410712743aeafb9e -\n" \
273 "" '
274for (b = 2; b <= 101; ++b) {
275 if (b == 10) continue
276 s = b * b
277 print "obase = ", b, "\n"
278 for (i = 0; i <= s; ++i) {
279 i
280 print "0.", i, "\n"
281 print "1.", i, "\n"
282 print i, ".", i, "\n"
283 }
284 2189432174861923048671023498128347619023487610234689172304.192748960128745108927461089237469018723460
285}'
Denys Vlasenko9a23b072018-12-12 21:41:40 +0100286
287for f in bc*.bc; do
288 r="`basename "$f" .bc`_results.txt"
289 test -f "$r" || continue
290 # testing "test name" "command" "expected result" "file input" "stdin"
291 testing "bc -lq $f" \
292 "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
293 "E:0\nE:0\n" \
294 "" ""
295done
296
297exit $FAILCOUNT