blob: 7795183a787ab0115af0c6049fb7488d12134ee1 [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 Vlasenkod1d29b42018-12-16 16:03:03 +0100106testing "bc define auto" \
107 "bc" \
108 "8\n9\n" \
109 "" "define w() { auto z; return 8; }; w(); 9"
110
Denys Vlasenko22314682019-01-01 21:50:14 +0100111testing "bc define auto array same name" \
112 "bc" \
113 "8\n9\n" \
114 "" "define w(x) { auto x[]; return x; }; w(8); 9"
115
Denys Vlasenkoe9519e42018-12-16 17:06:07 +0100116testing "bc define with body on next line" \
117 "bc" \
118 "8\n9\n" \
119 "" "define w()\n{ auto z; return 8; }\nw()\n9"
120
Denys Vlasenko202dd192018-12-16 17:30:35 +0100121testing "bc if(cond)<NL>" \
122 "bc" \
123 "9\n" \
124 "" "if(0)\n3\n9"
125
Denys Vlasenkoa50576a2018-12-16 19:21:57 +0100126testing "bc if(cond) stmt else<NL>" \
127 "bc" \
128 "4\n9\n" \
129 "" "if(0)3 else\n4\n9"
130
Denys Vlasenko202dd192018-12-16 17:30:35 +0100131testing "bc while(cond)<NL>" \
132 "bc" \
133 "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \
134 "" "i=9;while(--i)\ni\n9"
135
Denys Vlasenko5acd14b2018-12-20 16:48:50 +0100136testing "bc ifz does not match if keyword" \
137 "bc" \
138 "1\n2\n2\n3\n" \
139 "" "ifz=1;ifz\n++ifz;ifz++\nifz"
140
Denys Vlasenko22314682019-01-01 21:50:14 +0100141# had parse error on "f()-N"
142testing "bc -l 'e(0)-2'" \
143 "bc -l" \
144 "-1.00000000000000000000\n" \
145 "" "e(0)-2"
146
147testing "bc (!a&&b)" \
148 "bc" \
149 "0\n" \
150 "" "(!a&&b)"
151
Denys Vlasenko266bec82019-01-02 05:03:53 +0100152# check that dc code is not messing this up (no NUL printing!)
153testing "bc print \"\"" \
154 "bc" \
155 "" \
156 "" "print \"\""
157
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +0100158testing "bc print 1,2,3" \
159 "bc" \
160 "123" \
161 "" "print 1,2,3"
162
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100163testing "bc { print 1 }" \
164 "bc" \
165 "1" \
166 "" "{ print 1 }"
167
Denys Vlasenko06ade772018-12-16 22:44:51 +0100168testing "bc nested loops and breaks" \
169 "bc" \
170 "\
17111
17221
17331
17422
17512
17699
177" \
178 "" "\
179if(1) {
180 11
181 while(1) {
182 21
183 while(1) {
184 31
185 break
186 32
187 }
188 22
189 break
190 23
191 }
192 12
193} else {
194 88
195}
19699
197"
198
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100199testing "bc continue in if" \
200 "bc" \
201 "\
20211
20321
20411
20531
20699
207" \
208 "" "\
209i=2
210while(i--) {
Denys Vlasenkocb7c9552019-01-02 14:00:20 +0100211 11
212 if(i) {
213 21
214 continue
215 22
216 } else {
217 31
218 continue
219 32
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100220 }
Denys Vlasenkocb7c9552019-01-02 14:00:20 +0100221 12
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100222}
22399
224"
225
Denys Vlasenko266aa002018-12-16 23:24:25 +0100226testing "bc continue in for" \
227 "bc" \
228 "\
2291
23077
2312
23299
233" \
234 "" "\
235for(i=1; i<3; i++) {
236 i
237 if(i==2) continue
238 77
239}
24099
241"
242
Denys Vlasenko680ccd32018-12-31 19:42:13 +0100243testing "bc ibase" \
244 "bc" \
245 "99\n1295\n1224\n" \
246 "" "a=ZZ;a;ibase=36;a=ZZ;a;ibase=Z;a=ZZ;a"
247
Denys Vlasenkocb7c9552019-01-02 14:00:20 +0100248testing "bc parsing of numbers" \
249 "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
250 "465d8c01308d0863b6f5669e8a1c69fb -\n" \
251 "" '
252for (b = 2; b <= 16; ++b) {
253 if (b == 10) continue
254 obase = 10
255 print "ibase = A; ibase = ", b, "\n"
256 obase = b
257 for (i = 0; i <= 65536; ++i) {
258 i
259 print "0.", i, "\n"
260 print "1.", i, "\n"
261 print i, ".", i, "\n"
262 }
263}'
264
265testing "bc printing of numbers" \
266 "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
267 "d884b35d251ca096410712743aeafb9e -\n" \
268 "" '
269for (b = 2; b <= 101; ++b) {
270 if (b == 10) continue
271 s = b * b
272 print "obase = ", b, "\n"
273 for (i = 0; i <= s; ++i) {
274 i
275 print "0.", i, "\n"
276 print "1.", i, "\n"
277 print i, ".", i, "\n"
278 }
279 2189432174861923048671023498128347619023487610234689172304.192748960128745108927461089237469018723460
280}'
Denys Vlasenko9a23b072018-12-12 21:41:40 +0100281
282for f in bc*.bc; do
283 r="`basename "$f" .bc`_results.txt"
284 test -f "$r" || continue
285 # testing "test name" "command" "expected result" "file input" "stdin"
286 testing "bc -lq $f" \
287 "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
288 "E:0\nE:0\n" \
289 "" ""
290done
291
292exit $FAILCOUNT