improved more buffer tests + some doc fixes

Mon, 05 May 2014 15:56:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 May 2014 15:56:39 +0200
changeset 166
350a0e3898bd
parent 165
4d85da1f98db
child 167
aed60ba37acf

improved more buffer tests + some doc fixes

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
ucx/buffer.h file | annotate | diff | comparison | revisions
--- a/test/buffer_tests.c	Mon May 05 14:52:40 2014 +0200
+++ b/test/buffer_tests.c	Mon May 05 15:56:39 2014 +0200
@@ -34,15 +34,50 @@
  *  
  * 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_new) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND);
+    UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
+    UCX_TEST_BEGIN
+
+    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
+    UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity");
+    
+    UCX_TEST_ASSERT(b->size==0, "wrong size");
+    UCX_TEST_ASSERT(b2->size==0, "wrong size");
+    
+    UCX_TEST_ASSERT(b->pos==0, "wrong position");
+    UCX_TEST_ASSERT(b2->pos==0, "wrong position");
+    
+    UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE),
+        "wrong flags for autoextending buffer");
+    UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE,
+        "wrong flags for default bufer");
+    
+    UCX_TEST_END
+    ucx_buffer_free(b2);
+    ucx_buffer_free(b);
+}
+
+UCX_TEST(test_ucx_buffer_new_prealloc) {
+    char* test = (char*) malloc(16);
+    UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
+    UCX_TEST_BEGIN
+
+    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
+    UCX_TEST_ASSERT(b->size==0, "wrong size");    
+    UCX_TEST_ASSERT(b->pos==0, "wrong position");
+    
+    UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared");
+    
+    UCX_TEST_END
+    free(test);
+    ucx_buffer_free(b);
+}
+
 UCX_TEST(test_ucx_buffer_eof) {
     char *test = "0123456789ABCDEF";
     UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
@@ -315,15 +350,19 @@
 
     char rb[16];
     for (size_t i = 0 ; i < 16 ; i++) {
-        UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop");
-        UCX_TEST_ASSERT(!ucx_buffer_eof(b),
-                "EOF shall not be set during read loop");
+        UCX_TEST_ASSERT(b->pos == i, "wrong position");
+        UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive");
         rb[i] = ucx_buffer_getc(b);
     }
-    UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
-    UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
     UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
-            "read data incorrect");
+        "read data incorrect");
+        
+    UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read");
+    UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF");
+    
+    UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF,
+        "out of bounds read does not return EOF");
+    UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read");
 
     UCX_TEST_END
 
@@ -399,6 +438,8 @@
 
 UCX_TEST(test_ucx_buffer_read) {
     UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
+    ucx_buffer_write("01234567", 1, 8, b);
+    b->pos = 0;
     
     char buf[32];
     memset(buf, 'X', 32);
@@ -406,28 +447,50 @@
     
     UCX_TEST_BEGIN
     
-    ucx_buffer_write("01234567", 1, 8, b);
-    UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled");
-    b->pos = 0;
-    
+    ucx_buffer_seek(b, 2, SEEK_SET);
     r = ucx_buffer_read(buf, 1, 2, b);
-    UCX_TEST_ASSERT(r == 2, "wrong number of bytes read");
-    UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X',
-            "buffer incorrect after first read");
+    UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)");
+    UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X',
+            "buffer incorrect after read");
+    UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)");
+    
+    UCX_TEST_END
     
+    ucx_buffer_free(b);
+}
+
+UCX_TEST(test_ucx_buffer_read_oob) {
+    UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
+    ucx_buffer_write("01234567", 1, 8, b);
+    
+    char buf[32];
+    memset(buf, 'X', 32);
+    int r;
+    
+    UCX_TEST_BEGIN
+
+    b->pos = 2;
     r = ucx_buffer_read(buf + 2, 1, 8, b);
-    UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)");
-    UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
-            "buffer incorrect after second read");
+    UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)");
+    UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0,
+            "buffer incorrect after read");
+    UCX_TEST_ASSERT(b->pos == 8,
+        "wrong position after read (8 items out of bound)");
+
+    b->pos = 0;
+    memset(buf, 'X', 32);
     
-    memset(buf, 'X', 32);
-    ucx_buffer_seek(b, 0, SEEK_SET);
     r = ucx_buffer_read(buf, 3, 3, b);
     
     UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
     UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
-            "buffer incorrect after three byte read");
+            "buffer incorrect after block out of bounds read");
+    
+    r = ucx_buffer_read(buf+6, 1, 5, b);
     
+    UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read");
+    UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
+            "buffer incorrect after remaining byte read");
     
     UCX_TEST_END
     
--- a/test/buffer_tests.h	Mon May 05 14:52:40 2014 +0200
+++ b/test/buffer_tests.h	Mon May 05 15:56:39 2014 +0200
@@ -48,11 +48,15 @@
 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_new);
+UCX_TEST(test_ucx_buffer_new_prealloc);
+UCX_TEST(test_ucx_buffer_getc);
+UCX_TEST(test_ucx_buffer_read);
+UCX_TEST(test_ucx_buffer_read_oob);
 
-UCX_TEST(test_ucx_buffer_getc);
 UCX_TEST(test_ucx_buffer_write);
 UCX_TEST(test_ucx_buffer_write_ax);
-UCX_TEST(test_ucx_buffer_read);
+
 UCX_TEST(test_ucx_buffer_extract);
 UCX_TEST(test_ucx_stream_copy);
 
--- a/test/main.c	Mon May 05 14:52:40 2014 +0200
+++ b/test/main.c	Mon May 05 15:56:39 2014 +0200
@@ -177,6 +177,8 @@
         ucx_test_register(suite, test_ucx_properties_store);
         
         /* UcxBuffer Tests */
+        ucx_test_register(suite, test_ucx_buffer_new);
+        ucx_test_register(suite, test_ucx_buffer_new_prealloc);
         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);
@@ -190,9 +192,10 @@
         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_read);
+        ucx_test_register(suite, test_ucx_buffer_read_oob);
         ucx_test_register(suite, test_ucx_buffer_write);
         ucx_test_register(suite, test_ucx_buffer_write_ax);
-        ucx_test_register(suite, test_ucx_buffer_read);
         ucx_test_register(suite, test_ucx_buffer_extract);
         ucx_test_register(suite, test_ucx_stream_copy);
         
--- a/ucx/buffer.h	Mon May 05 14:52:40 2014 +0200
+++ b/ucx/buffer.h	Mon May 05 15:56:39 2014 +0200
@@ -220,7 +220,7 @@
  * @param size the length of one element
  * @param nitems the element count
  * @param buffer the UcxBuffer to read from
- * @return the total count of bytes read
+ * @return the total number of elements read
  */
 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
         UcxBuffer *buffer);

mercurial