62 lines
3.5 KiB
C
62 lines
3.5 KiB
C
|
|
/*
|
||
|
|
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
||
|
|
*
|
||
|
|
* 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 <stdio.h>
|
||
|
|
|
||
|
|
#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 */
|