diff --git a/lcthw-remnants-2/liblcthw/Makefile b/lcthw-remnants-2/liblcthw/Makefile index d908dbc..a167262 100644 --- a/lcthw-remnants-2/liblcthw/Makefile +++ b/lcthw-remnants-2/liblcthw/Makefile @@ -33,7 +33,7 @@ build: # The Unit Tests .PHONY: tests -tests: LDLIBS += -static -l$(LIBNAME) +tests: LDLIBS += -static -l$(LIBNAME) -lbsd tests: ./tests/runtests $(TESTS) ./tests/runtests ./tests/lcthw diff --git a/lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.c b/lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.c new file mode 100644 index 0000000..99ab122 --- /dev/null +++ b/lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int DArray_qsort(DArray *array, DArray_compare cmp) +{ + qsort(array->contents, DArray_count(array), sizeof(void *), cmp); + return 0; +} + +int DArray_heapsort(DArray *array, DArray_compare cmp) +{ + return heapsort(array->contents, DArray_count(array), sizeof(void *), cmp); +} + +int DArray_mergesort(DArray *array, DArray_compare cmp) +{ + return mergesort(array->contents, DArray_count(array), sizeof(void *), cmp); +} diff --git a/lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.h b/lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.h new file mode 100644 index 0000000..f9c0c10 --- /dev/null +++ b/lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.h @@ -0,0 +1,14 @@ +#ifndef lcthw_DArray_algos_h +#define lcthw_DArray_algos_h + +#include + +typedef int (*DArray_compare)(const void *a, const void *b); + +int DArray_qsort(DArray *array, DArray_compare cmp); + +int DArray_heapsort(DArray *array, DArray_compare cmp); + +int DArray_mergesort(DArray *array, DArray_compare cmp); + +#endif diff --git a/lcthw-remnants-2/liblcthw/tests/lcthw/darray_algos_tests.c b/lcthw-remnants-2/liblcthw/tests/lcthw/darray_algos_tests.c new file mode 100644 index 0000000..5a00e86 --- /dev/null +++ b/lcthw-remnants-2/liblcthw/tests/lcthw/darray_algos_tests.c @@ -0,0 +1,78 @@ +#include "../minunit.h" +#include + +int testcmp(char **a, char **b) +{ + return strcmp(*a, *b); +} + +DArray *create_words() +{ + DArray *result = DArray_create(0, 5); + char *words[] = {"asdfasfd", "werwar", "13234", "asdfasfd", "oioj"}; + int i = 0; + + for(i = 0; i < 5; i++) { + DArray_push(result, words[i]); + } + + return result; +} + +int is_sorted(DArray *array) +{ + int i = 0; + + for(i = 0; i < DArray_count(array) - 1; i++) { + if(strcmp(DArray_get(array, i), DArray_get(array, i+1)) > 0) { + return 0; + } + } + + return 1; +} + +char *run_sort_test(int (*func)(DArray *, DArray_compare), const char *name) +{ + DArray *words = create_words(); + mu_assert(!is_sorted(words), "Words should start not sorted."); + + name = name; + debug("--- Testing %s sorting algorithm", name); + int rc = func(words, (DArray_compare)testcmp); + mu_assert(rc == 0, "sort failed"); + mu_assert(is_sorted(words), "didn't sort it"); + + DArray_destroy(words); + + return NULL; +} + +char *test_qsort() +{ + return run_sort_test(DArray_qsort, "qsort"); +} + +char *test_heapsort() +{ + return run_sort_test(DArray_heapsort, "heapsort"); +} + +char *test_mergesort() +{ + return run_sort_test(DArray_mergesort, "mergesort"); +} + + +char *all_tests() +{ + mu_suite_start(); + + mu_run_test(test_qsort); + mu_run_test(test_heapsort); + mu_run_test(test_mergesort); + + return NULL; +} + +RUN_TESTS(all_tests);