From 111e71e1511b2abff9176bd6c714c8da796f770e Mon Sep 17 00:00:00 2001 From: lemon Date: Sun, 16 Nov 2025 12:11:18 +0100 Subject: basic automated testing --- .gitignore | 2 ++ test/01-hello.c | 8 ++++++ test/02-fib.c | 42 +++++++++++++++++++++++++++++ test/03-bf.c | 48 +++++++++++++++++++++++++++++++++ test/04-fact.c | 17 ++++++++++++ test/05-sort.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ test/06-goto.c | 32 ++++++++++++++++++++++ test/07-pp.c | 63 +++++++++++++++++++++++++++++++++++++++++++ test/07-pp.h | 24 +++++++++++++++++ test/08-bit.c | 64 +++++++++++++++++++++++++++++++++++++++++++ test/09-init.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/09-varargs.c | 23 ++++++++++++++++ test/bf.c | 45 ------------------------------- test/fact.c | 13 --------- test/fib.c | 36 ------------------------- test/goto.c | 25 ----------------- test/hello.c | 4 --- test/init.c | 65 -------------------------------------------- test/pp.c | 55 ------------------------------------- test/pp.h | 24 ----------------- test/run.sh | 51 +++++++++++++++++++++++++++++++++++ test/sort.c | 59 ---------------------------------------- test/test4.c | 59 ---------------------------------------- test/varargs.c | 17 ------------ 24 files changed, 521 insertions(+), 402 deletions(-) create mode 100644 test/01-hello.c create mode 100644 test/02-fib.c create mode 100644 test/03-bf.c create mode 100644 test/04-fact.c create mode 100644 test/05-sort.c create mode 100644 test/06-goto.c create mode 100644 test/07-pp.c create mode 100644 test/07-pp.h create mode 100644 test/08-bit.c create mode 100644 test/09-init.c create mode 100644 test/09-varargs.c delete mode 100644 test/bf.c delete mode 100644 test/fact.c delete mode 100644 test/fib.c delete mode 100644 test/goto.c delete mode 100644 test/hello.c delete mode 100644 test/init.c delete mode 100644 test/pp.c delete mode 100644 test/pp.h create mode 100755 test/run.sh delete mode 100644 test/sort.c delete mode 100644 test/test4.c delete mode 100644 test/varargs.c 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/01-hello.c b/test/01-hello.c new file mode 100644 index 0000000..69a8354 --- /dev/null +++ b/test/01-hello.c @@ -0,0 +1,8 @@ +/* EXPECT: +hello world +*/ + +#include +int main(int argc, char **argv) { + printf("hello world\n"); +} diff --git a/test/02-fib.c b/test/02-fib.c new file mode 100644 index 0000000..30bbdae --- /dev/null +++ b/test/02-fib.c @@ -0,0 +1,42 @@ +/* EXPECT: +fib(10) = 55 +fibf(10) = 55 +fibr(10) = 55 +*/ + +unsigned fib(unsigned x) { + unsigned r = 0, q = 1; + for (; x > 1; --x) { + unsigned s = r + q; + r = q; + q = s; + } + return q; +} + +unsigned fibr(unsigned x) { + if (x < 2) return x; + return fibr(x-1) + fibr(x-2); +} + +double fibf(unsigned x) { + double r = 0., q = 1.; + while (x-- > 1) { + double s = r + q; + r = q; + q = s; + } + return q; +} + +int atoi(const char *); +int printf(const char *, ...); + +int main(int argc, char **argv) { + unsigned n = argv[1] ? atoi(argv[1]) : 10; + printf("fib(%u) = %u\n", n, fib(n)); + printf("fibf(%u) = %g\n", n, fibf(n)); + printf("fibr(%u) = %u\n", n, fibr(n)); +} + +/* vim:set ts=3 sw=3 expandtab: */ diff --git a/test/03-bf.c b/test/03-bf.c new file mode 100644 index 0000000..5b8ff8a --- /dev/null +++ b/test/03-bf.c @@ -0,0 +1,48 @@ +/* EXPECT: +Hello World! +*/ +unsigned char M[1<<15]; + +void bf(const char *p) +{ + extern int putchar(int); + extern int getchar(void); + unsigned char *Mend = M + sizeof M; + int b; + unsigned char *m = M; + + while (*p) { + switch (*p) { + case '+': ++*m; break; + case '-': --*m; break; + case '<': if (m == M) m = Mend; else --m; break; + case '>': if (m == Mend) m = M; else ++m; break; + case '.': putchar(*m); break; + case ',': *m = getchar(); break; + case '[': + if (*m) break; + b = 0; + do switch (*p++) { + case '[': ++b; break; + case ']': --b; break; + case 0: return; + } while (b != 0); + continue; + case ']': + if (!*m) break; + b = 0; + do switch (*p--) { + case '[': ++b; break; + case ']': --b; break; + case 0: return; + } while (b != 0); + break; + } + ++p; + } +} + +int main(int argc, char **argv) { + const char *p = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; + bf(argc < 2 ? p : argv[1]); +} diff --git a/test/04-fact.c b/test/04-fact.c new file mode 100644 index 0000000..0982e08 --- /dev/null +++ b/test/04-fact.c @@ -0,0 +1,17 @@ +/* EXPECT: +6! = 720 +*/ + +int +fact(int x) +{ + int y = 1; + while (x >= 1) { + y *= x; + x -= 1; + } + return y; +} + +extern int printf(); +int main() { printf("6! = %d\n", fact(6)); } diff --git a/test/05-sort.c b/test/05-sort.c new file mode 100644 index 0000000..829e0d1 --- /dev/null +++ b/test/05-sort.c @@ -0,0 +1,66 @@ +/* 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 + +int printf(const char *, ...); +void *calloc(size_t, size_t); +void qsort(void *, size_t nmemb, size_t size, int (const void *, const void *)); +int atoi(const char *); + +int +icmp(const void *a, const void *b) +{ + int l = *(int *)a, r = *(int *)b; + return (l > r) - (l < r); +} + +int nswp = 0, ncmp = 0; +void +revsort(int *xs, long lo, long hi) +{ + while (lo < hi) { + long i = lo - 1, p = hi + 1; + int pivot = xs[lo], tmp; + for (;;) { + do ++i; while (++ncmp, xs[i] > pivot); + do --p; while (++ncmp, xs[p] < pivot); + if (i >= p) break; + tmp = xs[i]; + xs[i] = xs[p]; + xs[p] = tmp; + ++nswp; + } + if (p + 1 >= hi) { + hi = p; + } else { + if (lo < p) + revsort(xs, lo, p); + lo = p + 1; + } + } +} + +int +main(int argc, char **argv) +{ + int N = argc - 1; + int *xs = calloc(N, sizeof *xs); + + for (int i = 0; i < N; ++i) + xs[i] = atoi(argv[i+1]); + qsort(xs, N, sizeof *xs, icmp); + for (int i = 0; i < N; ++i) + printf("%d, ", xs[i]); + printf("\n"); + revsort(xs, 0,N-1); + printf("%d cmps, %d swaps\n", ncmp, nswp); + for (int i = 0; i < N; ++i) + printf("%d, ", xs[i]); + printf("\n"); + return N; +} 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/07-pp.c b/test/07-pp.c new file mode 100644 index 0000000..c26105d --- /dev/null +++ b/test/07-pp.c @@ -0,0 +1,63 @@ +/* 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 +#include +#include +#include +// +#define CATl(a) a##bar +#define CATr(a) foo##a +#define CAT(a,b) a##b +#define foobar() foo##bar + +#define hash_hash # ## # +#define mkstr(a) # a +#define in_between(a) mkstr(a) +#define join(c, d) in_between(c hash_hash d) +char p[] = join(x, y); // equivalent to char p[] = "x ## y"; + +#define PUTS p\ +u\ +t\ +s + + +int +main(void) +{ + int CATl(foo); + ++foobar; + --CATr(bar); + CAT(foo,bar) += 3; + foobar() /=2; + printf("%s %s\n",STR ( ok /1 "\n"n ;.& + 05.5), STR(ADD(1,2))); + hi(ADD(Foo, SQR(Bar+1))); + int foo123 = 77; + printf("%s " + "%s %g\n", str(Foo,5), xstr(Foo), CAT(1.5,e3f) + CAT(7,)-CAT(,1)); + printf("join: \"%s\"\n", p); + + setlocale(LC_ALL, "en_US.utf8"); + + printf("wide L\"%ls\", U+%x\n", L"abc123 猫,€á💫", L'🦋'); + + PUT\ +S\ +("Output ends here\\ +0Not printed" /* After line splicing, the remaining backslash + * escapes the 0, ending the string early. + */ +); + + CAT(ret,urn) 0; +} diff --git a/test/07-pp.h b/test/07-pp.h new file mode 100644 index 0000000..63de6f5 --- /dev/null +++ b/test/07-pp.h @@ -0,0 +1,24 @@ +#ifndef GUARD +#define GUARD + +extern int printf(const char *, ...); +extern warnhere(); +#define Foo 9 +void hi(int x) { + printf("hi from header ;%d\n", x); +} +#if 1 +#endif +#elifndef Ww +#define Bar 7 +#define SQR_(x) ((x)*(x)) +#define SQR(y) SQR_(1+(y)-1) +#define ADD(a,b) (a)+(b) +#define STR(h) #h + +#define xstr(s1) str(s1) +#define str(...) #__VA_ARGS__ + +#endif + +extern int printf(const char *, ...); diff --git a/test/08-bit.c b/test/08-bit.c new file mode 100644 index 0000000..5854cad --- /dev/null +++ b/test/08-bit.c @@ -0,0 +1,64 @@ +/* EXPECT: +expect 6, -4, 7, -1 + 6, -4, 7, -1 +*/ + +int xor(int a) { + return a ^ 3 | 555; +} + +int cmp(float x, float y) { + return x < y && x > 0.f; +} + +struct foo { + unsigned int x : 10; + unsigned y : 7; + short k:3; +int : 0; + short a:15; + long long h:60; +}; + +int bitf(struct foo *q) { + extern void aeiou(int); + aeiou(q->h); + return q->x + q->y - q->k + q->a; +} + +struct s1 { + short x : 3, y : 12; +}; +struct s2 { + struct s1 a; +}; + +struct s2 bitfcopy2(struct s2 x) { + return (struct s2){x.a}; +} + +int main(int p) { + extern int printf(const char *, ...); + + static struct foo foo; + + foo.x = 5+p; + foo.k = 3; + foo.h += 7; + bitf(&foo); + foo.a = -1; + foo.a = xor(foo.a |= 3); + printf("expect %d, -4, 7, %d\n", 5+p, (short)((-1|3)^3|555)); + printf(" %d, %d, %lld, %d\n", foo.x, foo.k+=1, foo.h, foo.a); + + int x = 42, + *a = &x, + **b = &a, + ***c = &b, + ****d = &c, + *****e = &d, + ******f = &e; + return ******f; +} + +void aeiou(int _){} diff --git a/test/09-init.c b/test/09-init.c new file mode 100644 index 0000000..dd9d2a3 --- /dev/null +++ b/test/09-init.c @@ -0,0 +1,81 @@ +/* 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, }; +_Static_assert(sizeof gimplicit/sizeof *gimplicit ==2, ""); +char dim2[][2] = { {1,2},4,3 }; +_Static_assert(sizeof dim2 == 4, "dim2"); +struct S{int x; int a[2][12];} S = {1,{{0,3},2}}; + +const union U {int x; int y[2];} U = {1,}; + +char str[] = "abcdef"; +_Static_assert(sizeof str == 7, "str[7]"); + +const char strs[][2] = {"ax", {'c','x'}, 'd',"?x"[1], "bx"}; + +/* +struct { int y; signed char x[]; } flex1 = {0, {0}}; +struct { int y; signed char x[]; } flex2 = {0, 1,4,5}; +struct { int y; signed char x[]; } flex3 = {0, "/"}; +*/ + +struct { float x, y; } desgn1[] = {[1]=1.f,3, -0.5}; +_Static_assert(sizeof desgn1/sizeof *desgn1 == 3,""); + +struct n { void * j; struct { int y; float z; }; void *g;} desgn2 = { .y = 0, 1.0f, 0, 8 }; +struct n2 { void * j; struct as { struct { int y; }; float z; } a[3]; char *g;} desgn3 = { .a={-1}, .a[2].y = 6, 1.0f, "k" }; + +char *s = "abc"; +const char *ss[] = {"red","blue","green"}; + +union fi {float f; int i;} fi = {.i = 0xFF<<22,}; + +char arrdsgn[] = { [4] = 1, [1] = 5 }; +_Static_assert(sizeof arrdsgn == 5, ""); + +// int q[1] = {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{1}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}; + +/* +void f() { + struct {int x,y;} a = {1,2}, b = {3,4}, c[] = {a,b}; +} +*/ + +void *rec[1] = {rec+1}; + +int printf(char *, ...); +int main() { + 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[] -> \"%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 -> {%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/09-varargs.c b/test/09-varargs.c new file mode 100644 index 0000000..c15b887 --- /dev/null +++ b/test/09-varargs.c @@ -0,0 +1,23 @@ +/* EXPECT: +1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36 +*/ + + +#include +#include + +int sum(int x, ...) { + va_list ap; + va_start(ap, x); + printf("%d", x); + for (int y; (y = va_arg(ap, int));) { + printf(" + %d",y); + x += y; + } + va_end(ap); + return x; +} + +int main() { + printf(" = %d\n", sum(1,2,3,4,5,6,7,8,0,0)); +} diff --git a/test/bf.c b/test/bf.c deleted file mode 100644 index 8eb7af3..0000000 --- a/test/bf.c +++ /dev/null @@ -1,45 +0,0 @@ -unsigned char M[1<<15]; - -void bf(const char *p) -{ - extern int putchar(int); - extern int getchar(void); - unsigned char *Mend = M + sizeof M; - int b; - unsigned char *m = M; - - while (*p) { - switch (*p) { - case '+': ++*m; break; - case '-': --*m; break; - case '<': if (m == M) m = Mend; else --m; break; - case '>': if (m == Mend) m = M; else ++m; break; - case '.': putchar(*m); break; - case ',': *m = getchar(); break; - case '[': - if (*m) break; - b = 0; - do switch (*p++) { - case '[': ++b; break; - case ']': --b; break; - case 0: return; - } while (b != 0); - continue; - case ']': - if (!*m) break; - b = 0; - do switch (*p--) { - case '[': ++b; break; - case ']': --b; break; - case 0: return; - } while (b != 0); - break; - } - ++p; - } -} - -int main(int argc, char **argv) { - const char *p = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; - bf(argc < 2 ? p : argv[1]); -} diff --git a/test/fact.c b/test/fact.c deleted file mode 100644 index 1147d8f..0000000 --- a/test/fact.c +++ /dev/null @@ -1,13 +0,0 @@ -int -fact(int x) -{ - int y = 1; - while (x >= 1) { - y *= x; - x -= 1; - } - return y; -} - -extern int printf(); -int main() { printf("6! = %d\n", fact(6)); } diff --git a/test/fib.c b/test/fib.c deleted file mode 100644 index 806b416..0000000 --- a/test/fib.c +++ /dev/null @@ -1,36 +0,0 @@ -unsigned fib(unsigned x) { - unsigned r = 0, q = 1; - for (; x > 1; --x) { - unsigned s = r + q; - r = q; - q = s; - } - return q; -} - -unsigned fibr(unsigned x) { - if (x < 2) return x; - return fibr(x-1) + fibr(x-2); -} - -double fibf(unsigned x) { - double r = 0., q = 1.; - while (x-- > 1) { - double s = r + q; - r = q; - q = s; - } - return q; -} - -int atoi(const char *); -int printf(const char *, ...); - -int main(int argc, char **argv) { - unsigned n = argv[1] ? atoi(argv[1]) : 10; - printf("fib(%u) = %u\n", n, fib(n)); - printf("fibf(%u) = %g\n", n, fibf(n)); - printf("fibr(%u) = %u\n", n, fibr(n)); -} - -/* vim:set ts=3 sw=3 expandtab: */ 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/hello.c b/test/hello.c deleted file mode 100644 index e27be8d..0000000 --- a/test/hello.c +++ /dev/null @@ -1,4 +0,0 @@ -#include -int main(int argc, char **argv) { - printf("hello world\n"); -} diff --git a/test/init.c b/test/init.c deleted file mode 100644 index 4087cd9..0000000 --- a/test/init.c +++ /dev/null @@ -1,65 +0,0 @@ -int gexplicit[4] = { 7, 3, 4, 5, }; -_Static_assert(sizeof gexplicit/sizeof *gexplicit ==4, ""); -int gimplicit[] = { 3, 5, }; -_Static_assert(sizeof gimplicit/sizeof *gimplicit ==2, ""); -char dim2[][2] = { {1,2},4,3 }; -_Static_assert(sizeof dim2 == 4, "dim2"); -struct S{int x; int a[2][12];} S = {1,{{0,3},2}}; - -const union U {int x; int y[2];} U = {1,}; - -char str[] = "abcdef"; -_Static_assert(sizeof str == 7, "str[7]"); - -const char strs[][2] = {"ax", {'c','x'}, 'd',"?x"[1], "bx"}; - -/* -struct { int y; signed char x[]; } flex1 = {0, {0}}; -struct { int y; signed char x[]; } flex2 = {0, 1,4,5}; -struct { int y; signed char x[]; } flex3 = {0, "/"}; -*/ - -struct { float x, y; } desgn1[] = {[1]=1.f,3, -0.5}; -_Static_assert(sizeof desgn1/sizeof *desgn1 == 3,""); - -struct n { void * j; struct { int y; float z; }; void *g;} desgn2 = { .y = 0, 1.0f, 0, 8 }; -struct n2 { void * j; struct as { struct { int y; }; float z; } a[3]; char *g;} desgn3 = { .a={-1}, .a[2].y = 6, 1.0f, "k" }; - -char *s = "abc"; -const char *ss[] = {"red","blue","green"}; - -union fi {float f; int i;} fi = {.i = 0xFF<<22,}; - -char arrdsgn[] = { [4] = 1, [1] = 5 }; -_Static_assert(sizeof arrdsgn == 5, ""); - -// int q[1] = {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{1}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}; - -/* -void f() { - struct {int x,y;} a = {1,2}, b = {3,4}, c[] = {a,b}; -} -*/ - -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("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, - 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]); -} diff --git a/test/pp.c b/test/pp.c deleted file mode 100644 index db90f23..0000000 --- a/test/pp.c +++ /dev/null @@ -1,55 +0,0 @@ - -#include "pp.h" -#include "pp.h" -#include -#include -#include -#include -// -#define CATl(a) a##bar -#define CATr(a) foo##a -#define CAT(a,b) a##b -#define foobar() foo##bar - -#define hash_hash # ## # -#define mkstr(a) # a -#define in_between(a) mkstr(a) -#define join(c, d) in_between(c hash_hash d) -char p[] = join(x, y); // equivalent to char p[] = "x ## y"; - -#define PUTS p\ -u\ -t\ -s - - -int -main(void) -{ - int CATl(foo); - ++foobar; - --CATr(bar); - CAT(foo,bar) += 3; - foobar() /=2; - printf("%s %s\n",STR ( ok /1 "\n"n ;.& - 05.5), STR(ADD(1,2))); - hi(ADD(Foo, SQR(Bar+1))); - int foo123 = 77; - printf("%s " - "%s %g\n", str(Foo,5), xstr(Foo), CAT(1.5,e3f) + CAT(7,)-CAT(,1)); - printf("join: \"%s\"\n", p); - - setlocale(LC_ALL, "en_US.utf8"); - - printf("wide\t L\"%ls\", U+%x\n", L"abc123 猫,€á💫", L'🦋'); - - PUT\ -S\ -("Output ends here\\ -0Not printed" /* After line splicing, the remaining backslash - * escapes the 0, ending the string early. - */ -); - - CAT(ret,urn) 0; -} diff --git a/test/pp.h b/test/pp.h deleted file mode 100644 index 63de6f5..0000000 --- a/test/pp.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef GUARD -#define GUARD - -extern int printf(const char *, ...); -extern warnhere(); -#define Foo 9 -void hi(int x) { - printf("hi from header ;%d\n", x); -} -#if 1 -#endif -#elifndef Ww -#define Bar 7 -#define SQR_(x) ((x)*(x)) -#define SQR(y) SQR_(1+(y)-1) -#define ADD(a,b) (a)+(b) -#define STR(h) #h - -#define xstr(s1) str(s1) -#define str(...) #__VA_ARGS__ - -#endif - -extern int printf(const char *, ...); 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 "$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 diff --git a/test/sort.c b/test/sort.c deleted file mode 100644 index 8507cb2..0000000 --- a/test/sort.c +++ /dev/null @@ -1,59 +0,0 @@ -typedef unsigned long size_t; - -int printf(const char *, ...); -void *calloc(size_t, size_t); -void qsort(void *, size_t nmemb, size_t size, int (const void *, const void *)); -int atoi(const char *); - -int -icmp(const void *a, const void *b) -{ - int l = *(int *)a, r = *(int *)b; - return (l > r) - (l < r); -} - -int nswp = 0, ncmp = 0; -void -revsort(int *xs, long lo, long hi) -{ - while (lo < hi) { - long i = lo - 1, p = hi + 1; - int pivot = xs[lo], tmp; - for (;;) { - do ++i; while (++ncmp, xs[i] > pivot); - do --p; while (++ncmp, xs[p] < pivot); - if (i >= p) break; - tmp = xs[i]; - xs[i] = xs[p]; - xs[p] = tmp; - ++nswp; - } - if (p + 1 >= hi) { - hi = p; - } else { - if (lo < p) - revsort(xs, lo, p); - lo = p + 1; - } - } -} - -int -main(int argc, char **argv) -{ - int N = argc - 1; - int *xs = calloc(N, sizeof *xs); - - for (int i = 0; i < N; ++i) - xs[i] = atoi(argv[i+1]); - qsort(xs, N, sizeof *xs, icmp); - for (int i = 0; i < N; ++i) - printf("%d, ", xs[i]); - printf("\n"); - revsort(xs, 0,N-1); - printf("%d cmps, %d swaps\n", ncmp, nswp); - for (int i = 0; i < N; ++i) - printf("%d, ", xs[i]); - printf("\n"); - return N; -} diff --git a/test/test4.c b/test/test4.c deleted file mode 100644 index 694d798..0000000 --- a/test/test4.c +++ /dev/null @@ -1,59 +0,0 @@ -int xor(int a) { - return a ^ 3 | 555; -} - -int cmp(float x, float y) { - return x < y && x > 0.f; -} - -struct foo { - unsigned int x : 10; - unsigned y : 7; - short k:3; -int : 0; - short a:15; - long long h:60; -}; - -int bitf(struct foo *q) { - extern void aeiou(int); - aeiou(q->h); - return q->x + q->y - q->k + q->a; -} - -struct s1 { - short x : 3, y : 12; -}; -struct s2 { - struct s1 a; -}; - -struct s2 bitfcopy2(struct s2 x) { - return (struct s2){x.a}; -} - -int main(int p) { - extern int printf(const char *, ...); - - static struct foo foo; - - foo.x = 5+p; - foo.k = 3; - foo.h += 7; - bitf(&foo); - foo.a = -1; - foo.a = xor(foo.a |= 3); - printf("expect %d, -4, 7, %d\n", 5+p, (short)((-1|3)^3|555)); - printf(" %d, %d, %lld, %d\n", foo.x, foo.k+=1, foo.h, foo.a); - - int x = 42, - *a = &x, - **b = &a, - ***c = &b, - ****d = &c, - *****e = &d, - ******f = &e; - return ******f; -} - -void aeiou(int _){} diff --git a/test/varargs.c b/test/varargs.c deleted file mode 100644 index 89ab21c..0000000 --- a/test/varargs.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -int sum(int x, ...) { - va_list ap; - va_start(ap, x); - for (int y; (y = va_arg(ap, int));) { - printf("got %d\n",y); - x += y; - } - va_end(ap); - return x; -} - -int main() { - printf("%d\n", sum(1,2,3,4,5,6,5.5,7,0,0)); -} -- cgit v1.2.3