mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-16 23:35:46 +01:00
rewrite testing code to be a bit more durable and clean.
This commit is contained in:
48
test/test.h
48
test/test.h
@@ -3,46 +3,14 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
const char *test_ctest;
|
||||
size_t test_runs = 0;
|
||||
#include "../src/util/intdef.h"
|
||||
|
||||
// evaluates the test
|
||||
// returns 1 upon error
|
||||
static inline int assert_helper(int cond, const char *restrict fname, unsigned ln, const char *restrict fnname, const char *restrict expr) {
|
||||
test_runs++;
|
||||
if (cond)
|
||||
printf("[\033[32;1m OK \033[0m] %s %s -> %s:%u (%s)\n", test_ctest, fnname, fname, ln, expr);
|
||||
else
|
||||
printf("[\033[31;1m FAIL \033[0m] %s %s -> %s:%u (%s)\n", test_ctest, fnname, fname, ln, expr);
|
||||
return !cond;
|
||||
}
|
||||
extern uint test_okay;
|
||||
extern uint test_fail;
|
||||
|
||||
#define assert_true(expr) assert_helper(!!(expr), __FILE__, __LINE__, __func__, #expr) // evaluation expected to be true
|
||||
#define assert_false(expr) assert_helper(!(expr), __FILE__, __LINE__, __func__, #expr) // evaluation expected to be false
|
||||
/* Prints the result `res` of a test.
|
||||
* Returns `res` back, for ease of chaining. */
|
||||
int test_process(int res, const char *restrict file, uint ln, const char *restrict function, const char *restrict expression);
|
||||
|
||||
// contains the data for executing a single test
|
||||
struct testdat {
|
||||
const char *name; // test name
|
||||
int (*test)(void *); // test, returns 0 upon success, non-zero upon failure
|
||||
void *args; // arguments to the test
|
||||
};
|
||||
typedef struct testdat testdat;
|
||||
|
||||
// executes the tests, returns the amount of failed tests; >0: failure
|
||||
static inline size_t exec_tests(testdat *dat, size_t ntests) {
|
||||
size_t i;
|
||||
size_t err = 0;
|
||||
|
||||
// perform tests and count the error state
|
||||
for (i = 0; i < ntests; i++) {
|
||||
test_ctest = dat[i].name;
|
||||
err += !!(dat[i].test(dat[i].args));
|
||||
}
|
||||
|
||||
// give final score
|
||||
if (!err)
|
||||
fprintf(stdout, "tests completed! (%zu/%zu)\n", test_runs - err, test_runs);
|
||||
else
|
||||
fprintf(stderr, " tests failed! (%zu/%zu)\n", test_runs - err, test_runs);
|
||||
return err;
|
||||
}
|
||||
#define assert_true(expr) test_process((expr), __FILE__, __LINE__, __func__, #expr)
|
||||
#define assert_false(expr) assert_true(!(expr))
|
||||
|
||||
Reference in New Issue
Block a user