mirror of
https://github.com/zaerl/mojibake.git
synced 2026-11-30 16:48:26 +01:00
543 lines
15 KiB
C
543 lines
15 KiB
C
/**
|
|
* The Mojibake library
|
|
*
|
|
* This file is distributed under the MIT License. See LICENSE for details.
|
|
*/
|
|
|
|
// clang-format off
|
|
#ifdef _WIN32
|
|
#include "../src/shell/getopt/getopt.h"
|
|
#include <direct.h>
|
|
#include <io.h>
|
|
#include <windows.h>
|
|
#define isatty _isatty
|
|
#ifndef STDOUT_FILENO
|
|
#define STDOUT_FILENO _fileno(stdout)
|
|
#endif
|
|
#define chdir _chdir
|
|
#else
|
|
#include <fcntl.h>
|
|
#include <getopt.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#define ATT_IMPLEMENTATION
|
|
#include "test.h"
|
|
|
|
#ifndef MJB_TEST_SOURCE_DIR
|
|
#define MJB_TEST_SOURCE_DIR "."
|
|
#endif
|
|
// clang-format on
|
|
|
|
#define MJB_TEST_COVERAGE_MAX_ENTRIES 256
|
|
#define MJB_TEST_COVERAGE_NAME_SIZE 128
|
|
|
|
static att_test_callback error_callback = NULL;
|
|
static bool exit_on_error = false;
|
|
static bool coverage_enabled = false;
|
|
static FILE *coverage_file = NULL;
|
|
|
|
typedef struct mjb_test_allocator_state {
|
|
size_t call_count;
|
|
size_t allocation_count;
|
|
size_t fail_after;
|
|
size_t live_count;
|
|
size_t violation_count;
|
|
} mjb_test_allocator_state;
|
|
|
|
static mjb_test_allocator_state allocator_state = { 0, 0, SIZE_MAX, 0, 0 };
|
|
static mjb_status allocator_invalid_status = MJB_STATUS_OK;
|
|
static mjb_status allocator_install_status = MJB_STATUS_INVALID_ARGUMENT;
|
|
static const char *allocator_current_test = "(none)";
|
|
|
|
// Every block starts with a header that records the size the library requested, so realloc and
|
|
// free can check the size the library passes back. The union keeps the payload aligned.
|
|
typedef union mjb_test_block_header {
|
|
size_t size;
|
|
long double long_double_alignment;
|
|
long long long_long_alignment;
|
|
void *pointer_alignment;
|
|
} mjb_test_block_header;
|
|
|
|
static void mjb_test_allocator_violation(const char *what, size_t expected, size_t actual) {
|
|
if(allocator_state.violation_count++ < 10) {
|
|
fprintf(stderr, "Allocator contract violation in %s: %s (expected %zu, got %zu)\n",
|
|
allocator_current_test, what, expected, actual);
|
|
}
|
|
}
|
|
|
|
static void *mjb_test_block_payload(mjb_test_block_header *header) {
|
|
return header == NULL ? NULL : (void *)(header + 1);
|
|
}
|
|
|
|
static mjb_test_block_header *mjb_test_block_header_of(void *ptr) {
|
|
return ptr == NULL ? NULL : (mjb_test_block_header *)ptr - 1;
|
|
}
|
|
|
|
static void *mjb_test_alloc(void *context, size_t size) {
|
|
mjb_test_allocator_state *state = (mjb_test_allocator_state *)context;
|
|
++state->call_count;
|
|
|
|
if(size == 0) {
|
|
mjb_test_allocator_violation("alloc requested 0 bytes", 1, 0);
|
|
}
|
|
|
|
if(state->allocation_count++ >= state->fail_after) {
|
|
return NULL;
|
|
}
|
|
|
|
mjb_test_block_header *header = (mjb_test_block_header *)malloc(sizeof(*header) + size);
|
|
|
|
if(header == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
header->size = size;
|
|
++state->live_count;
|
|
|
|
return mjb_test_block_payload(header);
|
|
}
|
|
|
|
static void *mjb_test_realloc(void *context, void *ptr, size_t old_size, size_t new_size) {
|
|
mjb_test_allocator_state *state = (mjb_test_allocator_state *)context;
|
|
mjb_test_block_header *header = mjb_test_block_header_of(ptr);
|
|
++state->call_count;
|
|
|
|
if(new_size == 0) {
|
|
mjb_test_allocator_violation("realloc requested 0 bytes", 1, 0);
|
|
}
|
|
|
|
if(header == NULL ? old_size != 0 : header->size != old_size) {
|
|
mjb_test_allocator_violation("realloc old_size", header == NULL ? 0 : header->size,
|
|
old_size);
|
|
}
|
|
|
|
if(state->allocation_count++ >= state->fail_after) {
|
|
return NULL;
|
|
}
|
|
|
|
mjb_test_block_header *new_header = (mjb_test_block_header *)realloc(header,
|
|
sizeof(*new_header) + new_size);
|
|
|
|
if(new_header == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
new_header->size = new_size;
|
|
|
|
if(header == NULL) {
|
|
++state->live_count;
|
|
}
|
|
|
|
return mjb_test_block_payload(new_header);
|
|
}
|
|
|
|
static void mjb_test_dealloc(void *context, void *ptr, size_t size) {
|
|
mjb_test_allocator_state *state = (mjb_test_allocator_state *)context;
|
|
mjb_test_block_header *header = mjb_test_block_header_of(ptr);
|
|
++state->call_count;
|
|
|
|
if(header == NULL) {
|
|
mjb_test_allocator_violation("free received NULL", 0, 0);
|
|
|
|
return;
|
|
}
|
|
|
|
if(header->size != size) {
|
|
mjb_test_allocator_violation("free size", header->size, size);
|
|
}
|
|
|
|
--state->live_count;
|
|
free(header);
|
|
}
|
|
|
|
bool mjb_test_allocator_initialize(void) {
|
|
mjb_allocator invalid = { &allocator_state, mjb_test_alloc, NULL, mjb_test_dealloc };
|
|
allocator_invalid_status = mjb_set_allocator(&invalid);
|
|
|
|
mjb_allocator allocator = { &allocator_state, mjb_test_alloc, mjb_test_realloc,
|
|
mjb_test_dealloc };
|
|
allocator_install_status = mjb_set_allocator(&allocator);
|
|
|
|
return allocator_invalid_status == MJB_STATUS_INVALID_ARGUMENT &&
|
|
allocator_install_status == MJB_STATUS_OK;
|
|
}
|
|
|
|
mjb_status mjb_test_allocator_invalid_status(void) {
|
|
return allocator_invalid_status;
|
|
}
|
|
|
|
mjb_status mjb_test_allocator_install_status(void) {
|
|
return allocator_install_status;
|
|
}
|
|
|
|
void mjb_test_allocator_reset(void) {
|
|
allocator_state.call_count = 0;
|
|
allocator_state.allocation_count = 0;
|
|
allocator_state.fail_after = SIZE_MAX;
|
|
}
|
|
|
|
void mjb_test_allocator_fail_after(size_t fail_after) {
|
|
mjb_test_allocator_reset();
|
|
allocator_state.fail_after = fail_after;
|
|
}
|
|
|
|
size_t mjb_test_allocator_call_count(void) {
|
|
return allocator_state.call_count;
|
|
}
|
|
|
|
size_t mjb_test_allocator_allocation_count(void) {
|
|
return allocator_state.allocation_count;
|
|
}
|
|
|
|
size_t mjb_test_allocator_live_count(void) {
|
|
return allocator_state.live_count;
|
|
}
|
|
|
|
size_t mjb_test_allocator_violation_count(void) {
|
|
return allocator_state.violation_count;
|
|
}
|
|
|
|
typedef struct {
|
|
char name[MJB_TEST_COVERAGE_NAME_SIZE];
|
|
unsigned long long count;
|
|
} mjb_test_coverage_entry;
|
|
|
|
static mjb_test_coverage_entry coverage_entries[MJB_TEST_COVERAGE_MAX_ENTRIES];
|
|
static size_t coverage_entry_count = 0;
|
|
static char coverage_current[MJB_TEST_COVERAGE_NAME_SIZE] = { 0 };
|
|
|
|
char *mjb_test_strsep(char **stringp, const char *delim) {
|
|
char *start = *stringp;
|
|
char *p;
|
|
|
|
if(start == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
p = strpbrk(start, delim);
|
|
if(p) {
|
|
*p = '\0';
|
|
*stringp = p + 1;
|
|
} else {
|
|
*stringp = NULL;
|
|
}
|
|
|
|
return start;
|
|
}
|
|
|
|
static bool coverage_name_char(char c) {
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
|
|
}
|
|
|
|
static bool coverage_name_from_expression(const char *expression, char *name, size_t size) {
|
|
const char *found = expression;
|
|
|
|
if(!expression || size == 0) {
|
|
return false;
|
|
}
|
|
|
|
// Check if it's a mjb_*( function call.
|
|
while((found = strstr(found, "mjb_")) != NULL) {
|
|
size_t length = 0;
|
|
const char *next;
|
|
|
|
while(coverage_name_char(found[length])) {
|
|
++length;
|
|
}
|
|
|
|
next = found + length;
|
|
|
|
while(*next == ' ' || *next == '\t' || *next == '\n') {
|
|
++next;
|
|
}
|
|
|
|
// Skip the mjb_test_* helpers, they are not library functions.
|
|
if(*next == '(' && length > 0 && length < size && strncmp(found, "mjb_test_", 9) != 0) {
|
|
memcpy(name, found, length);
|
|
name[length] = '\0';
|
|
|
|
return true;
|
|
}
|
|
|
|
found += 4;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void mjb_test_coverage_clear(void) {
|
|
coverage_current[0] = '\0';
|
|
}
|
|
|
|
void mjb_test_coverage_set(const char *name) {
|
|
if(!name || !name[0]) {
|
|
coverage_current[0] = '\0';
|
|
|
|
return;
|
|
}
|
|
|
|
snprintf(coverage_current, sizeof(coverage_current), "%s", name);
|
|
}
|
|
|
|
static void coverage_record(const char *name) {
|
|
if(!coverage_enabled || !name || !name[0]) {
|
|
return;
|
|
}
|
|
|
|
for(size_t i = 0; i < coverage_entry_count; ++i) {
|
|
if(strcmp(coverage_entries[i].name, name) == 0) {
|
|
++coverage_entries[i].count;
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
if(coverage_entry_count >= MJB_TEST_COVERAGE_MAX_ENTRIES) {
|
|
return;
|
|
}
|
|
|
|
snprintf(coverage_entries[coverage_entry_count].name,
|
|
sizeof(coverage_entries[coverage_entry_count].name), "%s", name);
|
|
coverage_entries[coverage_entry_count].count = 1;
|
|
++coverage_entry_count;
|
|
}
|
|
|
|
// Callback for test assertions to record coverage and handle errors
|
|
static int attractor_test_callback(int test, const char *description, const char *expression,
|
|
const char *file, unsigned int line) {
|
|
// Exit on error if the flag is set.
|
|
if(exit_on_error) {
|
|
if(!test) {
|
|
if(error_callback) {
|
|
error_callback(test, description, expression, file, line);
|
|
}
|
|
|
|
exit(1);
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// Save coverage for the current assertion if coverage is enabled.
|
|
if(coverage_enabled) {
|
|
char name[MJB_TEST_COVERAGE_NAME_SIZE];
|
|
|
|
if(coverage_name_from_expression(expression, name, sizeof(name))) {
|
|
// If it's a read mjb_* function, record it for coverage
|
|
mjb_test_coverage_set(name);
|
|
}
|
|
|
|
if(coverage_current[0]) {
|
|
coverage_record(coverage_current);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Open the coverage output file. This runs before the test binary changes into the data root, so
|
|
// a relative path given with -C is resolved against the directory the binary was launched from.
|
|
static bool open_coverage_file(const char *path) {
|
|
if(path == NULL) {
|
|
return false;
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
coverage_file = fopen(path, "w");
|
|
#else
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
|
|
|
coverage_file = fd < 0 ? NULL : fdopen(fd, "w");
|
|
|
|
if(coverage_file == NULL && fd >= 0) {
|
|
close(fd);
|
|
}
|
|
#endif
|
|
|
|
if(coverage_file == NULL) {
|
|
perror("coverage");
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void write_coverage_file(void) {
|
|
if(!coverage_file) {
|
|
return;
|
|
}
|
|
|
|
fputs("{\n \"coverage\": [\n", coverage_file);
|
|
|
|
for(size_t i = 0; i < coverage_entry_count; ++i) {
|
|
fprintf(coverage_file, " { \"name\": \"%s\", \"count\": %llu }%s\n",
|
|
coverage_entries[i].name, coverage_entries[i].count,
|
|
i + 1 == coverage_entry_count ? "" : ",");
|
|
}
|
|
|
|
fputs(" ]\n}\n", coverage_file);
|
|
fclose(coverage_file);
|
|
coverage_file = NULL;
|
|
}
|
|
|
|
// Resolve the directory that holds the Unicode data files used by the tests.
|
|
static const char *test_source_dir(void) {
|
|
const char *env = getenv("MJB_TEST_SOURCE_DIR");
|
|
|
|
if(env != NULL && env[0] != '\0') {
|
|
return env;
|
|
}
|
|
|
|
return MJB_TEST_SOURCE_DIR;
|
|
}
|
|
|
|
// Check that the current directory holds the Unicode data files the tests read.
|
|
static bool test_data_available(void) {
|
|
FILE *file = fopen("./unicode-data/UCD/CaseFolding.txt", "r");
|
|
|
|
if(file == NULL) {
|
|
return false;
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
return true;
|
|
}
|
|
|
|
static int show_version(void) {
|
|
printf("Mojibake v%s\n", MJB_VERSION);
|
|
|
|
return 0;
|
|
}
|
|
|
|
bool is_exit_on_error(void) {
|
|
return exit_on_error;
|
|
}
|
|
|
|
void set_error_callback(att_test_callback callback) {
|
|
error_callback = callback;
|
|
}
|
|
|
|
static void show_help(const char *executable, struct option options[], const char *descriptions[],
|
|
const char *error) {
|
|
FILE *stream = error ? stderr : stdout;
|
|
|
|
fprintf(stream, "%s%smojibake - Mojibake test client [v%s]\n\nUsage: %s [OPTIONS]\n",
|
|
error ? error : "", error ? "\n\n" : "", MJB_VERSION, executable);
|
|
fprintf(stream, "Options:\n");
|
|
|
|
for(unsigned long i = 0; options[i].name != NULL; ++i) {
|
|
fprintf(stream, " -%c%s, --%s%s\n\t%s\n", options[i].val,
|
|
options[i].has_arg == no_argument ? "" : " ARG", options[i].name,
|
|
options[i].has_arg == no_argument ? "" : "=ARG", descriptions[i]);
|
|
}
|
|
}
|
|
|
|
static void test_started(const char *name) {
|
|
allocator_current_test = name;
|
|
mjb_test_coverage_clear();
|
|
}
|
|
|
|
int main(int argc, char *const argv[]) {
|
|
unsigned int verbosity = 1;
|
|
int option = 0;
|
|
int option_index = 0;
|
|
const char *filter = NULL;
|
|
const char *coverage_output = NULL;
|
|
const char *source_dir = test_source_dir();
|
|
|
|
struct option long_options[] = { { "coverage", required_argument, NULL, 'C' },
|
|
{ "filter", required_argument, NULL, 'f' }, { "help", no_argument, NULL, 'h' },
|
|
{ "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' },
|
|
{ "exit-on-error", no_argument, NULL, 'e' }, { NULL, 0, NULL, 0 } };
|
|
const char *descriptions[] = { "Write runtime API coverage counts to the given JSON file",
|
|
"Filter tests by name in the form name1,name2,...", "Show this help message",
|
|
"Verbose output. -vv for more verbosity", "Print version",
|
|
"Exit immediately on first test failure" };
|
|
|
|
att_set_verbose(verbosity);
|
|
|
|
while((option = getopt_long(argc, argv, "C:f:ehvV", long_options, &option_index)) != -1) {
|
|
switch(option) {
|
|
case 'C':
|
|
coverage_enabled = true;
|
|
coverage_output = optarg;
|
|
break;
|
|
case 'f':
|
|
filter = optarg;
|
|
break;
|
|
case 'e':
|
|
exit_on_error = true;
|
|
break;
|
|
case 'h':
|
|
show_help(argv[0], long_options, descriptions, NULL);
|
|
return 0;
|
|
case 'v':
|
|
++verbosity;
|
|
break;
|
|
case 'V':
|
|
return show_version();
|
|
case '?':
|
|
// getopt_long already printed an error message
|
|
break;
|
|
default:
|
|
abort();
|
|
}
|
|
}
|
|
|
|
att_set_verbose(verbosity);
|
|
|
|
if(exit_on_error || coverage_enabled) {
|
|
att_set_test_callback(attractor_test_callback);
|
|
}
|
|
|
|
// Open the coverage file before changing directory so a relative -C path is resolved against
|
|
// the directory the binary was launched from, not against the data root.
|
|
if(coverage_enabled && !open_coverage_file(coverage_output)) {
|
|
return 1;
|
|
}
|
|
|
|
// The tests open the Unicode data files with paths relative to the repository root.
|
|
if(chdir(source_dir) != 0) {
|
|
// If the test source directory is not found, print an error message and exit
|
|
perror("chdir");
|
|
fprintf(stderr,
|
|
"Cannot change into the test source directory \"%s\". Set the "
|
|
"MJB_TEST_SOURCE_DIR environment variable to the Mojibake repository root.\n",
|
|
source_dir);
|
|
|
|
return 1;
|
|
}
|
|
|
|
if(!test_data_available()) {
|
|
fprintf(stderr,
|
|
"Warning: Unicode data files not found under \"%s\". Data-driven tests will fail. "
|
|
"Set the MJB_TEST_SOURCE_DIR environment variable to the Mojibake repository "
|
|
"root.\n",
|
|
source_dir);
|
|
}
|
|
|
|
if(!mjb_test_allocator_initialize()) {
|
|
fputs("Failed to install the test allocator\n", stderr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
att_set_test_start_callback(test_started);
|
|
printf("Running tests with %s\n", filter ? filter : "no filter");
|
|
att_run_tests(filter);
|
|
bool all_valid = att_report() == 0;
|
|
|
|
if(allocator_state.violation_count > 0) {
|
|
fprintf(stderr, "%zu allocator contract violations\n", allocator_state.violation_count);
|
|
all_valid = false;
|
|
}
|
|
|
|
write_coverage_file();
|
|
|
|
return all_valid ? 0 : -1;
|
|
}
|