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