102 lines
3.1 KiB
C
102 lines
3.1 KiB
C
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "ds.h"
|
|
|
|
#define test(name, tests) do { \
|
|
const char *_test_name = name; \
|
|
int _tests_passed = 0; \
|
|
int _tests = 0; \
|
|
tests \
|
|
_tests_passed += _tests; \
|
|
printf("%s: %d/%d tests passed\n", name, _tests_passed, _tests); \
|
|
} while (0)
|
|
|
|
#define it(message, test) do { \
|
|
_tests++; \
|
|
if (!(test)) { \
|
|
printf("%s %s FAILED @ %s:%d\n", _test_name, message, __FILE__, __LINE__); \
|
|
printf(" -> test condition: (%s)\n", #test); \
|
|
_tests_passed--; \
|
|
} \
|
|
} while (0)
|
|
|
|
typedef struct {
|
|
int num;
|
|
char *str;
|
|
} complex;
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
test("sll",
|
|
char *a = "a";
|
|
char *b = "b";
|
|
|
|
ds_sll_t *tmp;
|
|
it("creates a new linked list", tmp = ds_sll_init());
|
|
free(tmp);
|
|
|
|
ds_sll_t *ll = ds_sll_new_node(a);
|
|
it("appends an invalid item", ds_sll_append(ll, NULL) == -1);
|
|
it("removes an invalid index", ds_sll_remove(&ll, 1234) == NULL);
|
|
it("adds an item to the beginning", ds_sll_add(&ll, b, 0) == 0);
|
|
it("removes an item", b == ds_sll_remove(&ll, 0));
|
|
ds_sll_remove(&ll, 0);
|
|
);
|
|
|
|
test("dll",
|
|
char *a = "a";
|
|
char *b = "b";
|
|
|
|
ds_dll_t *tmp;
|
|
it("creates a new linked list", tmp = ds_dll_init());
|
|
free(tmp);
|
|
|
|
ds_dll_t *ll = ds_dll_new_node(a);
|
|
it("appends an invalid item", ds_dll_append(ll, NULL) == -1);
|
|
it("removes an invalid index", ds_dll_remove(&ll, 1234) == NULL);
|
|
it("adds an item to the beginning", ds_dll_add(&ll, b, 0) == 0);
|
|
it("removes an item", b == ds_dll_remove(&ll, 0));
|
|
ds_dll_remove(&ll, 0);
|
|
);
|
|
|
|
test("hmp",
|
|
complex *a = calloc(1, sizeof(complex));
|
|
a->num = 1;
|
|
a->str = "abc";
|
|
|
|
ds_hmp_t *hmp = ds_hmp_init(101);
|
|
it("inserts an item", ds_hmp_insert(hmp, a->str, a) == 0);
|
|
it("generates a hash", _ds_hmp_gen_hash("a") == 177670);
|
|
it("removes a non-existent item", ds_hmp_remove(hmp, "???") == NULL);
|
|
it("removes an invalid item", ds_hmp_remove(hmp, NULL) == NULL);
|
|
it("removes an item", a == ds_hmp_remove(hmp, a->str));
|
|
ds_hmp_free(&hmp, NULL);
|
|
|
|
free(a);
|
|
);
|
|
|
|
test("stack",
|
|
char *a = "a";
|
|
char *b = "b";
|
|
char *c = "c";
|
|
|
|
ds_stack_t *stack = ds_stack_init();
|
|
it("pushes an item", ds_stack_push(stack, a) == 0);
|
|
it("pops an item", ds_stack_pop(stack) == a);
|
|
|
|
ds_stack_push(stack, b);
|
|
ds_stack_push(stack, c);
|
|
|
|
it("peeks ahead", ds_stack_peek(stack, 0) == c);
|
|
it("peeks past the stack", ds_stack_peek(stack, INT_MAX) == NULL);
|
|
|
|
ds_stack_pop(stack);
|
|
ds_stack_pop(stack);
|
|
|
|
free(stack);
|
|
);
|
|
}
|