Move tons of stuff into oldstuff/
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
#include "../minunit.h"
|
||||
#include <lcthw/bstrlib.h>
|
||||
|
||||
char *test_bfromcstr()
|
||||
{
|
||||
bstring b = bfromcstr("oh hai");
|
||||
mu_assert(b != NULL, "bstring is NULL.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_blk2bstr()
|
||||
{
|
||||
bstring b = blk2bstr("hats", 5);
|
||||
mu_assert(b != NULL, "bstring is NULL.");
|
||||
|
||||
b = blk2bstr(NULL, 42);
|
||||
mu_assert(b == NULL, "bstring is not NULL.");
|
||||
|
||||
b = blk2bstr("bats", -17);
|
||||
mu_assert(b == NULL, "bstring is not NULL.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_bstrcpy()
|
||||
{
|
||||
bstring b = bfromcstr("mats");
|
||||
mu_assert(bstrcpy(b) != NULL, "bstring is NULL.");
|
||||
|
||||
mu_assert(bstrcpy(NULL) == NULL, "bstring is not NULL.");
|
||||
|
||||
int orig_len = b->slen;
|
||||
b->slen = -1;
|
||||
mu_assert(bstrcpy(b) == NULL, "bstring is not NULL.");
|
||||
b->slen = orig_len;
|
||||
|
||||
b->data = NULL;
|
||||
mu_assert(bstrcpy(b) == NULL, "bstring is not NULL.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *all_tests()
|
||||
{
|
||||
mu_suite_start();
|
||||
|
||||
mu_run_test(test_bfromcstr);
|
||||
mu_run_test(test_blk2bstr);
|
||||
mu_run_test(test_bstrcpy);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RUN_TESTS(all_tests);
|
@@ -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);
|
124
oldstuff/lcthw-remnants-2/liblcthw/tests/lcthw/darray_tests.c
Normal file
124
oldstuff/lcthw-remnants-2/liblcthw/tests/lcthw/darray_tests.c
Normal file
@@ -0,0 +1,124 @@
|
||||
#include "../minunit.h"
|
||||
#include <lcthw/darray.h>
|
||||
|
||||
static DArray *array = NULL;
|
||||
static int *val1 = NULL;
|
||||
static int *val2 = NULL;
|
||||
|
||||
char *test_create()
|
||||
{
|
||||
array = DArray_create(sizeof(int), 100);
|
||||
mu_assert(array != NULL, "DArray_create failed.");
|
||||
mu_assert(array->contents != NULL, "contents are wrong in darray");
|
||||
mu_assert(array->end == 0, "end isn't at the right spot");
|
||||
mu_assert(array->element_size == sizeof(int), "element size is wrong.");
|
||||
mu_assert(array->max == 100, "wrong max length on initial size");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_destroy()
|
||||
{
|
||||
DArray_destroy(array);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_new()
|
||||
{
|
||||
val1 = DArray_new(array);
|
||||
mu_assert(val1 != NULL, "failed to make a new element");
|
||||
|
||||
val2 = DArray_new(array);
|
||||
mu_assert(val2 != NULL, "failed to make a new element");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_set()
|
||||
{
|
||||
DArray_set(array, 0, val1);
|
||||
DArray_set(array, 1, val2);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_get()
|
||||
{
|
||||
mu_assert(DArray_get(array, 0) == val1, "Wrong first value.");
|
||||
mu_assert(DArray_get(array, 1) == val2, "Wrong second value.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_remove()
|
||||
{
|
||||
int *val_check = DArray_remove(array, 0);
|
||||
mu_assert(val_check != NULL, "Should not get NULL.");
|
||||
mu_assert(*val_check == *val1, "Should get the first value.");
|
||||
mu_assert(DArray_get(array, 0) == NULL, "Should be gone.");
|
||||
DArray_free(val_check);
|
||||
|
||||
val_check = DArray_remove(array, 1);
|
||||
mu_assert(val_check != NULL, "Should not get NULL.");
|
||||
mu_assert(*val_check == *val2, "Should get the second value.");
|
||||
mu_assert(DArray_get(array, 1) == NULL, "Should be gone.");
|
||||
DArray_free(val_check);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_expand_contract()
|
||||
{
|
||||
int old_max = array->max;
|
||||
DArray_expand(array);
|
||||
mu_assert((unsigned int)array->max == old_max + array->expand_rate, "Wrong size after expand.");
|
||||
|
||||
DArray_contract(array);
|
||||
mu_assert((unsigned int)array->max == array->expand_rate + 1, "Should stay at the expand_rate at least.");
|
||||
|
||||
DArray_contract(array);
|
||||
mu_assert((unsigned int)array->max == array->expand_rate + 1, "Should stay at the expand_rate at least.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_push_pop()
|
||||
{
|
||||
int i = 0;
|
||||
for(i = 0; i < 1000; i++) {
|
||||
int *val = DArray_new(array);
|
||||
*val = i * 333;
|
||||
DArray_push(array, val);;
|
||||
}
|
||||
|
||||
mu_assert(array->max == 1201, "Wrong max size.");
|
||||
|
||||
for(i = 999; i >= 0; i--) {
|
||||
int *val = DArray_pop(array);
|
||||
mu_assert(val != NULL, "Shouldn't get a NULL.");
|
||||
mu_assert(*val == i * 333, "Wrong value.");
|
||||
DArray_free(val);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *all_tests()
|
||||
{
|
||||
mu_suite_start();
|
||||
|
||||
mu_run_test(test_create);
|
||||
mu_run_test(test_new);
|
||||
mu_run_test(test_set);
|
||||
mu_run_test(test_get);
|
||||
mu_run_test(test_remove);
|
||||
mu_run_test(test_expand_contract);
|
||||
mu_run_test(test_push_pop);
|
||||
mu_run_test(test_destroy);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RUN_TESTS(all_tests);
|
@@ -0,0 +1,91 @@
|
||||
#include "../minunit.h"
|
||||
#include <lcthw/list_algos.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
char *values[] = {"XXXX", "1234", "abcd", "xjvef", "NDSS"};
|
||||
#define NUM_VALUES 5
|
||||
|
||||
List *create_words()
|
||||
{
|
||||
int i = 0;
|
||||
List *words = List_create();
|
||||
|
||||
for(i = 0; i < NUM_VALUES; i++) {
|
||||
List_push(words, values[i]);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
int is_sorted(List *words)
|
||||
{
|
||||
LIST_FOREACH(words, first, next, cur) {
|
||||
if(cur->next && strcmp(cur->value, cur->next->value) > 0) {
|
||||
debug("%s %s", (char *)cur->value, (char *)cur->next->value);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *test_bubble_sort()
|
||||
{
|
||||
List *words = create_words();
|
||||
|
||||
// should work on a list that needs sorting
|
||||
int rc = List_bubble_sort(words, (List_compare)strcmp);
|
||||
mu_assert(rc == 0, "Bubble sort failed.");
|
||||
mu_assert(is_sorted(words), "Words are not sorted after bubble sort.");
|
||||
|
||||
// should work on an already sorted list
|
||||
rc = List_bubble_sort(words, (List_compare)strcmp);
|
||||
mu_assert(rc == 0, "Bubble sort of already sorted failed.");
|
||||
mu_assert(is_sorted(words), "Words should be sorted if already bubble sorted.");
|
||||
|
||||
List_destroy(words);
|
||||
|
||||
// should work on an empty list
|
||||
words = List_create();
|
||||
rc = List_bubble_sort(words, (List_compare)strcmp);
|
||||
mu_assert(rc == 0, "Bubble sort failed on empty list.");
|
||||
mu_assert(is_sorted(words), "Words should be sorted if empty.");
|
||||
|
||||
List_destroy(words);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_merge_sort()
|
||||
{
|
||||
List *words = create_words();
|
||||
|
||||
// should work on a list that needs sorting
|
||||
List *res = List_merge_sort(words, (List_compare)strcmp);
|
||||
mu_assert(List_count(res) == List_count(words), "Sorted list has different count.");
|
||||
mu_assert(is_sorted(res), "Words are not sorted after merge sort.");
|
||||
|
||||
List *res2 = List_merge_sort(res, (List_compare)strcmp);
|
||||
mu_assert(List_count(res2) == List_count(res), "Sorted list has different count.");
|
||||
mu_assert(is_sorted(res), "Should still be sorted after merge sort.");
|
||||
|
||||
List_destroy(res2);
|
||||
List_destroy(res);
|
||||
|
||||
List_destroy(words);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *all_tests()
|
||||
{
|
||||
mu_suite_start();
|
||||
|
||||
mu_run_test(test_bubble_sort);
|
||||
mu_run_test(test_merge_sort);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RUN_TESTS(all_tests);
|
205
oldstuff/lcthw-remnants-2/liblcthw/tests/lcthw/list_tests.c
Normal file
205
oldstuff/lcthw-remnants-2/liblcthw/tests/lcthw/list_tests.c
Normal file
@@ -0,0 +1,205 @@
|
||||
#include "../minunit.h"
|
||||
#include <lcthw/list.h>
|
||||
#include <assert.h>
|
||||
|
||||
static List *list = NULL;
|
||||
char *test1 = "test1 data";
|
||||
char *test2 = "test2 data";
|
||||
char *test3 = "test3 data";
|
||||
char *test4 = "test4 data";
|
||||
|
||||
|
||||
char *test_create()
|
||||
{
|
||||
list = List_create();
|
||||
mu_assert(list != NULL, "Failed to create list.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *test_destroy()
|
||||
{
|
||||
List_clear_destroy(list);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *test_push_pop()
|
||||
{
|
||||
List_push(list, test1);
|
||||
mu_assert(List_last(list) == test1, "Wrong last value.");
|
||||
|
||||
List_push(list, test2);
|
||||
mu_assert(List_last(list) == test2, "Wrong last value");
|
||||
|
||||
List_push(list, test3);
|
||||
mu_assert(List_last(list) == test3, "Wrong last value");
|
||||
mu_assert(List_count(list) == 3, "Wrong count on push.");
|
||||
|
||||
char *val = List_pop(list);
|
||||
mu_assert(val == test3, "Wrong value on pop.");
|
||||
|
||||
val = List_pop(list);
|
||||
mu_assert(val == test2, "Wrong value on pop.");
|
||||
|
||||
val = List_pop(list);
|
||||
mu_assert(val == test1, "Wrong value on pop.");
|
||||
mu_assert(List_count(list) == 0, "Wrong count after pop.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_unshift()
|
||||
{
|
||||
List_unshift(list, test1);
|
||||
mu_assert(List_first(list) == test1, "Wrong first value.");
|
||||
|
||||
List_unshift(list, test2);
|
||||
mu_assert(List_first(list) == test2, "Wrong first value.");
|
||||
|
||||
List_unshift(list, test3);
|
||||
mu_assert(List_first(list) == test3, "Wrong first value.");
|
||||
mu_assert(List_count(list) == 3, "Wrong count on unshift.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_remove()
|
||||
{
|
||||
// we only need to test the middle remove case since push/shift
|
||||
// already tests the other cases
|
||||
|
||||
char *val = List_remove(list, list->first->next);
|
||||
mu_assert(val == test2, "Wrong removed element.");
|
||||
mu_assert(List_count(list) == 2, "Wrong count after remove.");
|
||||
mu_assert(List_first(list) == test3, "Wrong first after remove.");
|
||||
mu_assert(List_last(list) == test1, "Wrong last after remove.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *test_shift()
|
||||
{
|
||||
mu_assert(List_count(list) != 0, "Wrong count before shift.");
|
||||
|
||||
char *val = List_shift(list);
|
||||
mu_assert(val == test3, "Wrong value on shift.");
|
||||
|
||||
val = List_shift(list);
|
||||
mu_assert(val == test1, "Wrong value on shift.");
|
||||
mu_assert(List_count(list) == 0, "Wrong count after shift.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *test_copy()
|
||||
{
|
||||
list = List_create();
|
||||
|
||||
mu_assert(List_count(list) == 0, "Wrong count before copy.");
|
||||
List_push(list, test1);
|
||||
List_push(list, test2);
|
||||
List_push(list, test3);
|
||||
List_push(list, test4);
|
||||
|
||||
mu_assert(List_count(list) == 4, "Wrong count after push.");
|
||||
|
||||
List *copy = List_copy(list);
|
||||
mu_assert(copy != list, "Copy and list have same address.");
|
||||
mu_assert(List_count(copy) == 4, "Copy has wrong count.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *test_split()
|
||||
{
|
||||
mu_assert(List_count(list) == 4, "Wrong count before split.");
|
||||
|
||||
List *a = List_create();
|
||||
List *b = List_create();
|
||||
List *tmp = List_copy(list);
|
||||
|
||||
int rc = -1;
|
||||
rc = List_split(tmp, test2, a, b);
|
||||
mu_assert(rc == 0, "Failed to split.");
|
||||
mu_assert(List_count(a) == 2, "List 'a' has wrong count.");
|
||||
mu_assert(List_count(b) == 2, "List 'b' has wrong count.");
|
||||
|
||||
List_destroy(a);
|
||||
List_destroy(b);
|
||||
|
||||
a = List_create();
|
||||
b = List_create();
|
||||
tmp = List_copy(list);
|
||||
|
||||
rc = List_split(tmp, test1, a, b);
|
||||
mu_assert(rc == 0, "Failed to split.");
|
||||
mu_assert(List_count(a) == 1, "List 'a' has wrong count.");
|
||||
mu_assert(List_count(b) == 3, "List 'b' has wrong count.");
|
||||
|
||||
List_destroy(a);
|
||||
List_destroy(b);
|
||||
|
||||
a = List_create();
|
||||
b = List_create();
|
||||
tmp = List_copy(list);
|
||||
|
||||
rc = List_split(tmp, test3, a, b);
|
||||
mu_assert(rc == 0, "Failed to split.");
|
||||
mu_assert(List_count(a) == 3, "List 'a' has wrong count.");
|
||||
mu_assert(List_count(b) == 1, "List 'b' has wrong count.");
|
||||
|
||||
List_destroy(a);
|
||||
List_destroy(b);
|
||||
|
||||
a = List_create();
|
||||
b = List_create();
|
||||
tmp = List_copy(list);
|
||||
|
||||
rc = List_split(tmp, test4, a, b);
|
||||
mu_assert(rc == 0, "Failed to split.");
|
||||
mu_assert(List_count(a) == 4, "List 'a' has wrong count.");
|
||||
mu_assert(List_count(b) == 0, "List 'b' has wrong count.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *test_join()
|
||||
{
|
||||
mu_assert(List_count(list) == 4, "Wrong count before join.");
|
||||
|
||||
List *b = List_create();
|
||||
List_push(b, test4);
|
||||
mu_assert(List_count(b) == 1, "List 'b' has wrong count.");
|
||||
|
||||
List_join(list, b);
|
||||
mu_assert(List_count(list) == 5, "Wrong count after join.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *all_tests() {
|
||||
mu_suite_start();
|
||||
|
||||
mu_run_test(test_create);
|
||||
mu_run_test(test_push_pop);
|
||||
mu_run_test(test_unshift);
|
||||
mu_run_test(test_remove);
|
||||
mu_run_test(test_shift);
|
||||
mu_run_test(test_destroy);
|
||||
mu_run_test(test_copy);
|
||||
mu_run_test(test_split);
|
||||
mu_run_test(test_join);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RUN_TESTS(all_tests);
|
105
oldstuff/lcthw-remnants-2/liblcthw/tests/lcthw/radixmap_tests.c
Normal file
105
oldstuff/lcthw-remnants-2/liblcthw/tests/lcthw/radixmap_tests.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "../minunit.h"
|
||||
#include <stdint.h>
|
||||
#include <lcthw/radixmap.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
static int make_random(RadixMap *map)
|
||||
{
|
||||
size_t i = 0;
|
||||
|
||||
for(i = 0; i < map->max - 1; i++) {
|
||||
uint32_t key = (uint32_t)(rand() | (rand() << 16));
|
||||
check(RadixMap_add(map, key, i) == 0, "Failed to add key %u", key);
|
||||
}
|
||||
|
||||
return i;
|
||||
|
||||
error:
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int check_order(RadixMap *map)
|
||||
{
|
||||
RMElement d1, d2;
|
||||
unsigned int i = 0;
|
||||
|
||||
// only signal errors if any (should not be)
|
||||
for(i = 0; map->end > 0 && i < map->end-1; i++) {
|
||||
d1 = map->contents[i];
|
||||
d2 = map->contents[i+1];
|
||||
|
||||
if(d1.data.key > d2.data.key) {
|
||||
debug("FAIL:i=%u, key: %u, value: %u, equals max? %d\n", i, d1.data.key, d1.data.value,
|
||||
d2.data.key == UINT32_MAX);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_search(RadixMap *map)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
RMElement *d = NULL;
|
||||
RMElement *found = NULL;
|
||||
|
||||
for(i = map->end / 2; i < map->end; i++) {
|
||||
d = &map->contents[i];
|
||||
found = RadixMap_find(map, d->data.key);
|
||||
check(found != NULL, "Didn't find %u at %u.", d->data.key, i);
|
||||
check(found->data.key == d->data.key, "Got the wrong result: %p:%u looking for %u at %u",
|
||||
found, found->data.key, d->data.key, i);
|
||||
}
|
||||
|
||||
return 1;
|
||||
error:
|
||||
return 0;
|
||||
}
|
||||
|
||||
// test for big number of elements
|
||||
static char *test_operations()
|
||||
{
|
||||
size_t N = 200;
|
||||
|
||||
RadixMap *map = RadixMap_create(N);
|
||||
mu_assert(map != NULL, "Failed to make the map.");
|
||||
mu_assert(make_random(map), "Didn't make a random fake radix map.");
|
||||
|
||||
RadixMap_sort(map);
|
||||
mu_assert(check_order(map), "Failed to properly sort the RadixMap.");
|
||||
|
||||
mu_assert(test_search(map), "Failed the search test.");
|
||||
mu_assert(check_order(map), "RadixMap didn't stay sorted after search.");
|
||||
|
||||
while(map->end > 0) {
|
||||
RMElement *el = RadixMap_find(map, map->contents[map->end / 2].data.key);
|
||||
mu_assert(el != NULL, "Should get a result.");
|
||||
|
||||
size_t old_end = map->end;
|
||||
|
||||
mu_assert(RadixMap_delete(map, el) == 0, "Didn't delete it.");
|
||||
mu_assert(old_end - 1 == map->end, "Wrong size after delete.");
|
||||
|
||||
// test that the end is now the old value, but uint32 max so it trails off
|
||||
mu_assert(check_order(map), "RadixMap didn't stay sorted after delete.");
|
||||
}
|
||||
|
||||
RadixMap_destroy(map);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *all_tests()
|
||||
{
|
||||
mu_suite_start();
|
||||
srand(time(NULL));
|
||||
|
||||
mu_run_test(test_operations);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RUN_TESTS(all_tests);
|
Reference in New Issue
Block a user