diff options
Diffstat (limited to 'include')
| -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 |
3 files changed, 116 insertions, 54 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 |
