Merge branch 'master' of https://secure.softndesign.org/git/calc
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index d4d224387294d1c41ab7ac3b3492ff494a937249..5014f3ef8e4c5342ce0eb278dc0b9be66a169e8a 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,34 +1,32 @@
 /* depend: */
 /* cflags: */
-/* linker: debug.o parser.o -lm -lreadline */
+/* 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 */
 
 #include <malloc.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
-#include <readline/readline.h>
-#include <readline/history.h>
-
 #include "debug.h"
+#include "element.h"
+#include "format.h"
 #include "parser.h"
+#include "readline.h"
 
 /* constants */
 
 #define BUFFER_SIZE 4096
-#define HISTORY_LEN 10
 
 /* macros */
 
-#define CEIL(x, y) (((x) + (y) - 1) / (y))
-#define MIN(x, y) (((x) < (y)) ? (x) : (y))
-#define MAX(x, y) (((x) > (y)) ? (x) : (y))
-
 /* gobal variables */
 
 char *progname = NULL;
+char *iprompt = "<= ";
 int mode = 1;
+char *oprompt = "=> ";
 int precision = 6;
 
 /* help function */
@@ -38,7 +36,10 @@ int usage (int ret)
     FILE *fid = ret ? stderr : stdout;
     fprintf (fid, "usage: %s\n", progname);
     fprintf (fid, " -h : help message\n");
+    fprintf (fid, " -b : in/out-put base (%s)\n", show_base ());
+    fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
     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);
 
@@ -51,7 +52,7 @@ int main (int argc, char *argv[])
 {
     char *buffer = NULL;
     char buffer_static[BUFFER_SIZE + 1] = {0};
-    int i = 0, nb = 1;
+    int i = 0, nb = 1, in, out;
     int ret = 0;
 
     /* program name */
@@ -74,19 +75,54 @@ int main (int argc, char *argv[])
             VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
             return 1;
         }
+        char *pt = NULL;
         char c = arg[1];
         switch (c) {
+        case 'b':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing base definition\n", progname); usage (1));
+                return 1;
+            }
+            in = out = strtol (arg, &pt, 10);
+            if ((*pt == ' ') || (*pt == ',') || (*pt == '-')) {
+                pt++;
+                if (*pt == '\0') {
+                    VERBOSE (ERROR, fprintf (stderr, "%s: missing output base definition\n", progname); usage (1));
+                    return 1;
+                } else {
+                    out = atoi (pt);
+                }
+            }
+            set_base (in, out);
+            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 'n':
             mode = 0;
             buffer = buffer_static;
             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;
+            }
+            set_prompt (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);
+            set_precision (precision = atoi (arg));
             break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
@@ -102,9 +138,13 @@ int main (int argc, char *argv[])
         }
     }
 
-    /* format */
-    char format[9] = "=> %..g\n";
-    format[5] = '0' + precision;
+    /* set format */
+    set_format ();
+
+    /* init readline */
+    if (mode) {
+        init_read_line ();
+    }
 
     /* read from input stream */
 
@@ -112,44 +152,41 @@ int main (int argc, char *argv[])
         char *line[BUFFER_SIZE] = {0};
 
         if (mode) {
-            if ((buffer = readline ("<= ")) == NULL) {
+            if (read_line (&buffer, iprompt)) {
                 break;
             }
 
             /* check empty line */
-            if (strlen (buffer) == 0) {
-                free (buffer);
+            if (buffer == NULL) {
                 continue;
-            } else if (strcmp (buffer, ".") == 0) {
-                break;
             }
-            line[0] = buffer;
-            nb = 1;
 
             /* 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);
-                }
-            }
+            manage_history (buffer);
+
         } else {
+            printf ("%s", iprompt);
             if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
                 break;
             }
-            nb = 0;
-            char *pt = line[nb++] = buffer;
-            while (*pt != '\0') {
-                if (*pt == '\n') {
-                    *pt = '\0';
-                    line[nb++] = pt + 1;
-                }
-                pt++;
+        }
+
+        if (strcmp (buffer, ".") == 0) {
+            break;
+        }
+
+        /* 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;
             }
-            VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
+            pt++;
         }
 
         /* look for end of line */
@@ -159,13 +196,14 @@ int main (int argc, char *argv[])
             }
             element_t *element = parser (line[i], NULL, -9);
             if (element == ERROR_OP) {
-                VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout));
+                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);
-                fprintf (stdout, format, answer);
-                fflush (stdout);
+                if (!element->hidden) {
+                    print (answer);
+                }
                 delelement (element);
                 ret = 0;
             }
@@ -176,8 +214,13 @@ int main (int argc, char *argv[])
         } else {
             memset (buffer, 0, BUFFER_SIZE);
         }
+        fflush (stdout);
     }
 
+    clean_read_line (buffer);
+
+    free_format ();
+
     return ret;
 }
 
@@ -187,8 +230,17 @@ int main (int argc, char *argv[])
 // 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: calc.exe error 2>&1 | grep -q 'invalid option'
+// test: calc.exe -b 2>&1 | grep -q 'missing base'
+// test: calc.exe -b 10, 2>&1 | grep -q 'missing output base'
+// test: calc.exe -b 10- 2>&1 | grep -q 'missing output base'
+// test: calc.exe -b '10 ' 2>&1 | grep -q 'missing output base'
+// 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 + 1" | calc.exe -o '# ' -o 'x ' | grep -q 'x 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'
@@ -203,7 +255,8 @@ int main (int argc, char *argv[])
 // 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 "ln (2)" | calc.exe | grep -q '=> 0\.693147'
+// test: echo "log (10)" | calc.exe | grep -q '=> 1'
 // 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'
@@ -226,6 +279,7 @@ int main (int argc, char *argv[])
 // 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 'miscellaneous'
+// test: echo -e '1 + 1\nhist' | calc.exe | grep -q '2: 1 + 1'
 // 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'
@@ -237,21 +291,21 @@ int main (int argc, char *argv[])
 // 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 "1.23456789" | calc.exe -p 4 | grep -q '=> 1\.235'
+// test: echo . | calc.exe | grep -qv error
 // 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 '-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
+// 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 =
 // 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 '\n\n\n' | calc.exe -n | grep -qv 'error'
 // 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 (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c error | 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'
@@ -276,30 +330,90 @@ int main (int argc, char *argv[])
 // 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 '1 & 1\n1 | 1\n!1\nquit' | calc.exe -n -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 'sto (2, 3)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe  | grep -q '=> 4\.15888'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64), exp (75 / 10))' | calc.exe  | grep -q '=> 1808\.04'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, ln (64))' | calc.exe  | grep -q '=> 0'
+// test: echo -e 'cond (0, 1, 2)\nquit' | 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 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6\nquit' | 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 'inc (11)\ndec (0)' | calc.exe | grep -c error | xargs test 2 =
+// test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 4950'
 // 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 'Program'
+// test: echo -e 'while (0, 1)\nquit' | calc.exe -v 3 | grep -q While
+// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}\nquit' | 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)\nquit' | calc.exe -v 3 | grep -q Print
+// test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
+// test: echo -e '\t\t' | calc.exe | grep -q 'print'
+// test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2
+// test: echo -e 'mem\nmem (3)\nsto (4, pi)' | calc.exe | grep -q "error out of bound"
+// test: echo -e 'mem (-1)' | calc.exe | grep -q "error"
+// 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 'clr\nsto (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\nquit' | calc.exe -v 3 | grep -q Clear
+// 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'
+// 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
+// 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 =
+// 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:$'
+// test: echo -e 'erf (1)\nerfc (1)\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e 'erf ()\nerfc ()' | calc.exe | grep -c error | xargs test 2 =
+// test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\nget (4)' | calc.exe | grep -q '=> 3.11'
+// 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'
+// test: echo -e 'set (1, 2, 3, 3.11, pi, 4)\nset\nput (3, 4)\nshow' | calc.exe | grep -q 'stack: 0 0 4'
+// test: echo -e 'set (0, -1)\nset (1, 2, 3, 3.11, pi, 4)\nlen' | calc.exe | grep -q '=> 6'
+// 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
+// test: echo -e 'put\nget\nget (1)\npop\nput (0)' | calc.exe | grep -c 'error' | xargs test 5 =
+// test: echo -e 'push (2)' | calc.exe | grep -q '=> 2'
+// 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'
+// test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nshow\ndel (1)' | calc.exe -n | grep -q 'stack:$'
+// test: echo -e 'prog (1, {set (1, 2), push (arg (1)), show});\ncall (1, 10);\nlen' | calc.exe -n | grep -q '=> 0'
+// 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'
+// 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'
+// 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 }'
+// 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
+// test: echo -e 'min\nmean\nmed\nmax\nprod\nsum\nvar\nord\nset (1)\nord' | calc.exe -n | grep -c error | xargs test 9 =
+// test: echo -e 'prog (1, cos(pi * arg (1))) / 4' | calc.exe | grep -c error | xargs test 1 = 
+// test: echo -e 'format\n.12345678901' | calc.exe | grep -n '=> 6'
+// test: echo -e 'format (8)\n.12345678901' | calc.exe | grep -n '=> 0.12345679'
+// test: echo -e 'format (12)\n.12345678901' | calc.exe | grep -n '=> 0.12345678901'
+// test: echo -e 'format (4)\n.12345678901\format' | calc.exe | grep -n '=> 4'
+// test: echo -e 'format (0)' | calc.exe | grep -n 'error'
+// test: echo -e 'ff + ff' | calc.exe -b 16 | grep -q '=> 1fe'
+// test: echo -e '60 / 4' | calc.exe -b 8 | grep -q '=> 14'
+// test: echo -e 'z00-z0+1-2*z+20x' | calc.exe -b 36 | grep -q '=> 1000'
+// test: echo -e '255' | calc.exe -b 10,16 | grep -q '=> ff'
+// test: echo -e 'base (-2)\nbase (16, 0)' | calc.exe | grep -c error | xargs test 2 =
+// test: echo -e 'base (10, 16)\n255' | calc.exe | grep -q '=> ff'
+// 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'
+// test: echo -e 'base' | calc.exe | grep -q 'base (I/O): 10/10'
+// 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 }'
+// test: echo -e 'format\nbase\ndeg\ngrad\nrad\nquit' | calc.exe -v 3 | grep -q bye
+
+// 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)), sto (2, rcl (3))})}' | calc.exe | grep '=> 144'
+// 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'
+
+// Factorial sequence
+// 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'
 
 /* vim: set ts=4 sw=4 et: */