diff options
author | justanothercatgirl <sotov2070@gmail.com> | 2024-05-19 11:19:51 +0300 |
---|---|---|
committer | justanothercatgirl <sotov2070@gmail.com> | 2024-05-19 11:19:51 +0300 |
commit | fdaa318a775cbb2ab0377bb76239c10ced573f62 (patch) | |
tree | b3d70dc4d363b9e7f919d11e2d591e2778d3b78f | |
parent | a5f0d6ae9ace560b67c114af9071a268d8df2dc9 (diff) |
tobeamended
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/dynarray.h | 2 | ||||
-rw-r--r-- | include/utility.h | 15 | ||||
-rw-r--r-- | tests/dynarray.c | 31 |
4 files changed, 40 insertions, 10 deletions
@@ -1,6 +1,6 @@ CC := gcc -CFLAGS := -g -Wall -Wextra -Wpedantic -Werror +CFLAGS := -g -Wall -Wextra -Wpedantic -Werror -Wno-language-extension-token tests: dynarray diff --git a/include/dynarray.h b/include/dynarray.h index bdb9db9..79b548e 100644 --- a/include/dynarray.h +++ b/include/dynarray.h @@ -63,7 +63,7 @@ /// set capacity to minimum possible value #define array_shrink(array) array = _memshrink_array(array) /// bound-checks and returns a pointer to that element. on error returns NULL -#define array_at(array, idx) (idx < array_size(array) ? array[idx] : NULL) +#define array_at(array, idx) (idx < array_size(array) ? (typeof(array))((byte*)array + idx*array_size(array)) : NULL) /// sorts the array using compare_func for comparison #define array_qsort(array, compare_func) qsort(array, array_size(array), array_element_size(array), compare_func) /// sorts the array using pre-defined compariton functions for signed integers based on size (1, 2, 4, 8) diff --git a/include/utility.h b/include/utility.h index 4f264f0..e3e4639 100644 --- a/include/utility.h +++ b/include/utility.h @@ -11,7 +11,7 @@ typedef unsigned char byte; typedef int(*qsort_cmp_t)(const void*, const void*); #define get_qsort_cmp(type) __qsort_cmps[sizeof(type)] -extern const qsort_cmp_t __qsort_cmps[]; +extern const qsort_cmp_t __qsort_cmps[64]; #ifdef UTILITY_EXPOSE_HELPERS #define _UTILITY_STATIC @@ -58,17 +58,26 @@ _UTILITY_STATIC int __default_long_cmp(const void* a, const void* b) { return 0; } +#ifdef __GNUC__ // cope #pragma GCC diagnostic ignored "-Woverride-init" // Is is meant to override it on different platforms -const qsort_cmp_t __qsort_cmps[] = { +const qsort_cmp_t __qsort_cmps[64] = { [sizeof(char)] = __default_char_cmp, [sizeof(short)] = __default_short_cmp, [sizeof(int)] = __default_int_cmp, [sizeof(long)] = __default_long_cmp, [sizeof(long long)] = __default_long_long_cmp, - [64] = 0, + [63] = 0, }; #pragma GCC diagnostic warning "-Woverride-init" +#else // not __GNUC__ +const qsort_cmp_t __qsort_cmps[64] = { + 0, + __default_char_cmp, __default_short_cmp, 0, __default_int_cmp, + 0, 0, 0, __default_long_long_cmp, +}; +#endif // __GNUC__ + #endif // UTILITY_IMPLEMENTATION diff --git a/tests/dynarray.c b/tests/dynarray.c index e050d07..d7a79d7 100644 --- a/tests/dynarray.c +++ b/tests/dynarray.c @@ -39,7 +39,7 @@ void print_arr(TEST_TYPE *a, int line, const char* message) ); } -int main() +int main(void) { TEST_TYPE *arr = array_new(TEST_TYPE, 10); // size: 10, cap: 10 for (size_t i = 0; i < array_size(arr); ++i) { arr[i] = 127 - 2 * i; } @@ -67,16 +67,37 @@ int main() TEST_TYPE test_arr_data[] = {0, 42, 0, 20, 0, 69, 0, 123, 0, 125, 0, 127, 0, }; TEST_TYPE* test_arr = array_new_buffer_copy(TEST_TYPE, test_arr_data, sizeof(test_arr_data) / sizeof(test_arr_data[0])); - typeof(EXIT_SUCCESS) exit_status; if (array_compare(test_arr, arr, __qsort_cmps[sizeof(TEST_TYPE)])) { puts("\x1b[31mTest for \"" stringify(TEST_TYPE)"\" failed\x1b[0m"); - exit_status = EXIT_FAILURE; + goto exit_fail; } else { puts("\x1b[32mTest for \"" stringify(TEST_TYPE)"\" passed\x1b[0m"); - exit_status = EXIT_SUCCESS; + } + TEST_TYPE* elem; + if ((elem = array_at(arr, 2)) != NULL) { + if (*elem == 42) { + puts("array_at(1) = 42"); + } + else { + printf("%s@%i: array_at failed\n", __FILE__, __LINE__); + goto exit_fail; + } + } else { + printf("%s@%i: array_at failed\n", __FILE__, __LINE__); + goto exit_fail; + + } + if (( elem = array_at(arr, 69420)) != NULL) { + printf("%s@%i: array_at failed\n", __FILE__, __LINE__); + goto exit_fail; } +//exit: + array_free(arr); + array_free(test_arr); + exit(EXIT_SUCCESS); +exit_fail: array_free(arr); array_free(test_arr); - exit(exit_status); + exit(EXIT_FAILURE); } |