From: Michael D. Lowis Date: Mon, 5 Mar 2018 21:20:30 +0000 (-0500) Subject: Added more unit tests X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=7e91de3157ef82a0933e8ce568289c0aaa47e51d;p=archive%2Fgapbuf.git Added more unit tests --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cba7efc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +a.out diff --git a/buf.c b/buf.c index 973a8c0..5c45366 100644 --- a/buf.c +++ b/buf.c @@ -56,12 +56,15 @@ void buf_init(Buf* buf, void (*errfn)(char*)) { buf->undo = NULL; buf->redo = NULL; buf->errfn = errfn; + buf->path = NULL; assert(buf->bufstart); } void buf_load(Buf* buf, Sel* sel, char* path) { /* process the file path and address */ - if (path && path[0] == '.' && path[1] == '/') + if (sel) *sel = (Sel){0}; + if (!path) return; + if (path[0] == '.' && path[1] == '/') path += 2; buf->path = strdup(path); char* addr = strrchr(buf->path, ':'); @@ -84,7 +87,6 @@ void buf_load(Buf* buf, Sel* sel, char* path) { if (nread < 0) die("read() :"); } if (fd > 0) close(fd); - if (sel) *sel = (Sel){0}; } void buf_reload(Buf* buf) { diff --git a/testbuf.c b/testbuf.c index 54b00d4..6788c65 100644 --- a/testbuf.c +++ b/testbuf.c @@ -47,6 +47,7 @@ TEST_SUITE(BufferTests) { TEST(buf_init should initialize an empty buffer) { Buf buf = {0}; buf_init(&buf, (void*)0x12345678); + CHECK(buf.path == NULL); CHECK(buf.modified == false); CHECK(buf.expand_tabs == CfgExpandTabs); CHECK(buf.crlf == 0); @@ -65,6 +66,7 @@ TEST_SUITE(BufferTests) { buf_init(&buf, onerror); buf_putc(&buf, false, 0, 'a'); buf_init(&buf, (void*)0x12345678); + CHECK(buf.path == NULL); CHECK(buf.modified == false); CHECK(buf.expand_tabs == CfgExpandTabs); CHECK(buf.crlf == 0); @@ -126,28 +128,25 @@ TEST_SUITE(BufferTests) { CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt")); } - TEST(buf_reload should reload the file from disk) { + TEST(buf_load should handle non-existent paths) { Sel sel; buf_init(&TestBuf, NULL); - buf_load(&TestBuf, &sel, "testdocs/waf"); - TestBuf.path = "testdocs/lorem.txt"; - buf_reload(&TestBuf); + buf_load(&TestBuf, &sel, "nonexistent.txt"); CHECK(sel.end == 0); CHECK(TestBuf.modified == false); CHECK(TestBuf.expand_tabs == true); CHECK(TestBuf.crlf == 0); - CHECK(TestBuf.bufsize == 61440); + CHECK(TestBuf.bufsize == sysconf(_SC_PAGE_SIZE)); CHECK(TestBuf.undo == NULL); CHECK(TestBuf.redo == NULL); CHECK(TestBuf.errfn == NULL); - CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt")); + CHECK(!strcmp(TestBuf.path, "nonexistent.txt")); } - TEST(buf_load should handle non-existent paths) { + TEST(buf_load should handle NULL for selection) { Sel sel; buf_init(&TestBuf, NULL); - buf_load(&TestBuf, &sel, "nonexistent.txt"); - buf_reload(&TestBuf); + buf_load(&TestBuf, NULL, "nonexistent.txt"); CHECK(sel.end == 0); CHECK(TestBuf.modified == false); CHECK(TestBuf.expand_tabs == true); @@ -159,6 +158,38 @@ TEST_SUITE(BufferTests) { CHECK(!strcmp(TestBuf.path, "nonexistent.txt")); } + TEST(buf_load should handle NULL for path) { + Sel sel; + buf_init(&TestBuf, NULL); + buf_load(&TestBuf, NULL, NULL); + CHECK(sel.end == 0); + CHECK(TestBuf.modified == false); + CHECK(TestBuf.expand_tabs == true); + CHECK(TestBuf.crlf == 0); + CHECK(TestBuf.bufsize == sysconf(_SC_PAGE_SIZE)); + CHECK(TestBuf.undo == NULL); + CHECK(TestBuf.redo == NULL); + CHECK(TestBuf.errfn == NULL); + CHECK(TestBuf.path == NULL); + } + + TEST(buf_reload should reload the file from disk) { + Sel sel; + buf_init(&TestBuf, NULL); + buf_load(&TestBuf, &sel, "testdocs/waf"); + TestBuf.path = "testdocs/lorem.txt"; + buf_reload(&TestBuf); + CHECK(sel.end == 0); + CHECK(TestBuf.modified == false); + CHECK(TestBuf.expand_tabs == true); + CHECK(TestBuf.crlf == 0); + CHECK(TestBuf.bufsize == 61440); + CHECK(TestBuf.undo == NULL); + CHECK(TestBuf.redo == NULL); + CHECK(TestBuf.errfn == NULL); + CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt")); + } + #if 0 /* Saving *************************************************************************/