ds/test.c
Squibid 758edf9d30
feat(queue): introduce a queue ds, internally this is just a stack...
the only actual difference is a oneline change which makes pushing
append instead of prepend the new data.
2025-11-17 17:51:57 -05:00

123 lines
3.6 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);
);
test("queue",
char *a = "a";
char *b = "b";
char *c = "c";
ds_queue_t *queue = ds_queue_init();
it("pushes an item", ds_queue_push(queue, a) == 0);
it("pops an item", ds_queue_pop(queue) == a);
ds_queue_push(queue, b);
ds_queue_push(queue, c);
it("peeks ahead", ds_queue_peek(queue, 0) == b);
it("peeks past the queue", ds_queue_peek(queue, INT_MAX) == NULL);
ds_queue_pop(queue);
ds_queue_pop(queue);
free(queue);
);
}