blob: b3ce894ee46c8c2cd68070a7f367e2beb3872626 [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
9testing "bc comment 1" \
10 "bc" \
11 "3\n" \
12 "" "1 /* comment */ + 2"
13
14testing "bc comment 2: /*/ is not a closed comment" \
15 "bc" \
16 "4\n" \
17 "" "1 /*/ + 2 */ + 3"
18
Denys Vlasenko55f3cab2018-12-18 14:37:16 +010019testing "bc comment 3: unterminated #comment" \
20 "bc" \
21 "" \
22 "" "#foo" # no trailing newline
23
Denys Vlasenko818b6022018-12-13 17:56:35 +010024testing "bc backslash 1" \
25 "bc" \
26 "3\n" \
27 "" "1 \\\\\n + 2"
28
29testing "bc string 1" \
30 "bc" \
31 "STR\n" \
32 "" "\"STR\n\""
33
Denys Vlasenko99b37622018-12-15 20:06:59 +010034testing "bc if 0 else" \
35 "bc" \
36 "2\n9\n" \
37 "" "if (0) 1 else 2; 9"
38
39testing "bc if 1 else" \
40 "bc" \
41 "1\n9\n" \
42 "" "if (1) 1 else 2; 9"
43
44testing "bc if 1 if 1 else else" \
45 "bc" \
46 "1\n9\n" \
47 "" "if (1) if (1) 1 else 2 else 3; 9"
48
49testing "bc if 0 else if 1" \
50 "bc" \
51 "2\n9\n" \
52 "" "if (0) 1 else if (1) 2; 9"
53
Denys Vlasenko52caa002018-12-21 00:35:22 +010054testing "bc for(;;)" \
55 "bc" \
56 "2\n3\n2\n9\n" \
57 "" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9"
58
Denys Vlasenko19eee8e2018-12-21 20:29:34 +010059testing "bc for (;cond;)" \
60 "bc" \
61 "1\n2\n3\n9\n" \
62 "" "i=0; for(;i<3;)++i; 9"
63
64testing "bc for (;cond;upd)" \
65 "bc" \
66 "1\n2\n3\n9\n" \
67 "" "i=1; for(;i<4;i++)i; 9"
68
69testing "bc for (init;cond;upd)" \
70 "bc" \
71 "1\n2\n3\n9\n" \
72 "" "for(i=1;i<4;i++)i; 9"
73
Denys Vlasenkod1d29b42018-12-16 16:03:03 +010074testing "bc define auto" \
75 "bc" \
76 "8\n9\n" \
77 "" "define w() { auto z; return 8; }; w(); 9"
78
Denys Vlasenkoe9519e42018-12-16 17:06:07 +010079testing "bc define with body on next line" \
80 "bc" \
81 "8\n9\n" \
82 "" "define w()\n{ auto z; return 8; }\nw()\n9"
83
Denys Vlasenko202dd192018-12-16 17:30:35 +010084testing "bc if(cond)<NL>" \
85 "bc" \
86 "9\n" \
87 "" "if(0)\n3\n9"
88
Denys Vlasenkoa50576a2018-12-16 19:21:57 +010089testing "bc if(cond) stmt else<NL>" \
90 "bc" \
91 "4\n9\n" \
92 "" "if(0)3 else\n4\n9"
93
Denys Vlasenko202dd192018-12-16 17:30:35 +010094testing "bc while(cond)<NL>" \
95 "bc" \
96 "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \
97 "" "i=9;while(--i)\ni\n9"
98
Denys Vlasenko5acd14b2018-12-20 16:48:50 +010099testing "bc ifz does not match if keyword" \
100 "bc" \
101 "1\n2\n2\n3\n" \
102 "" "ifz=1;ifz\n++ifz;ifz++\nifz"
103
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +0100104testing "bc print 1,2,3" \
105 "bc" \
106 "123" \
107 "" "print 1,2,3"
108
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100109testing "bc { print 1 }" \
110 "bc" \
111 "1" \
112 "" "{ print 1 }"
113
Denys Vlasenko06ade772018-12-16 22:44:51 +0100114testing "bc nested loops and breaks" \
115 "bc" \
116 "\
11711
11821
11931
12022
12112
12299
123" \
124 "" "\
125if(1) {
126 11
127 while(1) {
128 21
129 while(1) {
130 31
131 break
132 32
133 }
134 22
135 break
136 23
137 }
138 12
139} else {
140 88
141}
14299
143"
144
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100145testing "bc continue in if" \
146 "bc" \
147 "\
14811
14921
15011
15131
15299
153" \
154 "" "\
155i=2
156while(i--) {
157 11
158 if(i) {
159 21
160 continue
161 22
162 } else {
163 31
164 continue
165 32
166 }
167 12
168}
16999
170"
171
Denys Vlasenko266aa002018-12-16 23:24:25 +0100172testing "bc continue in for" \
173 "bc" \
174 "\
1751
17677
1772
17899
179" \
180 "" "\
181for(i=1; i<3; i++) {
182 i
183 if(i==2) continue
184 77
185}
18699
187"
188
Denys Vlasenko9a23b072018-12-12 21:41:40 +0100189tar xJf bc_large.tar.xz
190
191for f in bc*.bc; do
192 r="`basename "$f" .bc`_results.txt"
193 test -f "$r" || continue
194 # testing "test name" "command" "expected result" "file input" "stdin"
195 testing "bc -lq $f" \
196 "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
197 "E:0\nE:0\n" \
198 "" ""
199done
200
201exit $FAILCOUNT