refactor dynamic array: update default size definitions and improve header guard

This commit is contained in:
2026-03-27 15:38:10 -03:00
parent cc1f5d47ad
commit 857205fe45
3 changed files with 17 additions and 8 deletions

View File

@@ -6,7 +6,7 @@
Array *array_create(const ArrayCreateOptions *options) {
const ArrayCreateOptions opt = options != NULL ? *options : DEFAULT_ARRAY_CREATE_OPTIONS;
const ArrayCreateOptions opt = options != NULL ? *options : DYNAMIC_ARRAY_DEFAULT_ARRAY_CREATE_OPTIONS;
Array *array = malloc(sizeof(Array));
array->capacity = opt.initial_size;

View File

@@ -1,8 +1,14 @@
#pragma once
#ifndef DYNAMIC_ARRAY_H
#define DYNAMIC_ARRAY_H
#define DYNAMIC_ARRAY_VERSION_MAJOR 0
#define DYNAMIC_ARRAY_VERSION_MINOR 1
#define DYNAMIC_ARRAY_VERSION_BUILD 0
#define DYNAMIC_ARRAY_VERSION ((DYNAMIC_ARRAY_VERSION_MAJOR << 16) | (DYNAMIC_ARRAY_VERSION_MINOR << 8) | DYNAMIC_ARRAY_VERSION_BUILD)
#include <stddef.h>
#define DEFAULT_ARRAY_SIZE 10
#define DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE 10
typedef struct {
@@ -20,8 +26,8 @@ typedef struct {
} ArrayCreateOptions;
#define DEFAULT_ARRAY_CREATE_OPTIONS \
(ArrayCreateOptions){ DEFAULT_ARRAY_SIZE, sizeof(int) }
#define DYNAMIC_ARRAY_DEFAULT_ARRAY_CREATE_OPTIONS \
(ArrayCreateOptions){ DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, sizeof(int) }
Array *array_create(const ArrayCreateOptions *options);
@@ -53,4 +59,7 @@ size_t array_get_capacity(const Array *p_array);
#define array_set_value_as(type, arr, idx, value) \
((sizeof(type) == (arr)->element_size) ? \
(array_set_value((arr), (idx), (value)), 0) : \
-1)
-1)
#endif

View File

@@ -12,7 +12,7 @@ void tearDown() {
void test_array_create_without_options(void) {
const Array *array = array_create(NULL);
TEST_ASSERT_EQUAL(DEFAULT_ARRAY_SIZE, array->capacity);
TEST_ASSERT_EQUAL(DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, array->capacity);
}
@@ -157,7 +157,7 @@ void test_array_get_capacity_default(void) {
const size_t capacity = array_get_capacity(array);
TEST_ASSERT_EQUAL(DEFAULT_ARRAY_SIZE, capacity);
TEST_ASSERT_EQUAL(DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, capacity);
array_deconstructor(&array);
}