bc.tests 4.9 KB

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