commit 0cd80ad628ee999f469e2cd0534bfb8da8bdc8cb Author: Vitor Hideyoshi Date: Thu Mar 26 15:00:00 2026 -0300 initialize dynamic array library with tests and configuration files diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..35f4b82 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "submodules/external/unity_cmake/Unity"] + path = submodules/external/unity_cmake/Unity + url = https://github.com/ThrowTheSwitch/Unity.git +[submodule "submodules/external/unity"] + path = submodules/external/unity + url = https://github.com/ThrowTheSwitch/Unity.git diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..a9ccc9d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +dynamic_array \ No newline at end of file diff --git a/.idea/copilot.data.migration.ask2agent.xml b/.idea/copilot.data.migration.ask2agent.xml new file mode 100644 index 0000000..1f2ea11 --- /dev/null +++ b/.idea/copilot.data.migration.ask2agent.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..8d0e15e --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,345 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b766632 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/test-meson.iml b/.idea/test-meson.iml new file mode 100644 index 0000000..4c94235 --- /dev/null +++ b/.idea/test-meson.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..1ae5cb1 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ae8d2c9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 4.2) +project(dynamic_array LANGUAGES C) + +set(CMAKE_C_STANDARD 23) + + +# Configures Testing Framework +include(CTest) +add_subdirectory(submodules/external/unity) +enable_testing() + + +# Configures the project +add_subdirectory(src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..8c36066 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,11 @@ +add_library(dynamic_array + dynamic_array.c + dynamic_array.h) +target_include_directories(dynamic_array INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) + + +add_executable(test_dynamic_array test_dynamic_array.c) +target_link_libraries(test_dynamic_array PRIVATE dynamic_array unity::framework) + + +add_test(NAME test_dynamic_array COMMAND test_dynamic_array) \ No newline at end of file diff --git a/src/dynamic_array.c b/src/dynamic_array.c new file mode 100644 index 0000000..3ddbde2 --- /dev/null +++ b/src/dynamic_array.c @@ -0,0 +1,76 @@ +#include +#include +#include + +#include "dynamic_array.h" + + +Array *array_builder(const ArrayBuilderOptions *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->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_resize(Array *array, const int new_size) { + int *new_ptr = (int *) realloc(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)); + } + + array->value = new_ptr; + array->capacity = new_size; +} + +int *array_get_value(const Array *array, const int index) { + if (index > array->capacity) { + return nullptr; + } + 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); + } + 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 new file mode 100644 index 0000000..4406356 --- /dev/null +++ b/src/dynamic_array.h @@ -0,0 +1,28 @@ +#pragma once + +#define DEFAULT_ARRAY_SIZE 10 + + +typedef struct Array { + int *value; + int capacity; +} Array; + + +typedef struct ArrayBuilderOptions { + int initial_size; +} ArrayBuilderOptions; + +Array *array_builder(const ArrayBuilderOptions *options); + + +void array_deconstructor(Array *array); + + +void array_resize(Array *array, int new_size); + + +int *array_get_value(const Array *array, int index); + + +void array_set_value(Array *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 new file mode 100644 index 0000000..3e6f650 --- /dev/null +++ b/src/test_dynamic_array.c @@ -0,0 +1,37 @@ +#include "unity.h" + +#include "dynamic_array.h" + + +void setUp() { +} + +void tearDown() { +} + + +void test_array_builder_without_options(void) { + Array *array = array_builder(NULL); + TEST_ASSERT_EQUAL(DEFAULT_ARRAY_SIZE, array->capacity); +} + + +void test_array_builder_with_options(void) { + const int EXPECTED_CAPACITY = 20; + + Array *array = array_builder(&(ArrayBuilderOptions){ + .initial_size = EXPECTED_CAPACITY + }); + TEST_ASSERT_EQUAL(EXPECTED_CAPACITY, array->capacity); +} + + +int main(void) +{ + UNITY_BEGIN(); + + RUN_TEST(test_array_builder_without_options); + RUN_TEST(test_array_builder_with_options); + + return UNITY_END(); +} \ No newline at end of file diff --git a/submodules/external/unity b/submodules/external/unity new file mode 160000 index 0000000..5e3a3db --- /dev/null +++ b/submodules/external/unity @@ -0,0 +1 @@ +Subproject commit 5e3a3db2f17aa71778b6e52c4879d884705625fb