3
0

bc.tests 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 comparison 1" \
  153. "bc" \
  154. "1\n" \
  155. "" "-10 < -9"
  156. testing "bc nested loops and breaks" \
  157. "bc" \
  158. "\
  159. 11
  160. 21
  161. 31
  162. 22
  163. 12
  164. 99
  165. " \
  166. "" "\
  167. if(1) {
  168. 11
  169. while(1) {
  170. 21
  171. while(1) {
  172. 31
  173. break
  174. 32
  175. }
  176. 22
  177. break
  178. 23
  179. }
  180. 12
  181. } else {
  182. 88
  183. }
  184. 99
  185. "
  186. testing "bc continue in if" \
  187. "bc" \
  188. "\
  189. 11
  190. 21
  191. 11
  192. 31
  193. 99
  194. " \
  195. "" "\
  196. i=2
  197. while(i--) {
  198. 11
  199. if(i) {
  200. 21
  201. continue
  202. 22
  203. } else {
  204. 31
  205. continue
  206. 32
  207. }
  208. 12
  209. }
  210. 99
  211. "
  212. testing "bc continue in for" \
  213. "bc" \
  214. "\
  215. 1
  216. 77
  217. 2
  218. 99
  219. " \
  220. "" "\
  221. for(i=1; i<3; i++) {
  222. i
  223. if(i==2) continue
  224. 77
  225. }
  226. 99
  227. "
  228. testing "bc ibase" \
  229. "bc" \
  230. "99\n1295\n1224\n" \
  231. "" "a=ZZ;a;ibase=36;a=ZZ;a;ibase=Z;a=ZZ;a"
  232. testing "bc parsing of numbers" \
  233. "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
  234. "465d8c01308d0863b6f5669e8a1c69fb -\n" \
  235. "" '
  236. for (b = 2; b <= 16; ++b) {
  237. if (b == 10) continue
  238. obase = 10
  239. print "ibase = A; ibase = ", b, "\n"
  240. obase = b
  241. for (i = 0; i <= 65536; ++i) {
  242. i
  243. print "0.", i, "\n"
  244. print "1.", i, "\n"
  245. print i, ".", i, "\n"
  246. }
  247. }'
  248. testing "bc printing of numbers" \
  249. "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
  250. "d884b35d251ca096410712743aeafb9e -\n" \
  251. "" '
  252. for (b = 2; b <= 101; ++b) {
  253. if (b == 10) continue
  254. s = b * b
  255. print "obase = ", b, "\n"
  256. for (i = 0; i <= s; ++i) {
  257. i
  258. print "0.", i, "\n"
  259. print "1.", i, "\n"
  260. print i, ".", i, "\n"
  261. }
  262. 2189432174861923048671023498128347619023487610234689172304.192748960128745108927461089237469018723460
  263. }'
  264. for f in bc*.bc; do
  265. r="`basename "$f" .bc`_results.txt"
  266. test -f "$r" || continue
  267. # testing "test name" "command" "expected result" "file input" "stdin"
  268. testing "bc -lq $f" \
  269. "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
  270. "E:0\nE:0\n" \
  271. "" ""
  272. done
  273. exit $FAILCOUNT