bc: upstream fixes
[oweals/busybox.git] / testsuite / bc.tests
1 #!/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
7 # testing "test name" "command" "expected result" "file input" "stdin"
8
9 testing "bc comment" \
10         "bc" \
11         "3\n" \
12         "" "1 /* comment */ + 2"
13
14 testing "bc /*/ is not a closed comment" \
15         "bc" \
16         "4\n" \
17         "" "1 /*/ + 2 */ + 3"
18
19 # this needs interactive testing
20 testing "bc comment with \"" \
21         "bc" \
22         "3\n" \
23         "" "1 /* \" */ + 2"
24
25 # this needs interactive testing
26 testing "bc \"string/*\" is not a comment" \
27         "bc" \
28         "string/*9\n" \
29         "" "\"string/*\";9"
30
31 testing "bc comment 3: unterminated #comment" \
32         "bc" \
33         "" \
34         "" "#foo"  # no trailing newline
35
36 testing "bc backslash 1" \
37         "bc" \
38         "3\n" \
39         "" "1 \\\\\n + 2"
40
41 testing "bc string 1" \
42         "bc" \
43         "STR\n" \
44         "" "\"STR\n\""
45
46 testing "bc read() 4<EOF>" \
47         "bc input" \
48         "4\n" \
49         "read();halt" "4"
50
51 testing "bc read()^2" \
52         "bc input" \
53         "16\n" \
54         "read()^2;halt" "4\n"
55
56 testing "bc read()*read()" \
57         "bc input" \
58         "20\n" \
59         "read()*read();halt" "4\n5"
60
61 testing "bc if 0 else" \
62         "bc" \
63         "2\n9\n" \
64         "" "if (0) 1 else 2; 9"
65
66 testing "bc if 1 else" \
67         "bc" \
68         "1\n9\n" \
69         "" "if (1) 1 else 2; 9"
70
71 testing "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
76 testing "bc if 0 else if 1" \
77         "bc" \
78         "2\n9\n" \
79         "" "if (0) 1 else if (1) 2; 9"
80
81 testing "bc for (;;)" \
82         "bc" \
83         "2\n3\n2\n9\n" \
84         "" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9"
85
86 testing "bc for (;cond;)" \
87         "bc" \
88         "1\n2\n3\n9\n" \
89         "" "i=0; for(;i<3;)++i; 9"
90
91 testing "bc for (;cond;upd)" \
92         "bc" \
93         "1\n2\n3\n9\n" \
94         "" "i=1; for(;i<4;i++)i; 9"
95
96 testing "bc for (init;cond;upd)" \
97         "bc" \
98         "1\n2\n3\n9\n" \
99         "" "for(i=1;i<4;i++)i; 9"
100
101 testing "bc for (;;) {break}" \
102         "bc" \
103         "2\n9\n" \
104         "" "for (;;) {2;break}; 9"
105
106 testing "bc define auto" \
107         "bc" \
108         "8\n9\n" \
109         "" "define w() { auto z; return 8; }; w(); 9"
110
111 testing "bc define auto array same name" \
112         "bc" \
113         "8\n9\n" \
114         "" "define w(x) { auto x[]; return x; }; w(8); 9"
115
116 testing "bc define with body on next line" \
117         "bc" \
118         "8\n9\n" \
119         "" "define w()\n{ auto z; return 8; }\nw()\n9"
120
121 testing "bc if(cond)<NL>" \
122         "bc" \
123         "9\n" \
124         "" "if(0)\n3\n9"
125
126 testing "bc if(cond) stmt else<NL>" \
127         "bc" \
128         "4\n9\n" \
129         "" "if(0)3 else\n4\n9"
130
131 testing "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
136 testing "bc ifz does not match if keyword" \
137         "bc" \
138         "1\n2\n2\n3\n" \
139         "" "ifz=1;ifz\n++ifz;ifz++\nifz"
140
141 # had parse error on "f()-N"
142 testing "bc -l 'e(0)-2'" \
143         "bc -l" \
144         "-1.00000000000000000000\n" \
145         "" "e(0)-2"
146
147 testing "bc (!a&&b)" \
148         "bc" \
149         "0\n" \
150         "" "(!a&&b)"
151
152 testing "bc print 1,2,3" \
153         "bc" \
154         "123" \
155         "" "print 1,2,3"
156
157 testing "bc { print 1 }" \
158         "bc" \
159         "1" \
160         "" "{ print 1 }"
161
162 testing "bc nested loops and breaks" \
163         "bc" \
164         "\
165 11
166 21
167 31
168 22
169 12
170 99
171 " \
172         "" "\
173 if(1) {
174         11
175         while(1) {
176                 21
177                 while(1) {
178                         31
179                         break
180                         32
181                 }
182                 22
183                 break
184                 23
185         }
186         12
187 } else {
188         88
189 }
190 99
191 "
192
193 testing "bc continue in if" \
194         "bc" \
195         "\
196 11
197 21
198 11
199 31
200 99
201 " \
202         "" "\
203 i=2
204 while(i--) {
205         11
206         if(i) {
207                 21
208                 continue
209                 22
210         } else {
211                 31
212                 continue
213                 32
214         }
215         12
216 }
217 99
218 "
219
220 testing "bc continue in for" \
221         "bc" \
222         "\
223 1
224 77
225 2
226 99
227 " \
228         "" "\
229 for(i=1; i<3; i++) {
230     i
231     if(i==2) continue
232     77
233 }
234 99
235 "
236
237 testing "bc ibase" \
238         "bc" \
239         "99\n1295\n1224\n" \
240         "" "a=ZZ;a;ibase=36;a=ZZ;a;ibase=Z;a=ZZ;a"
241
242 tar xJf bc_large.tar.xz
243
244 for f in bc*.bc; do
245         r="`basename "$f" .bc`_results.txt"
246         test -f "$r" || continue
247         # testing "test name" "command" "expected result" "file input" "stdin"
248         testing "bc -lq $f" \
249                 "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
250                 "E:0\nE:0\n" \
251                 "" ""
252 done
253
254 exit $FAILCOUNT