| 1 | /* depend: */ |
| 2 | /* cflags: */ |
| 3 | /* linker: alloc.o debug.o element.o format.o parser.o program.o stack.o storage.o -lm -lreadline */ |
| 4 | |
| 5 | #include <malloc.h> |
| 6 | #include <stddef.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include <readline/readline.h> |
| 12 | #include <readline/history.h> |
| 13 | |
| 14 | #include "debug.h" |
| 15 | #include "element.h" |
| 16 | #include "format.h" |
| 17 | #include "parser.h" |
| 18 | |
| 19 | /* constants */ |
| 20 | |
| 21 | #define BUFFER_SIZE 4096 |
| 22 | #define HISTORY_LEN 10 |
| 23 | |
| 24 | /* macros */ |
| 25 | |
| 26 | #define CEIL(x, y) (((x) + (y) - 1) / (y)) |
| 27 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 28 | #define MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 29 | |
| 30 | /* gobal variables */ |
| 31 | |
| 32 | char *progname = NULL; |
| 33 | char *iprompt = "<= "; |
| 34 | int mode = 1; |
| 35 | char *oprompt = "=> "; |
| 36 | int precision = 6; |
| 37 | char **completion_list = NULL; |
| 38 | |
| 39 | /* help function */ |
| 40 | |
| 41 | int usage (int ret) |
| 42 | { |
| 43 | FILE *fid = ret ? stderr : stdout; |
| 44 | fprintf (fid, "usage: %s\n", progname); |
| 45 | fprintf (fid, " -h : help message\n"); |
| 46 | fprintf (fid, " -i : input prompt (%s)\n", iprompt); |
| 47 | fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no"); |
| 48 | fprintf (fid, " -o : output prompt (%s)\n", oprompt); |
| 49 | fprintf (fid, " -p : precision (%d)\n", precision); |
| 50 | fprintf (fid, " -v : verbose level (%d)\n", verbose); |
| 51 | |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | /* completion function */ |
| 56 | |
| 57 | char *generator (const char *text, int state); |
| 58 | |
| 59 | char **completion (const char *text, __attribute__((unused)) int start, __attribute__((unused)) int end) |
| 60 | { |
| 61 | rl_attempted_completion_over = 1; |
| 62 | return rl_completion_matches (text, generator); |
| 63 | } |
| 64 | |
| 65 | char *generator (const char *text, int state) |
| 66 | { |
| 67 | static int index, len; |
| 68 | char *name; |
| 69 | |
| 70 | if (!state) { |
| 71 | index = 0; |
| 72 | len = strlen(text); |
| 73 | } |
| 74 | |
| 75 | while ((name = completion_list[index++])) { |
| 76 | if (strncmp (name, text, len) == 0) { |
| 77 | return strdup (name); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | /* main function */ |
| 85 | |
| 86 | int main (int argc, char *argv[]) |
| 87 | { |
| 88 | char *buffer = NULL; |
| 89 | char buffer_static[BUFFER_SIZE + 1] = {0}; |
| 90 | int i = 0, nb = 1; |
| 91 | int ret = 0; |
| 92 | |
| 93 | /* program name */ |
| 94 | |
| 95 | progname = argv[0]; |
| 96 | while (progname[i] != '\0') { |
| 97 | if ((progname[i] == '/') || (progname[i] == '\\')) { |
| 98 | progname += i + 1; |
| 99 | i = 0; |
| 100 | } else { |
| 101 | i++; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* argument processing */ |
| 106 | |
| 107 | while (argc-- > 1) { |
| 108 | char *arg = *(++argv); |
| 109 | if (arg[0] != '-') { |
| 110 | VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1)); |
| 111 | return 1; |
| 112 | } |
| 113 | char c = arg[1]; |
| 114 | switch (c) { |
| 115 | case 'n': |
| 116 | mode = 0; |
| 117 | buffer = buffer_static; |
| 118 | break; |
| 119 | case 'i': |
| 120 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 121 | if (arg == NULL) { |
| 122 | VERBOSE (ERROR, fprintf (stderr, "%s: missing input prompt\n", progname); usage (1)); |
| 123 | return 1; |
| 124 | } |
| 125 | iprompt = arg; |
| 126 | break; |
| 127 | case 'o': |
| 128 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 129 | if (arg == NULL) { |
| 130 | VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1)); |
| 131 | return 1; |
| 132 | } |
| 133 | oprompt = arg; |
| 134 | break; |
| 135 | case 'p': |
| 136 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 137 | if (arg == NULL) { |
| 138 | VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1)); |
| 139 | return 1; |
| 140 | } |
| 141 | precision = atoi (arg); |
| 142 | break; |
| 143 | case 'v': |
| 144 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 145 | if (arg == NULL) { |
| 146 | VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1)); |
| 147 | return 1; |
| 148 | } |
| 149 | verbose = atoi (arg); |
| 150 | break; |
| 151 | case 'h': |
| 152 | default: |
| 153 | return usage (c != 'h'); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* set format */ |
| 158 | set_format (oprompt, precision); |
| 159 | |
| 160 | /* completion list*/ |
| 161 | completion_list = generate_completion_list (); |
| 162 | rl_attempted_completion_function = completion; |
| 163 | |
| 164 | /* read from input stream */ |
| 165 | |
| 166 | while (1) { |
| 167 | char *line[BUFFER_SIZE] = {0}; |
| 168 | |
| 169 | if (mode) { |
| 170 | if ((buffer = readline (iprompt)) == NULL) { |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | /* check empty line */ |
| 175 | if (strlen (buffer) == 0) { |
| 176 | free (buffer); |
| 177 | continue; |
| 178 | } else if (strcmp (buffer, ".") == 0) { |
| 179 | free (buffer); |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | /* add line into history */ |
| 184 | add_history (buffer); |
| 185 | VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n", |
| 186 | where_history (), history_length, buffer)); |
| 187 | if (history_length > HISTORY_LEN) { |
| 188 | HIST_ENTRY *last = remove_history (0); |
| 189 | if (last) { |
| 190 | free_history_entry (last); |
| 191 | } |
| 192 | } |
| 193 | } else { |
| 194 | printf ("%s", iprompt); |
| 195 | if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) { |
| 196 | break; |
| 197 | } |
| 198 | VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer)); |
| 199 | } |
| 200 | |
| 201 | /* pre-process buffer */ |
| 202 | nb = 0; |
| 203 | char *pt = line[nb++] = buffer; |
| 204 | while (*pt != '\0') { |
| 205 | switch (*pt) { |
| 206 | case '\n': |
| 207 | *pt = '\0'; |
| 208 | // fallthrough |
| 209 | case ';': |
| 210 | line[nb++] = pt + 1; |
| 211 | } |
| 212 | pt++; |
| 213 | } |
| 214 | |
| 215 | /* look for end of line */ |
| 216 | for (i = 0; i < nb; i++) { |
| 217 | if (*line[i] == '\0') { |
| 218 | continue; |
| 219 | } |
| 220 | element_t *element = parser (line[i], NULL, -9); |
| 221 | if (element == ERROR_OP) { |
| 222 | VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i])); |
| 223 | ret = 1; |
| 224 | } else if (element != NULL) { |
| 225 | VERBOSE (INFO, print_element (element, 0)); |
| 226 | answer = evaluate_element (element, 0); |
| 227 | if (!element->hidden) { |
| 228 | print (answer); |
| 229 | } |
| 230 | delelement (element); |
| 231 | ret = 0; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (mode) { |
| 236 | free (buffer); |
| 237 | } else { |
| 238 | memset (buffer, 0, BUFFER_SIZE); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | free_completion_list (completion_list); |
| 243 | |
| 244 | free_format (); |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | // test: calc.exe -h |
| 250 | // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
| 251 | // test: echo 1 | calc.exe -v3 | grep -q value |
| 252 | // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' |
| 253 | // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
| 254 | // test: calc.exe error 2>&1 | grep -q 'invalid option' |
| 255 | // test: calc.exe -i 2>&1 | grep -q 'missing input prompt' |
| 256 | // test: calc.exe -o 2>&1 | grep -q 'missing output prompt' |
| 257 | // test: calc.exe -p 2>&1 | grep -q 'missing precision' |
| 258 | // test: calc.exe -v 2>&1 | grep -q 'missing verbose' |
| 259 | // test: echo "1 + 1" | calc.exe -i '# ' | grep -q '# 1 + 1' |
| 260 | // test: echo "1 + 1" | calc.exe -o '# ' | grep -q '# 2' |
| 261 | // test: echo "1 + 2" | calc.exe | grep -q '=> 3' |
| 262 | // test: echo "1 - 2" | calc.exe | grep -q '=> -1' |
| 263 | // test: echo "2 * 3" | calc.exe | grep -q '=> 6' |
| 264 | // test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5' |
| 265 | // test: echo "8 % 3" | calc.exe | grep -q '=> 2' |
| 266 | // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8' |
| 267 | // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8' |
| 268 | // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1' |
| 269 | // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421' |
| 270 | // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8' |
| 271 | // test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147' |
| 272 | // test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297' |
| 273 | // test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715' |
| 274 | // test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906' |
| 275 | // test: echo "ln (2)" | calc.exe | grep -q '=> 0\.693147' |
| 276 | // test: echo "log (10)" | calc.exe | grep -q '=> 1' |
| 277 | // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1' |
| 278 | // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403' |
| 279 | // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275' |
| 280 | // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3' |
| 281 | // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25' |
| 282 | // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988' |
| 283 | // test: echo "1 + -2" | calc.exe | grep -q '=> -1' |
| 284 | // test: echo "1 - +2" | calc.exe | grep -q '=> -1' |
| 285 | // test: echo "-1 + +2" | calc.exe | grep -q '=> 1' |
| 286 | // test: echo "-1+2" | calc.exe | grep -q '=> 1' |
| 287 | // test: echo "1-2" | calc.exe | grep -q '=> -1' |
| 288 | // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667' |
| 289 | // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37' |
| 290 | // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074' |
| 291 | // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724' |
| 292 | // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5' |
| 293 | // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5' |
| 294 | // test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7' |
| 295 | // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0' |
| 296 | // test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0' |
| 297 | // test: echo "quit" | calc.exe | grep -q 'bye' |
| 298 | // test: echo "help" | calc.exe | grep -q 'miscellaneous' |
| 299 | // test: echo "1 + 2 *" | calc.exe | grep -q 'error' |
| 300 | // test: echo "* 1 - 2" | calc.exe | grep -q 'error' |
| 301 | // test: echo "2 + * 3" | calc.exe | grep -q 'error' |
| 302 | // test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error' |
| 303 | // test: echo "2 + (foo)" | calc.exe | grep -q 'error' |
| 304 | // test: echo "2 + cos (pi +" | calc.exe | grep -q 'error' |
| 305 | // test: echo "2 + cos (pi" | calc.exe | grep -q 'error' |
| 306 | // test: echo "(2 + " | calc.exe | grep -q 'error' |
| 307 | // test: echo "cos (1, 2)" | calc.exe | grep -q 'error' |
| 308 | // test: echo "sqrt 2" | calc.exe | grep -q 'error' |
| 309 | // test: echo "pow (2)" | calc.exe | grep -q 'error' |
| 310 | // test: echo "1.23456789" | calc.exe -p 4 | grep -q '=> 1\.235' |
| 311 | // test: echo . | calc.exe | grep -qv error |
| 312 | // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe -n | grep -q 64 |
| 313 | // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe -n | grep -q 2 |
| 314 | // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 64 |
| 315 | // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 2 |
| 316 | // test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\ntan (0)\nacos (0)\nasin (0)\natan (0)\nln (1)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -n -v 3 | grep -q bye |
| 317 | // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\ntan ()\nacos ()\nasin ()\natan ()\nln ()\nlog ()\nexp ()\nabs ()\nceil ()\nfloor ()\n1 + (\n1+2(\n1+2cos\n1+2pi' | calc.exe | grep -c error | xargs test 22 = |
| 318 | // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 = |
| 319 | // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1 |
| 320 | // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906' |
| 321 | // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error' |
| 322 | // test: echo -e '\n\n\n' | calc.exe -n | grep -qv 'error' |
| 323 | // test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9 |
| 324 | // test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 = |
| 325 | // test: echo -e '1\nsto (2)\n3\nsto (5, 7)\nsto(9)\ndisp' | calc.exe | grep -q '0 1 0 0 7 0 0 0 7 0' |
| 326 | // test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1' |
| 327 | // test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1' |
| 328 | // test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0' |
| 329 | // test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1' |
| 330 | // test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0' |
| 331 | // test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1' |
| 332 | // test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1' |
| 333 | // test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1' |
| 334 | // test: echo -e '1 > 2' | calc.exe | grep -q '=> 0' |
| 335 | // test: echo -e '2 > 2' | calc.exe | grep -q '=> 0' |
| 336 | // test: echo -e '1 < 2' | calc.exe | grep -q '=> 1' |
| 337 | // test: echo -e '2 < 2' | calc.exe | grep -q '=> 0' |
| 338 | // test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye |
| 339 | // test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1' |
| 340 | // test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0' |
| 341 | // test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0' |
| 342 | // test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0' |
| 343 | // test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1' |
| 344 | // test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1' |
| 345 | // test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1' |
| 346 | // test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0' |
| 347 | // test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1' |
| 348 | // test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0' |
| 349 | // test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -n -v 3 | grep -qv error |
| 350 | // test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1' |
| 351 | // test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1' |
| 352 | // test: echo -e '1 + quit' | calc.exe | grep -q error |
| 353 | // test: echo -e 'cos (quit)' | calc.exe | grep -q error |
| 354 | // test: echo -e '(quit)' | calc.exe | grep -q error |
| 355 | // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 = |
| 356 | // test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe | grep -q '=> 4\.15888' |
| 357 | // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04' |
| 358 | // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64))' | calc.exe | grep -q '=> 0' |
| 359 | // test: echo -e 'cond (0, 1, 2)\nquit' | calc.exe -v 3 | grep -q Cond |
| 360 | // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 = |
| 361 | // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6\nquit' | calc.exe -v 3 | grep -q '=> 1' |
| 362 | // test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 = |
| 363 | // test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 = |
| 364 | // test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050' |
| 365 | // test: echo -e 'while\nwhile (inc (1) < 3,\nwhile (inc (1) < 100, sto (2, rcl (1) + rcl (2))' | calc.exe | grep -c error | xargs test 3 = |
| 366 | // test: echo -e 'while (0, 1)\nquit' | calc.exe -v 3 | grep -q While |
| 367 | // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}\nquit' | calc.exe -v 3 | grep -q 'Code' |
| 368 | // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6' |
| 369 | // test: echo -e '{\n{}\n{1, 2\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' | calc.exe | grep -c error | xargs test 6 = |
| 370 | // test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 = |
| 371 | // test: echo -e 'print (1)\nquit' | calc.exe -v 3 | grep -q Print |
| 372 | // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1' |
| 373 | // test: echo -e '\t\t' | calc.exe | grep -q 'print' |
| 374 | // test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2 |
| 375 | // test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q "invalid index" |
| 376 | // test: echo -e 'sto (2, 3)\nmem (2)\ndisp' | calc.exe | grep -q 'storage: 0 3$' |
| 377 | // test: echo -e 'disp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0" |
| 378 | // test: echo -e 'sto (3, 10)\ndisp' | calc.exe | grep -q "storage: 0 0 10 0 0 0 0 0 0 0" |
| 379 | // test: echo -e 'rcl (3)\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0" |
| 380 | // test: echo -e 'inc (2)\ndisp' | calc.exe | grep -q "storage: 0 1 0 0 0 0 0 0 0 0" |
| 381 | // test: echo -e 'dec (2)\ndisp' | calc.exe | grep -q "storage: 0 -1 0 0 0 0 0 0 0 0" |
| 382 | // test: echo -e 'sto (3, pi)\nclr\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0" |
| 383 | // test: echo -e 'mem (3)\nclr\nquit' | calc.exe -v 3 | grep -q Clear |
| 384 | // test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (1, {cos (arg (1)^2)})\ncall (1, pi/6)\nprog (2, {arg (1) * 3})\ncall (2, 1, 2)\nls' | calc.exe | grep -q 'programs: 2 1' |
| 385 | // test: echo -e 'prog (1, {arg (2) - arg (1)})\ncall (1, 2, 3)\nls\nedit (1)\nprog (1, {arg (2) + arg (1)})\nedit (1)\ndel (1)\nquit' | calc.exe -v 3 | grep -q bye |
| 386 | // test: echo -e 'prog (2, {arg (2) - arg (1)})\nprog (3, cos(arg (1) * pi / 3))\ncall (1, 2, 3)\ncall (2, 1)\nls\nedit (1)\ndel (1)\n\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 5 = |
| 387 | // test: echo -e 'erf (1)\nerfc (1)\nquit' | calc.exe -v 3 | grep -q bye |
| 388 | // test: echo -e 'erf ()\nerfc ()' | calc.exe | grep -c error | xargs test 2 = |
| 389 | // test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\nget (4)' | calc.exe | grep -q '=> 3.11' |
| 390 | // test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\npop\npop\n5\npush\nshow' | calc.exe | grep -q 'stack: 1 2 3 3.11 5' |
| 391 | // test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\nset\nput (3, 4)\nshow' | calc.exe | grep -q 'stack: 0 0 4' |
| 392 | // test: echo -e 'set (0, -1)\nset (1, 2, 3, 3.11, pi, 4)\nlen' | calc.exe | grep -q '=> 6' |
| 393 | // test: echo -e 'set (1, 2)\npop\npush (3)\nput (5, -1)\nlen\nshow\nget (3)\nquit' | calc.exe -n -v 3 | grep -q bye |
| 394 | // test: echo -e 'put\nget\nget (1)\npop\nput (0)' | calc.exe | grep -c 'error' | xargs test 5 = |
| 395 | // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show})\ncall (1, 10)\nprog (1, {mem (1), sto (1, cos (arg (1)))})\ncall (1, pi / 2)\ndel (1)' | calc.exe -n | grep -q 'stack: 1 2 10' |
| 396 | // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nshow\ndel (1)' | calc.exe -n | grep -q 'stack:$' |
| 397 | // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nlen' | calc.exe -n | grep -q '=> 0' |
| 398 | // test: echo -e 'prog (1, {mem (2), sto (1, arg (1) - rcl (1) / 2)})\ncall (1, 1)\ncall (1, 2)\ncall (1, 3)\nprog (1, {mem (2), sto (2, rcl (1)), sto (1, arg (1)), rcl (2)})\ncall (1, 10)' | calc.exe -n | grep -q '=> 2.25' |
| 399 | // test: echo -e 'set (1, 2, -5, 6, -8, 9, -2, 23, 4)\nord\nshow' | calc.exe | grep -q 'stack: -8 -5 -2 1 2 4 6 9 23' |
| 400 | // test: echo -e 'set (1, 2, -5, 6, -8, 9, -2, 23, 4)\nmin (5, -3)\nmax (-1)\nmean (1, 2)\nvar (1, 2)\nmin\nmean\nmed\nmax\nprod\nsum\nvar' | calc.exe | awk 'BEGIN { split("9 -3 -1 1.5 0.5 -8 3.33333 2 23 -794880 30 73.3333", v) } /=>/ { for (i in v) if ($2 == v[i]) n++ } END { exit n != 12 }' |
| 401 | // test: echo -e 'set (1, 2, -5, 6, -8, 9, -2, 23, 4)\nmin (5, -3)\nmax (-1)\nmin\nmean\nmed\nmax\nord\nprod\nsum\nvar\nquit' | calc.exe -n -v 3 | grep -q bye |
| 402 | // test: echo -e 'min\nmean\nmed\nmax\nprod\nsum\nvar\nord' | calc.exe -n | grep -c error | xargs test 8 = |
| 403 | // test: echo -e 'prog (1, cos(pi * arg (1))) / 4' | calc.exe | grep -c error | xargs test 1 = |
| 404 | // Gauss sequence |
| 405 | // test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) < 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})};' | calc.exe | grep -q '=> 5050' |
| 406 | |
| 407 | // Fibonacci sequence |
| 408 | // test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})};' | calc.exe | grep -q '=> 144' |
| 409 | |
| 410 | // Gold number |
| 411 | // test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 15 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)) / rcl (1))})};' | calc.exe | grep -q '=> 1.61803' |
| 412 | |
| 413 | /* vim: set ts=4 sw=4 et: */ |