483 lines
16 KiB
C
483 lines
16 KiB
C
#include <setjmp.h>
|
|
#include <getopt.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <inttypes.h>
|
|
|
|
#include "xtest.h"
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
struct xtest_assert_info {
|
|
const char *file;
|
|
int line;
|
|
const char *func;
|
|
const char *expr;
|
|
};
|
|
|
|
struct xtest_assert_info assertion_info;
|
|
|
|
#ifndef XTEST_MAX_GROUP_NESTING
|
|
#define XTEST_MAX_GROUP_NESTING 16
|
|
#endif
|
|
|
|
#ifndef XTEST_TEST_NAME_MAX_LENGTH
|
|
#define XTEST_TEST_NAME_MAX_LENGTH 1023
|
|
#endif
|
|
|
|
#ifndef XTEST_INDENT
|
|
#define XTEST_INDENT 2
|
|
#endif
|
|
|
|
|
|
enum output_format {
|
|
format_default,
|
|
format_subunit,
|
|
};
|
|
|
|
enum output_format output_format;
|
|
|
|
const char *name_filter;
|
|
|
|
const char *group_nesting[XTEST_MAX_GROUP_NESTING];
|
|
int group_nesting_pos = 0;
|
|
|
|
jmp_buf xtest_jmp;
|
|
|
|
#ifndef XTEST_ASSERT_MESSAGE_MAX_LEN
|
|
#define XTEST_ASSERT_MESSAGE_MAX_LEN 1023
|
|
#endif
|
|
char assert_message[XTEST_ASSERT_MESSAGE_MAX_LEN + 1];
|
|
const char *skip_reason;
|
|
|
|
int xtest_indent = 0;
|
|
|
|
int expecting_assertion = 0;
|
|
int num_tests = 0;
|
|
int successful_tests = 0;
|
|
int failed_tests = 0;
|
|
int skipped_tests = 0;
|
|
|
|
_Bool list_tests = 0;
|
|
|
|
|
|
#define USAGE_BASE "[-?|--help] [-l|--list] [-m|--match filter] [-f|--output-format format]"
|
|
|
|
#define USAGE_HELP_LIST "-l|--list list the available tests"
|
|
#define USAGE_HELP_MATCH "-m|--match filter only run/list tests whose name matches the filter"
|
|
#define USAGE_HELP_FORMAT "-f|--output-format format specify the output format; supported formats are \"default\" and \"subunit\""
|
|
#define USAGE_HELP_HELP "-?|--help show this message"
|
|
|
|
#define USAGE "Usage: %s " USAGE_BASE "\n " USAGE_HELP_LIST "\n " USAGE_HELP_MATCH "\n " USAGE_HELP_FORMAT "\n " USAGE_HELP_HELP
|
|
|
|
|
|
static _Noreturn void usage(const char *progname) {
|
|
printf(USAGE, progname);
|
|
exit(1);
|
|
}
|
|
|
|
static _Bool matches(const char *name, const char *filter) {
|
|
// eat up the matching parts of name and filter until the first * or end of either string
|
|
while (*name == *filter && *name != '\0' && *filter != '\0' && *filter != '*') {
|
|
name++;
|
|
filter++;
|
|
}
|
|
// if the next character in filter is not a * then there is no match (otherwise the previous loop would have continued)
|
|
if (*filter != '\0' && *filter != '*') return 0;
|
|
|
|
while (1) {
|
|
// if the filter ends before the name then they don't match
|
|
if (*filter == '\0') {
|
|
return *name == '\0';
|
|
}
|
|
// the filter points to a * at this point because everything else was eaten up either before the loop or in the previous iteration of the loop
|
|
assert (*filter == '*');
|
|
while (*filter == '*') filter++; // eat up consecutive *s
|
|
size_t part_len = strcspn(filter, "*");
|
|
if (part_len == 0) return 1; // filter ends with a * so every remaining input in name is accepted
|
|
char part[part_len + 1];
|
|
strncpy(part, filter, part_len);
|
|
part[part_len] = '\0'; // ensure null-termination since strncpy does not null-terminate on its own
|
|
name = strstr(name, part);
|
|
if (name == NULL) return 0;
|
|
name += part_len;
|
|
filter += part_len;
|
|
}
|
|
}
|
|
|
|
void xtest_init(int argc, char **argv) {
|
|
int c;
|
|
int list_flag = 0;
|
|
int help_flag = 0;
|
|
const char *format = NULL;
|
|
const char *progname = argv[0];
|
|
|
|
struct option long_options[] =
|
|
{
|
|
{"list", no_argument, &list_flag, 1}, // list the tests instead of running them
|
|
{"help", no_argument, &help_flag, 1}, // list the tests instead of running them
|
|
{"match", required_argument, 0, 'm'}, // only run tests that match the provided filter
|
|
{"output-format", required_argument, 0, 'f'}, // select output format
|
|
{0, 0, 0, 0}
|
|
};
|
|
while (1) {
|
|
int option_index = 0;
|
|
c = getopt_long(argc, argv, "?lm:f:", long_options, &option_index);
|
|
if (c == -1) break;
|
|
switch (c) {
|
|
case 0:
|
|
assert(long_options[option_index].flag != 0);
|
|
break;
|
|
case 'l':
|
|
list_flag = 1;
|
|
break;
|
|
case 'm':
|
|
name_filter = optarg;
|
|
break;
|
|
case 'f':
|
|
format = optarg;
|
|
break;
|
|
case '?':
|
|
help_flag = 1;
|
|
break;
|
|
default:
|
|
assert(0);
|
|
}
|
|
}
|
|
|
|
if (help_flag) {
|
|
usage(progname);
|
|
}
|
|
|
|
if (list_flag)
|
|
list_tests = 1;
|
|
|
|
if (format == NULL) {
|
|
format = "default";
|
|
}
|
|
if (strcmp(format, "default") == 0) {
|
|
output_format = format_default;
|
|
} else if (strcmp(format, "subunit") == 0) {
|
|
output_format = format_subunit;
|
|
} else {
|
|
printf("invalid output format '%s', allowed formats are 'default' and 'subunit'\n", format);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
|
|
int xtest_complete() {
|
|
if (!list_tests) {
|
|
if (output_format == format_default) {
|
|
printf("============\nTotal: %d, Failed: %d, Skipped: %d\n", num_tests, failed_tests, skipped_tests);
|
|
}
|
|
fflush(stdout);
|
|
return failed_tests != 0 ? 1 : 0;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void subunit_message(const char *name, const char *message, const char *details) {
|
|
printf("%s: %s", message, name);
|
|
if (details == NULL) {
|
|
printf("\n");
|
|
} else {
|
|
printf(" [\n%s%s]\n", details, details[strlen(details)] != '\n' ? "\n" : "");
|
|
}
|
|
}
|
|
|
|
void xtest_internal_run(xtest_test_fn fn, const char *name, xtest_param *params, xtest_setup_fn setup,
|
|
xtest_teardown_fn teardown) {
|
|
expecting_assertion = 0;
|
|
|
|
char full_name[XTEST_TEST_NAME_MAX_LENGTH + 1] = "";
|
|
size_t name_len = 0;
|
|
for (int i = 0; i < group_nesting_pos; ++i) {
|
|
name_len += snprintf(full_name + name_len, XTEST_TEST_NAME_MAX_LENGTH - name_len, "%s/", group_nesting[i]);
|
|
assert(name_len <= XTEST_TEST_NAME_MAX_LENGTH);
|
|
}
|
|
name_len += snprintf(full_name + name_len, XTEST_TEST_NAME_MAX_LENGTH - name_len, "%s", name);
|
|
assert(name_len <= XTEST_TEST_NAME_MAX_LENGTH);
|
|
|
|
if (name_filter != NULL) {
|
|
if (!matches(full_name, name_filter)) return;
|
|
}
|
|
|
|
if (list_tests) {
|
|
printf("%s\n", full_name);
|
|
return;
|
|
}
|
|
|
|
num_tests += 1;
|
|
|
|
|
|
void *fixture = NULL;
|
|
|
|
if (output_format == format_subunit) {
|
|
subunit_message(full_name, "test", NULL);
|
|
} else {
|
|
printf("%*s%s: ", xtest_indent, "", name);
|
|
}
|
|
if (setup != NULL) {
|
|
int jmpres = setjmp(xtest_jmp);
|
|
if (jmpres == 0) {
|
|
setup(&fixture);
|
|
} else if (jmpres == 3) {
|
|
goto skipped;
|
|
} else {
|
|
snprintf(assert_message, 1024, "assertion failure in setup function: %s in %s (%s:%d)", assertion_info.expr,
|
|
assertion_info.func, assertion_info.file, assertion_info.line);
|
|
goto failed;
|
|
}
|
|
}
|
|
|
|
int jmpres = setjmp(xtest_jmp);
|
|
if (jmpres == 0) {
|
|
if (params == NULL) {
|
|
expecting_assertion = 0;
|
|
fn(fixture, NULL);
|
|
} else {
|
|
int num_params = 0;
|
|
while (params[num_params].name != NULL) num_params++;
|
|
int validx[num_params];
|
|
int valmax[num_params];
|
|
void *current_vals[num_params];
|
|
for (int i = 0; i < num_params; ++i) {
|
|
validx[i] = 0;
|
|
valmax[i] = 0;
|
|
while ((*params[i].values)[valmax[i]] != NULL) valmax[i]++;
|
|
}
|
|
while (1) {
|
|
for (int i = 0; i < num_params; ++i) {
|
|
current_vals[i] = (*params[i].values)[validx[i]];
|
|
}
|
|
expecting_assertion = 0;
|
|
fn(fixture, current_vals);
|
|
_Bool carry = 1;
|
|
for (int i = num_params - 1; i >= 0; --i) {
|
|
validx[i] += 1;
|
|
if (validx[i] >= valmax[i]) {
|
|
validx[i] = 0;
|
|
} else {
|
|
carry = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (carry) break;
|
|
}
|
|
}
|
|
|
|
if (expecting_assertion) { // an assertion failure in the tested code was expected but didn't occur
|
|
snprintf(assert_message, 1024, "expected an assertion failure in the tested code");
|
|
goto failed;
|
|
} else {
|
|
goto success;
|
|
}
|
|
} else if (jmpres == 1) { // a testing assertion failed
|
|
goto failed;
|
|
} else if (jmpres == 2) { // an assertion in the tested code failed
|
|
if (expecting_assertion) {
|
|
goto success;
|
|
} else {
|
|
snprintf(assert_message, 1024, "unexpected assertion failure in tested code: %s in %s (%s:%d)",
|
|
assertion_info.expr, assertion_info.func, assertion_info.file, assertion_info.line);
|
|
goto failed;
|
|
}
|
|
} else if (jmpres == 3) {
|
|
goto skipped;
|
|
}
|
|
|
|
success:
|
|
|
|
if (output_format == format_subunit) {
|
|
subunit_message(full_name, "success", NULL);
|
|
} else {
|
|
printf("SUCCESS\n");
|
|
}
|
|
successful_tests += 1;
|
|
goto cleanup;
|
|
failed:
|
|
|
|
if (output_format == format_subunit) {
|
|
subunit_message(full_name, "failure", assert_message);
|
|
} else {
|
|
printf("FAILED:\n%*s %s\n", xtest_indent, "", assert_message);
|
|
}
|
|
failed_tests += 1;
|
|
goto cleanup;
|
|
skipped:
|
|
skipped_tests += 1;
|
|
|
|
if (output_format == format_subunit) {
|
|
subunit_message(full_name, "skipped", skip_reason);
|
|
} else {
|
|
if (skip_reason != NULL) {
|
|
printf("SKIPPED: %s\n", skip_reason);
|
|
} else {
|
|
printf("SKIPPED\n");
|
|
}
|
|
}
|
|
goto cleanup;
|
|
|
|
cleanup:
|
|
if (teardown != NULL && fixture != NULL) {
|
|
teardown(fixture);
|
|
}
|
|
}
|
|
|
|
|
|
/// this function replaces the standard library assert and jumps back to the xtest_internal_run invocation that is running the test
|
|
/// \param file file in which the assertion happened
|
|
/// \param line line on which the assertion happened
|
|
/// \param func function in which the assertion happened
|
|
/// \param expr stringified expression of the assertion
|
|
void xtest_internal_assert(const char *file, int line, const char *func, const char *expr) {
|
|
assertion_info = (struct xtest_assert_info) {
|
|
.file = file, .line = line, .func = func, .expr = expr
|
|
};
|
|
longjmp(xtest_jmp, 2);
|
|
}
|
|
|
|
|
|
#define FAIL_ASSERT_FORMAT_1 "assertion failed: %s, expected %s"
|
|
#define FAIL_ASSERT_FORMAT_2 " but got "
|
|
#define FAIL_ASSERT_FORMAT_3 " (%s:%d)"
|
|
#define FAIL_ASSERT_FORMAT(fmt) FAIL_ASSERT_FORMAT_1 fmt FAIL_ASSERT_FORMAT_2 fmt FAIL_ASSERT_FORMAT_3
|
|
#define CAST_PTR(type, ptr) *(type *)(ptr)
|
|
#define PRINT_ASSERT_MESSAGE(fmt, type) snprintf(assert_message, XTEST_ASSERT_MESSAGE_MAX_LEN, FAIL_ASSERT_FORMAT(fmt), \
|
|
expression, invert ? "not " : "", CAST_PTR(type, expected), CAST_PTR(type, actual), file, line)
|
|
|
|
void xtest_fail_assert(const char *expression, const char *file, int line, void *expected,
|
|
void *actual, _Bool invert, enum xtest_type type) {
|
|
switch (type) {
|
|
case xtest_type_bool:
|
|
snprintf(assert_message, XTEST_ASSERT_MESSAGE_MAX_LEN, FAIL_ASSERT_FORMAT("%s"), expression,
|
|
invert ? "not " : "",
|
|
CAST_PTR(_Bool, expected) ? "true" : "false", CAST_PTR(_Bool, actual) ? "true" : "false", file,
|
|
line);
|
|
break;
|
|
case xtest_type_char:
|
|
PRINT_ASSERT_MESSAGE("'%c'", char);
|
|
break;
|
|
case xtest_type_short:
|
|
PRINT_ASSERT_MESSAGE("%i", short);
|
|
break;
|
|
case xtest_type_unsigned_short:
|
|
PRINT_ASSERT_MESSAGE("%u", unsigned short);
|
|
break;
|
|
case xtest_type_int:
|
|
PRINT_ASSERT_MESSAGE("%i", int);
|
|
break;
|
|
case xtest_type_unsigned_int:
|
|
PRINT_ASSERT_MESSAGE("%u", unsigned int);
|
|
break;
|
|
case xtest_type_long:
|
|
PRINT_ASSERT_MESSAGE("%li", long);
|
|
break;
|
|
case xtest_type_unsigned_long:
|
|
PRINT_ASSERT_MESSAGE("%lu", unsigned long);
|
|
break;
|
|
case xtest_type_long_long:
|
|
PRINT_ASSERT_MESSAGE("%lli", long long);
|
|
break;
|
|
case xtest_type_unsigned_long_long:
|
|
PRINT_ASSERT_MESSAGE("%llu", unsigned long long);
|
|
break;
|
|
case xtest_type_float:
|
|
PRINT_ASSERT_MESSAGE("%f", float);
|
|
break;
|
|
case xtest_type_double:
|
|
PRINT_ASSERT_MESSAGE("%f", double);
|
|
break;
|
|
case xtest_type_long_double:
|
|
PRINT_ASSERT_MESSAGE("%Lf", long double);
|
|
break;
|
|
case xtest_type_string:
|
|
PRINT_ASSERT_MESSAGE("\"%s\"", char*);
|
|
break;
|
|
case xtest_type_void_pointer:
|
|
PRINT_ASSERT_MESSAGE("%p", void*);
|
|
break;
|
|
case xtest_type_other:
|
|
default:
|
|
snprintf(assert_message, XTEST_ASSERT_MESSAGE_MAX_LEN, FAIL_ASSERT_FORMAT("%s"), expression,
|
|
invert ? "not " : "",
|
|
"<value of unknown type>", "<value of unknown type>", file, line);
|
|
break;
|
|
}
|
|
longjmp(xtest_jmp, 1);
|
|
}
|
|
|
|
void
|
|
xtest_assert_float(double expected, double actual, int precision, _Bool invert, const char *expression,
|
|
const char *file,
|
|
int line) {
|
|
|
|
double epsilon = 1.0;
|
|
for (int i = 0; i < precision; ++i) epsilon /= 10.0;
|
|
double diff = expected - actual;
|
|
if (diff < 0) diff = -diff;
|
|
_Bool equals = diff < epsilon;
|
|
_Bool failed = invert == equals; // invert = true: fail if equals, invert = false: fail if not equals
|
|
if (!failed) return;
|
|
snprintf(assert_message, 1024,
|
|
"assertion failed: %s, expected %s%g but got %g (difference %g, precision %d) (%s:%d)",
|
|
expression, invert ? "not " : "", expected, actual, diff, precision, file, line);
|
|
longjmp(xtest_jmp, 1);
|
|
}
|
|
|
|
size_t find_diff_offset(const char *a, const char *b, size_t length) {
|
|
size_t offset = 0;
|
|
while (offset < length && *(a + offset) == *(b + offset)) offset++;
|
|
return offset;
|
|
}
|
|
|
|
void xtest_assert_mem(const char *expected, const char *actual, size_t length, _Bool invert, const char *expression,
|
|
const char *file,
|
|
int line) {
|
|
size_t offset = find_diff_offset(expected, actual, length);
|
|
_Bool equals = offset == length;
|
|
_Bool failed = invert == equals; // invert = true: fail if equals, invert = false: fail if not equals
|
|
if (!failed) return;
|
|
|
|
if (invert) {
|
|
snprintf(assert_message, 1024,
|
|
"assertion failed: %s, expected memory to not match but did (%s:%d)",
|
|
expression, file, line);
|
|
} else {
|
|
snprintf(assert_message, 1024,
|
|
"assertion failed: %s, expected memory to match but did not (first different byte at offset %zu, expected 0x%2x but got 0x%2x) (%s:%d)",
|
|
expression, offset, (unsigned int) expected[offset], (unsigned int) actual[offset], file, line);
|
|
}
|
|
|
|
longjmp(xtest_jmp, 1);
|
|
}
|
|
|
|
void xtest_skip(const char *reason) {
|
|
skip_reason = reason;
|
|
longjmp(xtest_jmp, 3);
|
|
}
|
|
|
|
void xtest_internal_start_group(const char *name) {
|
|
if (!list_tests) {
|
|
if (output_format == format_default) {
|
|
printf("%*s%s:\n", xtest_indent, "", name);
|
|
}
|
|
}
|
|
group_nesting[group_nesting_pos] = name;
|
|
group_nesting_pos += 1;
|
|
xtest_indent += XTEST_INDENT;
|
|
}
|
|
|
|
void xtest_internal_end_group() {
|
|
assert(group_nesting_pos > 0);
|
|
group_nesting_pos -= 1;
|
|
xtest_indent -= XTEST_INDENT;
|
|
}
|
|
|
|
void xtest_expect_assertion_failure() {
|
|
expecting_assertion = 1;
|
|
}
|