test: add structure & framework for writing tests

This commit is contained in:
Jef Roosens 2023-01-18 13:08:09 +01:00
parent 3da95a63fb
commit c018d8d86c
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 1953 additions and 5 deletions

1839
test/acutest.h Normal file

File diff suppressed because it is too large Load diff

69
test/cron/test_parse.c Normal file
View file

@ -0,0 +1,69 @@
#include "acutest.h"
#include "vieter_cron.h"
void test_tutorial(void) {
void *mem;
mem = malloc(10);
TEST_CHECK(mem != NULL);
mem = realloc(mem, 20);
TEST_CHECK(mem != NULL);
free(mem);
}
void test_fail(void) {
int a, b;
/* This condition is designed to fail so you can see what the failed test
* output looks like. */
a = 1;
b = 2;
TEST_CHECK(a + b == 5);
/* Here is TEST_CHECK_ in action. */
TEST_CHECK_(a + b == 5, "%d + %d == 5", a, b);
/* We may also show more information about the failure. */
if (!TEST_CHECK(a + b == 5)) {
TEST_MSG("a: %d", a);
TEST_MSG("b: %d", b);
}
/* The macro TEST_MSG() only outputs something when the preceding
* condition fails, so we can avoid the 'if' statement. */
TEST_CHECK(a + b == 3);
TEST_MSG("a: %d", a);
TEST_MSG("b: %d", b);
}
static void helper(void) {
/* Kill the current test with a condition which is never true. */
TEST_ASSERT(1 == 2);
/* This never happens because the test is aborted above. */
TEST_CHECK(1 + 2 == 2 + 1);
}
void test_abort(void) {
helper();
/* This test never happens because the test is aborted inside the helper()
* function. */
TEST_CHECK(1 * 2 == 2 * 1);
}
void test_crash(void) {
int *invalid = ((int *)NULL) + 0xdeadbeef;
*invalid = 42;
TEST_CHECK_(1 == 1, "This should never execute, due to a write into "
"an invalid address.");
}
TEST_LIST = {{"tutorial", test_tutorial},
{"fail", test_fail},
{"abort", test_abort},
{"crash", test_crash},
{NULL, NULL}};