feat(tests): add tests for all currently existing data structures

This commit is contained in:
2025-09-06 02:27:04 -04:00
parent 1a9b135f01
commit 6e98f293aa

89
test.c
View File

@@ -1,42 +1,73 @@
#include "ds.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct complex {
#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[])
{
char *a = "one";
char *b = "two";
ds_sll_t *ll = ds_sll_new_node(a); // [ "one" ]
ds_sll_insert(ll, b); // [ "one", "two" ]
ds_ll_foreach(ds_sll_t, ll) {
puts(cur->data);
}
ds_sll_remove(&ll, 1234);
char *pa = ds_sll_remove(&ll, 0); // [ "two" ]
char *pb = ds_sll_remove(&ll, 0); // [ ]
test("sll",
char *a = "one";
struct complex *a2 = calloc(1, sizeof(struct complex));
a2->num = 1;
a2->str = "abc";
struct complex *b2 = calloc(1, sizeof(struct complex));
b2->num = 2;
b2->str = "def";
ds_sll_t *tmp;
it("ensures creates a new linked list", tmp = ds_sll_init());
free(tmp);
ds_hmp_t *hmp = ds_hmp_init(101);
ds_hmp_insert(hmp, a2->str, a2); // [ (23)[ [ a->str, a ] ] ]
ds_hmp_insert(hmp, b2->str, b2); // [ (23)[ [ a->str, a ] ], (58)[ [ b->str, b ] ]
struct complex *pa2 = ds_hmp_remove(hmp, a2->str); // [ (23)[ [ a->str, a ] ]
struct complex *pb2 = ds_hmp_remove(hmp, b2->str); // [ (58)[ [ b->str, b ] ]
ds_hmp_free(&hmp);
puts(pa2->str);
puts(pb2->str);
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));
);
free(pa2);
free(pb2);
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);
);
}