refactor dynamic array API: rename builder functions, update memory management, and improve tests

This commit is contained in:
2026-03-26 17:04:48 -03:00
parent 0cd80ad628
commit a45a74ddff
4 changed files with 48 additions and 57 deletions

1
.idea/vcs.xml generated
View File

@@ -4,5 +4,6 @@
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/build/_deps/googletest-src" vcs="Git" /> <mapping directory="$PROJECT_DIR$/build/_deps/googletest-src" vcs="Git" />
<mapping directory="$PROJECT_DIR$/build/_deps/unity-test-src" vcs="Git" /> <mapping directory="$PROJECT_DIR$/build/_deps/unity-test-src" vcs="Git" />
<mapping directory="$PROJECT_DIR$/submodules/external/unity" vcs="Git" />
</component> </component>
</project> </project>

View File

@@ -5,39 +5,36 @@
#include "dynamic_array.h" #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)); const auto array = (Array*) malloc(sizeof(Array));
int size = DEFAULT_ARRAY_SIZE; array->capacity = opt.initial_size;
if (options != NULL && options->initial_size > 0) {
size = options->initial_size;
}
array->capacity = size;
array->value = (int*) calloc(array->capacity, sizeof(int)); array->value = (int*) calloc(array->capacity, sizeof(int));
return array; return array;
} }
void array_deconstructor(Array *array) { void array_deconstructor(Array **pp_array) {
if (array == NULL) { if (*pp_array == NULL) return;
return;
} free((*pp_array)->value);
free(array->value); free(*pp_array);
free(array); *pp_array = nullptr;
} }
void array_resize(Array *array, const int new_size) { void array_resize(Array *p_array, const int new_size) {
int *new_ptr = (int *) realloc(array->value, new_size*sizeof(int)); int *new_ptr = realloc(p_array->value, new_size*sizeof(int));
if (new_ptr == NULL) { if (new_ptr == NULL) {
exit(1); exit(1);
} }
if (new_size > array->capacity) { if (new_size > p_array->capacity) {
memset(new_ptr + array->capacity, 0, (new_size - array->capacity) * sizeof(int)); memset(new_ptr + p_array->capacity, 0, (new_size - p_array->capacity) * sizeof(int));
} }
array->value = new_ptr; p_array->value = new_ptr;
array->capacity = new_size; p_array->capacity = new_size;
} }
int *array_get_value(const Array *array, const int index) { 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]; return &array->value[index];
} }
void array_set_value(Array *array, const int index, const int value) { void array_set_value(Array *p_array, const int index, const int value) {
if (index > array->capacity) { if (index > p_array->capacity) {
const int new_size = (index - array->capacity) * 2 + array->capacity; const int new_size = (index - p_array->capacity) * 2 + p_array->capacity;
array_resize(array, new_size); 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;
// }

View File

@@ -5,24 +5,29 @@
typedef struct Array { typedef struct Array {
int *value; int *value;
int size;
int capacity; int capacity;
} Array; } Array;
typedef struct ArrayBuilderOptions { typedef struct ArrayCreateOptions {
int initial_size; int initial_size;
} ArrayBuilderOptions; } ArrayCreateOptions;
Array *array_builder(const ArrayBuilderOptions *options);
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); int *array_get_value(const Array *p_array, int index);
void array_set_value(Array *p_array, int index, int value);

View File

@@ -10,28 +10,36 @@ void tearDown() {
} }
void test_array_builder_without_options(void) { void test_array_create_without_options(void) {
Array *array = array_builder(NULL); Array *array = array_create(nullptr);
TEST_ASSERT_EQUAL(DEFAULT_ARRAY_SIZE, array->capacity); 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; const int EXPECTED_CAPACITY = 20;
Array *array = array_builder(&(ArrayBuilderOptions){ const Array *array = array_create(&(ArrayCreateOptions){
.initial_size = EXPECTED_CAPACITY .initial_size = EXPECTED_CAPACITY
}); });
TEST_ASSERT_EQUAL(EXPECTED_CAPACITY, array->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) int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
RUN_TEST(test_array_builder_without_options); RUN_TEST(test_array_create_without_options);
RUN_TEST(test_array_builder_with_options); RUN_TEST(test_array_create_with_options);
RUN_TEST(test_array_deconstruct);
return UNITY_END(); return UNITY_END();
} }