98372cef9f0c62d66a04b05619775ecb043d3661
[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 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 <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, " -b : in/out-put base (%s)\n", show_base ());
47 fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
48 fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
49 fprintf (fid, " -o : output prompt (%s)\n", oprompt);
50 fprintf (fid, " -p : precision (%d)\n", precision);
51 fprintf (fid, " -v : verbose level (%d)\n", verbose);
52
53 return ret;
54 }
55
56 /* completion function */
57
58 char *generator (const char *text, int state);
59
60 char **completion (const char *text, __attribute__((unused)) int start, __attribute__((unused)) int end)
61 {
62 rl_attempted_completion_over = 1;
63 return rl_completion_matches (text, generator);
64 }
65
66 char *generator (const char *text, int state)
67 {
68 static int index, len;
69 char *name;
70
71 if (!state) {
72 index = 0;
73 len = strlen(text);
74 }
75
76 while ((name = completion_list[index++])) {
77 if (strncmp (name, text, len) == 0) {
78 return strdup (name);
79 }
80 }
81
82 return NULL;
83 }
84
85 /* edit line */
86 char *edit_line = NULL;
87 int edit_hook ()
88 {
89 static int state = 0;
90 if (edit_line) {
91 if (state == 0) {
92 state = 1;
93 } else {
94 state = 0;
95 free (edit_line);
96 edit_line = NULL;
97 }
98 }
99 return rl_insert_text (edit_line);
100 }
101
102 /* history management */
103
104 void history ()
105 {
106 HIST_ENTRY **entries = history_list ();
107 if (entries == NULL) {
108 VERBOSE (WARNING, fprintf (stdout, "no history\n"));
109 } else {
110 int i = 0;
111 while (entries[i] != NULL) {
112 printf ("%d: %s\n", history_length - i, entries[i]->line);
113 i++;
114 }
115 }
116 }
117
118 /* main function */
119
120 int main (int argc, char *argv[])
121 {
122 char *buffer = NULL;
123 char buffer_static[BUFFER_SIZE + 1] = {0};
124 int i = 0, nb = 1, in, out;
125 int ret = 0;
126
127 /* program name */
128
129 progname = argv[0];
130 while (progname[i] != '\0') {
131 if ((progname[i] == '/') || (progname[i] == '\\')) {
132 progname += i + 1;
133 i = 0;
134 } else {
135 i++;
136 }
137 }
138
139 /* argument processing */
140
141 while (argc-- > 1) {
142 char *arg = *(++argv);
143 if (arg[0] != '-') {
144 VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
145 return 1;
146 }
147 char *pt = NULL;
148 char c = arg[1];
149 switch (c) {
150 case 'b':
151 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
152 if (arg == NULL) {
153 VERBOSE (ERROR, fprintf (stderr, "%s: missing base definition\n", progname); usage (1));
154 return 1;
155 }
156 in = out = strtol (arg, &pt, 10);
157 if ((*pt == ' ') || (*pt == ',') || (*pt == '-')) {
158 pt++;
159 if (*pt == '\0') {
160 VERBOSE (ERROR, fprintf (stderr, "%s: missing output base definition\n", progname); usage (1));
161 return 1;
162 } else {
163 out = atoi (pt);
164 }
165 }
166 set_base (in, out);
167 break;
168 case 'i':
169 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
170 if (arg == NULL) {
171 VERBOSE (ERROR, fprintf (stderr, "%s: missing input prompt\n", progname); usage (1));
172 return 1;
173 }
174 iprompt = arg;
175 break;
176 case 'n':
177 mode = 0;
178 buffer = buffer_static;
179 break;
180 case 'o':
181 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
182 if (arg == NULL) {
183 VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1));
184 return 1;
185 }
186 set_prompt (oprompt = arg);
187 break;
188 case 'p':
189 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
190 if (arg == NULL) {
191 VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1));
192 return 1;
193 }
194 set_precision (precision = atoi (arg));
195 break;
196 case 'v':
197 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
198 if (arg == NULL) {
199 VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1));
200 return 1;
201 }
202 verbose = atoi (arg);
203 break;
204 case 'h':
205 default:
206 return usage (c != 'h');
207 }
208 }
209
210 /* set format */
211 set_format ();
212
213 /* completion list*/
214 completion_list = generate_completion_list ();
215 rl_attempted_completion_function = completion;
216
217 /* readline parameters */
218 rl_startup_hook = edit_hook;
219 rl_variable_bind ("blink-matching-paren", "On");
220 //rl_set_screen_size (50, 40);
221
222 /* read from input stream */
223
224 while (1) {
225 char *line[BUFFER_SIZE] = {0};
226
227 if (mode) {
228 if ((buffer = readline (iprompt)) == NULL) {
229 break;
230 }
231
232 /* check empty line */
233 if (strlen (buffer) == 0) {
234 free (buffer);
235 continue;
236 } else if (strcmp (buffer, ".") == 0) {
237 free (buffer);
238 break;
239 }
240
241 /* add line into history */
242 add_history (buffer);
243 VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n",
244 where_history (), history_length, buffer));
245 if (history_length > HISTORY_LEN) {
246 HIST_ENTRY *last = remove_history (0);
247 if (last) {
248 free_history_entry (last);
249 }
250 }
251 } else {
252 printf ("%s", iprompt);
253 if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
254 break;
255 }
256 VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
257 }
258
259 /* pre-process buffer */
260 nb = 0;
261 char *pt = line[nb++] = buffer;
262 while (*pt != '\0') {
263 switch (*pt) {
264 case '\n':
265 *pt = '\0';
266 // fallthrough
267 case ';':
268 line[nb++] = pt + 1;
269 }
270 pt++;
271 }
272
273 /* look for end of line */
274 for (i = 0; i < nb; i++) {
275 if (*line[i] == '\0') {
276 continue;
277 }
278 element_t *element = parser (line[i], NULL, -9);
279 if (element == ERROR_OP) {
280 VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]));
281 ret = 1;
282 } else if (element != NULL) {
283 VERBOSE (INFO, print_element (element, 0));
284 answer = evaluate_element (element, 0);
285 if (!element->hidden) {
286 print (answer);
287 }
288 delelement (element);
289 ret = 0;
290 }
291 }
292
293 if (mode) {
294 free (buffer);
295 } else {
296 memset (buffer, 0, BUFFER_SIZE);
297 }
298 fflush (stdout);
299 }
300
301 free_completion_list (completion_list);
302
303 free_format ();
304
305 return ret;
306 }
307
308 // test: calc.exe -h
309 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
310 // test: echo 1 | calc.exe -v3 | grep -q value
311 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
312 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
313 // test: calc.exe error 2>&1 | grep -q 'invalid option'
314 // test: calc.exe -b 2>&1 | grep -q 'missing base'
315 // test: calc.exe -b 10, 2>&1 | grep -q 'missing output base'
316 // test: calc.exe -b 10- 2>&1 | grep -q 'missing output base'
317 // test: calc.exe -b '10 ' 2>&1 | grep -q 'missing output base'
318 // test: calc.exe -i 2>&1 | grep -q 'missing input prompt'
319 // test: calc.exe -o 2>&1 | grep -q 'missing output prompt'
320 // test: calc.exe -p 2>&1 | grep -q 'missing precision'
321 // test: calc.exe -v 2>&1 | grep -q 'missing verbose'
322 // test: echo "1 + 1" | calc.exe -i '# ' | grep -q '# 1 + 1'
323 // test: echo "1 + 1" | calc.exe -o '# ' | grep -q '# 2'
324 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
325 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
326 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
327 // test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5'
328 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
329 // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8'
330 // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
331 // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1'
332 // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421'
333 // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
334 // test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147'
335 // test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
336 // test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
337 // test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
338 // test: echo "ln (2)" | calc.exe | grep -q '=> 0\.693147'
339 // test: echo "log (10)" | calc.exe | grep -q '=> 1'
340 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
341 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
342 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
343 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
344 // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
345 // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
346 // test: echo "1 + -2" | calc.exe | grep -q '=> -1'
347 // test: echo "1 - +2" | calc.exe | grep -q '=> -1'
348 // test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
349 // test: echo "-1+2" | calc.exe | grep -q '=> 1'
350 // test: echo "1-2" | calc.exe | grep -q '=> -1'
351 // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667'
352 // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37'
353 // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074'
354 // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724'
355 // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5'
356 // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5'
357 // test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7'
358 // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0'
359 // test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0'
360 // test: echo "quit" | calc.exe | grep -q 'bye'
361 // test: echo "help" | calc.exe | grep -q 'miscellaneous'
362 // test: echo -e '1 + 1\nhist' | calc.exe | grep -q '2: 1 + 1'
363 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
364 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
365 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
366 // test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
367 // test: echo "2 + (foo)" | calc.exe | grep -q 'error'
368 // test: echo "2 + cos (pi +" | calc.exe | grep -q 'error'
369 // test: echo "2 + cos (pi" | calc.exe | grep -q 'error'
370 // test: echo "(2 + " | calc.exe | grep -q 'error'
371 // test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
372 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
373 // test: echo "pow (2)" | calc.exe | grep -q 'error'
374 // test: echo "1.23456789" | calc.exe -p 4 | grep -q '=> 1\.235'
375 // test: echo . | calc.exe | grep -qv error
376 // 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
377 // 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
378 // 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
379 // 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
380 // 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
381 // 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 =
382 // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
383 // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
384 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
385 // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
386 // test: echo -e '\n\n\n' | calc.exe -n | grep -qv 'error'
387 // test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
388 // test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c error | xargs test 4 =
389 // 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'
390 // test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
391 // test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
392 // test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0'
393 // test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1'
394 // test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0'
395 // test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1'
396 // test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1'
397 // test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1'
398 // test: echo -e '1 > 2' | calc.exe | grep -q '=> 0'
399 // test: echo -e '2 > 2' | calc.exe | grep -q '=> 0'
400 // test: echo -e '1 < 2' | calc.exe | grep -q '=> 1'
401 // test: echo -e '2 < 2' | calc.exe | grep -q '=> 0'
402 // test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye
403 // test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1'
404 // test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0'
405 // test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0'
406 // test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0'
407 // test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1'
408 // test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1'
409 // test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1'
410 // test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
411 // test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
412 // test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
413 // test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -n -v 3 | grep -qv error
414 // test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
415 // test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
416 // test: echo -e '1 + quit' | calc.exe | grep -q error
417 // test: echo -e 'cos (quit)' | calc.exe | grep -q error
418 // test: echo -e '(quit)' | calc.exe | grep -q error
419 // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 =
420 // test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe | grep -q '=> 4\.15888'
421 // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04'
422 // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64))' | calc.exe | grep -q '=> 0'
423 // test: echo -e 'cond (0, 1, 2)\nquit' | calc.exe -v 3 | grep -q Cond
424 // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 =
425 // 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'
426 // test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 =
427 // test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c error | xargs test 2 =
428 // test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 4950'
429 // 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 =
430 // test: echo -e 'while (0, 1)\nquit' | calc.exe -v 3 | grep -q While
431 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}\nquit' | calc.exe -v 3 | grep -q 'Code'
432 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6'
433 // 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 =
434 // test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 =
435 // test: echo -e 'print (1)\nquit' | calc.exe -v 3 | grep -q Print
436 // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
437 // test: echo -e '\t\t' | calc.exe | grep -q 'print'
438 // test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2
439 // test: echo -e 'mem\nmem (3)\nsto (4, pi)' | calc.exe | grep -q "error out of bound"
440 // test: echo -e 'mem (-1)' | calc.exe | grep -q "error"
441 // test: echo -e 'sto (2, 3)\nmem (2)\ndisp' | calc.exe | grep -q 'storage: 0 3$'
442 // test: echo -e 'disp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
443 // test: echo -e 'sto (3, 10)\ndisp' | calc.exe | grep -q "storage: 0 0 10 0 0 0 0 0 0 0"
444 // test: echo -e 'rcl (3)\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
445 // test: echo -e 'inc (2)\ndisp' | calc.exe | grep -q "storage: 0 1 0 0 0 0 0 0 0 0"
446 // test: echo -e 'dec (2)\ndisp' | calc.exe | grep -q "storage: 0 -1 0 0 0 0 0 0 0 0"
447 // test: echo -e 'clr\nsto (3, pi)\nclr\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
448 // test: echo -e 'mem (3)\nclr\nquit' | calc.exe -v 3 | grep -q Clear
449 // 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'
450 // 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
451 // 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)\ndel (3)\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 5 =
452 // 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:$'
453 // test: echo -e 'erf (1)\nerfc (1)\nquit' | calc.exe -v 3 | grep -q bye
454 // test: echo -e 'erf ()\nerfc ()' | calc.exe | grep -c error | xargs test 2 =
455 // test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\nget (4)' | calc.exe | grep -q '=> 3.11'
456 // 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'
457 // test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\nset\nput (3, 4)\nshow' | calc.exe | grep -q 'stack: 0 0 4'
458 // test: echo -e 'set (0, -1)\nset (1, 2, 3, 3.11, pi, 4)\nlen' | calc.exe | grep -q '=> 6'
459 // 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
460 // test: echo -e 'put\nget\nget (1)\npop\nput (0)' | calc.exe | grep -c 'error' | xargs test 5 =
461 // test: echo -e 'push (2)' | calc.exe | grep -q '=> 2'
462 // 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'
463 // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nshow\ndel (1)' | calc.exe -n | grep -q 'stack:$'
464 // test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nlen' | calc.exe -n | grep -q '=> 0'
465 // 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'
466 // 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'
467 // 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 }'
468 // 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
469 // test: echo -e 'min\nmean\nmed\nmax\nprod\nsum\nvar\nord\nset (1)\nord' | calc.exe -n | grep -c error | xargs test 9 =
470 // test: echo -e 'prog (1, cos(pi * arg (1))) / 4' | calc.exe | grep -c error | xargs test 1 =
471 // test: echo -e 'format\n.12345678901' | calc.exe | grep -n '=> 6'
472 // test: echo -e 'format (8)\n.12345678901' | calc.exe | grep -n '=> 0.12345679'
473 // test: echo -e 'format (12)\n.12345678901' | calc.exe | grep -n '=> 0.12345678901'
474 // test: echo -e 'format (4)\n.12345678901\format' | calc.exe | grep -n '=> 4'
475 // test: echo -e 'format (0)' | calc.exe | grep -n 'error'
476 // test: echo -e 'ff + ff' | calc.exe -b 16 | grep -q '=> 1fe'
477 // test: echo -e '60 / 4' | calc.exe -b 8 | grep -q '=> 14'
478 // test: echo -e 'z00-z0+1-2*z+20x' | calc.exe -b 36 | grep -q '=> 1000'
479 // test: echo -e '255' | calc.exe -b 10,16 | grep -q '=> ff'
480 // test: echo -e 'base (-2)\nbase (16, 0)' | calc.exe | grep -c error | xargs test 2 =
481 // test: echo -e 'base (10, 16)\n255' | calc.exe | grep -q '=> ff'
482 // test: echo -e 'base' | calc.exe | grep -q 'base (I/O): 10/10'
483 // 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 }'
484 // test: echo -e 'format\nbase\ndeg\ngrad\nrad\nquit' | calc.exe -v 3 | grep -q bye
485
486 // Gauss sequence
487 // 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'
488
489 // Fibonacci sequence
490 // 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'
491
492 // Gold number
493 // 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'
494
495 // Factorial sequence
496 // 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'
497
498 /* vim: set ts=4 sw=4 et: */