libds/src/main.c

396 lines
11 KiB
C

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ht.h"
#include "ll.h"
#include "vec.h"
#include "sprs.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(const void* a, const 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;
}
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;
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";
char* second_test_string = "test2";
int return_code;
vec_init(&test_vec);
vec_add(&test_vec, test_string);
vec_add(&test_vec, test_string);
vec_add(&test_vec, test_string);
vec_add(&test_vec, second_test_string);
vec_remove(&test_vec, test_string);
return_code = ((void**)test_vec.data)[0] == second_test_string;
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_vec_clear(){
vec test_vec;
char* test_string = "test";
void** vec_data = NULL;
int return_code;
vec_init(&test_vec);
vec_data = test_vec.data;
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;
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;
char* test_one = "one";
char* test_two = "two";
ht_init(&test_ht);
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 _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;
}
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_put_grow(){
char* test_string_one = "test 1";
char* test_string_two = "test 2";
int return_code = 1;
sprs sprs;
sprs_init(&sprs);
sprs_setup(&sprs, 2);
sprs_put_grow(&sprs, 1, test_string_one);
return_code &= sprs.dense[0] == 1 && sprs.sparse[1].index == 0 && sprs.sparse[1].data == test_string_one;
return_code &= sprs.capacity == 2;
sprs_put_grow(&sprs, 3, test_string_two);
return_code &= sprs.dense[1] == 3 && sprs.sparse[3].index == 1 && sprs.sparse[3].data == test_string_two;
return_code &= sprs.capacity == 4;
sprs_put_grow(&sprs, 12, test_string_one);
return_code &= sprs.dense[2] == 12 && sprs.sparse[12].index == 2 && sprs.sparse[12].data == test_string_one;
return_code &= sprs.capacity == 16;
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 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[27] = {"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_put_grow", "sprs_contains", "sprs_get", "sprs_find", "sprs_foreach"};
int (*test_functions[27])() = {
test_vec_basic, test_vec_add, test_vec_remove, test_vec_find,
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_put_grow, test_sprs_contains, test_sprs_get, test_sprs_find, test_sprs_foreach};
int test_index = 0;
int result = 1;
for (; test_index < 26 && result; test_index++) {
result = run_test(test_names[test_index], test_functions[test_index]);
}
return result ? EXIT_SUCCESS : EXIT_FAILURE;
}