aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2025-11-16 12:11:18 +0100
committerlemon <lsof@mailbox.org>2025-11-16 12:11:18 +0100
commit111e71e1511b2abff9176bd6c714c8da796f770e (patch)
tree352b723c9144c844037447bd865a8366378df7a5
parentb0c0f2d2d885c5901de08ed08dba9fff079bf6e3 (diff)
basic automated testing
-rw-r--r--.gitignore2
-rw-r--r--test/01-hello.c (renamed from test/hello.c)4
-rw-r--r--test/02-fib.c (renamed from test/fib.c)6
-rw-r--r--test/03-bf.c (renamed from test/bf.c)3
-rw-r--r--test/04-fact.c (renamed from test/fact.c)4
-rw-r--r--test/05-sort.c (renamed from test/sort.c)9
-rw-r--r--test/06-goto.c32
-rw-r--r--test/07-pp.c (renamed from test/pp.c)16
-rw-r--r--test/07-pp.h (renamed from test/pp.h)0
-rw-r--r--test/08-bit.c (renamed from test/test4.c)5
-rw-r--r--test/09-init.c (renamed from test/init.c)42
-rw-r--r--test/09-varargs.c (renamed from test/varargs.c)10
-rw-r--r--test/goto.c25
-rwxr-xr-xtest/run.sh51
14 files changed, 164 insertions, 45 deletions
diff --git a/.gitignore b/.gitignore
index 7d028ae..7288abd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
antcc
build/
+test/build/
+test/log.txt
compile_commands.json
.cache/
.gdb_history
diff --git a/test/hello.c b/test/01-hello.c
index e27be8d..69a8354 100644
--- a/test/hello.c
+++ b/test/01-hello.c
@@ -1,3 +1,7 @@
+/* EXPECT:
+hello world
+*/
+
#include <stdio.h>
int main(int argc, char **argv) {
printf("hello world\n");
diff --git a/test/fib.c b/test/02-fib.c
index 806b416..30bbdae 100644
--- a/test/fib.c
+++ b/test/02-fib.c
@@ -1,3 +1,9 @@
+/* EXPECT:
+fib(10) = 55
+fibf(10) = 55
+fibr(10) = 55
+*/
+
unsigned fib(unsigned x) {
unsigned r = 0, q = 1;
for (; x > 1; --x) {
diff --git a/test/bf.c b/test/03-bf.c
index 8eb7af3..5b8ff8a 100644
--- a/test/bf.c
+++ b/test/03-bf.c
@@ -1,3 +1,6 @@
+/* EXPECT:
+Hello World!
+*/
unsigned char M[1<<15];
void bf(const char *p)
diff --git a/test/fact.c b/test/04-fact.c
index 1147d8f..0982e08 100644
--- a/test/fact.c
+++ b/test/04-fact.c
@@ -1,3 +1,7 @@
+/* EXPECT:
+6! = 720
+*/
+
int
fact(int x)
{
diff --git a/test/sort.c b/test/05-sort.c
index 8507cb2..829e0d1 100644
--- a/test/sort.c
+++ b/test/05-sort.c
@@ -1,4 +1,11 @@
-typedef unsigned long size_t;
+/* ARGS: 3 392 91 -2 -100 0 7 */
+/* EXPECT:
+-100, -2, 0, 3, 7, 91, 392,
+36 cmps, 3 swaps
+392, 91, 7, 3, 0, -2, -100,
+*/
+
+#include <stddef.h>
int printf(const char *, ...);
void *calloc(size_t, size_t);
diff --git a/test/06-goto.c b/test/06-goto.c
new file mode 100644
index 0000000..075467c
--- /dev/null
+++ b/test/06-goto.c
@@ -0,0 +1,32 @@
+/* EXPECT:
+7 -> 14
+-3 -> 0
+0 -> 0
+2 -> 4
+*/
+int crz(int x) {
+/* x <<= 1; while (x < 0) ++x; return x; */
+ goto e;
+a:
+ return x;
+j:
+ ++x;
+ goto q;
+b:
+ if (x < 0)
+ goto j;
+ goto a;
+q:
+ goto b;
+e:
+ x <<= 1;
+ goto b;
+}
+
+int printf(const char *, ...);
+int main(int n) {
+ n = 7, printf("%d -> %d\n", n, crz(n));
+ n = -3, printf("%d -> %d\n", n, crz(n));
+ n = 0, printf("%d -> %d\n", n, crz(n));
+ n = 2, printf("%d -> %d\n", n, crz(n));
+}
diff --git a/test/pp.c b/test/07-pp.c
index db90f23..c26105d 100644
--- a/test/pp.c
+++ b/test/07-pp.c
@@ -1,6 +1,14 @@
-
-#include "pp.h"
-#include "pp.h"
+/* EXPECT:
+ok /1 "\n"n ;.& 05.5 ADD(1,2)
+hi from header ;73
+Foo,5 9 1506
+join: "x ## y"
+wide L"abc123 猫,€á💫", U+1f98b
+Output ends here
+*/
+
+#include "07-pp.h"
+#include "07-pp.h"
#include <stddef.h>
#include <stdio.h>
#include <wchar.h>
@@ -41,7 +49,7 @@ main(void)
setlocale(LC_ALL, "en_US.utf8");
- printf("wide\t L\"%ls\", U+%x\n", L"abc123 猫,€á💫", L'🦋');
+ printf("wide L\"%ls\", U+%x\n", L"abc123 猫,€á💫", L'🦋');
PUT\
S\
diff --git a/test/pp.h b/test/07-pp.h
index 63de6f5..63de6f5 100644
--- a/test/pp.h
+++ b/test/07-pp.h
diff --git a/test/test4.c b/test/08-bit.c
index 694d798..5854cad 100644
--- a/test/test4.c
+++ b/test/08-bit.c
@@ -1,3 +1,8 @@
+/* EXPECT:
+expect 6, -4, 7, -1
+ 6, -4, 7, -1
+*/
+
int xor(int a) {
return a ^ 3 | 555;
}
diff --git a/test/init.c b/test/09-init.c
index 4087cd9..dd9d2a3 100644
--- a/test/init.c
+++ b/test/09-init.c
@@ -1,3 +1,19 @@
+/* EXPECT:
+gexplicit[4] -> 7,3,4,5
+gimplicit[] -> 3,5
+dim2[2][2] -> {1,2}, {4,3}
+S.x = 1, S.a[0][1] = 3, S.a[1][0] = 2
+U.x = 1
+str[] -> "abcdef"
+strs -> "axcxdxbx"
+desgn1[] -> {0.000000,0.000000}, {1.000000,3.000000}, {-0.500000,0.000000}
+desgn2 -> {(nil),{0,1.000000},(null)}
+desgn3 -> {(nil),{{{-1},0.000000},{{0},0.000000},{{6},1.000000}},"k"}
+fi -> {1.500000 | 1069547520}
+arrdsgn[] -> {0,5,0,0,1}
+s -> "abc"
+ss[] -> {"red","blue","green"}
+*/
int gexplicit[4] = { 7, 3, 4, 5, };
_Static_assert(sizeof gexplicit/sizeof *gexplicit ==4, "");
int gimplicit[] = { 3, 5, };
@@ -45,21 +61,21 @@ void *rec[1] = {rec+1};
int printf(char *, ...);
int main() {
- printf("gexplicit[4] \t%d,%d,%d,%d\n", gexplicit[0], gexplicit[1], gexplicit[2], gexplicit[3]);
- printf("gimplicit[] \t%d,%d\n", gimplicit[0], gimplicit[1]);
- printf("dim2[2][2] \t{%d,%d}, {%d,%d}\n", dim2[0][0],dim2[0][1],dim2[1][0],dim2[1][1]);
+ printf("gexplicit[4] -> %d,%d,%d,%d\n", gexplicit[0], gexplicit[1], gexplicit[2], gexplicit[3]);
+ printf("gimplicit[] -> %d,%d\n", gimplicit[0], gimplicit[1]);
+ printf("dim2[2][2] -> {%d,%d}, {%d,%d}\n", dim2[0][0],dim2[0][1],dim2[1][0],dim2[1][1]);
printf("S.x = %d, S.a[0][1] = %d, S.a[1][0] = %d\n", S.x, S.a[0][1], S.a[1][0]);
printf("U.x = %d\n", U.x);
- printf("str[] \t\"%s\"\n", str);
- printf("strs \t\"%.*s\"\n", (int)sizeof strs, strs);
- printf("desgn1[] \t{%f,%f}, {%f,%f}, {%f,%f}\n", desgn1[0].x, desgn1[0].y, desgn1[1].x, desgn1[1].y, desgn1[2].x, desgn1[2].y);
- printf("desgn2 \t{%p,{%d,%f},%s}\n", desgn2.j, desgn2.y, desgn2.z, desgn2.g);
- printf("desgn3 \t{%p,{{{%d},%f},{{%d},%f},{{%d},%f}},\"%s\"}\n", desgn3.j,
+ printf("str[] -> \"%s\"\n", str);
+ printf("strs -> \"%.*s\"\n", (int)sizeof strs, strs);
+ printf("desgn1[] -> {%f,%f}, {%f,%f}, {%f,%f}\n", desgn1[0].x, desgn1[0].y, desgn1[1].x, desgn1[1].y, desgn1[2].x, desgn1[2].y);
+ printf("desgn2 -> {%p,{%d,%f},%s}\n", desgn2.j, desgn2.y, desgn2.z, desgn2.g);
+ printf("desgn3 -> {%p,{{{%d},%f},{{%d},%f},{{%d},%f}},\"%s\"}\n", desgn3.j,
desgn3.a[0].y, desgn3.a[0].z, desgn3.a[1].y, desgn3.a[1].z,
desgn3.a[2].y, desgn3.a[2].z, desgn3.g);
- printf("fi \t{%f | %d}\n", fi.f, fi.i);
- printf("arrdsgn[] \t{%d,%d,%d,%d,%d}\n", arrdsgn[0], arrdsgn[1], arrdsgn[2], arrdsgn[3], arrdsgn[4]);
- printf("s \t\"%s\"\n", s);
- printf("ss[] \t{\"%s\",\"%s\",\"%s\"}\n", ss[0],ss[1],ss[2]);
- printf("rec \t%p{%p}\n",rec,rec[0]);
+ printf("fi -> {%f | %d}\n", fi.f, fi.i);
+ printf("arrdsgn[] -> {%d,%d,%d,%d,%d}\n", arrdsgn[0], arrdsgn[1], arrdsgn[2], arrdsgn[3], arrdsgn[4]);
+ printf("s -> \"%s\"\n", s);
+ printf("ss[] -> {\"%s\",\"%s\",\"%s\"}\n", ss[0],ss[1],ss[2]);
+ //printf("rec -> %p{%p}\n",rec,rec[0]);
}
diff --git a/test/varargs.c b/test/09-varargs.c
index 89ab21c..c15b887 100644
--- a/test/varargs.c
+++ b/test/09-varargs.c
@@ -1,11 +1,17 @@
+/* EXPECT:
+1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
+*/
+
+
#include <stdarg.h>
#include <stdio.h>
int sum(int x, ...) {
va_list ap;
va_start(ap, x);
+ printf("%d", x);
for (int y; (y = va_arg(ap, int));) {
- printf("got %d\n",y);
+ printf(" + %d",y);
x += y;
}
va_end(ap);
@@ -13,5 +19,5 @@ int sum(int x, ...) {
}
int main() {
- printf("%d\n", sum(1,2,3,4,5,6,5.5,7,0,0));
+ printf(" = %d\n", sum(1,2,3,4,5,6,7,8,0,0));
}
diff --git a/test/goto.c b/test/goto.c
deleted file mode 100644
index 0ecbf28..0000000
--- a/test/goto.c
+++ /dev/null
@@ -1,25 +0,0 @@
-int crz(int x) {
-/* x <<= 1; while (x < 0) ++x; return x; */
- goto e;
-a:
- return x;
-j:
- ++x;
- goto q;
-b:
- if (x < 0)
- goto j;
- goto a;
-q:
- goto b;
-e:
- x <<= 1;
- goto b;
-}
-
-int printf(const char *, ...);
-int main() {
- printf("should print 14: %d\n", crz(7));
- printf("should print 0: %d\n", crz(-3));
- printf("should print 0: %d\n", crz(0));
-}
diff --git a/test/run.sh b/test/run.sh
new file mode 100755
index 0000000..dd8f431
--- /dev/null
+++ b/test/run.sh
@@ -0,0 +1,51 @@
+#!/bin/env sh
+
+cd $(dirname "$0")
+ANTCC=../antcc
+ntest=0
+npass=0
+
+x() {
+ echo X $@>>log.txt
+ $@ 2>> log.txt
+}
+run() {
+ ntest=$(expr $ntest + 1)
+ f="$(basename "$1")"
+ expected=build/"$(echo "$f" | sed -s 's/\.c$/.expected/')"
+ echo ---- $f ---- >> log.txt
+ args=$(awk '/\/\* ARGS:.*$/ {ORS=" ";for (i=3;i<NF;++i)print $i;ORS="\n";print""}' "$f")
+ awk '/\/\* EXPECT:$/ {x=k=any=1} x && /\*\// {x=0} x {if (!k)print $0;k=0} END{if(x||!any)exit 1;}' "$f" > "$expected"
+ if [ $? == 0 ]; then
+ mkdir -p build/
+ obj=build/"$(echo "$f" | sed -s 's/\.c$/.o/')"
+ exe=build/"$(echo "$f" | sed -s 's/\.c$//')"
+ if ! ( x $ANTCC "$f" -c -o "$obj" && x $ANTCC "$obj" -o "$exe" ); then
+ echo !TEST ERROR $f
+ echo !FAILED TO COMPILE
+ echo '-------'
+ else
+ actual=build/"$(echo "$f" | sed -s 's/\.c$/.actual/')"
+ x "$exe" $args > "$actual"
+ if [ "$(md5sum < "$actual")" != "$(md5sum < "$expected")" ]; then
+ echo --- !TEST ERROR $f
+ diff --unified=0 --color=auto "$expected" "$actual"
+ echo '-------'
+ else
+ npass=$(expr $npass + 1)
+ fi
+ fi
+ else
+ echo --- !ignore $f
+ fi
+}
+
+< /dev/null > log.txt
+tests=$(find . -regex '\./[0-9]+-.*\.c' | sort)
+for test in $tests; do
+ run $test
+done
+
+echo TESTS PASSED: $npass/$ntest
+printf 'wc log.txt;'
+wc log.txt