summaryrefslogtreecommitdiff
path: root/api_test/harness.c
diff options
context:
space:
mode:
authorNick Wellnhofer <wellnhofer@aevum.de>2014-11-18 18:37:56 +0100
committerNick Wellnhofer <wellnhofer@aevum.de>2014-11-18 18:43:04 +0100
commit8dba3f7f3591f4271522835cb2057acf4ae56f53 (patch)
tree8bf044f7c0fa0e13925db0c64e8720a86c0b2336 /api_test/harness.c
parent3f9fec6998fccabe5d61bfe84b9d2431f9d5ae53 (diff)
Start with tests for the C API
The C API tests can be run individually via build/api_test/api_test Or together with the spec tests via cmake --build build --target test
Diffstat (limited to 'api_test/harness.c')
-rw-r--r--api_test/harness.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/api_test/harness.c b/api_test/harness.c
new file mode 100644
index 0000000..6b38d41
--- /dev/null
+++ b/api_test/harness.c
@@ -0,0 +1,102 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "harness.h"
+
+test_batch_runner*
+test_batch_runner_new()
+{
+ return (test_batch_runner *)calloc(1, sizeof(test_batch_runner));
+}
+
+static void
+test_result(test_batch_runner *runner, int cond, const char *msg, va_list ap)
+{
+ ++runner->test_num;
+
+ if (cond) {
+ ++runner->num_passed;
+ }
+ else {
+ fprintf(stderr, "FAILED test %d: ", runner->test_num);
+ vfprintf(stderr, msg, ap);
+ fprintf(stderr, "\n");
+ ++runner->num_failed;
+ }
+}
+
+void
+SKIP(test_batch_runner *runner, int num_tests)
+{
+ runner->test_num += num_tests;
+ runner->num_skipped += num_tests;
+}
+
+void
+OK(test_batch_runner *runner, int cond, const char *msg, ...)
+{
+ va_list ap;
+ va_start(ap, msg);
+ test_result(runner, cond, msg, ap);
+ va_end(ap);
+}
+
+void
+INT_EQ(test_batch_runner *runner, int got, int expected, const char *msg, ...)
+{
+ int cond = got == expected;
+
+ va_list ap;
+ va_start(ap, msg);
+ test_result(runner, cond, msg, ap);
+ va_end(ap);
+
+ if (!cond) {
+ fprintf(stderr, " Got: %d\n", got);
+ fprintf(stderr, " Expected: %d\n", expected);
+ }
+}
+
+void
+STR_EQ(test_batch_runner *runner, const char *got, const char *expected,
+ const char *msg, ...)
+{
+ int cond = strcmp(got, expected) == 0;
+
+ va_list ap;
+ va_start(ap, msg);
+ test_result(runner, cond, msg, ap);
+ va_end(ap);
+
+ if (!cond) {
+ fprintf(stderr, " Got: \"%s\"\n", got);
+ fprintf(stderr, " Expected: \"%s\"\n", expected);
+ }
+}
+
+int
+test_ok(test_batch_runner *runner)
+{
+ return runner->num_failed == 0;
+}
+
+void
+test_print_summary(test_batch_runner *runner)
+{
+ int num_passed = runner->num_passed;
+ int num_skipped = runner->num_skipped;
+ int num_failed = runner->num_failed;
+
+ fprintf(stderr, "%d tests passed, %d failed, %d skipped\n",
+ num_passed, num_skipped, num_failed);
+
+ if (test_ok(runner)) {
+ fprintf(stderr, "PASS\n");
+ }
+ else {
+ fprintf(stderr, "FAIL\n");
+ }
+}
+