feat(tests): add tests for all currently existing data structures
This commit is contained in:
85
test.c
85
test.c
@@ -1,42 +1,73 @@
|
|||||||
#include "ds.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.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;
|
int num;
|
||||||
char *str;
|
char *str;
|
||||||
};
|
} complex;
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
test("sll",
|
||||||
char *a = "one";
|
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); // [ ]
|
|
||||||
|
|
||||||
struct complex *a2 = calloc(1, sizeof(struct complex));
|
ds_sll_t *tmp;
|
||||||
a2->num = 1;
|
it("ensures creates a new linked list", tmp = ds_sll_init());
|
||||||
a2->str = "abc";
|
free(tmp);
|
||||||
struct complex *b2 = calloc(1, sizeof(struct complex));
|
|
||||||
b2->num = 2;
|
ds_sll_t *ll = ds_sll_new_node(a);
|
||||||
b2->str = "def";
|
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);
|
ds_hmp_t *hmp = ds_hmp_init(101);
|
||||||
ds_hmp_insert(hmp, a2->str, a2); // [ (23)[ [ a->str, a ] ] ]
|
it("inserts an item", ds_hmp_insert(hmp, a->str, a) == 0);
|
||||||
ds_hmp_insert(hmp, b2->str, b2); // [ (23)[ [ a->str, a ] ], (58)[ [ b->str, b ] ]
|
it("generates a hash", _ds_hmp_gen_hash("a") == 177670);
|
||||||
struct complex *pa2 = ds_hmp_remove(hmp, a2->str); // [ (23)[ [ a->str, a ] ]
|
it("removes a non-existent item", ds_hmp_remove(hmp, "???") == NULL);
|
||||||
struct complex *pb2 = ds_hmp_remove(hmp, b2->str); // [ (58)[ [ b->str, b ] ]
|
it("removes an invalid item", ds_hmp_remove(hmp, NULL) == NULL);
|
||||||
ds_hmp_free(&hmp);
|
it("removes an item", a == ds_hmp_remove(hmp, a->str));
|
||||||
puts(pa2->str);
|
ds_hmp_free(&hmp, NULL);
|
||||||
puts(pb2->str);
|
|
||||||
|
|
||||||
free(pa2);
|
free(a);
|
||||||
free(pb2);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user