correct a test
[brainfuck.git] / bf.c
diff --git a/bf.c b/bf.c
index c71b46785fcc047d10da40120bcf3c91b7db1917..552c5b0bc30dd992a67495446bf7b3d51d8c33f7 100644 (file)
--- a/bf.c
+++ b/bf.c
@@ -2,7 +2,7 @@
 /* cflags: */
 /* linker: debug.o */
 
-#include <assert.h>
+#include <errno.h>
 #include <getopt.h>
 #include <malloc.h>
 #include <stdio.h>
@@ -186,7 +186,7 @@ int main (int argc, char *argv[])
                 }
             }
             if (*optarg != 0) {
-                VERBOSE (WARNING, fprintf (stderr, "%s: too many memory values\n", progname));
+                VERBOSE (WARNING, fprintf (stderr, "%s: too many memory values or incorrect value\n", progname));
                 return 1;
             }
             break;
@@ -248,6 +248,7 @@ int main (int argc, char *argv[])
         fid = fopen (output, "w");
         if (fid == NULL) {
             VERBOSE (ERROR, fprintf (stderr, "%s: can't open file '%s' for writing\n", progname, output));
+            if (buffer) free (buffer);
             return 1;
         }
     } else {
@@ -262,6 +263,11 @@ int main (int argc, char *argv[])
         fclose (fid);
     }
 
+    /* free buffer */
+    if (buffer) {
+        free (buffer);
+    }
+
     VERBOSE (INFO, fprintf (stdout, "\nmemory:"); int _i; for (_i = 0; _i < MEMSIZE; _i++) fprintf (stdout," %d", mem[_i]); fprintf (stdout,"\n"));
 
     return rc;
@@ -269,15 +275,35 @@ int main (int argc, char *argv[])
 
 // test: bf.exe -h
 // test: bf.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+
 // test: bf.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: bf.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: bf.exe error 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+
 // test: bf.exe -i error.b 2>&1 | grep "can't open" | grep -q "reading"
 // test: echo ">>." | bf.exe -o error/error.b 2>&1 | grep "can't open" | grep -q "writing"
+// test: echo "error" | bf.exe -v1 2>&1 | grep -q "can't understand"
+// test: echo "<+" | bf.exe -v1 2>&1 | grep -q "invalid address"
+// test: echo "<-" | bf.exe -v1 2>&1 | grep -q "invalid address"
+// test: echo "<." | bf.exe -v1 2>&1 | grep -q "invalid address"
+// test: echo "<," | bf.exe -v1 2>&1 | grep -q "invalid address"
+// test: echo "<[" | bf.exe -v1 2>&1 | grep -q "invalid address"
+// test: echo "[" | bf.exe -v1 2>&1 | grep -q "brace not closed"
+// test: echo "+[[[[[]]]]]" | bf.exe -v1 2>&1 | grep -q "too many jump"
+// test: bf.exe -m "0 1 2 3 4 5 6 7 8 9 10" -e '.' -v1 2>&1 | grep -q "too many memory values"
+// test: bf.exe -m "0 error" -e '.' -v1 2>&1 | grep -q "incorrect value"
+// test: echo "]" | bf.exe -v1 2>&1 | grep -q "can't jump back"
+
 // test: echo '+++>++>>-<--' | bf.exe -v2 | grep -q "memory: 3 2 -2 -1 0"
+// test: bf.exe -e '+++' -e '+++>++>>-<--' -v2 | grep -q "memory: 3 2 -2 -1 0"
 // test: bf.exe -e '+++>++>>-<--' -v2 | grep -q "memory: 3 2 -2 -1 0"
 // test: bf.exe -m "51 50 49" -e '.>.>.' |grep -q "321"
+// test: bf.exe -m "51 50 49" -e '.>.>.' -o test.log && grep -q "321" test.log; rc=$?; rm test.log; test $rc -eq 0
+// test: echo '+++>++>>-<--' > test.b; bf.exe -i test.b | grep -q "memory: 3 2 -2 -1 0"; rc=$?; rm test.b; test $rc -eq 0
+
 // test: echo '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' | bf.exe -v1 | grep -q "Hello World!"
 // test: echo '++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++++++++++++++.>+++++++++++++++++.<<++.>+++++++++++++.>--.<<.>+++.+.--.>----.++++++.+.<++.>----.++.<<.>>+.-------.<<.>>++.<.>+++++.<<.>-.+.<.>---.>---.<-.++++++++.>----.<---.>+++++++.<---.++++++++.' | bf.exe -v1 | grep -q "Tu as decouvert un peu de brainfuck"
+
 // test: echo -e "123\0" | bf.exe -e ',[>,]' -v2 | grep -q "memory: 49 50 51 0"
 // test: echo -e "4+3\0" | bf.exe -e ',>++++++[<-------->-],,[<+>-]<.' -v1 | grep -q 7
 // test: echo -e "1+7\0" | bf.exe -e ',>++++++[<-------->-],,[<+>-]<.' -v1 | grep -q 8