Files
ds/test.c

74 lines
1.9 KiB
C

#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\n", _test_name, message); \
_tests_passed--; \
} \
} while (0)
typedef struct {
int num;
char *str;
} complex;
int
main(int argc, char *argv[])
{
test("sll",
char *a = "one";
ds_sll_t *tmp;
it("ensures creates a new linked list", tmp = ds_sll_init());
free(tmp);
ds_sll_t *ll = ds_sll_new_node(a);
it("inserts an invalid item", ds_sll_insert(ll, NULL) == -1);
it("removes an invalid index", ds_sll_remove(&ll, 1234) == NULL);
it("removes an item", a == ds_sll_remove(&ll, 0));
);
test("dll",
char *a = "one";
ds_dll_t *tmp;
it("ensures creates a new linked list", tmp = ds_dll_init());
free(tmp);
ds_dll_t *ll = ds_dll_new_node(a);
it("inserts an invalid item", ds_dll_insert(ll, NULL) == -1);
it("removes an invalid index", ds_dll_remove(&ll, 1234) == NULL);
it("removes an item", a == 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);
);
}