210 lines
5.3 KiB
C
210 lines
5.3 KiB
C
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "vec.h"
|
|
#include "ht.h"
|
|
|
|
int _test_vec_foreach_func(void* data, va_list args){
|
|
char* real_data = data;
|
|
int* sum = va_arg(args, int*);
|
|
|
|
*sum += *real_data;
|
|
return 0;
|
|
}
|
|
int _test_vec_find_string(void* a, void* b){
|
|
return strcmp(a, b) == 0;
|
|
}
|
|
|
|
int test_vec_basic(){
|
|
vec test_vec;
|
|
libds_result result = vec_init(&test_vec);
|
|
vec_free(&test_vec);
|
|
|
|
return result == SUCCESS;
|
|
}
|
|
int test_vec_add(){
|
|
vec test_vec;
|
|
char* test_string = "test";
|
|
int return_code;
|
|
|
|
vec_init(&test_vec);
|
|
return_code = vec_add(&test_vec, test_string) == SUCCESS;
|
|
if(return_code) return_code = *((void**) test_vec.data) == test_string;
|
|
vec_free(&test_vec);
|
|
|
|
return return_code;
|
|
}
|
|
int test_vec_remove(){
|
|
vec test_vec;
|
|
char* test_string = "test";
|
|
int return_code;
|
|
|
|
vec_init(&test_vec);
|
|
vec_add(&test_vec, test_string);
|
|
vec_remove(&test_vec, test_string);
|
|
return_code = *((void**) test_vec.data) == NULL;
|
|
vec_free(&test_vec);
|
|
|
|
return return_code;
|
|
}
|
|
int test_vec_find(){
|
|
vec test_vec;
|
|
char* test_string = "test";
|
|
int return_code;
|
|
|
|
vec_init(&test_vec);
|
|
vec_add(&test_vec, test_string);
|
|
return_code = vec_find(&test_vec, test_string, _test_vec_find_string) == test_string;
|
|
vec_free(&test_vec);
|
|
return return_code;
|
|
}
|
|
int test_vec_foreach(){
|
|
vec test_vec;
|
|
int return_code;
|
|
int sum = 0;
|
|
char* test_strings[3] = {
|
|
"1",
|
|
"2",
|
|
"3"
|
|
};
|
|
|
|
vec_init(&test_vec);
|
|
vec_add(&test_vec, test_strings[0]);
|
|
vec_add(&test_vec, test_strings[1]);
|
|
vec_add(&test_vec, test_strings[2]);
|
|
vec_foreach(&test_vec, NULL, compare_always, _test_vec_foreach_func, &sum);
|
|
return_code = test_strings[0][0] + test_strings[1][0] + test_strings[2][0] == sum;
|
|
vec_free(&test_vec);
|
|
return return_code;
|
|
}
|
|
int test_vec_index(){
|
|
vec test_vec;
|
|
int return_code;
|
|
char* test_strings[3] = {
|
|
"1",
|
|
"2",
|
|
"3"
|
|
};
|
|
|
|
vec_init(&test_vec);
|
|
vec_add(&test_vec, test_strings[0]);
|
|
vec_add(&test_vec, test_strings[1]);
|
|
vec_add(&test_vec, test_strings[2]);
|
|
return_code = vec_index(&test_vec, 1) == test_strings[1];
|
|
vec_free(&test_vec);
|
|
return return_code;
|
|
}
|
|
|
|
int _test_ht_foreach_func(void* data, va_list args){
|
|
char* real_data = data;
|
|
int* sum = va_arg(args, int*);
|
|
|
|
*sum += *real_data;
|
|
return 0;
|
|
}
|
|
|
|
int test_ht_basic() {
|
|
ht test_ht;
|
|
ht_init(&test_ht);
|
|
ht_free(&test_ht);
|
|
return 1;
|
|
}
|
|
int test_ht_put() {
|
|
int return_code;
|
|
ht test_ht;
|
|
char* test_key = "creamwove";
|
|
char* test_value = "quists";
|
|
unsigned int index = ht_default_hash_func(test_key) % LIBDS_HT_SIZE;
|
|
ht_init(&test_ht);
|
|
ht_put(&test_ht, test_key, test_value);
|
|
return_code = test_ht.data[index] && test_ht.data[index]->data == test_value;
|
|
ht_free(&test_ht);
|
|
return return_code;
|
|
}
|
|
int test_ht_get() {
|
|
int return_code;
|
|
ht test_ht;
|
|
char* test_one = "creamwove";
|
|
char* test_two = "quists";
|
|
ht_init(&test_ht);
|
|
ht_put(&test_ht, test_one, test_two);
|
|
ht_put(&test_ht, test_two, test_one);
|
|
return_code = ht_get(&test_ht, test_one) == test_two && ht_get(&test_ht, test_two) == test_one;
|
|
ht_free(&test_ht);
|
|
return return_code;
|
|
}
|
|
int test_ht_remove(){
|
|
int return_code;
|
|
ht test_ht;
|
|
char* test_one = "creamwove";
|
|
char* test_two = "quists";
|
|
unsigned int index = ht_default_hash_func(test_one) % LIBDS_HT_SIZE;
|
|
ht_init(&test_ht);
|
|
ht_put(&test_ht, test_one, test_two);
|
|
ht_put(&test_ht, test_two, test_one);
|
|
ht_remove(&test_ht, test_two);
|
|
return_code = test_ht.data[index] && test_ht.data[index]->data == test_two;
|
|
ht_free(&test_ht);
|
|
return return_code;
|
|
}
|
|
int test_ht_foreach(){
|
|
int return_code;
|
|
int sum = 0;
|
|
ht test_ht;
|
|
ht_init(&test_ht);
|
|
char* test_one = "one";
|
|
char* test_two = "two";
|
|
ht_put(&test_ht, test_one, test_two);
|
|
ht_put(&test_ht, test_two, test_one);
|
|
ht_foreach(&test_ht, NULL, compare_always, _test_ht_foreach_func, &sum);
|
|
return_code = *test_one + *test_two == sum;
|
|
ht_free(&test_ht);
|
|
return return_code;
|
|
}
|
|
|
|
int run_test(char* test_name, int (*test_func)()) {
|
|
int success = test_func();
|
|
printf("Running test %-15s . . . ", test_name);
|
|
printf("%s\n", success ? "Passed" : "Failed");
|
|
return success;
|
|
}
|
|
|
|
int main(int argc, char** argv){
|
|
|
|
char* test_names[11] = {
|
|
"vec_basic",
|
|
"vec_add",
|
|
"vec_remove",
|
|
"vec_find",
|
|
"vec_foreach",
|
|
"vec_index",
|
|
"ht_basic",
|
|
"ht_put",
|
|
"ht_get",
|
|
"ht_remove",
|
|
"ht_foreach"
|
|
};
|
|
|
|
int (*test_functions[11])() = {
|
|
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_ht_get,
|
|
test_ht_remove,
|
|
test_ht_foreach
|
|
};
|
|
|
|
int test_index = 0;
|
|
int result = 1;
|
|
for(; test_index < 11 && result; test_index++){
|
|
result = run_test(test_names[test_index], test_functions[test_index]);
|
|
}
|
|
return result ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
}
|