bc.tests 5.1 KB

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