From ab839efbcdfb5c754a2f688abf38c0ece81df2b1 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 28 Apr 2024 17:27:42 +0530 Subject: [PATCH] Defined a base testing library header This defines useful macros, in particular to print information and to define tests and test suites. The idea is a suite will be a set of test functions, where a test function will fail <=> an assert is called. This allows us to stop testing immediately upon a test failing and produce viable output. --- test/testing.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/testing.h diff --git a/test/testing.h b/test/testing.h new file mode 100644 index 0000000..c63b0f7 --- /dev/null +++ b/test/testing.h @@ -0,0 +1,38 @@ +/* Copyright (C) 2024 Aryadev Chavali + + * You may distribute and modify this code under the terms of the + * GPLv2 license. You should have received a copy of the GPLv2 + * license with this file. If not, please write to: + * aryadev@aryadevchavali.com. + + * Created: 2024-04-28 + * Author: Aryadev Chavali + * Description: Base library for creating tests + */ + +#ifndef TESTING_H +#define TESTING_H + +#include +#include +#include +#include + +#define MESSAGE(COLOUR, NAME, FORMAT, ...) \ + printf("\t[" COLOUR "%s" TERM_RESET "]: " FORMAT, NAME, __VA_ARGS__) + +#define INFO(NAME, FORMAT, ...) MESSAGE(TERM_YELLOW, NAME, FORMAT, __VA_ARGS__) +#define FAIL(NAME, FORMAT, ...) MESSAGE(TERM_RED, NAME, FORMAT, __VA_ARGS__) +#define SUCCESS(NAME, FORMAT, ...) \ + MESSAGE(TERM_GREEN, NAME, FORMAT, __VA_ARGS__) + +typedef void (*test_fn)(void); + +#define TEST_SUITE(NAME, ...) test_fn NAME[] = {__VA_ARGS__} +#define RUN_TEST_SUITE(NAME) \ + INFO(#NAME, "%s", "Starting test suite...\n"); \ + for (size_t i = 0; i < ARR_SIZE(NAME); ++i) \ + NAME[i](); \ + SUCCESS(#NAME, "%s", "Finished test suite!\n") + +#endif