Denys Vlasenko | 9a23b07 | 2018-12-12 21:41:40 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 766f672 | 2018-12-13 17:23:24 +0100 | [diff] [blame] | 7 | # testing "test name" "command" "expected result" "file input" "stdin" |
| 8 | |
Denys Vlasenko | 94576d2 | 2018-12-25 23:45:57 +0100 | [diff] [blame] | 9 | testing "bc comment" \ |
Denys Vlasenko | 766f672 | 2018-12-13 17:23:24 +0100 | [diff] [blame] | 10 | "bc" \ |
| 11 | "3\n" \ |
| 12 | "" "1 /* comment */ + 2" |
| 13 | |
Denys Vlasenko | 94576d2 | 2018-12-25 23:45:57 +0100 | [diff] [blame] | 14 | testing "bc /*/ is not a closed comment" \ |
Denys Vlasenko | 766f672 | 2018-12-13 17:23:24 +0100 | [diff] [blame] | 15 | "bc" \ |
| 16 | "4\n" \ |
| 17 | "" "1 /*/ + 2 */ + 3" |
| 18 | |
Denys Vlasenko | 94576d2 | 2018-12-25 23:45:57 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 55f3cab | 2018-12-18 14:37:16 +0100 | [diff] [blame] | 31 | testing "bc comment 3: unterminated #comment" \ |
| 32 | "bc" \ |
| 33 | "" \ |
| 34 | "" "#foo" # no trailing newline |
| 35 | |
Denys Vlasenko | 818b602 | 2018-12-13 17:56:35 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 81 | testing "bc for (;;)" \ |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 82 | "bc" \ |
| 83 | "2\n3\n2\n9\n" \ |
| 84 | "" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9" |
| 85 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 101 | testing "bc for (;;) {break}" \ |
| 102 | "bc" \ |
| 103 | "2\n9\n" \ |
| 104 | "" "for (;;) {2;break}; 9" |
| 105 | |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 106 | testing "bc define {return}" \ |
| 107 | "bc" \ |
| 108 | "0\n9\n" \ |
| 109 | "" "define w() {return}\nw();9" |
| 110 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 111 | testing "bc define auto" \ |
| 112 | "bc" \ |
| 113 | "8\n9\n" \ |
| 114 | "" "define w() { auto z; return 8; }; w(); 9" |
| 115 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 116 | testing "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 Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 121 | testing "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 Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 126 | testing "bc void function" \ |
| 127 | "bc" \ |
| 128 | "void9\n" \ |
| 129 | "" "define void w() {print \"void\"}\nw()\n9" |
| 130 | |
| 131 | # Extra POSIX compat - GNU bc does not allow this |
| 132 | testing "bc function named 'void'" \ |
| 133 | "bc" \ |
| 134 | "void0\n9\n" \ |
| 135 | "" "define void() {print \"void\"}\nvoid()\n9" |
| 136 | |
| 137 | # Extra POSIX compat - GNU bc does not allow this |
| 138 | testing "bc variable named 'void'" \ |
| 139 | "bc" \ |
| 140 | "6\n9\n" \ |
| 141 | "" "void=6\nvoid\n9" |
| 142 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 143 | testing "bc if(cond)<NL>" \ |
| 144 | "bc" \ |
| 145 | "9\n" \ |
| 146 | "" "if(0)\n3\n9" |
| 147 | |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 148 | testing "bc if(cond) stmt else<NL>" \ |
| 149 | "bc" \ |
| 150 | "4\n9\n" \ |
| 151 | "" "if(0)3 else\n4\n9" |
| 152 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 153 | testing "bc while(cond)<NL>" \ |
| 154 | "bc" \ |
| 155 | "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \ |
| 156 | "" "i=9;while(--i)\ni\n9" |
| 157 | |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 158 | testing "bc ifz does not match if keyword" \ |
| 159 | "bc" \ |
| 160 | "1\n2\n2\n3\n" \ |
| 161 | "" "ifz=1;ifz\n++ifz;ifz++\nifz" |
| 162 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 163 | # had parse error on "f()-N" |
| 164 | testing "bc -l 'e(0)-2'" \ |
| 165 | "bc -l" \ |
| 166 | "-1.00000000000000000000\n" \ |
| 167 | "" "e(0)-2" |
| 168 | |
| 169 | testing "bc (!a&&b)" \ |
| 170 | "bc" \ |
| 171 | "0\n" \ |
| 172 | "" "(!a&&b)" |
| 173 | |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 174 | # check that dc code is not messing this up (no NUL printing!) |
| 175 | testing "bc print \"\"" \ |
| 176 | "bc" \ |
| 177 | "" \ |
| 178 | "" "print \"\"" |
| 179 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 180 | testing "bc print 1,2,3" \ |
| 181 | "bc" \ |
| 182 | "123" \ |
| 183 | "" "print 1,2,3" |
| 184 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 185 | testing "bc { print 1 }" \ |
| 186 | "bc" \ |
| 187 | "1" \ |
| 188 | "" "{ print 1 }" |
| 189 | |
Denys Vlasenko | 0084137 | 2019-11-23 17:25:21 +0100 | [diff] [blame] | 190 | testing "bc comparison 1" \ |
| 191 | "bc" \ |
| 192 | "1\n" \ |
| 193 | "" "-10 < -9" |
| 194 | |
Denys Vlasenko | 06ade77 | 2018-12-16 22:44:51 +0100 | [diff] [blame] | 195 | testing "bc nested loops and breaks" \ |
| 196 | "bc" \ |
| 197 | "\ |
| 198 | 11 |
| 199 | 21 |
| 200 | 31 |
| 201 | 22 |
| 202 | 12 |
| 203 | 99 |
| 204 | " \ |
| 205 | "" "\ |
| 206 | if(1) { |
| 207 | 11 |
| 208 | while(1) { |
| 209 | 21 |
| 210 | while(1) { |
| 211 | 31 |
| 212 | break |
| 213 | 32 |
| 214 | } |
| 215 | 22 |
| 216 | break |
| 217 | 23 |
| 218 | } |
| 219 | 12 |
| 220 | } else { |
| 221 | 88 |
| 222 | } |
| 223 | 99 |
| 224 | " |
| 225 | |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 226 | testing "bc continue in if" \ |
| 227 | "bc" \ |
| 228 | "\ |
| 229 | 11 |
| 230 | 21 |
| 231 | 11 |
| 232 | 31 |
| 233 | 99 |
| 234 | " \ |
| 235 | "" "\ |
| 236 | i=2 |
| 237 | while(i--) { |
Denys Vlasenko | cb7c955 | 2019-01-02 14:00:20 +0100 | [diff] [blame] | 238 | 11 |
| 239 | if(i) { |
| 240 | 21 |
| 241 | continue |
| 242 | 22 |
| 243 | } else { |
| 244 | 31 |
| 245 | continue |
| 246 | 32 |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 247 | } |
Denys Vlasenko | cb7c955 | 2019-01-02 14:00:20 +0100 | [diff] [blame] | 248 | 12 |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 249 | } |
| 250 | 99 |
| 251 | " |
| 252 | |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 253 | testing "bc continue in for" \ |
| 254 | "bc" \ |
| 255 | "\ |
| 256 | 1 |
| 257 | 77 |
| 258 | 2 |
| 259 | 99 |
| 260 | " \ |
| 261 | "" "\ |
| 262 | for(i=1; i<3; i++) { |
| 263 | i |
| 264 | if(i==2) continue |
| 265 | 77 |
| 266 | } |
| 267 | 99 |
| 268 | " |
| 269 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 270 | testing "bc ibase" \ |
| 271 | "bc" \ |
| 272 | "99\n1295\n1224\n" \ |
| 273 | "" "a=ZZ;a;ibase=36;a=ZZ;a;ibase=Z;a=ZZ;a" |
| 274 | |
Denys Vlasenko | cb7c955 | 2019-01-02 14:00:20 +0100 | [diff] [blame] | 275 | testing "bc parsing of numbers" \ |
| 276 | "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \ |
| 277 | "465d8c01308d0863b6f5669e8a1c69fb -\n" \ |
| 278 | "" ' |
| 279 | for (b = 2; b <= 16; ++b) { |
| 280 | if (b == 10) continue |
| 281 | obase = 10 |
| 282 | print "ibase = A; ibase = ", b, "\n" |
| 283 | obase = b |
| 284 | for (i = 0; i <= 65536; ++i) { |
| 285 | i |
| 286 | print "0.", i, "\n" |
| 287 | print "1.", i, "\n" |
| 288 | print i, ".", i, "\n" |
| 289 | } |
| 290 | }' |
| 291 | |
| 292 | testing "bc printing of numbers" \ |
| 293 | "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \ |
| 294 | "d884b35d251ca096410712743aeafb9e -\n" \ |
| 295 | "" ' |
| 296 | for (b = 2; b <= 101; ++b) { |
| 297 | if (b == 10) continue |
| 298 | s = b * b |
| 299 | print "obase = ", b, "\n" |
| 300 | for (i = 0; i <= s; ++i) { |
| 301 | i |
| 302 | print "0.", i, "\n" |
| 303 | print "1.", i, "\n" |
| 304 | print i, ".", i, "\n" |
| 305 | } |
| 306 | 2189432174861923048671023498128347619023487610234689172304.192748960128745108927461089237469018723460 |
| 307 | }' |
Denys Vlasenko | 9a23b07 | 2018-12-12 21:41:40 +0100 | [diff] [blame] | 308 | |
| 309 | for f in bc*.bc; do |
| 310 | r="`basename "$f" .bc`_results.txt" |
| 311 | test -f "$r" || continue |
| 312 | # testing "test name" "command" "expected result" "file input" "stdin" |
| 313 | testing "bc -lq $f" \ |
| 314 | "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \ |
| 315 | "E:0\nE:0\n" \ |
| 316 | "" "" |
| 317 | done |
| 318 | |
| 319 | exit $FAILCOUNT |