completed buffer tests

Tue, 06 May 2014 12:03:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 06 May 2014 12:03:16 +0200
changeset 168
24a012440dee
parent 167
aed60ba37acf
child 169
279dd3ca7a77

completed buffer tests

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
test/utils_tests.c file | annotate | diff | comparison | revisions
test/utils_tests.h file | annotate | diff | comparison | revisions
ucx/buffer.h file | annotate | diff | comparison | revisions
     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  }
     2.1 --- a/test/buffer_tests.h	Tue May 06 10:56:54 2014 +0200
     2.2 +++ b/test/buffer_tests.h	Tue May 06 12:03:16 2014 +0200
     2.3 @@ -56,10 +56,11 @@
     2.4  UCX_TEST(test_ucx_buffer_extract);
     2.5  UCX_TEST(test_ucx_buffer_extract_oob);
     2.6  UCX_TEST(test_ucx_buffer_extract_overflow);
     2.7 +UCX_TEST(test_ucx_buffer_extend);
     2.8 +UCX_TEST(test_ucx_buffer_write);
     2.9 +UCX_TEST(test_ucx_buffer_write_oob);
    2.10 +UCX_TEST(test_ucx_buffer_write_ax);
    2.11  
    2.12 -UCX_TEST(test_ucx_buffer_write);
    2.13 -UCX_TEST(test_ucx_buffer_write_ax);
    2.14 -UCX_TEST(test_ucx_stream_copy);
    2.15  
    2.16  #ifdef	__cplusplus
    2.17  }
     3.1 --- a/test/main.c	Tue May 06 10:56:54 2014 +0200
     3.2 +++ b/test/main.c	Tue May 06 12:03:16 2014 +0200
     3.3 @@ -197,13 +197,15 @@
     3.4          ucx_test_register(suite, test_ucx_buffer_extract);
     3.5          ucx_test_register(suite, test_ucx_buffer_extract_oob);
     3.6          ucx_test_register(suite, test_ucx_buffer_extract_overflow);
     3.7 +        ucx_test_register(suite, test_ucx_buffer_extend);
     3.8          ucx_test_register(suite, test_ucx_buffer_write);
     3.9 +        ucx_test_register(suite, test_ucx_buffer_write_oob);
    3.10          ucx_test_register(suite, test_ucx_buffer_write_ax);
    3.11 -        ucx_test_register(suite, test_ucx_stream_copy);
    3.12          
    3.13          /* Utils Tests*/
    3.14          ucx_test_register(suite, test_ucx_fprintf);
    3.15          ucx_test_register(suite, test_ucx_asprintf);
    3.16 +        ucx_test_register(suite, test_ucx_stream_copy);
    3.17  
    3.18          ucx_test_run(suite, stdout);
    3.19          fflush(stdout);
     4.1 --- a/test/utils_tests.c	Tue May 06 10:56:54 2014 +0200
     4.2 +++ b/test/utils_tests.c	Tue May 06 12:03:16 2014 +0200
     4.3 @@ -90,3 +90,54 @@
     4.4      free(teststr1);
     4.5      free(teststr2);
     4.6  }
     4.7 +
     4.8 +UCX_TEST(test_ucx_stream_copy) {
     4.9 +    UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
    4.10 +    UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
    4.11 +    
    4.12 +    UCX_TEST_BEGIN
    4.13 +    
    4.14 +    ucx_buffer_write("01234567", 1, 8, b1);
    4.15 +    ucx_buffer_write("abcdefgh", 1, 8, b1);
    4.16 +    UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
    4.17 +    ucx_buffer_seek(b1, 0, SEEK_SET);
    4.18 +    
    4.19 +    size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
    4.20 +    UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
    4.21 +    UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
    4.22 +    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
    4.23 +            "b1 and b2 have not the same content");
    4.24 +    
    4.25 +    memset(b2->space, 0, b2->capacity);
    4.26 +    b2->pos = 0;
    4.27 +    b2->size = 0;
    4.28 +    ucx_buffer_seek(b1, 0, SEEK_SET);
    4.29 +    
    4.30 +    FILE *file = tmpfile();
    4.31 +    UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
    4.32 +    
    4.33 +    ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
    4.34 +    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
    4.35 +    
    4.36 +    fseek(file, 0, SEEK_SET);
    4.37 +    
    4.38 +    ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
    4.39 +    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
    4.40 +    
    4.41 +    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
    4.42 +            "b1 and b2 content mismatch");
    4.43 +    
    4.44 +    fclose(file);
    4.45 +
    4.46 +    ucx_buffer_clear(b1);
    4.47 +    ucx_buffer_seek(b2, 0, SEEK_SET);
    4.48 +    ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
    4.49 +    UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
    4.50 +    UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
    4.51 +        "content wrong after ncopy");
    4.52 +    
    4.53 +    UCX_TEST_END
    4.54 +    
    4.55 +    ucx_buffer_free(b1);
    4.56 +    ucx_buffer_free(b2);
    4.57 +}
     5.1 --- a/test/utils_tests.h	Tue May 06 10:56:54 2014 +0200
     5.2 +++ b/test/utils_tests.h	Tue May 06 12:03:16 2014 +0200
     5.3 @@ -38,6 +38,7 @@
     5.4  
     5.5  UCX_TEST(test_ucx_fprintf);
     5.6  UCX_TEST(test_ucx_asprintf);
     5.7 +UCX_TEST(test_ucx_stream_copy);
     5.8  
     5.9  #ifdef	__cplusplus
    5.10  }
     6.1 --- a/ucx/buffer.h	Tue May 06 10:56:54 2014 +0200
     6.2 +++ b/ucx/buffer.h	Tue May 06 12:03:16 2014 +0200
     6.3 @@ -120,8 +120,7 @@
     6.4   * 
     6.5   * @param src the source buffer
     6.6   * @param start the start position of extraction
     6.7 - * @param length the count of bytes to extract or 0 if all of the remaining
     6.8 - * bytes shall be extracted
     6.9 + * @param length the count of bytes to extract (must not be zero)
    6.10   * @param flags feature mask for the new buffer
    6.11   * @return a new buffer containing the extraction
    6.12   */
    6.13 @@ -136,7 +135,7 @@
    6.14   * @return a new buffer with the extracted content
    6.15   */
    6.16  #define ucx_buffer_clone(src,flags) \
    6.17 -    ucx_buffer_extract(src, 0, 0, flags)
    6.18 +    ucx_buffer_extract(src, 0, (src)->capacity, flags)
    6.19  
    6.20  /**
    6.21   * Moves the position of the buffer.
    6.22 @@ -186,12 +185,12 @@
    6.23   * the buffer capacity is doubled, as long as it would not hold the current
    6.24   * content plus the additional required bytes.
    6.25   * 
    6.26 - * <b>Attention:</b> the argument provided is the count of <i>additional</i>
    6.27 - * bytes the buffer shall hold. It is <b>NOT</b> the total count of bytes the
    6.28 + * <b>Attention:</b> the argument provided is the number of <i>additional</i>
    6.29 + * bytes the buffer shall hold. It is <b>NOT</b> the total number of bytes the
    6.30   * buffer shall hold.
    6.31   * 
    6.32   * @param buffer the buffer to extend
    6.33 - * @param additional_bytes the count of additional bytes the buffer shall
    6.34 + * @param additional_bytes the number of additional bytes the buffer shall
    6.35   * <i>at least</i> hold
    6.36   * @return 0 on success or a non-zero value on failure
    6.37   */

mercurial