From 4c7870c21b9e645b349ddb77b091543b72c46bf5 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 23 Apr 2020 21:09:30 -0700 Subject: feat: made calling signature of interface accepting functions more reliable --- sys/libbio/test.c | 102 ++++++++---------------------------------------------- 1 file changed, 15 insertions(+), 87 deletions(-) (limited to 'sys/libbio/test.c') diff --git a/sys/libbio/test.c b/sys/libbio/test.c index fb79302..115ee46 100644 --- a/sys/libbio/test.c +++ b/sys/libbio/test.c @@ -2,109 +2,37 @@ #include #include -// ----------------------------------------------------------------------- -// Arena allocator - -static mem·Arena* ARENA; - -static -void* -bio·alloc(ulong size) -{ - return mem·arenaalloc(ARENA, size); -} - -static -void* -bio·realloc(void *ptr, ulong size) -{ - void* new = mem·arenaalloc(ARENA, size); - memcpy(new, ptr, size); - - return new; -} - -static -void -bio·free(void *ptr) -{ - /* stub */ -} - -static mem·Allocator arena = {.alloc = &bio·alloc, .realloc = &bio·realloc, .free = &bio·free }; - -// ----------------------------------------------------------------------- -// Read/writer - -/* Static reader thunk */ -static Stream* INPUT; - -static -byte -get() -{ - return io·getbyte(INPUT); -} - -static -error -unget(byte c) -{ - return io·ungetbyte(INPUT, c); -} - -static io·Peeker rdr = {.get = &get, .unget = &unget}; - -/* Static writer thunk */ -static Stream* OUTPUT; - -static -error -put(byte b) -{ - return io·putbyte(OUTPUT, b); -} - -static -int -putstr(string s) -{ - return io·putstring(OUTPUT, s); -} - -static io·Putter wtr = {.put = &put, .putstr = &putstr}; - // ----------------------------------------------------------------------- // Point of entry for testing -void -init() -{ - ARENA = mem·newarena(mem·sys); -} int main() { - init(); - error err; bio·Tree t; - Stream *fd[2]; + mem·Arena *mem; + Stream *fd[2]; + + io·Peeker rdr; + io·Putter wtr; + mem·Allocator al; + + mem = mem·newarena(mem·sys, nil); + rdr = (io·Peeker){.get = &io·getbyte, .unget = &io·ungetbyte}; + wtr = (io·Putter){.put = &io·putbyte, .putstr = &io·putstring}; + al = (mem·Allocator) { .alloc = &mem·arenaalloc, .free = nil, }; fd[0] = io·open("/home/nolln/root/data/test/example.nwk", "r"); fd[1] = io·open("/home/nolln/root/data/test/example.proc.nwk", "w"); - INPUT = fd[0]; - OUTPUT = fd[1]; - - t = bio·readnewick(rdr, arena); - err = bio·writenewick(t, wtr); + t = bio·readnewick(rdr, fd[0], al, mem); + err = bio·writenewick(t, wtr, fd[1]); io·flush(fd[1]); - io·close(fd[0]); io·close(fd[1]); + io·close(fd[0]); + io·close(fd[1]); - mem·freearena(ARENA); return 0; } -- cgit v1.2.1