From 63facb3954b81a470aa574f492c51c6a9fb36656 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Fri, 25 Oct 2024 12:30:45 +0100 Subject: [PATCH] Write testing framework --- tests/testing.c | 8 ++++++++ tests/testing.h | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/testing.c create mode 100644 tests/testing.h diff --git a/tests/testing.c b/tests/testing.c new file mode 100644 index 0000000..2ea878d --- /dev/null +++ b/tests/testing.c @@ -0,0 +1,8 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "testing.h" + +int fail_count; diff --git a/tests/testing.h b/tests/testing.h new file mode 100644 index 0000000..2291441 --- /dev/null +++ b/tests/testing.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#ifndef TESTING_H +#define TESTING_H + +#include +#include +#include + +#define TESTING_BEGIN() fail_count = 0 +#define TESTING_END() fail_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE + +#define FAIL \ + do { \ + ++fail_count; \ + printf("%s: FAIL @ %s:%d\n", __func__, __FILE__, __LINE__); \ + return; \ + } while (0) + +#define ASSERT_FALSE(p) \ + do { \ + if (p) \ + FAIL; \ + } while (0) + +#define ASSERT_TRUE(p) ASSERT_FALSE(!(p)) +#define ASSERT_EQ(x, y) ASSERT_FALSE((x) != (y)) +#define ASSERT_NE(x, y) ASSERT_FALSE((x) == (y)) +#define ASSERT_NULL(p) ASSERT_FALSE(NULL != (p)) +#define ASSERT_NOT_NULL(p) ASSERT_FALSE(NULL == (p)) +#define ASSERT_MEM_EQ(p, q, n) ASSERT_FALSE(memcmp(p, q, n) != 0) + +extern int fail_count; + +#endif