remove unnecessary code
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index 9811642f46e6bfe07559a3c1e3e1f3447837cdbd..869372f7c761af4fcca745ae048294f3a1a2fe7a 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,21 +1,23 @@
 /* depend: */
 /* cflags: */
-/* linker: atoi.o debug.o fdprintf.o parser.o */
+/* linker: debug.o parser.o -lm -lreadline */
 
-//#include <malloc.h>
+#include <malloc.h>
 #include <stddef.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 
-#include "atoi.h"
+#include <readline/readline.h>
+#include <readline/history.h>
+
 #include "debug.h"
-#include "fdprintf.h"
 #include "parser.h"
 
 /* constants */
 
-//#define BUFFER_SIZE 4096
-#define BUFFER_SIZE 256
+#define BUFFER_SIZE 4096
+#define HISTORY_LEN 10
 
 /* macros */
 
 /* gobal variables */
 
 char *progname = NULL;
+char *iprompt = "<= ";
+int mode = 1;
+char *oprompt = "=> ";
+int precision = 6;
+char **completion_list = NULL;
 
 /* help function */
 
 int usage (int ret)
 {
-    int fd = ret ? stdfderr : stdfdout;
-    fdprintf (fd, "usage: %s\n", progname);
-    fdprintf (fd, " -h : help message\n");
-    fdprintf (fd, " -v : verbose level (%d)\n", verbose);
+    FILE *fid = ret ? stderr : stdout;
+    fprintf (fid, "usage: %s\n", progname);
+    fprintf (fid, " -h : help message\n");
+    fprintf (fid, " -i : input prompt (%s)\n", iprompt);
+    fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
+    fprintf (fid, " -o : output prompt (%s)\n", oprompt);
+    fprintf (fid, " -p : precision (%d)\n", precision);
+    fprintf (fid, " -v : verbose level (%d)\n", verbose);
 
     return ret;
 }
 
+/* completion function */
+
+char *generator (const char *text, int state);
+
+char **completion (const char *text, __attribute__((unused)) int start, __attribute__((unused)) int end)
+{
+    rl_attempted_completion_over = 1;
+    return rl_completion_matches (text, generator);
+}
+
+char *generator (const char *text, int state)
+{
+    static int index, len;
+    char *name;
+
+    if (!state) {
+        index = 0;
+        len = strlen(text);
+    }
+
+    while ((name = completion_list[index++])) {
+        if (strncmp (name, text, len) == 0) {
+            return strdup (name);
+        }
+    }
+
+    return NULL;
+}
+
 /* main function */
 
-int main (int argc, char *argv[]) 
+int main (int argc, char *argv[])
 {
-    char buffer[BUFFER_SIZE + 1] = {0};
-    char *pt = buffer;
-    int i = 0, j = 0, n;
+    char *buffer = NULL;
+    char buffer_static[BUFFER_SIZE + 1] = {0};
+    int i = 0, nb = 1;
     int ret = 0;
 
     /* program name */
@@ -65,16 +105,44 @@ int main (int argc, char *argv[])
      while (argc-- > 1) {
         char *arg = *(++argv);
         if (arg[0] != '-') {
-            PRINTERR ("%s: invalid option -- %s\n", progname, arg);
-            return usage (1);
+            VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
+            return 1;
         }
         char c = arg[1];
         switch (c) {
+        case 'n':
+            mode = 0;
+            buffer = buffer_static;
+            break;
+        case 'i':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing input prompt\n", progname); usage (1));
+                return 1;
+            }
+            iprompt = arg;
+            break;
+        case 'o':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1));
+                return 1;
+            }
+            oprompt = arg;
+            break;
+        case 'p':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1));
+                return 1;
+            }
+            precision = atoi (arg);
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
-                PRINTERR ("%s: missing verbose level\n", progname);
-                return usage (1);
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1));
+                return 1;
             }
             verbose = atoi (arg);
             break;
@@ -84,76 +152,241 @@ int main (int argc, char *argv[])
         }
     }
 
+    /* set format */
+    set_format (oprompt, precision);
+
+    /* completion list*/
+    completion_list = generate_completion_list ();
+    rl_attempted_completion_function = completion;
+
     /* read from input stream */
 
-    while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
-        VERBOSE (INFO, PRINTOUT ("read %d bytes\n", n));
-        n += (pt - buffer);
-        if ((n == 2) && (buffer[0] == '.')) {
-            return 0;
-        }
+    while (1) {
+        char *line[BUFFER_SIZE] = {0};
 
-        /* look for end of line */
-        for (i = 0, j = 0; i < n; i++) {
-            if (buffer[i] == '\n') {
-                buffer[i] = 0;
-                VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j));
-                element_t *element = parser (buffer, NULL);
-                if (element == (void *)(-1)) {
-                    VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
-                    ret = 1;
-               } else {
-                    print_element (element, 0);
-                    ret = 0;
+        if (mode) {
+            if ((buffer = readline (iprompt)) == NULL) {
+                break;
+            }
+
+            /* check empty line */
+            if (strlen (buffer) == 0) {
+                free (buffer);
+                continue;
+            } else if (strcmp (buffer, ".") == 0) {
+                break;
+            }
+
+            /* add line into history */
+            add_history (buffer);
+            VERBOSE (INFO, fprintf (stdout, "line (%d/%d): '%s'\n",
+                                    where_history (), history_length, buffer));
+            if (history_length > HISTORY_LEN) {
+                HIST_ENTRY *last = remove_history (0);
+                if (last) {
+                    free_history_entry (last);
                 }
-                //fsync (stdfdout);
-                j = i + 1;
             }
+        } else {
+            printf ("%s", iprompt);
+            if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
+                break;
+            }
+            VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
         }
 
-        /* keep remainding */
-        if (j < n) {
-            for (i = 0; i < n - j; i++) {
-                buffer[i] = buffer[i + j];
+        /* pre-process buffer */
+        nb = 0;
+        char *pt = line[nb++] = buffer;
+        while (*pt != '\0') {
+            switch (*pt) {
+            case '\n':
+                *pt = '\0';
+                // fallthrough
+            case ';':
+                line[nb++] = pt + 1;
             }
-            pt = buffer + n - j;
-            for (i = n - j; i < BUFFER_SIZE; i++) {
-                buffer[i] = 0;
+            pt++;
+        }
+
+        /* look for end of line */
+        for (i = 0; i < nb; i++) {
+            if (*line[i] == '\0') {
+                continue;
+            }
+            element_t *element = parser (line[i], NULL, -9);
+            if (element == ERROR_OP) {
+                VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]));
+                ret = 1;
+            } else if (element != NULL) {
+                VERBOSE (INFO, print_element (element, 0));
+                answer = evaluate_element (element, 0);
+                if (!element->hidden) {
+                    print (answer);
+                }
+                delelement (element);
+                ret = 0;
             }
         }
+
+        if (mode) {
+            free (buffer);
+        } else {
+            memset (buffer, 0, BUFFER_SIZE);
+        }
     }
 
+    free_completion_list (completion_list);
+
+    free_format ();
+
     return ret;
 }
 
 // test: calc.exe -h
 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: echo 1 | calc.exe -v3 | grep -q value
 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
-// test: echo "1 + 2" | calc.exe
-// test: echo "1 - 2" | calc.exe
-// test: echo "1 * 2" | calc.exe
-// test: echo "1 / 2" | calc.exe
-// test: echo "2 ^ 3" | calc.exe
-// test: echo "1e-1 + 2.34e5" | calc.exe
-// test: echo "sqrt (2)" | calc.exe
-// test: echo "pow (2, 3)" | calc.exe
-// test: echo "cos (2)" | calc.exe
-// test: echo "sin (2)" | calc.exe
-// test: echo "atan (2)" | calc.exe
-// test: echo "exp (2)" | calc.exe
-// test: echo "log (2)" | calc.exe
-// test: echo "1 + 2 - 3" | calc.exe
-// test: echo "1 + cos (2 - 3)" | calc.exe
-// test: echo "1 + 4 * (2 - 3)" | calc.exe
-// test: echo "(2 - 3) / 4" | calc.exe
-// test: echo "pow (2 - 3, 8 / 3)" | calc.exe
-// test: echo "1 + -2" | calc.exe
-// test: echo "1 - +2" | calc.exe
-// test: echo "-1 + +2" | calc.exe
-// test: echo "-1+2" | calc.exe
-// test: echo "1-2" | calc.exe
-// test: echo "1 * 2 / 3 + 4" | calc.exe
-// test: echo "2 ^ 3 * 4 + cos (5)" | calc.exe
+// test: calc.exe error 2>&1 | grep -q 'invalid option'
+// test: calc.exe -i 2>&1 | grep -q 'missing input prompt'
+// test: calc.exe -o 2>&1 | grep -q 'missing output prompt'
+// test: calc.exe -p 2>&1 | grep -q 'missing precision'
+// test: calc.exe -v 2>&1 | grep -q 'missing verbose'
+// test: echo "1 + 1" | calc.exe -i '# ' | grep -q '# 1 + 1'
+// test: echo "1 + 1" | calc.exe -o '# ' | grep -q '# 2'
+// test: echo "1 + 2" | calc.exe | grep -q '=> 3'
+// test: echo "1 - 2" | calc.exe | grep -q '=> -1'
+// test: echo "2 * 3" | calc.exe | grep -q '=> 6'
+// test: echo "1 / 2" | calc.exe | grep -q '=> 0\.5'
+// test: echo "8 % 3" | calc.exe | grep -q '=> 2'
+// test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2\.8'
+// test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
+// test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2340\.1'
+// test: echo "sqrt (2)" | calc.exe | grep -q '=> 1\.41421'
+// test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
+// test: echo "cos (2)" | calc.exe | grep -q '=> -0\.416147'
+// test: echo "sin (2)" | calc.exe | grep -q '=> 0\.909297'
+// test: echo "atan (2)" | calc.exe | grep -q '=> 1\.10715'
+// test: echo "exp (2)" | calc.exe | grep -q '=> 7\.38906'
+// test: echo "log (2)" | calc.exe | grep -q '=> 0\.693147'
+// test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
+// test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1\.5403'
+// test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2\.63275'
+// test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
+// test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -0\.25'
+// test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8\.54988'
+// test: echo "1 + -2" | calc.exe | grep -q '=> -1'
+// test: echo "1 - +2" | calc.exe | grep -q '=> -1'
+// test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
+// test: echo "-1+2" | calc.exe | grep -q '=> 1'
+// test: echo "1-2" | calc.exe | grep -q '=> -1'
+// test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4\.66667'
+// test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 37'
+// test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3074'
+// test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 32\.6724'
+// test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -0.5'
+// test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5'
+// test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7'
+// test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0'
+// test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0'
+// test: echo "quit" | calc.exe | grep -q 'bye'
+// test: echo "help" | calc.exe | grep -q 'misc\.'
+// test: echo "1 + 2 *" | calc.exe | grep -q 'error'
+// test: echo "* 1 - 2" | calc.exe | grep -q 'error'
+// test: echo "2 + * 3" | calc.exe | grep -q 'error'
+// test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
+// test: echo "2 + (foo)" | calc.exe | grep -q 'error'
+// test: echo "2 + cos (pi +" | calc.exe | grep -q 'error'
+// test: echo "2 + cos (pi" | calc.exe | grep -q 'error'
+// test: echo "(2 + " | calc.exe | grep -q 'error'
+// test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
+// test: echo "sqrt 2" | calc.exe | grep -q 'error'
+// test: echo "pow (2)" | calc.exe | grep -q 'error'
+// test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
+// test: echo . | calc.exe
+// 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
+// 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
+// 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
+// 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
+// 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)\nlog (1)\nexp (1)\nabs (-1)\nceil (1.2)\nfloor (-1.2)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\ntan ()\nacos ()\nasin ()\natan ()\nlog ()\nexp ()\nabs ()\nceil ()\nfloor ()\n1 + (\n1+2(\n1+2cos\n1+2pi' | calc.exe | grep -c error | xargs test 21 =
+// test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
+// test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
+// test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
+// test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
+// test: echo -e '\n\n\n' | calc.exe -n
+// test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
+// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
+// 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'
+// test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0'
+// test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 > 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '2 > 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 < 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '2 < 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0'
+// test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0'
+// test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0'
+// test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1'
+// test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
+// test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
+// test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -qv error
+// test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
+// test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 + quit' | calc.exe | grep -q error
+// test: echo -e 'cos (quit)' | calc.exe | grep -q error
+// test: echo -e '(quit)' | calc.exe | grep -q error
+// test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 =
+// test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 4\.15888'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 1808\.04'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe  | grep -q '=> 0'
+// test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
+// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 =
+// test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1'
+// test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 =
+// test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 =
+// test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050'
+// 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 =
+// test: echo -e 'while (0, 1)' | calc.exe -v 3 | grep -q While
+// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Code'
+// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6'
+// 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 =
+// test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 =
+// test: echo -e 'print (1)' | calc.exe -v 3 | grep -q Print
+// test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2
+// test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q "invalid index"
+// test: echo -e 'sto (2, 3)\nmem (2)\ndisp' | calc.exe | grep -q 'storage: 0 3$'
+// test: echo -e 'disp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
+// test: echo -e 'sto (3, 10)\ndisp' | calc.exe | grep -q "storage: 0 0 10 0 0 0 0 0 0 0"
+// test: echo -e 'rcl (3)\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
+// test: echo -e 'inc (2)\ndisp' | calc.exe | grep -q "storage: 0 1 0 0 0 0 0 0 0 0"
+// test: echo -e 'dec (2)\ndisp' | calc.exe | grep -q "storage: 0 -1 0 0 0 0 0 0 0 0"
+// test: echo -e 'sto (3, pi)\nclr\ndisp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
+// test: echo -e 'mem (3)\nclr' | calc.exe -v 3 | grep -q Clear
+// test: echo -e 'prog (2, 2, {rcl (2) - rcl (1)})\nprog (1, 1, {cos (rcl (1)^2)})\ncall (1, pi/6)\nprog (2, 1, {rcl (1) * 3})\ncall (2, 1, 2)\nls' | calc.exe
+// test: echo -e 'prog (1, 2, {rcl (2) - rcl (1)})\ncall (1, 2, 3)\nls\nedit (1)\nprog (1, 2, {rcl (2) + rcl (1)})\nedit (1)\ndel (1)' | calc.exe -v 3 |grep -q Program
+// test: echo -e 'prog (2, 2, {rcl (2) - rcl (1)})\nprog (3, 1, cos(rcl (1) * pi / 3))\ncall (1, 2, 3)\nls\nedit (1)\ndel (1)\n\ndel (2)\ncall (2, 1, 4)' | calc.exe | grep -c error | xargs test 4 =
+
+// Gauss sequence
+// 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'
+
+// Fibonacci sequence
+// 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'
+
+// Gold number
+// 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'
 
 /* vim: set ts=4 sw=4 et: */