shithub: ircd

ref: 564f0cc5963d4055fe4ac30e6d4f6a63e9ca086b
dir: /test/lltest.c/

View raw version
#include <u.h>
#include <libc.h>
#include "../ll.h"

Linked *list = nil;

int fail = 0;

static int
find(void *ptr, void *aux)
{
	return ptr == aux;
}

static void
destr(void *ptr)
{
	char c = (char)ptr;
	fprint(2, "fail: destructor called for %c\n", c);
	fail = 1;
}

static void
test(char c, int exp)
{
	char r = (char)lfind(&list, find, (void*)c);
	char found = r == c;
	if (found) {
		if (!exp) {
			fail = 1;
			goto Fail;
		}
	} else {
		if (exp) {
			fail = 1;
			goto Fail;
		}
	}
	return;
Fail:
	fprint(2, "fail: %c was%s expected, but was%s found!\n", c,
		exp ? "" : " not",
		found ? "" : " not"
	);
}

static void
testall(int a, int b, int c, int d)
{
	test('a', a);
	test('b', b);
	test('c', c);
	test('d', d);
}

void
main(int, char**)
{
	testall(0, 0, 0, 0);
	ladd(&list, (void*)'a');
	testall(1, 0, 0, 0);
	ladd(&list, (void*)'b');
	testall(1, 1, 0, 0);
	ladd(&list, (void*)'c');
	testall(1, 1, 1, 0);
	
	ldel(&list, (void*)'b', nil);
	testall(1, 0, 1, 0);
	ladd(&list, (void*)'b');
	testall(1, 1, 1, 0);
	
	ldel(&list, (void*)'a', nil);
	testall(0, 1, 1, 0);
	
	ldel(&list, (void*)'c', nil);
	testall(0, 1, 0, 0);
	
	ldel(&list, (void*)'c', destr);
	
	exits(fail ? "failed" : nil);
}