cleanup
This commit is contained in:
+18
-147
@@ -2,25 +2,13 @@
|
||||
#include <stdbool.h>
|
||||
#include "cig.h"
|
||||
|
||||
static int int_cmp(const void *a, const void *b) {
|
||||
int ia = *(const int*)a;
|
||||
int ib = *(const int*)b;
|
||||
if (ia < ib) return -1;
|
||||
if (ia > ib) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool int_eq(const void *a, const void *b) {
|
||||
return *(const int*)a == *(const int*)b;
|
||||
}
|
||||
|
||||
static int reverse_cmp(const void *a, const void *b) {
|
||||
return -int_cmp(a, b);
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, append) {
|
||||
with_borrow(alloc) {
|
||||
int *numbers = make_arr(alloc, int);
|
||||
int *numbers = make_arr(int, alloc);
|
||||
arr_append(numbers, 40);
|
||||
arr_append(numbers, 41);
|
||||
arr_append(numbers, 42);
|
||||
@@ -40,7 +28,7 @@ Test(dynamic_arrays, append) {
|
||||
|
||||
Test(dynamic_arrays, pop) {
|
||||
with_borrow(alloc) {
|
||||
int *numbers = make_arr(alloc, int);
|
||||
int *numbers = make_arr(int, alloc);
|
||||
arr_append(numbers, 40);
|
||||
arr_append(numbers, 41);
|
||||
arr_append(numbers, 42);
|
||||
@@ -67,7 +55,7 @@ Test(dynamic_arrays, pop) {
|
||||
|
||||
Test(dynamic_arrays, contains) {
|
||||
with_borrow(alloc) {
|
||||
int *numbers = make_arr(alloc, int);
|
||||
int *numbers = make_arr(int, alloc);
|
||||
arr_append(numbers, 20);
|
||||
cr_expect(arr_contains(numbers, &(int){20}));
|
||||
arr_reset(numbers);
|
||||
@@ -82,72 +70,12 @@ Test(dynamic_arrays, contains) {
|
||||
cr_assert_eq(arr_len(numbers), 100);
|
||||
}
|
||||
}
|
||||
Test(dynamic_arrays, insert_sorted_ascending) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
|
||||
// Insert in random order
|
||||
arr_insert_sorted(arr, 5, int_cmp);
|
||||
arr_insert_sorted(arr, 2, int_cmp);
|
||||
arr_insert_sorted(arr, 8, int_cmp);
|
||||
arr_insert_sorted(arr, 1, int_cmp);
|
||||
arr_insert_sorted(arr, 9, int_cmp);
|
||||
arr_insert_sorted(arr, 3, int_cmp);
|
||||
|
||||
// Verify sorted order
|
||||
cr_assert_eq(arr_len(arr), 6);
|
||||
cr_assert_eq(arr[0], 1);
|
||||
cr_assert_eq(arr[1], 2);
|
||||
cr_assert_eq(arr[2], 3);
|
||||
cr_assert_eq(arr[3], 5);
|
||||
cr_assert_eq(arr[4], 8);
|
||||
cr_assert_eq(arr[5], 9);
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, insert_sorted_descending) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
|
||||
// Insert in random order with reverse comparison
|
||||
arr_insert_sorted(arr, 5, reverse_cmp);
|
||||
arr_insert_sorted(arr, 2, reverse_cmp);
|
||||
arr_insert_sorted(arr, 8, reverse_cmp);
|
||||
arr_insert_sorted(arr, 1, reverse_cmp);
|
||||
|
||||
// Verify descending order
|
||||
cr_assert_eq(arr_len(arr), 4);
|
||||
cr_assert_eq(arr[0], 8);
|
||||
cr_assert_eq(arr[1], 5);
|
||||
cr_assert_eq(arr[2], 2);
|
||||
cr_assert_eq(arr[3], 1);
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, insert_sorted_duplicates) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
|
||||
// Insert duplicates
|
||||
arr_insert_sorted(arr, 5, int_cmp);
|
||||
arr_insert_sorted(arr, 3, int_cmp);
|
||||
arr_insert_sorted(arr, 5, int_cmp);
|
||||
arr_insert_sorted(arr, 3, int_cmp);
|
||||
arr_insert_sorted(arr, 5, int_cmp);
|
||||
|
||||
// Verify all elements are present
|
||||
cr_assert_eq(arr_len(arr), 5);
|
||||
cr_assert_eq(arr[0], 3);
|
||||
cr_assert_eq(arr[1], 3);
|
||||
cr_assert_eq(arr[2], 5);
|
||||
cr_assert_eq(arr[3], 5);
|
||||
cr_assert_eq(arr[4], 5);
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, contains_found) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
int *arr = make_arr(int, alloc, .initial_capacity = 5);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
arr_append(arr, 30);
|
||||
@@ -160,7 +88,7 @@ Test(dynamic_arrays, contains_found) {
|
||||
|
||||
Test(dynamic_arrays, contains_not_found) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
int *arr = make_arr(int, alloc, .initial_capacity = 5);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
arr_append(arr, 30);
|
||||
@@ -173,63 +101,16 @@ Test(dynamic_arrays, contains_not_found) {
|
||||
|
||||
Test(dynamic_arrays, contains_empty) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
int *arr = make_arr(int, alloc, .initial_capacity = 5);
|
||||
|
||||
cr_assert_not(arr_contains(arr, &(int){10}));
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, insert_sorted_single_element) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
|
||||
arr_insert_sorted(arr, 42, int_cmp);
|
||||
|
||||
cr_assert_eq(arr_len(arr), 1);
|
||||
cr_assert_eq(arr[0], 42);
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, insert_sorted_already_sorted) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
|
||||
// Insert in already sorted order
|
||||
arr_insert_sorted(arr, 1, int_cmp);
|
||||
arr_insert_sorted(arr, 2, int_cmp);
|
||||
arr_insert_sorted(arr, 3, int_cmp);
|
||||
arr_insert_sorted(arr, 4, int_cmp);
|
||||
|
||||
// Verify order maintained
|
||||
cr_assert_eq(arr_len(arr), 4);
|
||||
for (size_t i = 0; i < arr_len(arr); i++) {
|
||||
cr_assert_eq(arr[i], (int)(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, insert_sorted_reverse_order) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
|
||||
// Insert in reverse order (worst case for bubble sort)
|
||||
arr_insert_sorted(arr, 5, int_cmp);
|
||||
arr_insert_sorted(arr, 4, int_cmp);
|
||||
arr_insert_sorted(arr, 3, int_cmp);
|
||||
arr_insert_sorted(arr, 2, int_cmp);
|
||||
arr_insert_sorted(arr, 1, int_cmp);
|
||||
|
||||
// Verify correct ascending order
|
||||
cr_assert_eq(arr_len(arr), 5);
|
||||
for (size_t i = 0; i < arr_len(arr); i++) {
|
||||
cr_assert_eq(arr[i], (int)(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, contains_cmp_found) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
int *arr = make_arr(int, alloc, .initial_capacity = 5);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
arr_append(arr, 30);
|
||||
@@ -242,7 +123,7 @@ Test(dynamic_arrays, contains_cmp_found) {
|
||||
|
||||
Test(dynamic_arrays, contains_cmp_not_found) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
int *arr = make_arr(int, alloc, .initial_capacity = 5);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
arr_append(arr, 30);
|
||||
@@ -255,7 +136,7 @@ Test(dynamic_arrays, contains_cmp_not_found) {
|
||||
|
||||
Test(dynamic_arrays, contains_cmp_empty) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int, .initial_capacity = 5);
|
||||
int *arr = make_arr(int, alloc, .initial_capacity = 5);
|
||||
|
||||
cr_assert_not(arr_contains_cmp(arr, int_eq, &(int){10}));
|
||||
}
|
||||
@@ -275,7 +156,7 @@ static bool kv_eq_by_key(const void *a, const void *b) {
|
||||
|
||||
Test(dynamic_arrays, contains_cmp_key_value_map) {
|
||||
with_borrow(alloc) {
|
||||
kv_pair_t *map = make_arr(alloc, kv_pair_t, .initial_capacity = 10);
|
||||
kv_pair_t *map = make_arr(kv_pair_t, alloc, .initial_capacity = 10);
|
||||
|
||||
// Add some key-value pairs
|
||||
arr_append(map, ((kv_pair_t){.key = 1, .value = "one"}));
|
||||
@@ -295,7 +176,7 @@ Test(dynamic_arrays, contains_cmp_key_value_map) {
|
||||
|
||||
Test(dynamic_arrays, get_valid_indices) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int);
|
||||
int *arr = make_arr(int, alloc);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
arr_append(arr, 30);
|
||||
@@ -310,7 +191,7 @@ Test(dynamic_arrays, get_valid_indices) {
|
||||
|
||||
Test(dynamic_arrays, set_valid_indices) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int);
|
||||
int *arr = make_arr(int, alloc);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
arr_append(arr, 30);
|
||||
@@ -327,36 +208,26 @@ Test(dynamic_arrays, set_valid_indices) {
|
||||
|
||||
Test(dynamic_arrays, get_out_of_bounds, .exit_code = 1) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int);
|
||||
int *arr = make_arr(int, alloc);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
|
||||
(void)arr_get(arr, 2);
|
||||
dyn_array_bounds_check_func( arr, 2, __FILE__, __LINE__, false);
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, set_out_of_bounds, .exit_code = 1) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int);
|
||||
int *arr = make_arr(int, alloc);
|
||||
arr_append(arr, 10);
|
||||
arr_append(arr, 20);
|
||||
|
||||
arr_set(arr, 2, 999);
|
||||
dyn_array_bounds_check_func( arr, 2, __FILE__, __LINE__, false);
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, get_empty_array, .exit_code = 1) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int);
|
||||
(void)arr_get(arr, 0);
|
||||
int *arr = make_arr(int, alloc);
|
||||
dyn_array_bounds_check_func( arr, 0, __FILE__, __LINE__, false);
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, set_empty_array, .exit_code = 1) {
|
||||
with_borrow(alloc) {
|
||||
int *arr = make_arr(alloc, int);
|
||||
arr_set(arr, 0, 42);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user