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