diff options
author | sanine <sanine.not@pm.me> | 2022-11-27 20:03:27 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-11-27 20:03:27 -0600 |
commit | 6ca18697f9eb332c38f0b46b823089ce80347580 (patch) | |
tree | 267d074f74ebfa54f1c90fd3361e6b2dbf793613 /src | |
parent | 21e8b8ad389dda55d92af4af1fd22aac657074b4 (diff) |
add dynamic array functions
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/node.c | 5 | ||||
-rw-r--r-- | src/node.h | 9 | ||||
-rw-r--r-- | src/node.test.c | 8 | ||||
-rw-r--r-- | src/test/test.h | 4 | ||||
-rw-r--r-- | src/util.c | 39 | ||||
-rw-r--r-- | src/util.h | 10 | ||||
-rw-r--r-- | src/util.test.c | 57 |
8 files changed, 140 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a9067ca..a82103b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,11 @@ project(kalmia) -target_sources(kalmia PUBLIC transform.c) +target_sources( + kalmia PUBLIC + transform.c + node.c + util.c +) target_include_directories(kalmia PUBLIC ezxml) target_link_libraries(kalmia ezxml) @@ -13,5 +18,7 @@ if (KALMIA_BUILD_TESTS) test/test.c transform.test.c + node.test.c + util.test.c ) endif() diff --git a/src/node.c b/src/node.c new file mode 100644 index 0000000..1ba862d --- /dev/null +++ b/src/node.c @@ -0,0 +1,5 @@ +#include <kalmia.h> +#include <ezxml.h> +#include "node.h" + + diff --git a/src/node.h b/src/node.h new file mode 100644 index 0000000..ea0bedf --- /dev/null +++ b/src/node.h @@ -0,0 +1,9 @@ +#ifndef KALMIA_NODE_H +#define KALMIA_NODE_H + +#include <kalmia.h> +#include <ezxml.h> + + + +#endif diff --git a/src/node.test.c b/src/node.test.c new file mode 100644 index 0000000..236d200 --- /dev/null +++ b/src/node.test.c @@ -0,0 +1,8 @@ +#include <kalmia.h> +#include <ezxml.h> +#include "test/test.h" + + +void suite_node() +{ +} diff --git a/src/test/test.h b/src/test/test.h index 17731b9..881384e 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -5,11 +5,15 @@ void suite_transform(); +void suite_node(); +void suite_util(); #define RUN_TESTS() \ do { \ lily_run_suite(suite_transform); \ + lily_run_suite(suite_node); \ + lily_run_suite(suite_util); \ } while (0) #endif diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..7b8120b --- /dev/null +++ b/src/util.c @@ -0,0 +1,39 @@ +#include <stdlib.h> + +#include <kalmia.h> +#include "util.h" + +void * kai_next_array_element(void **array, size_t *count, size_t *max, size_t sz) +{ + void *array_new = NULL; + if (*max == 0) { + array_new = malloc(sz); + *count = 1; + *max = 1; + } + else if (*count == *max) { + array_new = realloc(*array, 2*(*max)*sz); + *count += 1; + *max *= 2; + } + else { + array_new = *array; + *count += 1; + } + + if (array_new == NULL) + return NULL; + + *array = array_new; + return array_new + ((*count-1) * sz); +} + + +ka_node_t * kai_next_node(kalmia_t *k) +{ + return kai_next_array_element( + (void**) &(k->node), + &(k->n_nodes), &(k->n_nodes_max), + sizeof(ka_node_t) + ); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..7cf72bf --- /dev/null +++ b/src/util.h @@ -0,0 +1,10 @@ +#ifndef KALMIA_UTIL_H +#define KALMIA_UTIL_H + +#include <kalmia.h> + +void * kai_next_array_element(void **array, size_t *count, size_t *max, size_t sz); + +ka_node_t * kai_next_node(kalmia_t *k); + +#endif diff --git a/src/util.test.c b/src/util.test.c new file mode 100644 index 0000000..c2f8462 --- /dev/null +++ b/src/util.test.c @@ -0,0 +1,57 @@ +#include <stdlib.h> + +#include <kalmia.h> +#include "util.h" +#include "test/test.h" + +void next_array_element(); + +void suite_util() +{ + lily_run_test(next_array_element); +} + +void next_array_element() +{ + int *arr = NULL; + size_t count = 0; + size_t max = 0; + + int *next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + lily_assert_not_null(arr); + lily_assert_not_null(next); + + lily_assert_ptr_equal(arr+0, next); + lily_assert_int_equal(count, 1); + lily_assert_int_equal(max, 1); + *next = 2; + + next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + + lily_assert_not_null(next); + lily_assert_ptr_equal(arr+1, next); + lily_assert_int_equal(count, 2); + lily_assert_int_equal(max, 2); + *next = 1; + + next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + + lily_assert_not_null(next); + lily_assert_ptr_equal(arr+2, next); + lily_assert_int_equal(count, 3); + lily_assert_int_equal(max, 4); + *next = 3; + + next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + + lily_assert_not_null(next); + lily_assert_ptr_equal(arr+3, next); + lily_assert_int_equal(count, 4); + lily_assert_int_equal(max, 4); + *next = 4; + + int expected[] = { 2, 1, 3, 4 }; + lily_assert_memory_equal(expected, arr, 4 * sizeof(int)); + + free(arr); +} |