refactor dynamic array: update index types to size_t for better compatibility and memory safety
This commit is contained in:
@@ -35,7 +35,7 @@ void test_array_deconstruct(void) {
|
||||
|
||||
void test_array_resize_expand(void) {
|
||||
Array *array = array_create(NULL);
|
||||
const int ORIGINAL_CAPACITY = array->capacity;
|
||||
const size_t ORIGINAL_CAPACITY = array->capacity;
|
||||
const int NEW_CAPACITY = 30;
|
||||
|
||||
array_resize(array, NEW_CAPACITY);
|
||||
@@ -78,7 +78,7 @@ void test_array_set_value_beyond_capacity(void) {
|
||||
Array *p_array = array_create(NULL);
|
||||
const int INDEX = 50;
|
||||
const int EXPECTED_VALUE = 99;
|
||||
const int ORIGINAL_CAPACITY = p_array->capacity;
|
||||
const size_t ORIGINAL_CAPACITY = p_array->capacity;
|
||||
|
||||
array_set_value(p_array, INDEX, &EXPECTED_VALUE);
|
||||
|
||||
@@ -155,7 +155,7 @@ void test_array_multiple_operations(void) {
|
||||
void test_array_get_capacity_default(void) {
|
||||
Array *array = array_create(NULL);
|
||||
|
||||
int capacity = array_get_capacity(array);
|
||||
const size_t capacity = array_get_capacity(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(DEFAULT_ARRAY_SIZE, capacity);
|
||||
|
||||
@@ -167,7 +167,7 @@ void test_array_get_capacity_custom_size(void) {
|
||||
const int EXPECTED_CAPACITY = 25;
|
||||
Array *array = array_create(&(ArrayCreateOptions){.initial_size = EXPECTED_CAPACITY});
|
||||
|
||||
int capacity = array_get_capacity(array);
|
||||
const size_t capacity = array_get_capacity(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(EXPECTED_CAPACITY, capacity);
|
||||
|
||||
@@ -177,11 +177,11 @@ void test_array_get_capacity_custom_size(void) {
|
||||
|
||||
void test_array_get_capacity_after_resize(void) {
|
||||
Array *array = array_create(NULL);
|
||||
const int ORIGINAL_CAPACITY = array_get_capacity(array);
|
||||
const int NEW_CAPACITY = 50;
|
||||
const size_t ORIGINAL_CAPACITY = array_get_capacity(array);
|
||||
const size_t NEW_CAPACITY = 50;
|
||||
|
||||
array_resize(array, NEW_CAPACITY);
|
||||
int capacity = array_get_capacity(array);
|
||||
const size_t capacity = array_get_capacity(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(NEW_CAPACITY, capacity);
|
||||
TEST_ASSERT_NOT_EQUAL(ORIGINAL_CAPACITY, capacity);
|
||||
@@ -193,7 +193,7 @@ void test_array_get_capacity_after_resize(void) {
|
||||
void test_array_get_size_initial(void) {
|
||||
Array *array = array_create(NULL);
|
||||
|
||||
int size = array_get_size(array);
|
||||
const size_t size = array_get_size(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, size);
|
||||
|
||||
@@ -207,7 +207,7 @@ void test_array_get_size_after_single_set(void) {
|
||||
const int VALUE = 42;
|
||||
|
||||
array_set_value(array, 0, &VALUE);
|
||||
int size = array_get_size(array);
|
||||
const size_t size = array_get_size(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(1, size);
|
||||
|
||||
@@ -223,7 +223,7 @@ void test_array_get_size_after_multiple_sets(void) {
|
||||
array_set_value(array, 0, &VALUE);
|
||||
array_set_value(array, 1, &VALUE);
|
||||
array_set_value(array, 2, &VALUE);
|
||||
int size = array_get_size(array);
|
||||
const size_t size = array_get_size(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(3, size);
|
||||
|
||||
@@ -238,7 +238,7 @@ void test_array_get_size_with_gap_in_indices(void) {
|
||||
|
||||
array_set_value(array, 0, &VALUE);
|
||||
array_set_value(array, 5, &VALUE);
|
||||
int size = array_get_size(array);
|
||||
const size_t size = array_get_size(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(6, size);
|
||||
|
||||
@@ -255,8 +255,8 @@ void test_array_get_size_capacity_relationship(void) {
|
||||
array_set_value(array, 1, &VALUE);
|
||||
array_set_value(array, 2, &VALUE);
|
||||
|
||||
int size = array_get_size(array);
|
||||
int capacity = array_get_capacity(array);
|
||||
const size_t size = array_get_size(array);
|
||||
const size_t capacity = array_get_capacity(array);
|
||||
|
||||
TEST_ASSERT_LESS_OR_EQUAL(capacity, size);
|
||||
TEST_ASSERT_EQUAL(3, size);
|
||||
@@ -273,8 +273,8 @@ void test_array_get_size_after_resize_beyond_capacity(void) {
|
||||
|
||||
array_set_value(array, 0, &VALUE);
|
||||
array_set_value(array, 15, &VALUE);
|
||||
const int size = array_get_size(array);
|
||||
const int capacity = array_get_capacity(array);
|
||||
const size_t size = array_get_size(array);
|
||||
const size_t capacity = array_get_capacity(array);
|
||||
|
||||
TEST_ASSERT_EQUAL(16, size);
|
||||
TEST_ASSERT_LESS_OR_EQUAL(capacity, size);
|
||||
@@ -283,6 +283,62 @@ void test_array_get_size_after_resize_beyond_capacity(void) {
|
||||
}
|
||||
|
||||
|
||||
void test_array_get_value_as(void) {
|
||||
Array *p_array = array_create(&(ArrayCreateOptions){
|
||||
.initial_size = 5,
|
||||
.element_size = sizeof(int)
|
||||
});
|
||||
const int VALUE = 42;
|
||||
|
||||
array_set_value(p_array, 0, &VALUE);
|
||||
|
||||
const int *value = array_get_value_as(int, p_array, 0);
|
||||
|
||||
TEST_ASSERT_EQUAL(VALUE, *value);
|
||||
}
|
||||
|
||||
|
||||
void test_array_get_value_as_invalid_type(void) {
|
||||
Array *p_array = array_create(&(ArrayCreateOptions){
|
||||
.initial_size = 5,
|
||||
.element_size = sizeof(int)
|
||||
});
|
||||
const int VALUE = 42;
|
||||
|
||||
array_set_value(p_array, 0, &VALUE);
|
||||
|
||||
const void *value = array_get_value_as(double, p_array, 0);
|
||||
|
||||
TEST_ASSERT_NULL(value);
|
||||
}
|
||||
|
||||
|
||||
void test_array_set_value_as(void) {
|
||||
Array *p_array = array_create(&(ArrayCreateOptions){
|
||||
.initial_size = 5,
|
||||
.element_size = sizeof(double)
|
||||
});
|
||||
|
||||
const double VALUE = 42;
|
||||
|
||||
const int result = array_set_value_as(double, p_array, 0, &VALUE);
|
||||
TEST_ASSERT_EQUAL(result, 0);
|
||||
}
|
||||
|
||||
|
||||
void test_array_set_value_as_invalid_type(void) {
|
||||
Array *p_array = array_create(&(ArrayCreateOptions){
|
||||
.initial_size = 5,
|
||||
.element_size = sizeof(double)
|
||||
});
|
||||
|
||||
const int VALUE = 42;
|
||||
|
||||
const int result = array_set_value_as(double, p_array, 0, &VALUE);
|
||||
TEST_ASSERT_EQUAL(result, 0);
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
@@ -307,6 +363,10 @@ int main(void)
|
||||
RUN_TEST(test_array_get_size_with_gap_in_indices);
|
||||
RUN_TEST(test_array_get_size_capacity_relationship);
|
||||
RUN_TEST(test_array_get_size_after_resize_beyond_capacity);
|
||||
RUN_TEST(test_array_get_value_as);
|
||||
RUN_TEST(test_array_get_value_as_invalid_type);
|
||||
RUN_TEST(test_array_set_value_as);
|
||||
RUN_TEST(test_array_set_value_as_invalid_type);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user