style: update code style

This commit is contained in:
thetek 2023-04-25 12:40:14 +02:00
parent 389e5f1d69
commit 2cc4fcc83b
8 changed files with 89 additions and 53 deletions

View File

@ -91,8 +91,7 @@ strcount (const char str[static 1], char c)
void
strdowncase (char str[static 1])
{
while (*str)
{
while (*str) {
if (isupper (*str))
*str += 0x20;
str++;
@ -107,8 +106,7 @@ strdowncase (char str[static 1])
void
strupcase (char str[static 1])
{
while (*str)
{
while (*str) {
if (islower (*str))
*str -= 0x20;
str++;

View File

@ -24,10 +24,17 @@ debug_malloc (size_t size, const char *file, size_t line)
void *ptr;
ptr = malloc (size);
if (ptr == NULL)
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu \x1b[31mreturned null\x1b[0m\n", size);
else
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu to ptr %p\n", size, ptr);
if (ptr == NULL) {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"malloc of size %zu \x1b[31mreturned null\x1b[0m\n", size
);
} else {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"malloc of size %zu to ptr %p\n", size, ptr
);
}
return ptr;
}
@ -55,12 +62,24 @@ debug_realloc (void *ptr, size_t size, const char *file, size_t line)
old_ptr = (uintptr_t) ptr;
new_ptr = realloc (ptr, size);
if (new_ptr == NULL && size != 0)
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %#" PRIxPTR " to size %zu \x1b[31mreturned null\x1b[0m\n", old_ptr, size);
else if (new_ptr == ptr)
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %p to size %zu\n", new_ptr, size);
else
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %#" PRIxPTR " to new ptr %p with size %zu\n", old_ptr, new_ptr, size);
if (new_ptr == NULL && size != 0) {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"realloc of ptr %#" PRIxPTR " to size %zu \x1b[31mreturned null\x1b[0m\n",
old_ptr, size
);
} else if (new_ptr == ptr) {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"realloc of ptr %p to size %zu\n", new_ptr, size
);
} else {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"realloc of ptr %#" PRIxPTR " to new ptr %p with size %zu\n",
old_ptr, new_ptr, size
);
}
return new_ptr;
}
@ -101,7 +120,8 @@ debug_smalloc (size_t size, const char *file, size_t line)
void *ptr;
ptr = smalloc (size);
log_print_fl (LOG_LEVEL_DEBUG, file, line, "smalloc of size %zu to ptr %p\n", size, ptr);
log_print_fl (LOG_LEVEL_DEBUG, file, line,
"smalloc of size %zu to ptr %p\n", size, ptr);
return ptr;
}
@ -128,10 +148,19 @@ debug_srealloc (void *ptr, size_t size, const char *file, size_t line)
old_ptr = (uintptr_t) ptr;
new_ptr = srealloc (ptr, size);
if (new_ptr == ptr)
log_print_fl (LOG_LEVEL_DEBUG, file, line, "srealloc of ptr %p to size %zu\n", new_ptr, size);
else
log_print_fl (LOG_LEVEL_DEBUG, file, line, "srealloc of ptr %#" PRIxPTR " to new ptr %p with size %zu\n", old_ptr, new_ptr, size);
if (new_ptr == ptr) {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"srealloc of ptr %p to size %zu\n",
new_ptr, size
);
} else {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"srealloc of ptr %#" PRIxPTR " to new ptr %p with size %zu\n",
old_ptr, new_ptr, size
);
}
return new_ptr;
}

View File

@ -17,8 +17,7 @@
#define log_fatal(...) do { log_print (LOG_LEVEL_ERROR, __VA_ARGS__); exit (EXIT_FAILURE); } while (0) /* print a log message with level error and exit */
#define log_fatal_fl(...) do { log_print_fl (LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__) exit (EXIT_FAILURE); } while (0) /* print a log message with the current file and line and log level error and exit */
typedef enum
{
typedef enum {
LOG_LEVEL_ERROR,
LOG_LEVEL_WARNING,
LOG_LEVEL_INFO,

View File

@ -6,8 +6,7 @@
#include <sys/types.h>
/* represents a string, where length and capacity are stored alongside the string data. */
typedef struct
{
typedef struct {
char *str; /* string data */
size_t len; /* string length */
size_t cap; /* capacity (allocated size) */

View File

@ -8,8 +8,7 @@
#define test_assert(a) ((bool) (a)), (#a) /* provides the `success` and `assertion` parameters for `test_add()` / `test_group_add()` */
/* results of a test group or suite */
typedef struct
{
typedef struct {
size_t success; /* number of successful tests */
size_t failure; /* number of failed tests */
} test_results_t;
@ -18,16 +17,14 @@ typedef struct
typedef test_results_t (*test_entry_func_t) (void);
/* test suite. a suite contains multiple test groups. */
typedef struct
{
typedef struct {
test_entry_func_t *entries; /* test groups as executable function pointers */
size_t entries_len; /* number of functions */
size_t entries_cap; /* allocated space for functions */
} test_suite_t;
/* test group. contains multiple tests. */
typedef struct
{
typedef struct {
test_results_t results;
} test_group_t;

View File

@ -6,23 +6,27 @@
static void
_log_print_type (log_level_t type)
{
switch (type)
{
case LOG_LEVEL_ERROR:
switch (type) {
case LOG_LEVEL_ERROR: {
printf ("[\x1b[31merror\x1b[0m] ");
break;
case LOG_LEVEL_WARNING:
}
case LOG_LEVEL_WARNING: {
printf ("[\x1b[33mwarn\x1b[0m] ");
break;
case LOG_LEVEL_INFO:
}
case LOG_LEVEL_INFO: {
printf ("[\x1b[34minfo\x1b[0m] ");
break;
case LOG_LEVEL_OK:
}
case LOG_LEVEL_OK: {
printf ("[\x1b[32mok\x1b[0m] ");
break;
case LOG_LEVEL_DEBUG:
}
case LOG_LEVEL_DEBUG: {
printf ("[\x1b[35mdebug\x1b[0m] ");
break;
}
}
}
@ -65,7 +69,8 @@ log_print (log_level_t type, const char *restrict fmt, ...)
*/
__attribute__ ((format (printf, 4, 5)))
void
log_print_fl (log_level_t type, const char *file, size_t line, const char *restrict fmt, ...)
log_print_fl (log_level_t type, const char *file, size_t line,
const char *restrict fmt, ...)
{
va_list ap;

View File

@ -318,8 +318,7 @@ void
str_resize (str_t str[static 1], size_t cap)
{
cap = max (next_pow_of_two (cap), STR_MIN_ALLOC);
if (cap > str->len && cap != str->cap)
{
if (cap > str->len && cap != str->cap) {
str->str = srealloc (str->str, cap * sizeof (char));
str->cap = cap;
}

View File

@ -45,10 +45,10 @@ test_suite_free (test_suite_t suite[static 1])
void
test_suite_add (test_suite_t suite[static 1], test_entry_func_t func)
{
if (suite->entries_len == suite->entries_cap - 1)
{
if (suite->entries_len == suite->entries_cap - 1) {
suite->entries_cap += TEST_GROUP_ENTRIES_ALLOC;
suite->entries = realloc (suite->entries, (sizeof *(suite->entries)) * suite->entries_cap);
suite->entries = realloc (suite->entries, (sizeof *(suite->entries)) *
suite->entries_cap);
}
suite->entries[suite->entries_len] = func;
suite->entries_len += 1;
@ -71,17 +71,25 @@ test_suite_run (test_suite_t suite[static 1])
total_result = (test_results_t) {0};
for (i = 0; i < suite->entries_len; i++)
{
for (i = 0; i < suite->entries_len; i++) {
result = suite->entries[i]();
total_result.success += result.success;
total_result.failure += result.failure;
}
if (total_result.failure == 0)
log_ok ("all tests successful (%zu tests in %zu groups)\n", total_result.success, suite->entries_len);
else
log_error ("tests unsuccessful (%zu tests in %zu groups, out of which %zu successful and %zu failures)\n", total_result.success + total_result.failure, suite->entries_len, total_result.success, total_result.failure);
if (total_result.failure == 0) {
log_ok (
"all tests successful (%zu tests in %zu groups)\n",
total_result.success, suite->entries_len
);
} else {
log_error (
"tests unsuccessful (%zu tests in %zu groups, out of which %zu "
"successful and %zu failures)\n",
total_result.success + total_result.failure,
suite->entries_len, total_result.success, total_result.failure
);
}
return total_result;
}
@ -113,14 +121,16 @@ test_group_new (void)
* `assertion` parameters.
*/
void
test_group_add (test_group_t group[static 1], bool success, const char *assertion, const char *name, const char *file, size_t line)
test_group_add (test_group_t group[static 1], bool success,
const char *assertion, const char *name, const char *file,
size_t line)
{
if (success)
if (success) {
group->results.success += 1;
else
{
} else {
group->results.failure += 1;
log_print_fl (LOG_LEVEL_ERROR, file, line, "test '%s' (assertion '%s') failed\n", name, assertion);
log_print_fl (LOG_LEVEL_ERROR, file, line,
"test '%s' (assertion '%s') failed\n", name, assertion);
}
}