refactor dynamic array: fix index check in array_set_value and add test for adding elements
This commit is contained in:
@@ -47,7 +47,7 @@ void *array_get_value(const Array *p_array, const size_t index) {
|
||||
}
|
||||
|
||||
void array_set_value(Array *p_array, const size_t index, const void *value) {
|
||||
if (index > p_array->capacity) {
|
||||
if ((index + 1) > p_array->capacity) {
|
||||
size_t new_size = p_array->capacity;
|
||||
while (index >= new_size) {
|
||||
new_size *= 2;
|
||||
|
||||
@@ -339,6 +339,27 @@ void test_array_set_value_as_invalid_type(void) {
|
||||
}
|
||||
|
||||
|
||||
void test_adding_elements(void) {
|
||||
typedef struct {
|
||||
int value;
|
||||
} Element;
|
||||
|
||||
Array *p_array = array_create(&(ArrayCreateOptions){
|
||||
.initial_size = 10,
|
||||
.element_size = sizeof(Element)
|
||||
});
|
||||
|
||||
int number_of_elements = 1000;
|
||||
|
||||
for (int index = 0; index < number_of_elements; index++) {
|
||||
Element element = { index };
|
||||
array_set_value(p_array, index, &element);
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(array_get_size(p_array), number_of_elements);
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
@@ -367,6 +388,7 @@ int main(void)
|
||||
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);
|
||||
RUN_TEST(test_adding_elements);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user