blob: d33f8c90ae14e7562c272c83b2b95874a084746a [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 Vlasenkod1d29b42018-12-16 16:03:03 +010054testing "bc define auto" \
55 "bc" \
56 "8\n9\n" \
57 "" "define w() { auto z; return 8; }; w(); 9"
58
Denys Vlasenkoe9519e42018-12-16 17:06:07 +010059testing "bc define with body on next line" \
60 "bc" \
61 "8\n9\n" \
62 "" "define w()\n{ auto z; return 8; }\nw()\n9"
63
Denys Vlasenko202dd192018-12-16 17:30:35 +010064testing "bc if(cond)<NL>" \
65 "bc" \
66 "9\n" \
67 "" "if(0)\n3\n9"
68
Denys Vlasenkoa50576a2018-12-16 19:21:57 +010069testing "bc if(cond) stmt else<NL>" \
70 "bc" \
71 "4\n9\n" \
72 "" "if(0)3 else\n4\n9"
73
Denys Vlasenko202dd192018-12-16 17:30:35 +010074testing "bc while(cond)<NL>" \
75 "bc" \
76 "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \
77 "" "i=9;while(--i)\ni\n9"
78
Denys Vlasenko5acd14b2018-12-20 16:48:50 +010079testing "bc ifz does not match if keyword" \
80 "bc" \
81 "1\n2\n2\n3\n" \
82 "" "ifz=1;ifz\n++ifz;ifz++\nifz"
83
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +010084testing "bc print 1,2,3" \
85 "bc" \
86 "123" \
87 "" "print 1,2,3"
88
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +010089testing "bc { print 1 }" \
90 "bc" \
91 "1" \
92 "" "{ print 1 }"
93
Denys Vlasenko06ade772018-12-16 22:44:51 +010094testing "bc nested loops and breaks" \
95 "bc" \
96 "\
9711
9821
9931
10022
10112
10299
103" \
104 "" "\
105if(1) {
106 11
107 while(1) {
108 21
109 while(1) {
110 31
111 break
112 32
113 }
114 22
115 break
116 23
117 }
118 12
119} else {
120 88
121}
12299
123"
124
Denys Vlasenkode24e9d2018-12-16 23:02:22 +0100125testing "bc continue in if" \
126 "bc" \
127 "\
12811
12921
13011
13131
13299
133" \
134 "" "\
135i=2
136while(i--) {
137 11
138 if(i) {
139 21
140 continue
141 22
142 } else {
143 31
144 continue
145 32
146 }
147 12
148}
14999
150"
151
Denys Vlasenko266aa002018-12-16 23:24:25 +0100152testing "bc continue in for" \
153 "bc" \
154 "\
1551
15677
1572
15899
159" \
160 "" "\
161for(i=1; i<3; i++) {
162 i
163 if(i==2) continue
164 77
165}
16699
167"
168
Denys Vlasenko9a23b072018-12-12 21:41:40 +0100169tar xJf bc_large.tar.xz
170
171for f in bc*.bc; do
172 r="`basename "$f" .bc`_results.txt"
173 test -f "$r" || continue
174 # testing "test name" "command" "expected result" "file input" "stdin"
175 testing "bc -lq $f" \
176 "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
177 "E:0\nE:0\n" \
178 "" ""
179done
180
181exit $FAILCOUNT