From a45a74ddff53d5587fcd77dcbe3bb9741b3370fe Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Thu, 26 Mar 2026 17:04:48 -0300 Subject: [PATCH] refactor dynamic array API: rename builder functions, update memory management, and improve tests --- .idea/vcs.xml | 1 + src/dynamic_array.c | 63 +++++++++++++--------------------------- src/dynamic_array.h | 21 +++++++++----- src/test_dynamic_array.c | 20 +++++++++---- 4 files changed, 48 insertions(+), 57 deletions(-) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 1ae5cb1..1b807d5 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -4,5 +4,6 @@ + \ No newline at end of file diff --git a/src/dynamic_array.c b/src/dynamic_array.c index 3ddbde2..06839c9 100644 --- a/src/dynamic_array.c +++ b/src/dynamic_array.c @@ -5,39 +5,36 @@ #include "dynamic_array.h" -Array *array_builder(const ArrayBuilderOptions *options) { +Array *array_create(const ArrayCreateOptions *options) { + const ArrayCreateOptions opt = options != NULL ? *options : DEFAULT_ARRAY_CREATE_OPTIONS; const auto array = (Array*) malloc(sizeof(Array)); - int size = DEFAULT_ARRAY_SIZE; - if (options != NULL && options->initial_size > 0) { - size = options->initial_size; - } - array->capacity = size; + array->capacity = opt.initial_size; array->value = (int*) calloc(array->capacity, sizeof(int)); return array; } -void array_deconstructor(Array *array) { - if (array == NULL) { - return; - } - free(array->value); - free(array); +void array_deconstructor(Array **pp_array) { + if (*pp_array == NULL) return; + + free((*pp_array)->value); + free(*pp_array); + *pp_array = nullptr; } -void array_resize(Array *array, const int new_size) { - int *new_ptr = (int *) realloc(array->value, new_size*sizeof(int)); +void array_resize(Array *p_array, const int new_size) { + int *new_ptr = realloc(p_array->value, new_size*sizeof(int)); if (new_ptr == NULL) { exit(1); } - if (new_size > array->capacity) { - memset(new_ptr + array->capacity, 0, (new_size - array->capacity) * sizeof(int)); + if (new_size > p_array->capacity) { + memset(new_ptr + p_array->capacity, 0, (new_size - p_array->capacity) * sizeof(int)); } - array->value = new_ptr; - array->capacity = new_size; + p_array->value = new_ptr; + p_array->capacity = new_size; } int *array_get_value(const Array *array, const int index) { @@ -47,30 +44,10 @@ int *array_get_value(const Array *array, const int index) { return &array->value[index]; } -void array_set_value(Array *array, const int index, const int value) { - if (index > array->capacity) { - const int new_size = (index - array->capacity) * 2 + array->capacity; - array_resize(array, new_size); +void array_set_value(Array *p_array, const int index, const int value) { + if (index > p_array->capacity) { + const int new_size = (index - p_array->capacity) * 2 + p_array->capacity; + array_resize(p_array, new_size); } - array->value[index] = value; + p_array->value[index] = value; } -// -// int main() { -// Array *a = array_builder(); -// -// int index = 1000; -// int expected_value = 99; -// -// array_set_value(a, index, expected_value); -// -// int *value = array_get_value(a, index); -// if (value != NULL) { -// printf("Found the value %d in the index %d of array", *value, index); -// } else { -// printf("Found invalid index for array"); -// } -// -// array_deconstructor(a); -// -// return 0; -// } diff --git a/src/dynamic_array.h b/src/dynamic_array.h index 4406356..8fe8297 100644 --- a/src/dynamic_array.h +++ b/src/dynamic_array.h @@ -5,24 +5,29 @@ typedef struct Array { int *value; + int size; int capacity; } Array; -typedef struct ArrayBuilderOptions { +typedef struct ArrayCreateOptions { int initial_size; -} ArrayBuilderOptions; - -Array *array_builder(const ArrayBuilderOptions *options); +} ArrayCreateOptions; -void array_deconstructor(Array *array); +#define DEFAULT_ARRAY_CREATE_OPTIONS \ + (ArrayCreateOptions){ DEFAULT_ARRAY_SIZE } + +Array *array_create(const ArrayCreateOptions *options); -void array_resize(Array *array, int new_size); +void array_deconstructor(Array **pp_array); -int *array_get_value(const Array *array, int index); +void array_resize(Array *p_array, int new_size); -void array_set_value(Array *array, int index, int value); \ No newline at end of file +int *array_get_value(const Array *p_array, int index); + + +void array_set_value(Array *p_array, int index, int value); \ No newline at end of file diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c index 3e6f650..f5ab602 100644 --- a/src/test_dynamic_array.c +++ b/src/test_dynamic_array.c @@ -10,28 +10,36 @@ void tearDown() { } -void test_array_builder_without_options(void) { - Array *array = array_builder(NULL); +void test_array_create_without_options(void) { + Array *array = array_create(nullptr); TEST_ASSERT_EQUAL(DEFAULT_ARRAY_SIZE, array->capacity); } -void test_array_builder_with_options(void) { +void test_array_create_with_options(void) { const int EXPECTED_CAPACITY = 20; - Array *array = array_builder(&(ArrayBuilderOptions){ + const Array *array = array_create(&(ArrayCreateOptions){ .initial_size = EXPECTED_CAPACITY }); TEST_ASSERT_EQUAL(EXPECTED_CAPACITY, array->capacity); } +void test_array_deconstruct(void) { + Array *array = array_create(nullptr); + array_deconstructor(&array); + TEST_ASSERT_NULL(array); +} + + int main(void) { UNITY_BEGIN(); - RUN_TEST(test_array_builder_without_options); - RUN_TEST(test_array_builder_with_options); + RUN_TEST(test_array_create_without_options); + RUN_TEST(test_array_create_with_options); + RUN_TEST(test_array_deconstruct); return UNITY_END(); } \ No newline at end of file