From 31dfcc882434a7f85258d98d5a9d80cb4c83ab65 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Mon, 26 Mar 2012 13:20:36 -0700 Subject: [PATCH] Initial commit --- Makefile | 8 ++ ctap.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ctap.h | 23 ++++++ main.c | 19 +++++ 4 files changed, 279 insertions(+) create mode 100644 Makefile create mode 100644 ctap.c create mode 100644 ctap.h create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6da29d4 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +OBJS := ctap.o main.o +CFLAGS := -Wall + +main: $(OBJS) + $(CC) $(CFLAGS) -o $@ $+ + +clean: + rm -rf *.o main diff --git a/ctap.c b/ctap.c new file mode 100644 index 0000000..6238747 --- /dev/null +++ b/ctap.c @@ -0,0 +1,229 @@ +#include +#include +#include +#include +#include +#include + +#include "ctap.h" + +/* global variable testnum? */ +static int test = 0; /* the test number */ +static int planned = 0; + +void plan(int tests) { + test = 0; + planned = tests; + printf("1..%d\n", tests); +} + +static void print_lazy_plan(void) { + printf("1..%d\n", test); + fflush(stdout); +} + +void plan_lazy(void) { + test = 0; + planned = 0; + atexit(print_lazy_plan); +} + +void skip_all(const char *why, ...) { + printf("1..0"); + if (why) { + va_list args; + printf(" # skip "); + va_start(args, why); + vfprintf(stdout, why, args); + va_end(args); + } + printf("\n"); +} + +void okv(int pass, const char *fmt, va_list args) { + printf("%sok %d", pass ? "" : "not ", ++test); + if (fmt) { + printf(" # "); + vfprintf(stdout, fmt, args); + } + printf("\n"); +} + +void ok(int pass, char *fmt, ...) { + va_list args; + + printf("%sok %d", pass ? "" : "not ", ++test); + if (fmt) { + printf(" # "); + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + } + printf("\n"); +} + +void ok_block(unsigned long count, int pass, const char *fmt, ...) { + if (count == 0) { + return; + } + if (fmt) { + va_list args; + va_list copy; + va_start(args, fmt); + while (count--) { + va_copy(copy, args); + okv(pass, fmt, copy); + va_end(copy); + } + va_end(args); + } else { + while (count--) { + ok(pass, NULL); + } + } +} + +void skip(const char *why, ...) { + va_list args; + + printf("ok %d # skip", ++test); + if (why) { + printf(" "); + va_start(args, why); + vfprintf(stdout, why, args); + va_end(args); + } + printf("\n"); +} + +void skip_block(unsigned long count, const char *why, ...) { + if (why) { + va_list args; + va_list copy; + va_start(args, why); + while (count--) { + va_copy(copy, args); + printf("ok %d # skip", ++test); + printf(" "); + vfprintf(stdout, why, copy); + va_end(copy); + printf("\n"); + } + va_end(args); + } else { + printf("ok %d # skip\n", ++test); + } +} + +void bail(const char *fmt, ...) { + va_list args; + printf("Bail out!"); + if (fmt) { + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + } + printf("\n"); + fflush(stdout); + exit(1); +} + +void sysbail(const char *fmt, ...) { + va_list args; + printf("Bail out!"); + if (fmt) { + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + } + printf(": %s\n", strerror(errno)); + fflush(stdout); + exit(1); +} + +void sysdiag(const char *fmt, ...) { + va_list args; + if (!fmt) { return; } + printf("# "); + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + printf(": %s\n", strerror(errno)); +} + +void diag(const char *fmt, ...) { + va_list args; + if (!fmt) { return; } + printf("# "); + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + printf("\n"); +} + +void is_hex(unsigned long wanted, unsigned long seen, const char *fmt, ...) { + va_list args; + if (fmt) { + va_start(args, fmt); + okv(wanted == seen, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (wanted != seen) { + diag("wanted: %ld", wanted); + diag("got : %ld", seen); + } +} + + +void is_int(long wanted, long seen, const char *fmt, ...) { + va_list args; + if (fmt) { + va_start(args, fmt); + okv(wanted == seen, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (wanted != seen) { + diag("wanted: %ld", wanted); + diag("got : %ld", seen); + } +} + +void is_double(double wanted, double seen, double eps, const char *fmt, ...) { + int pass; + va_list args; + + pass = fabs(wanted - seen) <= eps; + if (fmt) { + va_start(args, fmt); + okv(pass, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (!pass) { + diag("wanted: %f", wanted); + diag("got : %f", seen); + } +} + +void is_string(const char *wanted, const char *seen, const char *fmt, ...) { + int pass; + va_list args; + + pass = !strcmp(wanted,seen); + if (fmt) { + va_start(args, fmt); + okv(pass, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (!pass) { + diag("wanted: %s", wanted); + diag("got : %s", seen); + } +} diff --git a/ctap.h b/ctap.h new file mode 100644 index 0000000..43d8087 --- /dev/null +++ b/ctap.h @@ -0,0 +1,23 @@ +#ifndef TAP_H_ +#define TAP_H_ 1 + +#include + +void plan(int tests); +void plan_lazy(void); +void skip_all(const char *why, ...); +void okv(int pass, const char *fmt, va_list args); +void ok(int pass, char *fmt, ...); +void ok_block(unsigned long count, int pass, const char *fmt, ...); +void skip(const char *why, ...); +void skip_block(unsigned long count, const char *why, ...); +void bail(const char *fmt, ...); +void sysbail(const char *fmt, ...); +void sysdiag(const char *fmt, ...); +void diag(const char *fmt, ...); +void is_hex(unsigned long wanted, unsigned long seen, const char *fmt, ...); +void is_int(long wanted, long seen, const char *fmt, ...); +void is_double(double wanted, double seen, double eps, const char *fmt, ...); +void is_string(const char *wanted, const char *seen, const char *fmt, ...); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..da49aee --- /dev/null +++ b/main.c @@ -0,0 +1,19 @@ +#include "ctap.h" + +int main(void) { + /* plan(4); */ + plan_lazy(); + ok(1, "ok pass"); + ok(0, "ok fail"); + skip("skip one"); + skip_block(2, "skip 2 block"); + is_int(1,1,"is_int pass"); + is_int(1,2,"is_int fail"); + is_double(1.0,1.0,0.0,"is_double perfect pass"); + is_double(1.0,1.2,0.0,"is_double perfect fail"); + is_double(1.0,1.1,0.5,"is_double epsilon pass"); + is_double(1.0,2.0,0.5,"is_double epsilon fail"); + is_string("foo", "foo", "is_string pass"); + is_string("foo", "bar", "is_string fail"); + return 0; +} -- 2.40.0