libds/src/main.c

279 lines
7.7 KiB
C
Raw Normal View History

#include <stdarg.h>
#include <stdio.h>
2016-12-21 14:50:32 -08:00
#include <stdlib.h>
#include <string.h>
2016-12-21 14:37:21 -08:00
#include "ht.h"
2016-12-23 22:39:10 -08:00
#include "ll.h"
2016-12-21 14:50:32 -08:00
#include "vec.h"
2016-12-21 14:50:32 -08:00
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 == LIBDS_SUCCESS;
2016-12-21 14:50:32 -08:00
}
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) == LIBDS_SUCCESS;
2016-12-21 14:50:32 -08:00
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;
2016-12-21 14:37:21 -08:00
}
int test_ht_basic() {
2016-12-21 14:50:32 -08:00
ht test_ht;
ht_init(&test_ht);
ht_free(&test_ht);
return 1;
2016-12-21 14:37:21 -08:00
}
int test_ht_put() {
2016-12-21 14:50:32 -08:00
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;
2016-12-21 14:37:21 -08:00
}
int test_ht_get() {
2016-12-21 14:50:32 -08:00
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;
char* test_one = "one";
char* test_two = "two";
2016-12-23 22:39:10 -08:00
ht_init(&test_ht);
2016-12-21 14:50:32 -08:00
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;
2016-12-21 14:37:21 -08:00
}
2016-12-23 22:39:10 -08:00
int _test_ll_foreach_count(void* data, va_list args){
(*va_arg(args, int*))++;
return 0;
}
int test_ll_basic() {
ll test_ll;
ll_init(&test_ll);
ll_free(&test_ll);
return 1;
}
int test_ll_append() {
int return_code;
ll test_ll;
char* test_string = "test";
ll_init(&test_ll);
ll_append(&test_ll, test_string);
return_code = test_ll.head->data == test_string && test_ll.tail->data == test_string;
ll_free(&test_ll);
return return_code;
}
int test_ll_prepend() {
int return_code;
ll test_ll;
char* test_string = "test";
ll_init(&test_ll);
ll_prepend(&test_ll, test_string);
return_code = test_ll.head->data == test_string && test_ll.tail->data == test_string;
ll_free(&test_ll);
return return_code;
}
int test_ll_remove() {
int return_code;
ll test_ll;
char* test_string = "test";
ll_init(&test_ll);
ll_prepend(&test_ll, test_string);
ll_prepend(&test_ll, test_string);
ll_remove(&test_ll, test_string);
return_code = test_ll.tail == NULL && test_ll.head == NULL;
ll_free(&test_ll);
return return_code;
}
int test_ll_find() {
int return_code;
ll test_ll;
char* test_string = "test";
ll_init(&test_ll);
ll_prepend(&test_ll, test_string);
return_code = ll_find(&test_ll, "test", _test_vec_find_string) == test_string;
ll_free(&test_ll);
return return_code;
}
int test_ll_foreach(){
int return_code;
int count = 0;
ll test_ll;
char* test_string = "test";
ll_init(&test_ll);
ll_append(&test_ll, test_string);
ll_append(&test_ll, test_string);
ll_foreach(&test_ll, NULL, compare_always, _test_ll_foreach_count, &count);
return_code = count == 2;
ll_free(&test_ll);
return return_code;
}
int test_ll_pophead(){
int return_code;
ll test_ll;
char* test_string_one = "test 1";
char* test_string_two = "test 2";
ll_init(&test_ll);
ll_append(&test_ll, test_string_one);
ll_append(&test_ll, test_string_two);
return_code = ll_pophead(&test_ll) == test_string_one
&& ll_head(&test_ll) == ll_tail(&test_ll)
&& ll_head(&test_ll) == test_string_two;
ll_free(&test_ll);
return return_code;
}
int test_ll_poptail(){
int return_code;
ll test_ll;
char* test_string_one = "test 1";
char* test_string_two = "test 2";
ll_init(&test_ll);
ll_append(&test_ll, test_string_one);
ll_append(&test_ll, test_string_two);
return_code = ll_poptail(&test_ll) == test_string_two
&& ll_head(&test_ll) == ll_tail(&test_ll)
&& ll_head(&test_ll) == test_string_one;
ll_free(&test_ll);
return return_code;
}
2016-12-21 14:44:39 -08:00
int run_test(char* test_name, int (*test_func)()) {
2016-12-21 14:50:32 -08:00
int success = test_func();
printf("Running test %-15s . . . ", test_name);
printf("%s\n", success ? "Passed" : "Failed");
return success;
}
int main(int argc, char** argv) {
2016-12-23 22:39:10 -08:00
char* test_names[19] = {"vec_basic", "vec_add", "vec_remove", "vec_find",
2016-12-21 14:50:32 -08:00
"vec_foreach", "vec_index", "ht_basic", "ht_put",
2016-12-23 22:39:10 -08:00
"ht_get", "ht_remove", "ht_foreach", "ll_basic", "ll_append", "ll_prepend", "ll_remove", "ll_find" , "ll_foreach", "ll_pophead", "ll_poptail"};
2016-12-21 14:50:32 -08:00
2016-12-23 22:39:10 -08:00
int (*test_functions[19])() = {
2016-12-21 14:50:32 -08:00
test_vec_basic, test_vec_add, test_vec_remove, test_vec_find,
test_vec_foreach, test_vec_index, test_ht_basic, test_ht_put,
2016-12-23 22:39:10 -08:00
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};
2016-12-21 14:50:32 -08:00
int test_index = 0;
int result = 1;
2016-12-23 22:39:10 -08:00
for (; test_index < 19 && result; test_index++) {
2016-12-21 14:50:32 -08:00
result = run_test(test_names[test_index], test_functions[test_index]);
}
2016-12-21 14:44:39 -08:00
return result ? EXIT_SUCCESS : EXIT_FAILURE;
}