refactor dynamic array: update default size definitions and improve header guard
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
|
|
||||||
Array *array_create(const ArrayCreateOptions *options) {
|
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 *array = malloc(sizeof(Array));
|
||||||
|
|
||||||
array->capacity = opt.initial_size;
|
array->capacity = opt.initial_size;
|
||||||
|
|||||||
@@ -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>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define DEFAULT_ARRAY_SIZE 10
|
#define DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE 10
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -20,8 +26,8 @@ typedef struct {
|
|||||||
} ArrayCreateOptions;
|
} ArrayCreateOptions;
|
||||||
|
|
||||||
|
|
||||||
#define DEFAULT_ARRAY_CREATE_OPTIONS \
|
#define DYNAMIC_ARRAY_DEFAULT_ARRAY_CREATE_OPTIONS \
|
||||||
(ArrayCreateOptions){ DEFAULT_ARRAY_SIZE, sizeof(int) }
|
(ArrayCreateOptions){ DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, sizeof(int) }
|
||||||
|
|
||||||
Array *array_create(const ArrayCreateOptions *options);
|
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) \
|
#define array_set_value_as(type, arr, idx, value) \
|
||||||
((sizeof(type) == (arr)->element_size) ? \
|
((sizeof(type) == (arr)->element_size) ? \
|
||||||
(array_set_value((arr), (idx), (value)), 0) : \
|
(array_set_value((arr), (idx), (value)), 0) : \
|
||||||
-1)
|
-1)
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -12,7 +12,7 @@ void tearDown() {
|
|||||||
|
|
||||||
void test_array_create_without_options(void) {
|
void test_array_create_without_options(void) {
|
||||||
const Array *array = array_create(NULL);
|
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);
|
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);
|
array_deconstructor(&array);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user