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