diff --git a/include/vec.h b/include/vec.h index 7f887c2..0d39c2c 100644 --- a/include/vec.h +++ b/include/vec.h @@ -88,5 +88,11 @@ int vec_foreach(vec* vec, void* data, compare_func compare, foreach_func foreach * @return pointer to the value, or, if the index is out of bounds (or there is nothing there) NULL. */ void* vec_index(vec* vec, int index); +/** + * Clears the vector, removing all elements from it + * but not resizing it down. + * @param vec the vector to clear. + */ +void vec_clear(vec* vec); #endif diff --git a/src/main.c b/src/main.c index 34302d6..2caf821 100644 --- a/src/main.c +++ b/src/main.c @@ -93,6 +93,22 @@ int test_vec_index() { vec_free(&test_vec); return return_code; } +int test_vec_clear(){ + vec test_vec; + void** vec_data = NULL; + int return_code; + vec_init(&test_vec); + vec_data = test_vec.data; + char* test_string = "test"; + vec_add(&test_vec, test_string); + vec_add(&test_vec, test_string); + vec_add(&test_vec, test_string); + vec_add(&test_vec, test_string); + vec_clear(&test_vec); + return_code = (vec_data[0] == vec_data[1]) && (vec_data[1] == vec_data[2]) && (vec_data[2] == vec_data[3]) && (test_vec.size == 0); + vec_free(&test_vec); + return return_code; +} int _test_ht_foreach_func(void* data, va_list args) { char* real_data = data; @@ -341,18 +357,18 @@ int run_test(char* test_name, int (*test_func)()) { } int main(int argc, char** argv) { - char* test_names[25] = {"vec_basic", "vec_add", "vec_remove", "vec_find", - "vec_foreach", "vec_index", "ht_basic", "ht_put", + char* test_names[26] = {"vec_basic", "vec_add", "vec_remove", "vec_find", + "vec_foreach", "vec_index", "vec_clear", "ht_basic", "ht_put", "ht_get", "ht_remove", "ht_foreach", "ll_basic", "ll_append", "ll_prepend", "ll_remove", "ll_find" , "ll_foreach", "ll_pophead", "ll_poptail", "sprs_basic", "sprs_put", "sprs_contains", "sprs_get", "sprs_find", "sprs_foreach"}; - int (*test_functions[25])() = { + int (*test_functions[26])() = { test_vec_basic, test_vec_add, test_vec_remove, test_vec_find, - test_vec_foreach, test_vec_index, test_ht_basic, test_ht_put, + test_vec_foreach, test_vec_index, test_vec_clear, test_ht_basic, test_ht_put, test_ht_get, test_ht_remove, test_ht_foreach, test_ll_basic, test_ll_append, test_ll_prepend, test_ll_remove, test_ll_find, test_ll_foreach, test_ll_pophead, test_ll_poptail, test_sprs_basic, test_sprs_put, test_sprs_contains, test_sprs_get, test_sprs_find, test_sprs_foreach}; int test_index = 0; int result = 1; - for (; test_index < 25 && result; test_index++) { + for (; test_index < 26 && result; test_index++) { result = run_test(test_names[test_index], test_functions[test_index]); } return result ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/src/vec.c b/src/vec.c index 277fc18..b56aa14 100644 --- a/src/vec.c +++ b/src/vec.c @@ -97,3 +97,6 @@ void* vec_index(vec* v, int index) { } return to_return; } +void vec_clear(vec* vec) { + vec->size = 0; +}