From b9d291f1452071dac9817f7dadfecadc64d2eba5 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 18 Jan 2026 21:51:34 +0100 Subject: [PATCH 1/2] feat(tests): added basic testing framework --- tests/main.c | 12 ++++++++++ tests/test.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/main.c create mode 100644 tests/test.h diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..6bbf08d --- /dev/null +++ b/tests/main.c @@ -0,0 +1,12 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ + +int main(void) { + return 0; +} diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..f179eab --- /dev/null +++ b/tests/test.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#ifndef QNET_TEST +#define QNET_TEST + +#include + +#define QNET_TEST_EXPORT_TEST(name) { #name, qnet_##name##_test } +#define QNET_TEST_DEFINE(name) const char *qnet_##name##_test(void) +#define QNET_TEST_IMPLEMENT(name, implementation) const char *qnet_##name##_test(void) { \ + implementation \ +} + +/** + * @brief Creates a list to export so it can be imported into the main test list. + */ +#define QNET_TEST_EXPORT(name, ...) static const struct qnet_test qnet_test_##name[] = { \ + __VA_ARGS__, \ + 0 \ + } + +/** + * @brief Creates a list of lists from exported tests so all tests can be easly executed. + */ +#define QNET_TEST_START static const struct qnet_test *qnet_test_tests[] = { +#define QNET_TEST_IMPORT(name) qnet_test_##name +#define QNET_TEST_END }; + +/** + * @brief Runs all tests specified in the qnet_test_tests array; + */ +#define QNET_TEST_RUN() \ + for (unsigned long long index = 0; index < sizeof(qnet_test_tests) / sizeof(qnet_test_tests[0]); index += 1) { \ + unsigned long long test_index = 0; \ + struct qnet_test current_test = qnet_test_tests[index][test_index]; \ + while (current_test.name != 0) { \ + printf("[*] running test %s...", current_test.name); \ + fflush(stdout); \ + const char *result = current_test.function(); \ + if (result == NULL) { \ + printf("\r[PASS] test %s completed successfully!\n", current_test.name); \ + } else { \ + printf("\r[FAIL] test %s failed with error:\n\t%s\n", current_test.name, result); \ + } \ + \ + test_index += 1; \ + current_test = qnet_test_tests[index][test_index]; \ + } \ + } + +struct qnet_test { + const char *name; + const char *(*function)(void); +}; + +#endif /* QNET_TEST */ \ No newline at end of file From fcc5bec00324c4779edabe8048d3a12f7d37c3ab Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 20 Jan 2026 20:11:42 +0100 Subject: [PATCH 2/2] feat(tests): added unit-test list --- tests/main.c | 2 ++ tests/unit/unit.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/unit/unit.h diff --git a/tests/main.c b/tests/main.c index 6bbf08d..8c548fa 100644 --- a/tests/main.c +++ b/tests/main.c @@ -6,7 +6,9 @@ * You may obtain a copy of the License in the file LICENSE or at * http://www.apache.org/licenses/LICENSE-2.0 */ +#include "unit/unit.h" int main(void) { + QNET_TEST_RUN(); return 0; } diff --git a/tests/unit/unit.h b/tests/unit/unit.h new file mode 100644 index 0000000..a4ff6d3 --- /dev/null +++ b/tests/unit/unit.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#ifndef QNET_UNIT +#define QNET_UNIT + +#include "test.h" + +QNET_TEST_START +QNET_TEST_END + +#endif /* QNET_UNIT */ \ No newline at end of file