commit d8d96e4c782f8f1a1d9151dfab4a49681955040d
parent 447710c421fce2c97f0321897125eed224233906
Author: kyle <kyle@getaddrinfo.net>
Date: Mon, 26 Oct 2015 20:20:48 -0600
tests: initial commit
Diffstat:
4 files changed, 112 insertions(+), 0 deletions(-)
diff --git a/run_tests.sh b/run_tests.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+make || exit 1
+# make sure we have a .c extension
+temp_file=$(mktemp).c
+
+if which tput > /dev/null; then
+ RED=`tput setaf 1 0 0`
+ GREEN=`tput setaf 2 0 0`
+ RESET=`tput sgr0`
+fi
+
+echo "starting tests"
+for t in `ls tests/*.c`; do
+ ./instrument $t -- > $temp_file
+ if ! diff -u ${t}.instrumented $temp_file; then
+ echo "$t:$RED source compare failed$RESET"
+ continue
+ fi
+
+ if ! gcc -o /tmp/bin $temp_file; then
+ # /tmp/bin won't be created here
+ echo "$t:$RED post compilation failed$RESET"
+ continue
+ fi
+
+ if ! sh ${t}.sh /tmp/bin; then
+ echo "$t:$RED tests failed!"
+ rm /tmp/bin
+ continue
+ fi
+ rm /tmp/bin
+
+ echo "$t:$GREEN ok$RESET"
+done
+
+rm $temp_file
diff --git a/tests/fibonacci.c b/tests/fibonacci.c
@@ -0,0 +1,35 @@
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+
+long long
+fibonacci(long long n)
+{
+ if (n == 0)
+ return 0;
+ else if (n == 1)
+ return 1;
+
+ return fibonacci(n - 1) + fibonacci(n - 2);
+}
+
+int
+main(int argc, char *argv[])
+{
+ long long n;
+ const char *errstr = NULL;
+
+ if (argc != 2) {
+ printf("usage: %s <N>\n", argv[0]);
+ return 1;
+ }
+
+ n = strtonum(argv[1], LONG_MIN, LONG_MAX, &errstr);
+ if (errstr)
+ err(1, "%s", errstr);
+
+ printf("result: %lli\n", fibonacci(n));
+
+ return 0;
+}
diff --git a/tests/fibonacci.c.instrumented b/tests/fibonacci.c.instrumented
@@ -0,0 +1,36 @@
+static unsigned int lines[512];
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+
+long long
+fibonacci(long long n)
+{
+ if (lines[9] = 1, n == 0)
+ return lines[10] = 1, 0;
+ else if (lines[11] = 1, n == 1)
+ return lines[12] = 1, 1;
+
+ return lines[14] = 1, fibonacci(n - 1) + fibonacci(n - 2);
+}
+
+int
+main(int argc, char *argv[])
+{
+ lines[20] = 1; long long n;
+ lines[21] = 1; const char *errstr = NULL;
+
+ if (lines[23] = 1, argc != 2) {
+ printf("usage: %s <N>\n", argv[0]);
+ return lines[25] = 1, 1;
+ }
+
+ n = strtonum(argv[1], LONG_MIN, LONG_MAX, &errstr);
+ if (lines[29] = 1, errstr)
+ err(1, "%s", errstr);
+
+ printf("result: %lli\n", fibonacci(n));
+
+ return lines[34] = 1, 0;
+}
diff --git a/tests/fibonacci.c.sh b/tests/fibonacci.c.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+/tmp/bin 10
+/tmp/bin 20