test/buffer_tests.c

changeset 168
24a012440dee
parent 167
aed60ba37acf
child 169
279dd3ca7a77
     1.1 --- a/test/buffer_tests.c	Tue May 06 10:56:54 2014 +0200
     1.2 +++ b/test/buffer_tests.c	Tue May 06 12:03:16 2014 +0200
     1.3 @@ -27,16 +27,7 @@
     1.4   */
     1.5  
     1.6  #include "buffer_tests.h"
     1.7 -#include "ucx/utils.h"
     1.8 -
     1.9 -/*
    1.10 - * TODO: refactor tests
    1.11 - *  
    1.12 - * ucx_buffer_extend
    1.13 - * ucx_buffer_extract
    1.14 - * ucx_buffer_write
    1.15 - * 
    1.16 - */
    1.17 +#include <stdint.h>
    1.18  
    1.19  UCX_TEST(test_ucx_buffer_new) {
    1.20      UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND);
    1.21 @@ -371,41 +362,68 @@
    1.22  }
    1.23  
    1.24  UCX_TEST(test_ucx_buffer_write) {
    1.25 -    char *buffer = (char*) malloc(16);
    1.26 +    char *buffer = (char*) malloc(32);
    1.27      memset(buffer, 32, 8);
    1.28      for (int i = 8; i < 16 ; i++) {
    1.29          buffer[i] = 40+i;
    1.30      }
    1.31  
    1.32 -    UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
    1.33 +    UcxBuffer *b = ucx_buffer_new(buffer, 32, UCX_BUFFER_DEFAULT);
    1.34      int r;
    1.35  
    1.36      UCX_TEST_BEGIN
    1.37 +    b->pos = 4;
    1.38 +    r = ucx_buffer_write("test string", 1, 11, b);
    1.39 +    UCX_TEST_ASSERT(r == 11, "returned incorrect number of written bytes");
    1.40 +    UCX_TEST_ASSERT(b->pos == 15, "incorrect position");
    1.41 +    UCX_TEST_ASSERT(memcmp(buffer, "    test string7\0\0", 18) == 0,
    1.42 +        "incorrect buffer content (test string)");
    1.43 +    
    1.44 +    int32_t testarr[4] = {0x09abcdef, 0x05fedcba, 0x01abefcd, 0x3cd07ab};
    1.45 +    r = ucx_buffer_write(testarr, 4, 4, b);
    1.46 +    UCX_TEST_ASSERT(r = 4, "returned incorrect number of written elements");
    1.47 +    UCX_TEST_ASSERT(b->pos == 31, "incorrect position");
    1.48 +    
    1.49 +    char cmp[32];
    1.50 +    memset(cmp, 0, 32);
    1.51 +    memcpy(cmp, "    test string", 15);
    1.52 +    int32_t *ptr = (int32_t*) (cmp+15);
    1.53 +    ptr[0] = testarr[0];
    1.54 +    ptr[1] = testarr[1];
    1.55 +    ptr[2] = testarr[2];
    1.56 +    ptr[3] = testarr[3];
    1.57 +    UCX_TEST_ASSERT(memcmp(buffer, cmp, 32) == 0,
    1.58 +        "incorrect buffer content (int array)");
    1.59 +    
    1.60 +    UCX_TEST_END
    1.61  
    1.62 -    const char* teststring = "this is way too much";
    1.63 -    r = ucx_buffer_write((void*)teststring, 1, 20, b);
    1.64 -    UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
    1.65 -    UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
    1.66 -            "buffer data incorrect");
    1.67 -    UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
    1.68 +    ucx_buffer_free(b);
    1.69 +    free(buffer);
    1.70 +}
    1.71  
    1.72 -    ucx_buffer_seek(b, 8, SEEK_SET);
    1.73 -    r = ucx_buffer_write((void*)"not", 1, 3, b);
    1.74 -    UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
    1.75 -    UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
    1.76 -            "modified buffer is incorrect");
    1.77 +UCX_TEST(test_ucx_buffer_write_oob) {
    1.78 +    char *buffer = (char*) malloc(32);
    1.79 +    memset(buffer, 0, 32);
    1.80  
    1.81 -    const char* threebytestring = "  t  h  r  e  e   ";
    1.82 -    memset(buffer, 49, 16);
    1.83 -    ucx_buffer_seek(b, 0, SEEK_SET);
    1.84 -    r = ucx_buffer_write((void*)threebytestring, 3, 6, b);
    1.85 -    UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed");
    1.86 -    UCX_TEST_ASSERT(b->pos == 15,
    1.87 -            "position after write of three byte string incorrect");
    1.88 -    UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
    1.89 -    UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
    1.90 -                "bufer is incorrect after three byte string has been written");
    1.91 +    UcxBuffer *b = ucx_buffer_new(buffer, 15, UCX_BUFFER_DEFAULT);
    1.92 +    int r;
    1.93  
    1.94 +    UCX_TEST_BEGIN
    1.95 +    r = ucx_buffer_write("a very long string", 1, 18, b);
    1.96 +    UCX_TEST_ASSERT(r == 15, "incorrect number of written bytes");
    1.97 +    UCX_TEST_ASSERT(memcmp(buffer, "a very long str\0\0\0", 18) == 0,
    1.98 +        "invalid buffer content (test string)");
    1.99 +    
   1.100 +    b->size = b->pos = 0;
   1.101 +    int32_t intarr[4] = {0,-1,0,-1};
   1.102 +    memset(buffer, 0, 32);
   1.103 +    
   1.104 +    r = ucx_buffer_write(intarr, 4, 4, b);
   1.105 +    UCX_TEST_ASSERT(r == 3, "incorrect number of written elements");
   1.106 +    UCX_TEST_ASSERT(memcmp(buffer,
   1.107 +        "\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0", 16) == 0,
   1.108 +        "invalid buffer content (test string)");
   1.109 +    
   1.110      UCX_TEST_END
   1.111  
   1.112      ucx_buffer_free(b);
   1.113 @@ -502,7 +520,7 @@
   1.114      strcpy(buffer, "this is a test!");
   1.115  
   1.116      UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   1.117 -    src->size = 15;
   1.118 +    src->size = 16;
   1.119      UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND);
   1.120  
   1.121      UCX_TEST_BEGIN
   1.122 @@ -528,6 +546,8 @@
   1.123      strcpy(buffer, "this is a test!");
   1.124  
   1.125      UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   1.126 +    src->size = 16;
   1.127 +    
   1.128      UCX_TEST_BEGIN
   1.129      
   1.130      UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL,
   1.131 @@ -547,6 +567,8 @@
   1.132      strcpy(buffer, "this is a test!");
   1.133  
   1.134      UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   1.135 +    src->size = 16;
   1.136 +    
   1.137      UCX_TEST_BEGIN
   1.138      
   1.139      UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4,
   1.140 @@ -557,53 +579,21 @@
   1.141      ucx_buffer_free(src);
   1.142  }
   1.143  
   1.144 -UCX_TEST(test_ucx_stream_copy) {
   1.145 -    UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
   1.146 -    UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   1.147 +UCX_TEST(test_ucx_buffer_extend) {
   1.148 +    
   1.149 +    UcxBuffer *b = ucx_buffer_new(NULL, 10, UCX_BUFFER_DEFAULT);
   1.150      
   1.151      UCX_TEST_BEGIN
   1.152      
   1.153 -    ucx_buffer_write("01234567", 1, 8, b1);
   1.154 -    ucx_buffer_write("abcdefgh", 1, 8, b1);
   1.155 -    UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
   1.156 -    ucx_buffer_seek(b1, 0, SEEK_SET);
   1.157 +    UCX_TEST_ASSERT(ucx_buffer_extend(b, 15) == 0, "shall return 0 on success");
   1.158 +    UCX_TEST_ASSERT(b->capacity = 40, "wrong capacity");
   1.159 +    UCX_TEST_ASSERT((b->size == 0 && b->pos == 0),
   1.160 +        "pos and size shall remain unchanged");
   1.161      
   1.162 -    size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
   1.163 -    UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
   1.164 -    UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
   1.165 -    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   1.166 -            "b1 and b2 have not the same content");
   1.167 -    
   1.168 -    memset(b2->space, 0, b2->capacity);
   1.169 -    b2->pos = 0;
   1.170 -    b2->size = 0;
   1.171 -    ucx_buffer_seek(b1, 0, SEEK_SET);
   1.172 -    
   1.173 -    FILE *file = tmpfile();
   1.174 -    UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
   1.175 -    
   1.176 -    ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
   1.177 -    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
   1.178 -    
   1.179 -    fseek(file, 0, SEEK_SET);
   1.180 -    
   1.181 -    ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
   1.182 -    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
   1.183 -    
   1.184 -    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   1.185 -            "b1 and b2 content mismatch");
   1.186 -    
   1.187 -    fclose(file);
   1.188 -
   1.189 -    ucx_buffer_clear(b1);
   1.190 -    ucx_buffer_seek(b2, 0, SEEK_SET);
   1.191 -    ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
   1.192 -    UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
   1.193 -    UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
   1.194 -        "content wrong after ncopy");
   1.195 +    UCX_TEST_ASSERT(ucx_buffer_extend(b, SIZE_MAX - 60) != 0,
   1.196 +        "shall fail and return a non-zero value on overflow");
   1.197      
   1.198      UCX_TEST_END
   1.199      
   1.200 -    ucx_buffer_free(b1);
   1.201 -    ucx_buffer_free(b2);
   1.202 +    ucx_buffer_free(b);
   1.203  }

mercurial