From 2281dcf6f29f940499e6cccb8b4e54f59daa2efe Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 7 May 2019 17:13:21 -0700 Subject: dll: initial commit of a doubly linked list header Looking to retire using the kernel's list.h, this is a minimal fast and nasty implementation I've barely tested to fulfill my immediate needs. --- test.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 test.c (limited to 'test.c') diff --git a/test.c b/test.c new file mode 100644 index 0000000..6e3ccd6 --- /dev/null +++ b/test.c @@ -0,0 +1,79 @@ +#include +#include + +#include "dll.h" + +#define NUM_NODES 100 + +DLL_INIT(foo); + +typedef struct entry_t { + dll_t node; + int id; +} entry_t; + + +int main(int argc, char *argv[]) +{ + entry_t entries[NUM_NODES], *entry, *_entry; + dll_t nodes[NUM_NODES]; + dll_t *tmp, *_tmp; + + for (int i = 0; i < NUM_NODES; i++) { + dll_pre(&foo, &nodes[i]); + printf("appended %i:%p to tail\n", i, &nodes[i]); + } + + DLL_FOR_EACH(&foo, tmp) + printf("node=%p\n", tmp); + + for (int i = 0; i < NUM_NODES; i++) { + if (!(i % 2)) + continue; + + printf("deleted %i:%p\n", i, dll_del(&nodes[i])); + } + + DLL_FOR_EACH(&foo, tmp) + printf("node=%p\n", tmp); + + assert(!dll_empty(&foo)); + + DLL_FOR_EACH_SAFE(&foo, tmp, _tmp) + printf("deleted %p\n", dll_del(tmp)); + + DLL_FOR_EACH(&foo, tmp) + printf("!!!notempty!!! node=%p\n", tmp); + + assert(dll_empty(&foo)); + + for (int i = 0; i < NUM_NODES; i++) { + dll_pre(&foo, &entries[i].node); + entries[i].id = i; + } + + DLL_FOR_EACH_ENTRY(&foo, entry, entry_t, node) { + printf("id=%i entry=%p node=%p\n", + entry->id, entry, &entry->node); + } + + for (int i = 0; i < NUM_NODES; i++) { + if (!(i % 2)) + continue; + + printf("deleted %i:%p\n", i, dll_del(&entries[i].node)); + } + + DLL_FOR_EACH_ENTRY(&foo, entry, entry_t, node) { + printf("id=%i entry=%p node=%p\n", + entry->id, entry, &entry->node); + } + + DLL_FOR_EACH_ENTRY_SAFE(&foo, entry, _entry, entry_t, node) + printf("deleted %p\n", dll_del(&entry->node)); + + DLL_FOR_EACH_ENTRY(&foo, entry, entry_t, node) + printf("!!!notempty!!! entry=%p\n", entry); + + assert(dll_empty(&foo)); +} -- cgit v1.2.3