add unit tests
authorLaurent Mazet <mazet@softndesign.org>
Tue, 27 Dec 2022 19:38:39 +0000 (20:38 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 27 Dec 2022 19:38:39 +0000 (20:38 +0100)
calc.c
debug.c
makefile
parser.c

diff --git a/calc.c b/calc.c
index d2fdc8c231ae7a43c2244d9adae1cc5642826490..ba187005fa321f27c8514a32514c225275207f48 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: atoi.o fdprintf.o parser.o */
+/* linker: atoi.o debug.o fdprintf.o parser.o */
 
 //#include <malloc.h>
 #include <stddef.h>
@@ -26,7 +26,6 @@
 /* gobal variables */
 
 char *progname = NULL;
-int verbose = 2;
 
 /* help function */
 
@@ -47,6 +46,7 @@ int main (int argc, char *argv[])
     char buffer[BUFFER_SIZE + 1] = {0};
     char *pt = buffer;
     int i = 0, j = 0, n;
+    int ret = 0;
 
     /* program name */
 
@@ -101,8 +101,10 @@ int main (int argc, char *argv[])
                 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;
                 }
                 //fsync (stdfdout);
                 j = i + 1;
@@ -121,13 +123,34 @@ int main (int argc, char *argv[])
         }
     }
 
-    return 0;
+    return ret;
 }
 
 // test: calc.exe -h
 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
 // 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 "foo\nbar\nfoobar" | calc.exe -v3
+// 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
 
 /* vim: set ts=4 sw=4 et: */
diff --git a/debug.c b/debug.c
index f2ff1ce41b100cfef07e510b599cf4d1fdd28ef4..8c18ffd94f11e9504e73a7bd49c164d633562614 100644 (file)
--- a/debug.c
+++ b/debug.c
@@ -1,5 +1,5 @@
 #include "debug.h"
 
-int verbose = 2;
+int verbose = 1;
 
 /* vim: set ts=4 sw=4 et */
index 34218b4f15a8fe793b568ec3bfcc8a647cd98372..99f9d6ea0e37ee3e65be817502a3b7e35f13806c 100644 (file)
--- a/makefile
+++ b/makefile
@@ -18,7 +18,7 @@ LDFLAGS += -g
 
 ALLEXE  =
 ALLEXE += calc
-ALLEXE += skel
+#ALLEXE += skel
 
 SHELL = bash
 
index 48fb98009b9ace46f0df19152c1a0ed8a4957a51..e7c91888f15cec4380aabf2d6b65537a06ca200a 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -87,14 +87,34 @@ element_t *parser (char *str, char **next)
         /* skip commas */
 
         if (*str == ',') {
+            VERBOSE (DEBUG, PRINTOUT ("start processing coma\n"));
+            str++;
             if (root == NULL) {
-                return ERROR_OP;
+                return parser (str, &str);
             } else if (root->func != Set) {
                 new = newelement (Set, MAX_OPERANDS);
+                if (new == NULL) {
+                    return ERROR_OP;
+                }
                 new->ops[0] = root;
                 root = new;
+                VERBOSE (DEBUG, PRINTOUT ("end processing first coma\n"));
+            } else /* if (root->func == Set) */ {
+                new = parser (str, &str);
+                if (!found){
+                    return ERROR_OP;
+                }
+                for (i = 0; i < root->nbops; i++) {
+                    if (root->ops[i] == NULL) {
+                        root->ops[i] = new;
+                        found = 1;
+                    }
+                }
+                if (!found){
+                    return ERROR_OP;
+                }
+                VERBOSE (DEBUG, PRINTOUT ("end processing other coma\n"));
             }
-            str++;
             continue;
         }
 
@@ -125,12 +145,19 @@ element_t *parser (char *str, char **next)
                 if (!found) {
                     return ERROR_OP;
                 }
+            } else {
+                if (new->func != Set) {
+                    root = new;
+                } else {
+                    return ERROR_OP;
+                }
             }
             VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
-            if (next != NULL) {
-                *next = str;
-            }
-            return root;
+            //if (next != NULL) {
+            //    *next = str;
+            //}
+            //return root;
+            continue;
         }
         if (*str == ')') {
             if (next != NULL) {