initialize dynamic array library with tests and configuration files
This commit is contained in:
11
src/CMakeLists.txt
Normal file
11
src/CMakeLists.txt
Normal file
@@ -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)
|
||||
76
src/dynamic_array.c
Normal file
76
src/dynamic_array.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
// }
|
||||
28
src/dynamic_array.h
Normal file
28
src/dynamic_array.h
Normal file
@@ -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);
|
||||
37
src/test_dynamic_array.c
Normal file
37
src/test_dynamic_array.c
Normal file
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user