added allocator tests + started refactoring UcxBuffer tests (HINT: don't fix issues yet, complete tests first)

Mon, 05 May 2014 13:53:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 May 2014 13:53:27 +0200
changeset 164
1fa3f13f774c
parent 163
5ec9a2ca6328
child 165
4d85da1f98db

added allocator tests + started refactoring UcxBuffer tests (HINT: don't fix issues yet, complete tests first)

test/Makefile file | annotate | diff | comparison | revisions
test/allocator_tests.c file | annotate | diff | comparison | revisions
test/allocator_tests.h file | annotate | diff | comparison | revisions
test/buffer_tests.c file | annotate | diff | comparison | revisions
test/buffer_tests.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
--- a/test/Makefile	Thu Apr 17 14:33:06 2014 +0200
+++ b/test/Makefile	Mon May 05 13:53:27 2014 +0200
@@ -29,6 +29,7 @@
 include ../$(CONF).mk
 
 SRC  = main.c
+SRC += allocator_tests.c
 SRC += list_tests.c
 SRC += mpool_tests.c
 SRC += map_tests.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/allocator_tests.c	Mon May 05 13:53:27 2014 +0200
@@ -0,0 +1,41 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "allocator_tests.h"
+
+UCX_TEST(test_ucx_default_allocator) {
+    UcxAllocator *alloc = ucx_default_allocator();
+    
+    UCX_TEST_BEGIN
+    UCX_TEST_ASSERT(alloc->pool == NULL, "pool must be NULL");
+    UCX_TEST_ASSERT(alloc->malloc == ucx_default_malloc, "wrong malloc");
+    UCX_TEST_ASSERT(alloc->calloc == ucx_default_calloc, "wrong calloc");
+    UCX_TEST_ASSERT(alloc->realloc == ucx_default_realloc, "wrong realloc");
+    UCX_TEST_ASSERT(alloc->free == ucx_default_free, "wrong free");
+    UCX_TEST_END
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/allocator_tests.h	Mon May 05 13:53:27 2014 +0200
@@ -0,0 +1,46 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef ALLOCATOR_TEST_H
+#define	ALLOCATOR_TEST_H
+
+#include "ucx/test.h"
+#include "ucx/allocator.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+UCX_TEST(test_ucx_default_allocator);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* ALLOCATOR_TEST_H */
+
--- a/test/buffer_tests.c	Thu Apr 17 14:33:06 2014 +0200
+++ b/test/buffer_tests.c	Mon May 05 13:53:27 2014 +0200
@@ -29,9 +29,120 @@
 #include "buffer_tests.h"
 #include "ucx/utils.h"
 
-UCX_TEST(test_ucx_buffer_seek) {
+/*
+ * TODO: refactor tests
+ *  
+ * ucx_buffer_extend
+ * ucx_buffer_extract
+ * ucx_buffer_free
+ * ucx_buffer_getc
+ * ucx_buffer_new
+ * ucx_buffer_puts
+ * ucx_buffer_read
+ * ucx_buffer_write
+ * 
+ */
+
+UCX_TEST(test_ucx_buffer_eof) {
+    char *test = "0123456789ABCDEF";
+    UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
+    UCX_TEST_BEGIN
+    b->pos = 9; b->size = 10;
+    UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive");
+    b->pos = 10; b->size = 10;
+    UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF");
+    b->pos = 11; b->size = 10;
+    UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative");
+    UCX_TEST_END
+}
+
+UCX_TEST(test_ucx_buffer_seek_overflow) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
+    b->size = 32;
+    int r;
+    
+    UCX_TEST_BEGIN
+    size_t bigpos = SIZE_MAX - 5000;
+    b->pos = bigpos;
+    r = ucx_buffer_seek(b, 5016, SEEK_CUR);
+    UCX_TEST_ASSERT(r != 0, "seek cur overflow");
+    UCX_TEST_ASSERT(b->pos == bigpos,
+            "failed seek shall leave pos unchanged");
+    
+    b->pos = 0;
+    b->size = SIZE_MAX / 2 + 32;
+    off_t bigoff = SIZE_MAX / 2 - 16;
+    r = ucx_buffer_seek(b, -bigoff, SEEK_CUR);
+    UCX_TEST_ASSERT(r != 0, "seek cur underflow");
+    UCX_TEST_ASSERT(b->pos == 0,
+            "failed seek shall leave pos unchanged");
+    
+    UCX_TEST_END
+    
+    ucx_buffer_free(b);
+}
+
+UCX_TEST(test_ucx_buffer_seek_invalid) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
+    b->pos = 7;
+    int r;
+    
+    UCX_TEST_BEGIN
+    r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END));
+    UCX_TEST_ASSERT(r != 0, "invalid whence shall fail");
+    UCX_TEST_ASSERT(b->pos == 7,
+            "failed seek shall leave pos unchanged");
+    UCX_TEST_END
+    
+    ucx_buffer_free(b);
+}
+
+UCX_TEST(test_ucx_buffer_seek_oob) {
     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
     b->size = 16; // less than capacity
+    b->pos = 7;
+    int r;
+
+    UCX_TEST_BEGIN
+    
+    r = ucx_buffer_seek(b, -1, SEEK_SET);
+    UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail");
+    UCX_TEST_ASSERT(b->pos == 7,
+            "failed seek shall leave pos unchanged");
+    
+    r = ucx_buffer_seek(b, 16, SEEK_SET);
+    UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail");
+    UCX_TEST_ASSERT(b->pos == 7,
+            "failed seek shall leave pos unchanged");
+    
+    r = ucx_buffer_seek(b, -8, SEEK_CUR);
+    UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail");
+    UCX_TEST_ASSERT(b->pos == 7,
+            "failed seek shall leave pos unchanged");
+    
+    r = ucx_buffer_seek(b, 9, SEEK_CUR);
+    UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail");
+    UCX_TEST_ASSERT(b->pos == 7,
+            "failed seek shall leave pos unchanged");
+    
+    r = ucx_buffer_seek(b, -17, SEEK_END);
+    UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail");
+    UCX_TEST_ASSERT(b->pos == 7,
+            "failed seek shall leave pos unchanged");
+    
+    r = ucx_buffer_seek(b, 1, SEEK_END);
+    UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail");
+    UCX_TEST_ASSERT(b->pos == 7,
+            "failed seek shall leave pos unchanged");
+
+    UCX_TEST_END
+
+    ucx_buffer_free(b);
+}
+
+UCX_TEST(test_ucx_buffer_seek_set) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
+    b->size = 16;
     int r;
 
     UCX_TEST_BEGIN
@@ -39,29 +150,45 @@
     r = ucx_buffer_seek(b, 5, SEEK_SET);
     UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
     UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
+    
+    
+    r = ucx_buffer_seek(b, 10, SEEK_SET);
+    UCX_TEST_ASSERT(r == 0, "seek SET+10 failed");
+    UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position");
 
-    r = ucx_buffer_seek(b, 20, SEEK_SET);
-    UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
-    UCX_TEST_ASSERT(b->pos == 5,
-            "failed seek shall leave pos unchanged");
+    UCX_TEST_END
+
+    ucx_buffer_free(b);
+}
 
+UCX_TEST(test_ucx_buffer_seek_cur) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
+    b->size = 16;
+    int r;
+
+    UCX_TEST_BEGIN
+    
+    b->pos = 7;
     r = ucx_buffer_seek(b, 5, SEEK_CUR);
     UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
-    UCX_TEST_ASSERT(b->pos == 10, "seek CUR+5 set wrong position");
+    UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position");
+
+    UCX_TEST_END
+
+    ucx_buffer_free(b);
+}
 
-    r = ucx_buffer_seek(b, 10, SEEK_CUR);
-    UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
-    UCX_TEST_ASSERT(b->pos == 10,
-            "failed seek shall leave pos unchanged");
+UCX_TEST(test_ucx_buffer_seek_end) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
+    b->size = 16;
+    int r;
 
+    UCX_TEST_BEGIN
+    
     r = ucx_buffer_seek(b, -5, SEEK_END);
     UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
     UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");
 
-    r = ucx_buffer_seek(b, -20, SEEK_END);
-    UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
-    UCX_TEST_ASSERT(b->pos == 11,
-            "failed seek shall leave pos unchanged");
 
     UCX_TEST_END
 
@@ -77,7 +204,9 @@
 
     UCX_TEST_BEGIN
 
-    ucx_buffer_putc(b, '0');
+    ucx_buffer_seek(b, 0, SEEK_SET);
+    UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0',
+            "putc shall return (arg & 0xFF)");
     ucx_buffer_putc(b, '0');
     ucx_buffer_putc(b, '0');
     
@@ -89,49 +218,89 @@
     ucx_buffer_putc(b, '0');
     
     UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
-    UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set");
-    UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF,
-            "put shall return EOF when buffer is full");
+    
+    UCX_TEST_ASSERT(!memcmp(b->space, "000          000", 16),
+            "buffer content wrong")
+    UCX_TEST_END
+    ucx_buffer_free(b);
+    free(buffer);
+}
     
-    ucx_buffer_seek(b, 3, SEEK_SET);
-    ucx_buffer_putc(b, 'a');
-    ucx_buffer_putc(b, 'b');
-    ucx_buffer_putc(b, 'c');
+UCX_TEST(test_ucx_buffer_putc_oob) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT);
     
-    UCX_TEST_ASSERT(b->size == 16, "wrong size after seek and puts");
-    UCX_TEST_ASSERT(memcmp(buffer, "000abc       000", 16) == 0,
-            "buffer contains incorrect content");
+    UCX_TEST_BEGIN
+    b->pos = b->size = b->capacity = 1;
+    b->space[1] = 'X';
+    
+    UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF "
+            "when buffer is full and auto extend is disabled");
+    UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2),
+            "wrong buffer content after failed putc");
 
     UCX_TEST_END
 
     ucx_buffer_free(b);
-    free(buffer);
 }
 
-UCX_TEST(test_ucx_buffer_putc_ax) {
+
+UCX_TEST(test_ucx_buffer_putc_ae) {
     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
+    ucx_buffer_putc(b, '0');
+    ucx_buffer_putc(b, '1');
     
     UCX_TEST_BEGIN
     
+    UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
+    UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts");
+    UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
+    UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content");
+    
+    UCX_TEST_END
+    
+    ucx_buffer_free(b);
+}
+
+UCX_TEST(test_ucx_buffer_putc_oobae) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
     ucx_buffer_putc(b, '0');
     ucx_buffer_putc(b, '1');
     
-    UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
-    UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
+    UCX_TEST_BEGIN
     
     ucx_buffer_putc(b, 'a');
     
-    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after 1 put");
+    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put");
     UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
     UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
     
-    UCX_TEST_ASSERT(b->space[2] == 'a', "wrong content");
+    UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content");
     
     UCX_TEST_END
     
     ucx_buffer_free(b);
 }
 
+UCX_TEST(test_ucx_buffer_putc_size) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT);
+    
+    UCX_TEST_BEGIN
+    
+    UCX_TEST_ASSERT(b->size == 0, "wrong initial size");
+    ucx_buffer_putc(b, 'a');
+    ucx_buffer_putc(b, 'b');
+    ucx_buffer_putc(b, 'c');
+    UCX_TEST_ASSERT(b->size == 3, "size does not increase");
+    ucx_buffer_seek(b, 1, SEEK_SET);
+    ucx_buffer_putc(b, 'd');
+    UCX_TEST_ASSERT(b->size == 3, "size shall not decrease");
+    UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc");
+
+    UCX_TEST_END
+
+    ucx_buffer_free(b);
+}
+
 UCX_TEST(test_ucx_buffer_getc) {
     char *buffer = (char*) malloc(16);
     memset(buffer, 32, 8);
--- a/test/buffer_tests.h	Thu Apr 17 14:33:06 2014 +0200
+++ b/test/buffer_tests.h	Mon May 05 13:53:27 2014 +0200
@@ -26,8 +26,8 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef MEMSTREAM_TEST_H
-#define	MEMSTREAM_TEST_H
+#ifndef BUFFER_TEST_H
+#define	BUFFER_TEST_H
 
 #include "ucx/test.h"
 #include "ucx/buffer.h"
@@ -36,9 +36,19 @@
 extern "C" {
 #endif
 
-UCX_TEST(test_ucx_buffer_seek);
+UCX_TEST(test_ucx_buffer_eof);
+UCX_TEST(test_ucx_buffer_seek_set);
+UCX_TEST(test_ucx_buffer_seek_end);
+UCX_TEST(test_ucx_buffer_seek_cur);
+UCX_TEST(test_ucx_buffer_seek_oob);
+UCX_TEST(test_ucx_buffer_seek_invalid);
+UCX_TEST(test_ucx_buffer_seek_overflow);
 UCX_TEST(test_ucx_buffer_putc);
-UCX_TEST(test_ucx_buffer_putc_ax);
+UCX_TEST(test_ucx_buffer_putc_ae);
+UCX_TEST(test_ucx_buffer_putc_oob);
+UCX_TEST(test_ucx_buffer_putc_oobae);
+UCX_TEST(test_ucx_buffer_putc_size);
+
 UCX_TEST(test_ucx_buffer_getc);
 UCX_TEST(test_ucx_buffer_write);
 UCX_TEST(test_ucx_buffer_write_ax);
@@ -50,5 +60,5 @@
 }
 #endif
 
-#endif	/* MEMSTREAM_TEST_H */
+#endif	/* BUFFER_TEST_H */
 
--- a/test/main.c	Thu Apr 17 14:33:06 2014 +0200
+++ b/test/main.c	Mon May 05 13:53:27 2014 +0200
@@ -33,6 +33,7 @@
 
 #include "main.h"
 
+#include "allocator_tests.h"
 #include "logging_tests.h"
 #include "list_tests.h"
 #include "string_tests.h"
@@ -112,6 +113,9 @@
 
         printf("\nLibrary function tests\n");
         suite = ucx_test_suite_new();
+        /* UcxAllocator Tests */
+        ucx_test_register(suite, test_ucx_default_allocator);
+        
         /* sstring Tests */
         ucx_test_register(suite, test_sstr);
         ucx_test_register(suite, test_sstr_len_cat);
@@ -173,9 +177,18 @@
         ucx_test_register(suite, test_ucx_properties_store);
         
         /* UcxBuffer Tests */
-        ucx_test_register(suite, test_ucx_buffer_seek);
+        ucx_test_register(suite, test_ucx_buffer_eof);
+        ucx_test_register(suite, test_ucx_buffer_seek_set);
+        ucx_test_register(suite, test_ucx_buffer_seek_cur);
+        ucx_test_register(suite, test_ucx_buffer_seek_end);
+        ucx_test_register(suite, test_ucx_buffer_seek_oob);
+        ucx_test_register(suite, test_ucx_buffer_seek_invalid);
+        ucx_test_register(suite, test_ucx_buffer_seek_overflow);
         ucx_test_register(suite, test_ucx_buffer_putc);
-        ucx_test_register(suite, test_ucx_buffer_putc_ax);
+        ucx_test_register(suite, test_ucx_buffer_putc_ae);
+        ucx_test_register(suite, test_ucx_buffer_putc_oob);
+        ucx_test_register(suite, test_ucx_buffer_putc_oobae);
+        ucx_test_register(suite, test_ucx_buffer_putc_size);
         ucx_test_register(suite, test_ucx_buffer_getc);
         ucx_test_register(suite, test_ucx_buffer_write);
         ucx_test_register(suite, test_ucx_buffer_write_ax);

mercurial