First bit of ex35
This commit is contained in:
parent
64dfc0cb6f
commit
fcf1c92608
@ -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
|
||||
|
||||
|
19
lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.c
Normal file
19
lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <lcthw/darray_algos.h>
|
||||
#include <stdlib.h>
|
||||
#include <bsd/stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
14
lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.h
Normal file
14
lcthw-remnants-2/liblcthw/src/lcthw/darray_algos.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef lcthw_DArray_algos_h
|
||||
#define lcthw_DArray_algos_h
|
||||
|
||||
#include <lcthw/darray.h>
|
||||
|
||||
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
|
78
lcthw-remnants-2/liblcthw/tests/lcthw/darray_algos_tests.c
Normal file
78
lcthw-remnants-2/liblcthw/tests/lcthw/darray_algos_tests.c
Normal file
@ -0,0 +1,78 @@
|
||||
#include "../minunit.h"
|
||||
#include <lcthw/darray_algos.h>
|
||||
|
||||
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);
|
Loading…
Reference in New Issue
Block a user