255 lines
6.5 KiB
C
255 lines
6.5 KiB
C
#include "unity.h"
|
|
|
|
#include "dynamic_array.h"
|
|
|
|
void setUp(void) {
|
|
}
|
|
|
|
void tearDown(void) {
|
|
}
|
|
|
|
void test_array_create_without_options(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
const ArrayHeader* header = array_get_header(arr);
|
|
TEST_ASSERT_EQUAL(DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, header->capacity);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_destroy(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
array_destroy(arr);
|
|
TEST_ASSERT_NULL(arr);
|
|
}
|
|
|
|
void test_array_size_initial(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
TEST_ASSERT_EQUAL(0, array_get_header(arr)->size);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_push_value(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
array_push_value(arr, 42);
|
|
|
|
TEST_ASSERT_EQUAL(42, arr[0]);
|
|
TEST_ASSERT_EQUAL(1, array_get_header(arr)->size);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_push_multiple_values(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
array_push_value(arr, 10);
|
|
array_push_value(arr, 20);
|
|
array_push_value(arr, 30);
|
|
|
|
TEST_ASSERT_EQUAL(10, arr[0]);
|
|
TEST_ASSERT_EQUAL(20, arr[1]);
|
|
TEST_ASSERT_EQUAL(30, arr[2]);
|
|
TEST_ASSERT_EQUAL(3, array_get_header(arr)->size);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_pop_decrements_size(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
array_push_value(arr, 42);
|
|
array_push_value(arr, 99);
|
|
array_pop(arr);
|
|
|
|
TEST_ASSERT_EQUAL(1, array_get_header(arr)->size);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_pop_on_empty_is_noop(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
array_push_value(arr, 99);
|
|
TEST_ASSERT_EQUAL(1, array_get_header(arr)->size);
|
|
TEST_ASSERT_EQUAL(99, arr[0]);
|
|
|
|
array_pop(arr);
|
|
TEST_ASSERT_EQUAL(0, array_get_header(arr)->size);
|
|
|
|
array_pop(arr); // should not underflow
|
|
TEST_ASSERT_EQUAL(0, array_get_header(arr)->size);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_get_capacity_default(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
TEST_ASSERT_EQUAL(DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, array_get_header(arr)->capacity);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_size_capacity_relationship(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
array_push_value(arr, 1);
|
|
array_push_value(arr, 2);
|
|
array_push_value(arr, 3);
|
|
|
|
const ArrayHeader* header = array_get_header(arr);
|
|
TEST_ASSERT_EQUAL(3, header->size);
|
|
TEST_ASSERT_LESS_OR_EQUAL(header->capacity, header->size);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_push_beyond_capacity(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
const int num_elements = DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE * 2;
|
|
for (int i = 0; i < num_elements; i++) {
|
|
array_push_value(arr, i);
|
|
}
|
|
|
|
const ArrayHeader* header = array_get_header(arr);
|
|
TEST_ASSERT_EQUAL(num_elements, header->size);
|
|
TEST_ASSERT_GREATER_THAN(DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, header->capacity);
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_values_correct_after_growth(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
const int num_elements = 1000;
|
|
for (int i = 0; i < num_elements; i++) {
|
|
array_push_value(arr, i);
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL(num_elements, array_get_header(arr)->size);
|
|
for (int i = 0; i < num_elements; i++) {
|
|
TEST_ASSERT_EQUAL(i, arr[i]);
|
|
}
|
|
|
|
array_destroy(arr);
|
|
}
|
|
|
|
void test_array_looping(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
|
|
const int num_elements = DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE * 2;
|
|
for (int i = 0; i < num_elements; i++) {
|
|
array_push_value(arr, i);
|
|
}
|
|
|
|
const ArrayHeader* header = array_get_header(arr);
|
|
TEST_ASSERT_EQUAL(num_elements, header->size);
|
|
|
|
for (int i = 0; i < header->size; i++) {
|
|
TEST_ASSERT_EQUAL(i, arr[i]);
|
|
}
|
|
}
|
|
|
|
/* --- array_get_header branch: NULL arr → returns NULL --- */
|
|
void test_array_get_header_on_null_returns_null(void) {
|
|
int* arr = NULL;
|
|
TEST_ASSERT_NULL(array_get_header(arr));
|
|
}
|
|
|
|
/* --- array_destroy branch: NULL arr → no-op, does not crash --- */
|
|
void test_array_destroy_on_null_is_noop(void) {
|
|
int* arr = NULL;
|
|
array_destroy(arr);
|
|
TEST_ASSERT_NULL(arr);
|
|
}
|
|
|
|
/* --- array_push_value branch: NULL arr → auto-creates the array --- */
|
|
void test_array_push_value_on_null_auto_creates(void) {
|
|
int* arr = NULL;
|
|
array_push_value(arr, 7);
|
|
TEST_ASSERT_NOT_NULL(arr);
|
|
TEST_ASSERT_EQUAL(7, arr[0]);
|
|
TEST_ASSERT_EQUAL(1, array_get_header(arr)->size);
|
|
array_destroy(arr);
|
|
}
|
|
|
|
/* --- array_pop branch: NULL arr → no-op, does not crash --- */
|
|
void test_array_pop_on_null_is_noop(void) {
|
|
int* arr = NULL;
|
|
array_pop(arr);
|
|
TEST_ASSERT_NULL(arr);
|
|
}
|
|
|
|
/* --- array_size branch: NULL arr → 0 --- */
|
|
void test_array_size_on_null_returns_zero(void) {
|
|
int* arr = NULL;
|
|
TEST_ASSERT_EQUAL(0, array_size(arr));
|
|
}
|
|
|
|
/* --- array_size branch: non-NULL arr → current size --- */
|
|
void test_array_size_returns_current_size(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
array_push_value(arr, 1);
|
|
array_push_value(arr, 2);
|
|
TEST_ASSERT_EQUAL(2, array_size(arr));
|
|
array_destroy(arr);
|
|
}
|
|
|
|
/* --- array_capacity branch: NULL arr → 0 --- */
|
|
void test_array_capacity_on_null_returns_zero(void) {
|
|
int* arr = NULL;
|
|
TEST_ASSERT_EQUAL(0, array_capacity(arr));
|
|
}
|
|
|
|
/* --- array_capacity branch: non-NULL arr → current capacity --- */
|
|
void test_array_capacity_returns_current_capacity(void) {
|
|
int* arr = NULL;
|
|
array_create(arr);
|
|
TEST_ASSERT_EQUAL(DYNAMIC_ARRAY_DEFAULT_ARRAY_SIZE, array_capacity(arr));
|
|
array_destroy(arr);
|
|
}
|
|
|
|
int main(void) {
|
|
UNITY_BEGIN();
|
|
|
|
RUN_TEST(test_array_create_without_options);
|
|
RUN_TEST(test_array_destroy);
|
|
RUN_TEST(test_array_size_initial);
|
|
RUN_TEST(test_array_push_value);
|
|
RUN_TEST(test_array_push_multiple_values);
|
|
RUN_TEST(test_array_pop_decrements_size);
|
|
RUN_TEST(test_array_pop_on_empty_is_noop);
|
|
RUN_TEST(test_array_get_capacity_default);
|
|
RUN_TEST(test_array_size_capacity_relationship);
|
|
RUN_TEST(test_array_push_beyond_capacity);
|
|
RUN_TEST(test_array_values_correct_after_growth);
|
|
RUN_TEST(test_array_looping);
|
|
|
|
RUN_TEST(test_array_get_header_on_null_returns_null);
|
|
RUN_TEST(test_array_destroy_on_null_is_noop);
|
|
RUN_TEST(test_array_push_value_on_null_auto_creates);
|
|
RUN_TEST(test_array_pop_on_null_is_noop);
|
|
RUN_TEST(test_array_size_on_null_returns_zero);
|
|
RUN_TEST(test_array_size_returns_current_size);
|
|
RUN_TEST(test_array_capacity_on_null_returns_zero);
|
|
RUN_TEST(test_array_capacity_returns_current_capacity);
|
|
|
|
return UNITY_END();
|
|
} |