test/buffer_tests.c

changeset 168
24a012440dee
parent 167
aed60ba37acf
child 169
279dd3ca7a77
--- a/test/buffer_tests.c	Tue May 06 10:56:54 2014 +0200
+++ b/test/buffer_tests.c	Tue May 06 12:03:16 2014 +0200
@@ -27,16 +27,7 @@
  */
 
 #include "buffer_tests.h"
-#include "ucx/utils.h"
-
-/*
- * TODO: refactor tests
- *  
- * ucx_buffer_extend
- * ucx_buffer_extract
- * ucx_buffer_write
- * 
- */
+#include <stdint.h>
 
 UCX_TEST(test_ucx_buffer_new) {
     UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND);
@@ -371,41 +362,68 @@
 }
 
 UCX_TEST(test_ucx_buffer_write) {
-    char *buffer = (char*) malloc(16);
+    char *buffer = (char*) malloc(32);
     memset(buffer, 32, 8);
     for (int i = 8; i < 16 ; i++) {
         buffer[i] = 40+i;
     }
 
-    UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
+    UcxBuffer *b = ucx_buffer_new(buffer, 32, UCX_BUFFER_DEFAULT);
     int r;
 
     UCX_TEST_BEGIN
-
-    const char* teststring = "this is way too much";
-    r = ucx_buffer_write((void*)teststring, 1, 20, b);
-    UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
-    UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
-            "buffer data incorrect");
-    UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
+    b->pos = 4;
+    r = ucx_buffer_write("test string", 1, 11, b);
+    UCX_TEST_ASSERT(r == 11, "returned incorrect number of written bytes");
+    UCX_TEST_ASSERT(b->pos == 15, "incorrect position");
+    UCX_TEST_ASSERT(memcmp(buffer, "    test string7\0\0", 18) == 0,
+        "incorrect buffer content (test string)");
+    
+    int32_t testarr[4] = {0x09abcdef, 0x05fedcba, 0x01abefcd, 0x3cd07ab};
+    r = ucx_buffer_write(testarr, 4, 4, b);
+    UCX_TEST_ASSERT(r = 4, "returned incorrect number of written elements");
+    UCX_TEST_ASSERT(b->pos == 31, "incorrect position");
+    
+    char cmp[32];
+    memset(cmp, 0, 32);
+    memcpy(cmp, "    test string", 15);
+    int32_t *ptr = (int32_t*) (cmp+15);
+    ptr[0] = testarr[0];
+    ptr[1] = testarr[1];
+    ptr[2] = testarr[2];
+    ptr[3] = testarr[3];
+    UCX_TEST_ASSERT(memcmp(buffer, cmp, 32) == 0,
+        "incorrect buffer content (int array)");
+    
+    UCX_TEST_END
 
-    ucx_buffer_seek(b, 8, SEEK_SET);
-    r = ucx_buffer_write((void*)"not", 1, 3, b);
-    UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
-    UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
-            "modified buffer is incorrect");
+    ucx_buffer_free(b);
+    free(buffer);
+}
+
+UCX_TEST(test_ucx_buffer_write_oob) {
+    char *buffer = (char*) malloc(32);
+    memset(buffer, 0, 32);
+
+    UcxBuffer *b = ucx_buffer_new(buffer, 15, UCX_BUFFER_DEFAULT);
+    int r;
 
-    const char* threebytestring = "  t  h  r  e  e   ";
-    memset(buffer, 49, 16);
-    ucx_buffer_seek(b, 0, SEEK_SET);
-    r = ucx_buffer_write((void*)threebytestring, 3, 6, b);
-    UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed");
-    UCX_TEST_ASSERT(b->pos == 15,
-            "position after write of three byte string incorrect");
-    UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
-    UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
-                "bufer is incorrect after three byte string has been written");
-
+    UCX_TEST_BEGIN
+    r = ucx_buffer_write("a very long string", 1, 18, b);
+    UCX_TEST_ASSERT(r == 15, "incorrect number of written bytes");
+    UCX_TEST_ASSERT(memcmp(buffer, "a very long str\0\0\0", 18) == 0,
+        "invalid buffer content (test string)");
+    
+    b->size = b->pos = 0;
+    int32_t intarr[4] = {0,-1,0,-1};
+    memset(buffer, 0, 32);
+    
+    r = ucx_buffer_write(intarr, 4, 4, b);
+    UCX_TEST_ASSERT(r == 3, "incorrect number of written elements");
+    UCX_TEST_ASSERT(memcmp(buffer,
+        "\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0", 16) == 0,
+        "invalid buffer content (test string)");
+    
     UCX_TEST_END
 
     ucx_buffer_free(b);
@@ -502,7 +520,7 @@
     strcpy(buffer, "this is a test!");
 
     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
-    src->size = 15;
+    src->size = 16;
     UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND);
 
     UCX_TEST_BEGIN
@@ -528,6 +546,8 @@
     strcpy(buffer, "this is a test!");
 
     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
+    src->size = 16;
+    
     UCX_TEST_BEGIN
     
     UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL,
@@ -547,6 +567,8 @@
     strcpy(buffer, "this is a test!");
 
     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
+    src->size = 16;
+    
     UCX_TEST_BEGIN
     
     UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4,
@@ -557,53 +579,21 @@
     ucx_buffer_free(src);
 }
 
-UCX_TEST(test_ucx_stream_copy) {
-    UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
-    UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
+UCX_TEST(test_ucx_buffer_extend) {
+    
+    UcxBuffer *b = ucx_buffer_new(NULL, 10, UCX_BUFFER_DEFAULT);
     
     UCX_TEST_BEGIN
     
-    ucx_buffer_write("01234567", 1, 8, b1);
-    ucx_buffer_write("abcdefgh", 1, 8, b1);
-    UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
-    ucx_buffer_seek(b1, 0, SEEK_SET);
-    
-    size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
-    UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
-    UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
-    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
-            "b1 and b2 have not the same content");
-    
-    memset(b2->space, 0, b2->capacity);
-    b2->pos = 0;
-    b2->size = 0;
-    ucx_buffer_seek(b1, 0, SEEK_SET);
-    
-    FILE *file = tmpfile();
-    UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
+    UCX_TEST_ASSERT(ucx_buffer_extend(b, 15) == 0, "shall return 0 on success");
+    UCX_TEST_ASSERT(b->capacity = 40, "wrong capacity");
+    UCX_TEST_ASSERT((b->size == 0 && b->pos == 0),
+        "pos and size shall remain unchanged");
     
-    ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
-    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
-    
-    fseek(file, 0, SEEK_SET);
-    
-    ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
-    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
-    
-    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
-            "b1 and b2 content mismatch");
-    
-    fclose(file);
-
-    ucx_buffer_clear(b1);
-    ucx_buffer_seek(b2, 0, SEEK_SET);
-    ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
-    UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
-    UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
-        "content wrong after ncopy");
+    UCX_TEST_ASSERT(ucx_buffer_extend(b, SIZE_MAX - 60) != 0,
+        "shall fail and return a non-zero value on overflow");
     
     UCX_TEST_END
     
-    ucx_buffer_free(b1);
-    ucx_buffer_free(b2);
+    ucx_buffer_free(b);
 }

mercurial