diff options
| -rw-r--r-- | include/moslib/ds/array.h | 102 | ||||
| -rw-r--r-- | include/moslib/mem/alloc.h | 18 | ||||
| -rw-r--r-- | include/moslib/tester.h | 50 | ||||
| -rw-r--r-- | src/mem/alloc.c | 19 | ||||
| -rw-r--r-- | src/tester.c | 49 | ||||
| -rw-r--r-- | tests/ds/array_tests.c | 2 | ||||
| -rw-r--r-- | tests/mem/alloc_tests.c | 2 |
7 files changed, 153 insertions, 89 deletions
diff --git a/include/moslib/ds/array.h b/include/moslib/ds/array.h index 7b553d7..68f6deb 100644 --- a/include/moslib/ds/array.h +++ b/include/moslib/ds/array.h @@ -72,7 +72,7 @@ // Returns index of the first item of array2 in array1. // // ---------------------------------------------------------------------------------------------------- -// +// // To delete item from an array at specific index and retrieve it you can use: // // T item = arr_pop_i(array, i); @@ -89,7 +89,7 @@ // It also has an n variant: // // size_t deleted = arr_del_n(array, i, n); -// +// // It works the same way but deleted n consecutive items instead of 1. // // You can also remove from the front or the back: @@ -114,11 +114,11 @@ // To get array's current length use: // // size_t length = arr_len(array); -// +// // You can also set length: // // size_t deleted = arr_set_len(array, new_len); -// +// // If it's bigger than current length - it will zero new items. // If it's smaller than current length - it will delete items. // Returns amount of items deleted in this way. @@ -128,11 +128,11 @@ // To get array's current capacity use: // // size_t cap = arr_cap(array); -// +// // You can also set capacity: // // size_t new_cap = arr_set_cap(array, target_cap); -// +// // When target_cap is lower than array length it will set capacity to array length. // Returns the new capacity. // @@ -145,33 +145,69 @@ // ---------------------------------------------------------------------------------------------------- // ===================================== END OF QUICK USAGE GUIDE ===================================== -#include <moslib/mem/alloc.h> +#include "moslib/mem/alloc.h" -#define ARR_INIT_CAP 16 -#define arr_get(arr, i) ((arr) ? arr : NULL) -#define arr_first(arr) NULL -#define arr_last(arr) NULL +#ifndef MOSLIB_DS_ARRAY_H +#define MOSLIB_DS_ARRAY_H + +#ifndef MOSLIB_FORCE_PREFIXES + +#define ARR_INIT_CAP MOS_ARR_INIT_CAP +#define arr_get mos_arr_get +#define arr_first mos_arr_first +#define arr_last mos_arr_last +#define arr_append mos_arr_append +#define arr_push mos_arr_push +#define arr_insert mos_arr_insert +#define arr_put mos_arr_put +#define arr_append_n mos_arr_append_n +#define arr_push_n mos_arr_push_n +#define arr_insert_n mos_arr_insert_n +#define arr_put_n mos_arr_put_n +#define arr_concat mos_arr_concat +#define arr_pop_i mos_arr_pop_i +#define arr_pop mos_arr_pop +#define arr_del mos_arr_del +#define arr_del_n mos_arr_del_n +#define arr_del_front mos_arr_del_front +#define arr_del_back mos_arr_del_back +#define arr_prev mos_arr_prev +#define arr_next mos_arr_next +#define arr_len mos_arr_len +#define arr_set_len mos_arr_set_len +#define arr_cap mos_arr_cap +#define arr_set_cap mos_arr_set_cap +#define arr_free mos_arr_free + +#endif // MOSLIB_FORCE_PREFIXES + +#define MOS_ARR_INIT_CAP 16 +#define mos_arr_get(arr, i) ((arr) ? arr : NULL) +#define mos_arr_first(arr) NULL +#define mos_arr_last(arr) NULL // ... - item // variadic to make something like (struct T){1,1} work properly -#define arr_append(arr, ...) (!(arr) ? (arr) = safe_malloc(sizeof(*(arr))) : 0, *(arr) = (__VA_ARGS__)) -#define arr_push(arr, ...) 0 -#define arr_insert(arr, i, ...) 0 -#define arr_put(arr, i, ...) 0 -#define arr_append_n(arr, items, n) (!(arr) ? (arr) = safe_malloc(sizeof(*(arr))) : 0, *(arr) = (items)[0]) -#define arr_push_n(arr, items, n) 0 -#define arr_insert_n(arr, i, items, n) 0 -#define arr_put_n(arr, i, items, n) 0 -#define arr_concat(arr1, arr2) 0 -#define arr_pop_i(arr, i) (arr[0]) -#define arr_pop(arr) (arr[0]) -#define arr_del(arr, i) 0 -#define arr_del_n(arr, i, n) 0 -#define arr_del_front(arr, n) 0 -#define arr_del_back(arr, n) 0 -#define arr_prev(arr, p_item) NULL -#define arr_next(arr, p_item) NULL -#define arr_len(arr) 0 -#define arr_set_len(array, len) 0 -#define arr_cap(arr) 0 -#define arr_set_cap(arr, cap) 0 -#define arr_free(arr) void +#define mos_arr_append(arr, ...) (!(arr) ? (arr) = mos_safe_malloc(sizeof(*(arr))) : 0, *(arr) = (__VA_ARGS__)) +#define mos_arr_push(arr, ...) 0 +#define mos_arr_insert(arr, i, ...) 0 +#define mos_arr_put(arr, i, ...) 0 +#define mos_arr_append_n(arr, items, n) (!(arr) ? (arr) = mos_safe_malloc(sizeof(*(arr))) : 0, *(arr) = (items)[0]) +#define mos_arr_push_n(arr, items, n) 0 +#define mos_arr_insert_n(arr, i, items, n) 0 +#define mos_arr_put_n(arr, i, items, n) 0 +#define mos_arr_concat(arr1, arr2) 0 +#define mos_arr_pop_i(arr, i) (arr[0]) +#define mos_arr_pop(arr) (arr[0]) +#define mos_arr_del(arr, i) 0 +#define mos_arr_del_n(arr, i, n) 0 +#define mos_arr_del_front(arr, n) 0 +#define mos_arr_del_back(arr, n) 0 +#define mos_arr_prev(arr, p_item) NULL +#define mos_arr_next(arr, p_item) NULL +#define mos_arr_len(arr) 0 +#define mos_arr_set_len(array, len) 0 +#define mos_arr_cap(arr) 0 +#define mos_arr_set_cap(arr, cap) 0 +#define mos_arr_free(arr) void + +#endif // MOSLIB_DS_ARRAY_H diff --git a/include/moslib/mem/alloc.h b/include/moslib/mem/alloc.h index b3f4885..eb557e5 100644 --- a/include/moslib/mem/alloc.h +++ b/include/moslib/mem/alloc.h @@ -6,7 +6,15 @@ #ifndef MOSLIB_MEM_ALLOC_H #define MOSLIB_MEM_ALLOC_H -#define MOSLIB_MAX_ALLOC_ATTEMPTS 3 +#ifndef MOSLIB_FORCE_PREFIXES + +#define MAX_ALLOC_ATTEMPTS MOS_MAX_ALLOC_ATTEMPTS +#define safe_malloc mos_safe_malloc +#define safe_realloc mos_safe_realloc + +#endif // MOSLIB_FORCE_PREFIXES + +#define MOS_MAX_ALLOC_ATTEMPTS 3 // Tries to malloc MOSLIB_MAX_ALLOC_ATTEMPTS times // If all attempts fail - exits with EXIT_FAILURE code @@ -17,7 +25,7 @@ // // Returns: // pointer to the allocated memory -#define safe_malloc(size) safe_malloc_fn(size, __FILE__, __LINE__, __func__) +#define mos_safe_malloc(size) mos_safe_malloc_fn(size, __FILE__, __LINE__, __func__) // Tries to realloc MOSLIB_MAX_ALLOC_ATTEMPTS times // If all attempts fail - exits with EXIT_FAILURE code @@ -31,10 +39,10 @@ // // Returns: // pointer to the reallocated memory -#define safe_realloc(ptr, size) safe_realloc_fn(ptr, size, __FILE__, __LINE__, __func__) +#define mos_safe_realloc(ptr, size) mos_safe_realloc_fn(ptr, size, __FILE__, __LINE__, __func__) // Function prototypes for macros -extern void *safe_malloc_fn(size_t size, const char *file, int line, const char *func); -extern void *safe_realloc_fn(void *ptr, size_t size, const char *file, int line, const char *func); +extern void *mos_safe_malloc_fn(size_t size, const char *file, int line, const char *func); +extern void *mos_safe_realloc_fn(void *ptr, size_t size, const char *file, int line, const char *func); #endif // MOSLIB_MEM_ALLOC_H diff --git a/include/moslib/tester.h b/include/moslib/tester.h index b48a03a..aa0adb2 100644 --- a/include/moslib/tester.h +++ b/include/moslib/tester.h @@ -83,17 +83,35 @@ #ifndef MOSLIB_TESTER_H #define MOSLIB_TESTER_H -typedef void Test; // this is just for nice indication that a function is a test -typedef struct Tester Tester; -typedef struct TestGroup TestGroup; -typedef Test (*TestFn)(void); +#ifndef MOSLIB_FORCE_PREFIXES + +#define Test MosTest +#define Tester MosTester +#define TestGroup MosTestGroup +#define TestFn MosTestFn +#define new_tester mos_new_tester +#define add_test_group mos_add_test_group +#define test_assert mos_test_assert +#define test_assert_exit mos_test_assert_exit +#define suppress_output mos_suppress_output +#define unsuppress_outputs mos_unsuppress_outputs +#define add_test mos_add_test +#define run_tests mos_run_tests +#define free_tester mos_free_tester + +#endif // MOSLIB_FORCE_PREFIXES + +typedef void MosTest; // this is just for nice indication that a function is a test +typedef struct MosTester MosTester; +typedef struct MosTestGroup MosTestGroup; +typedef MosTest (*MosTestFn)(void); // Initalize new tester // You can skip this and just define it as NULL - it will initalize automatically // // Returns: // pointer to the new tester -extern Tester *new_tester(void); +extern MosTester *mos_new_tester(void); // Add new test group to the tester // @@ -106,7 +124,7 @@ extern Tester *new_tester(void); // // Returns: // pointer to the added group -#define add_test_group(tester, group_name) ((!(tester) ? (tester) = new_tester() : 0), add_test_group_fn((tester), (group_name))) +#define mos_add_test_group(tester, group_name) ((!(tester) ? (tester) = mos_new_tester() : 0), mos_add_test_group_fn((tester), (group_name))) // Assert that an expression is true // Use in test functions to test correctness @@ -118,7 +136,7 @@ extern Tester *new_tester(void); // // fail_message // message to display when test fails -extern void test_assert(int expression, const char *fail_message); +extern void mos_test_assert(int expression, const char *fail_message); // Assert that function exits with given exit code // @@ -128,18 +146,18 @@ extern void test_assert(int expression, const char *fail_message); // // code // expected exit code -#define test_assert_exit(function, code) test_assert_exit_fn((function), (code), #function); +#define mos_test_assert_exit(function, code) mos_test_assert_exit_fn((function), (code), #function); // Suppress given output // // Arguments: // out // pointer to outpout to suppress -extern void suppress_output(FILE **out); +extern void mos_suppress_output(FILE **out); // Unuppress previously suppressed outputs // Will unsupress all of the outputs that were suppressed -extern void unsuppress_outputs(void); +extern void mos_unsuppress_outputs(void); // Add test to a test group // @@ -149,25 +167,25 @@ extern void unsuppress_outputs(void); // // test // test to add to the group -#define add_test(group, test) add_test_fn((group), (test), #test); +#define mos_add_test(group, test) mos_add_test_fn((group), (test), #test); // Run defined tests // // Arguments: // tester // tester with tests to run -extern void run_tests(Tester *tester); +extern void mos_run_tests(MosTester *tester); // Free the tester // // Arguments: // tester // tester to free -extern void free_tester(Tester *tester); +extern void mos_free_tester(MosTester *tester); // Function prototypes for macros -extern TestGroup *add_test_group_fn(Tester *tester, const char *group_name); -extern void test_assert_exit_fn(TestFn function, int code, const char *name); -extern void add_test_fn(TestGroup *group, TestFn test, const char *name); +extern MosTestGroup *mos_add_test_group_fn(MosTester *tester, const char *group_name); +extern void mos_test_assert_exit_fn(MosTestFn function, int code, const char *name); +extern void mos_add_test_fn(MosTestGroup *group, MosTestFn test, const char *name); #endif // MOSLIB_TESTER_H diff --git a/src/mem/alloc.c b/src/mem/alloc.c index 7acea02..7a85d2d 100644 --- a/src/mem/alloc.c +++ b/src/mem/alloc.c @@ -1,3 +1,4 @@ +#define MOSLIB_FORCE_PREFIXES #include "moslib/mem/alloc.h" #include <stdio.h> @@ -6,39 +7,39 @@ #define ERR_MESS(fl, ln, fn, msg) "%s:%d - error in function %s:\n%s\n", fl, ln, fn, msg -void *safe_malloc_fn(size_t size, const char *file, int line, const char *func) { - for (size_t i = 0; i < MOSLIB_MAX_ALLOC_ATTEMPTS; i++) { +void *mos_safe_malloc_fn(size_t size, const char *file, int line, const char *func) { + for (size_t i = 0; i < MOS_MAX_ALLOC_ATTEMPTS; i++) { void *p = malloc(size); if (p) return p; - if (i < MOSLIB_MAX_ALLOC_ATTEMPTS - 1) { + if (i < MOS_MAX_ALLOC_ATTEMPTS - 1) { fprintf(stderr, ERR_MESS(file, line, func, "malloc failed - retrying in 1s...")); sleep(1); } } char errmes[64]; - sprintf(errmes, "FATAL: malloc failed %d times - exiting...", MOSLIB_MAX_ALLOC_ATTEMPTS); + sprintf(errmes, "FATAL: malloc failed %d times - exiting...", MOS_MAX_ALLOC_ATTEMPTS); fprintf(stderr, ERR_MESS(file, line, func, errmes)); exit(EXIT_FAILURE); } -void *safe_realloc_fn(void *ptr, size_t size, const char *file, int line, const char *func) { +void *mos_safe_realloc_fn(void *ptr, size_t size, const char *file, int line, const char *func) { if (!ptr) - return safe_malloc_fn(size, file, line, func); + return mos_safe_malloc_fn(size, file, line, func); - for (size_t i = 0; i < MOSLIB_MAX_ALLOC_ATTEMPTS; i++) { + for (size_t i = 0; i < MOS_MAX_ALLOC_ATTEMPTS; i++) { void *p = realloc(ptr, size); if (p) return p; - if (i < MOSLIB_MAX_ALLOC_ATTEMPTS - 1) { + if (i < MOS_MAX_ALLOC_ATTEMPTS - 1) { fprintf(stderr, ERR_MESS(file, line, func, "realloc failed - retrying in 1s...")); sleep(1); } } char errmes[64]; - sprintf(errmes, "FATAL: realloc failed %d times - exiting...", MOSLIB_MAX_ALLOC_ATTEMPTS); + sprintf(errmes, "FATAL: realloc failed %d times - exiting...", MOS_MAX_ALLOC_ATTEMPTS); fprintf(stderr, ERR_MESS(file, line, func, errmes)); exit(EXIT_FAILURE); } diff --git a/src/tester.c b/src/tester.c index afabc22..43efa07 100644 --- a/src/tester.c +++ b/src/tester.c @@ -1,3 +1,4 @@ +#define MOSLIB_FORCE_PREFIXES #include "moslib/tester.h" #include <setjmp.h> @@ -53,50 +54,50 @@ void print_center(const char *text, int color, char padding_char) { } typedef struct { - TestFn test; + MosTestFn test; const char *name; } TestName; -struct Tester { - TestGroup *groups; +struct MosTester { + MosTestGroup *groups; size_t n_groups; size_t group_cap; }; -struct TestGroup { +struct MosTestGroup { const char *name; TestName *tests; size_t n_tests; size_t test_cap; }; -Tester *new_tester(void) { - Tester *tester = safe_malloc(sizeof(Tester)); - *tester = (Tester){ - .groups = safe_malloc(INIT_GROUP_CAP * sizeof(TestGroup)), +MosTester *mos_new_tester(void) { + MosTester *tester = mos_safe_malloc(sizeof(MosTester)); + *tester = (MosTester){ + .groups = mos_safe_malloc(INIT_GROUP_CAP * sizeof(MosTestGroup)), .n_groups = 0, .group_cap = INIT_GROUP_CAP, }; return tester; } -TestGroup *add_test_group_fn(Tester *tester, const char *group_name) { +MosTestGroup *mos_add_test_group_fn(MosTester *tester, const char *group_name) { if (tester->n_groups >= tester->group_cap) { tester->group_cap *= 2; - tester->groups = safe_realloc(tester->groups, tester->group_cap * sizeof(TestGroup)); + tester->groups = mos_safe_realloc(tester->groups, tester->group_cap * sizeof(MosTestGroup)); } - TestGroup *group = tester->groups + tester->n_groups++; - *group = (TestGroup){ + MosTestGroup *group = tester->groups + tester->n_groups++; + *group = (MosTestGroup){ .name = group_name, - .tests = safe_malloc(INIT_TEST_CAP * sizeof(TestName)), + .tests = mos_safe_malloc(INIT_TEST_CAP * sizeof(TestName)), .n_tests = 0, .test_cap = INIT_TEST_CAP, }; return group; } -void test_assert(int expression, const char *fail_message) { +void mos_test_assert(int expression, const char *fail_message) { if (!expression) { failed = 1; print_color("ASSERT FAILED: ", RED); @@ -116,7 +117,7 @@ void exit(int code) { longjmp(jump_env, 1); } -void test_assert_exit_fn(TestFn function, int code, const char *name) { +void mos_test_assert_exit_fn(MosTestFn function, int code, const char *name) { expected_code = code; fn_name = name; if (setjmp(jump_env) == 0) { @@ -127,12 +128,12 @@ void test_assert_exit_fn(TestFn function, int code, const char *name) { } } -void suppress_output(FILE **out) { +void mos_suppress_output(FILE **out) { if (!suppressed.outs || suppressed.n_outs >= suppressed.out_cap) { while (suppressed.n_outs >= suppressed.out_cap) suppressed.out_cap *= 2; - suppressed.outs = safe_realloc(suppressed.outs, suppressed.out_cap); - suppressed.ogs = safe_realloc(suppressed.ogs, suppressed.out_cap); + suppressed.outs = mos_safe_realloc(suppressed.outs, suppressed.out_cap); + suppressed.ogs = mos_safe_realloc(suppressed.ogs, suppressed.out_cap); } suppressed.outs[suppressed.n_outs] = out; suppressed.ogs[suppressed.n_outs++] = *out; @@ -146,7 +147,7 @@ void suppress_output(FILE **out) { *out = dev_null; } -void unsuppress_outputs(void) { +void mos_unsuppress_outputs(void) { if (!suppressed.outs || !suppressed.ogs) return; @@ -156,16 +157,16 @@ void unsuppress_outputs(void) { suppressed.n_outs = 0; } -void add_test_fn(TestGroup *group, TestFn test, const char *name) { +void mos_add_test_fn(MosTestGroup *group, MosTestFn test, const char *name) { if (group->n_tests >= group->test_cap) { group->test_cap *= 2; - group->tests = safe_realloc(group->tests, group->test_cap * sizeof(TestName)); + group->tests = mos_safe_realloc(group->tests, group->test_cap * sizeof(TestName)); } group->tests[group->n_tests++] = (TestName){.test = test, .name = name}; } -void run_tests(Tester *tester) { +void mos_run_tests(MosTester *tester) { if (!tester) return; @@ -177,7 +178,7 @@ void run_tests(Tester *tester) { for (size_t i = 0; i < tester->n_groups; i++) { nl; - TestGroup *group = tester->groups + i; + MosTestGroup *group = tester->groups + i; size_t tests_passed = 0; size_t tests_failed = 0; @@ -221,7 +222,7 @@ void run_tests(Tester *tester) { print_center("END", WHITE, '='); } -void free_tester(Tester *tester) { +void mos_free_tester(MosTester *tester) { if (tester) { for (size_t i = 0; i < tester->n_groups; i++) free(tester->groups[i].tests); diff --git a/tests/ds/array_tests.c b/tests/ds/array_tests.c index a9d4ec7..0cefb78 100644 --- a/tests/ds/array_tests.c +++ b/tests/ds/array_tests.c @@ -1,6 +1,6 @@ #include "array_tests.h" +#include "moslib/ds/array.h" -#include <moslib/ds/array.h> #include <string.h> Test cap_test() { diff --git a/tests/mem/alloc_tests.c b/tests/mem/alloc_tests.c index 17973c7..41feb3e 100644 --- a/tests/mem/alloc_tests.c +++ b/tests/mem/alloc_tests.c @@ -1,7 +1,7 @@ #include "alloc_tests.h" +#include "moslib/mem/alloc.h" #include <limits.h> -#include <moslib/mem/alloc.h> #include <stdlib.h> Test malloc_exit() { |
