Add unit tests for sparse array functions.

This commit is contained in:
Danila Fedorin 2017-01-21 16:43:47 -08:00
parent 3aedc61da4
commit 5247394d35

View File

@ -5,6 +5,7 @@
#include "ht.h" #include "ht.h"
#include "ll.h" #include "ll.h"
#include "vec.h" #include "vec.h"
#include "sprs.h"
int _test_vec_foreach_func(void* data, va_list args) { int _test_vec_foreach_func(void* data, va_list args) {
char* real_data = data; char* real_data = data;
@ -252,6 +253,82 @@ int test_ll_poptail(){
return return_code; return return_code;
} }
int _test_sprs_foreach_count(void* data, va_list args){
(*(va_arg(args, int*)))++;
return 0;
}
int test_sprs_basic(){
sprs sprs;
sprs_init(&sprs);
sprs_setup(&sprs, 1);
sprs_free(&sprs);
return 1;
}
int test_sprs_put(){
int return_code;
sprs sprs;
sprs_init(&sprs);
sprs_setup(&sprs, 4);
sprs_put(&sprs, 2, NULL);
return_code = sprs.dense[0] == 2 && sprs.sparse[2].index == 0 && sprs.sparse[2].data == NULL;
sprs_free(&sprs);
return return_code;
}
int test_sprs_contains(){
int return_code;
sprs sprs;
sprs_init(&sprs);
sprs_setup(&sprs, 4);
sprs_put(&sprs, 2, NULL);
sprs_put(&sprs, 3, NULL);
return_code = (sprs_contains(&sprs, 2) == 1) && (sprs_contains(&sprs, 3) == 1) && (sprs_contains(&sprs, 0) == 0);
sprs_free(&sprs);
return return_code;
}
int test_sprs_get(){
int return_code;
sprs sprs;
char* test_string = "test";
sprs_init(&sprs);
sprs_setup(&sprs, 4);
sprs_put(&sprs, 2, test_string);
sprs_put(&sprs, 3, NULL);
return_code = sprs_get(&sprs, 2) == test_string && sprs_get(&sprs, 3) == NULL && sprs_get(&sprs, 0) == NULL;
sprs_free(&sprs);
return return_code;
}
int test_sprs_find(){
int return_code;
sprs sprs;
char* test_string = "test";
sprs_init(&sprs);
sprs_setup(&sprs, 4);
sprs_put(&sprs, 3, test_string);
return_code = sprs_find(&sprs, test_string, compare_pointer) == test_string;
sprs_free(&sprs);
return return_code;
}
int test_sprs_foreach(){
int return_code;
sprs sprs;
int count = 0;
sprs_init(&sprs);
sprs_setup(&sprs, 4);
sprs_put(&sprs, 0, NULL);
sprs_put(&sprs, 2, NULL);
sprs_put(&sprs, 3, NULL);
sprs_foreach(&sprs, NULL, compare_always, _test_sprs_foreach_count, &count);
return_code = count == 3;
sprs_free(&sprs);
return return_code;
}
int run_test(char* test_name, int (*test_func)()) { int run_test(char* test_name, int (*test_func)()) {
int success = test_func(); int success = test_func();
printf("Running test %-15s . . . ", test_name); printf("Running test %-15s . . . ", test_name);
@ -260,18 +337,18 @@ int run_test(char* test_name, int (*test_func)()) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
char* test_names[19] = {"vec_basic", "vec_add", "vec_remove", "vec_find", char* test_names[25] = {"vec_basic", "vec_add", "vec_remove", "vec_find",
"vec_foreach", "vec_index", "ht_basic", "ht_put", "vec_foreach", "vec_index", "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"}; "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[19])() = { int (*test_functions[25])() = {
test_vec_basic, test_vec_add, test_vec_remove, test_vec_find, 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_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_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 test_index = 0;
int result = 1; int result = 1;
for (; test_index < 19 && result; test_index++) { for (; test_index < 25 && result; test_index++) {
result = run_test(test_names[test_index], test_functions[test_index]); result = run_test(test_names[test_index], test_functions[test_index]);
} }
return result ? EXIT_SUCCESS : EXIT_FAILURE; return result ? EXIT_SUCCESS : EXIT_FAILURE;