Add a clear function for the vector.

This commit is contained in:
Danila Fedorin 2017-06-11 21:08:39 -07:00
parent 20507f73bb
commit b1a7c21caa
3 changed files with 30 additions and 5 deletions

View File

@ -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

View File

@ -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;

View File

@ -97,3 +97,6 @@ void* vec_index(vec* v, int index) {
}
return to_return;
}
void vec_clear(vec* vec) {
vec->size = 0;
}