test/buffer_tests.c

changeset 166
350a0e3898bd
parent 164
1fa3f13f774c
child 167
aed60ba37acf
--- 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
     

mercurial