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 | |
| 9 | testing "bc comment 1" \ |
| 10 | "bc" \ |
| 11 | "3\n" \ |
| 12 | "" "1 /* comment */ + 2" |
| 13 | |
| 14 | testing "bc comment 2: /*/ is not a closed comment" \ |
| 15 | "bc" \ |
| 16 | "4\n" \ |
| 17 | "" "1 /*/ + 2 */ + 3" |
| 18 | |
Denys Vlasenko | 55f3cab | 2018-12-18 14:37:16 +0100 | [diff] [blame] | 19 | testing "bc comment 3: unterminated #comment" \ |
| 20 | "bc" \ |
| 21 | "" \ |
| 22 | "" "#foo" # no trailing newline |
| 23 | |
Denys Vlasenko | 818b602 | 2018-12-13 17:56:35 +0100 | [diff] [blame] | 24 | testing "bc backslash 1" \ |
| 25 | "bc" \ |
| 26 | "3\n" \ |
| 27 | "" "1 \\\\\n + 2" |
| 28 | |
| 29 | testing "bc string 1" \ |
| 30 | "bc" \ |
| 31 | "STR\n" \ |
| 32 | "" "\"STR\n\"" |
| 33 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 34 | testing "bc if 0 else" \ |
| 35 | "bc" \ |
| 36 | "2\n9\n" \ |
| 37 | "" "if (0) 1 else 2; 9" |
| 38 | |
| 39 | testing "bc if 1 else" \ |
| 40 | "bc" \ |
| 41 | "1\n9\n" \ |
| 42 | "" "if (1) 1 else 2; 9" |
| 43 | |
| 44 | testing "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 | |
| 49 | testing "bc if 0 else if 1" \ |
| 50 | "bc" \ |
| 51 | "2\n9\n" \ |
| 52 | "" "if (0) 1 else if (1) 2; 9" |
| 53 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 54 | testing "bc define auto" \ |
| 55 | "bc" \ |
| 56 | "8\n9\n" \ |
| 57 | "" "define w() { auto z; return 8; }; w(); 9" |
| 58 | |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 59 | testing "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 Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 64 | testing "bc if(cond)<NL>" \ |
| 65 | "bc" \ |
| 66 | "9\n" \ |
| 67 | "" "if(0)\n3\n9" |
| 68 | |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 69 | testing "bc if(cond) stmt else<NL>" \ |
| 70 | "bc" \ |
| 71 | "4\n9\n" \ |
| 72 | "" "if(0)3 else\n4\n9" |
| 73 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 74 | testing "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 Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame^] | 79 | testing "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 Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 84 | testing "bc print 1,2,3" \ |
| 85 | "bc" \ |
| 86 | "123" \ |
| 87 | "" "print 1,2,3" |
| 88 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 89 | testing "bc { print 1 }" \ |
| 90 | "bc" \ |
| 91 | "1" \ |
| 92 | "" "{ print 1 }" |
| 93 | |
Denys Vlasenko | 06ade77 | 2018-12-16 22:44:51 +0100 | [diff] [blame] | 94 | testing "bc nested loops and breaks" \ |
| 95 | "bc" \ |
| 96 | "\ |
| 97 | 11 |
| 98 | 21 |
| 99 | 31 |
| 100 | 22 |
| 101 | 12 |
| 102 | 99 |
| 103 | " \ |
| 104 | "" "\ |
| 105 | if(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 | } |
| 122 | 99 |
| 123 | " |
| 124 | |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 125 | testing "bc continue in if" \ |
| 126 | "bc" \ |
| 127 | "\ |
| 128 | 11 |
| 129 | 21 |
| 130 | 11 |
| 131 | 31 |
| 132 | 99 |
| 133 | " \ |
| 134 | "" "\ |
| 135 | i=2 |
| 136 | while(i--) { |
| 137 | 11 |
| 138 | if(i) { |
| 139 | 21 |
| 140 | continue |
| 141 | 22 |
| 142 | } else { |
| 143 | 31 |
| 144 | continue |
| 145 | 32 |
| 146 | } |
| 147 | 12 |
| 148 | } |
| 149 | 99 |
| 150 | " |
| 151 | |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 152 | testing "bc continue in for" \ |
| 153 | "bc" \ |
| 154 | "\ |
| 155 | 1 |
| 156 | 77 |
| 157 | 2 |
| 158 | 99 |
| 159 | " \ |
| 160 | "" "\ |
| 161 | for(i=1; i<3; i++) { |
| 162 | i |
| 163 | if(i==2) continue |
| 164 | 77 |
| 165 | } |
| 166 | 99 |
| 167 | " |
| 168 | |
Denys Vlasenko | 9a23b07 | 2018-12-12 21:41:40 +0100 | [diff] [blame] | 169 | tar xJf bc_large.tar.xz |
| 170 | |
| 171 | for 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 | "" "" |
| 179 | done |
| 180 | |
| 181 | exit $FAILCOUNT |