Skip to content

Commit

Permalink
Un-allman
Browse files Browse the repository at this point in the history
  • Loading branch information
cadmic committed Jun 4, 2024
1 parent fb2700a commit d0b3c91
Showing 1 changed file with 38 additions and 85 deletions.
123 changes: 38 additions & 85 deletions c_bindings/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ const char *get_crunch64_error_str(Crunch64Error error) {
return crunch64_error_str[error];
}

bool has_suffix(const char *str, const char *suffix)
{
bool has_suffix(const char *str, const char *suffix) {
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
if (str_len < suffix_len)
return false;
return strcmp(str + str_len - suffix_len, suffix) == 0;
return str_len >= suffix_len && strcmp(str + str_len - suffix_len, suffix) == 0;
}

uint8_t *read_binary_file(const char *path, size_t *size)
{
uint8_t *read_binary_file(const char *path, size_t *size) {
assert(path != NULL);
assert(size != NULL);

FILE *f = fopen(path, "rb");
if (f == NULL)
{
if (f == NULL) {
return NULL;
}

Expand All @@ -52,15 +47,13 @@ uint8_t *read_binary_file(const char *path, size_t *size)
fseek(f, 0, SEEK_SET);

uint8_t *data = malloc(*size * sizeof(uint8_t));
if (data == NULL)
{
if (data == NULL) {
fclose(f);
return NULL;
}

size_t count = fread(data, sizeof(uint8_t), *size, f);
if (count != *size)
{
if (count != *size) {
free(data);
fclose(f);
return NULL;
Expand All @@ -70,20 +63,17 @@ uint8_t *read_binary_file(const char *path, size_t *size)
return data;
}

bool write_binary_file(const char *path, uint8_t *data, size_t size)
{
bool write_binary_file(const char *path, uint8_t *data, size_t size) {
assert(path != NULL);
assert(data != NULL);

FILE *f = fopen(path, "wb");
if (f == NULL)
{
if (f == NULL) {
return false;
}

size_t count = fwrite(data, sizeof(uint8_t), size, f);
if (count != size)
{
if (count != size) {
return false;
}

Expand All @@ -92,16 +82,13 @@ bool write_binary_file(const char *path, uint8_t *data, size_t size)
return true;
}

bool compare_buffers(size_t a_size, const uint8_t *a, size_t b_size, const uint8_t *b)
{
if (a_size != b_size)
{
bool compare_buffers(size_t a_size, const uint8_t *a, size_t b_size, const uint8_t *b) {
if (a_size != b_size) {
fprintf(stderr, " sizes don't match\n");
return false;
}

if (memcmp(a, b, a_size) != 0)
{
if (memcmp(a, b, a_size) != 0) {
fprintf(stderr, " data doesn't match\n");
return false;
}
Expand All @@ -111,37 +98,28 @@ bool compare_buffers(size_t a_size, const uint8_t *a, size_t b_size, const uint8
return true;
}

bool test_matching_decompression(
compress_bound_fn decompress_bound,
compress_fn decompress,
size_t bin_size,
uint8_t *bin,
size_t compressed_size,
uint8_t *compressed_data)
{
bool test_matching_decompression(compress_bound_fn decompress_bound, compress_fn decompress, size_t bin_size,
uint8_t *bin, size_t compressed_size, uint8_t *compressed_data) {
fprintf(stderr, "Testing matching decompression:\n");

fprintf(stderr, " decompressing: ");
size_t decompressed_size;
uint8_t *decompressed_data = NULL;

Crunch64Error size_request_ok = decompress_bound(&decompressed_size, compressed_size, compressed_data);
if (size_request_ok != Crunch64Error_Okay)
{
if (size_request_ok != Crunch64Error_Okay) {
fprintf(stderr, " failed to request size for buffer. Reason: %s\n", get_crunch64_error_str(size_request_ok));
return false;
}

decompressed_data = malloc(decompressed_size * sizeof(uint8_t));
if (decompressed_data == NULL)
{
if (decompressed_data == NULL) {
fprintf(stderr, " malloc fail: 0x%zX bytes\n", decompressed_size * sizeof(uint8_t));
return false;
}

Crunch64Error decompress_ok = decompress(&decompressed_size, decompressed_data, compressed_size, compressed_data);
if (decompress_ok != Crunch64Error_Okay)
{
if (decompress_ok != Crunch64Error_Okay) {
fprintf(stderr, " failed to decompress data. Reason: %s\n", get_crunch64_error_str(decompress_ok));
free(decompressed_data);
return false;
Expand All @@ -151,8 +129,7 @@ bool test_matching_decompression(

fprintf(stderr, " validating data: ");
bool matches = compare_buffers(decompressed_size, decompressed_data, bin_size, bin);
if (!matches)
{
if (!matches) {
free(decompressed_data);
return false;
}
Expand All @@ -161,37 +138,28 @@ bool test_matching_decompression(
return true;
}

bool test_matching_compression(
compress_bound_fn compress_bound,
compress_fn compress,
size_t bin_size,
uint8_t *bin,
size_t compressed_size,
uint8_t *compressed_data)
{
bool test_matching_compression(compress_bound_fn compress_bound, compress_fn compress, size_t bin_size, uint8_t *bin,
size_t compressed_size, uint8_t *compressed_data) {
fprintf(stderr, "Testing matching compression:\n");
fprintf(stderr, " compressing: ");

size_t recompressed_size;
uint8_t *recompressed_data = NULL;

Crunch64Error size_request_ok = compress_bound(&recompressed_size, bin_size, bin);
if (size_request_ok != Crunch64Error_Okay)
{
if (size_request_ok != Crunch64Error_Okay) {
fprintf(stderr, " failed to request size for buffer. Reason: %s\n", get_crunch64_error_str(size_request_ok));
return false;
}

recompressed_data = malloc(recompressed_size * sizeof(uint8_t));
if (recompressed_data == NULL)
{
if (recompressed_data == NULL) {
fprintf(stderr, " malloc fail: 0x%zX bytes\n", recompressed_size * sizeof(uint8_t));
return false;
}

Crunch64Error compress_ok = compress(&recompressed_size, recompressed_data, bin_size, bin);
if (compress_ok != Crunch64Error_Okay)
{
if (compress_ok != Crunch64Error_Okay) {
fprintf(stderr, " failed to decompress data. Reason: %s\n", get_crunch64_error_str(compress_ok));
free(recompressed_data);
return false;
Expand All @@ -201,8 +169,7 @@ bool test_matching_compression(

fprintf(stderr, " validating data: ");
bool matches = compare_buffers(recompressed_size, recompressed_data, compressed_size, compressed_data);
if (!matches)
{
if (!matches) {
free(recompressed_data);
return false;
}
Expand All @@ -213,37 +180,30 @@ bool test_matching_compression(

int errors = 0;

void run_tests(
const char *name,
const char *file_extension,
compress_bound_fn compress_bound,
compress_fn compress,
compress_bound_fn decompress_bound,
compress_fn decompress)
{
void run_tests(const char *name, const char *file_extension, compress_bound_fn compress_bound, compress_fn compress,
compress_bound_fn decompress_bound, compress_fn decompress) {
struct dirent *entry;
DIR *dir = opendir("test_data");
if (!dir)
{
if (!dir) {
fprintf(stderr, "Could not open test_data directory\n");
errors++;
return;
}

fprintf(stderr, "Running tests for %s\n", name);
fprintf(stderr, "\n");
fprintf(stderr, "\n");

bool found_tests = false;
while ((entry = readdir(dir)) != NULL)
{
if (!has_suffix(entry->d_name, file_extension))
while ((entry = readdir(dir)) != NULL) {
if (!has_suffix(entry->d_name, file_extension)) {
continue;
}

found_tests = true;

char bin_path[512];
snprintf(bin_path, sizeof(bin_path), "test_data/%s", entry->d_name);
bin_path[strlen(bin_path) - strlen(file_extension)] = '\0'; // remove file extension
bin_path[strlen(bin_path) - strlen(file_extension)] = '\0'; // remove file extension

char compressed_path[512];
snprintf(compressed_path, sizeof(compressed_path), "test_data/%s", entry->d_name);
Expand All @@ -260,12 +220,10 @@ void run_tests(
assert(compressed_data_size > 0);
assert(compressed_data != NULL);

if (!test_matching_decompression(decompress_bound, decompress, bin_size, bin, compressed_data_size, compressed_data))
{
if (!test_matching_decompression(decompress_bound, decompress, bin_size, bin, compressed_data_size, compressed_data)) {
errors++;
}
if (!test_matching_compression(compress_bound, compress, bin_size, bin, compressed_data_size, compressed_data))
{
if (!test_matching_compression(compress_bound, compress, bin_size, bin, compressed_data_size, compressed_data)) {
errors++;
}

Expand All @@ -275,27 +233,22 @@ void run_tests(
free(compressed_data);
}

if (!found_tests)
{
if (!found_tests) {
fprintf(stderr, "No test files found for %s\n", name);
errors++;
return;
}
}

int main(void)
{
int main(void) {
run_tests("yay0", ".Yay0", crunch64_yay0_compress_bound, crunch64_yay0_compress, crunch64_yay0_decompress_bound, crunch64_yay0_decompress);
run_tests("yaz0", ".Yaz0", crunch64_yaz0_compress_bound, crunch64_yaz0_compress, crunch64_yaz0_decompress_bound, crunch64_yaz0_decompress);
run_tests("mio0", ".MIO0", crunch64_mio0_compress_bound, crunch64_mio0_compress, crunch64_mio0_decompress_bound, crunch64_mio0_decompress);

if (errors == 0)
{
if (errors == 0) {
fprintf(stderr, "All tests passed\n");
return 0;
}
else
{
} else {
fprintf(stderr, "%d tests failed\n", errors);
return 1;
}
Expand Down

0 comments on commit d0b3c91

Please sign in to comment.