feat/adds-null-checks #4

Merged
HideyoshiNakazone merged 2 commits from feat/adds-null-checks into main 2026-04-16 00:07:00 +00:00
Showing only changes of commit a8cd5fb7a8 - Show all commits

View File

@@ -178,6 +178,74 @@ void test_array_looping(void) {
} }
/* --- 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) int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
@@ -195,5 +263,14 @@ int main(void)
RUN_TEST(test_array_values_correct_after_growth); RUN_TEST(test_array_values_correct_after_growth);
RUN_TEST(test_array_looping); 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(); return UNITY_END();
} }