test/buffer_tests.c

changeset 166
350a0e3898bd
parent 164
1fa3f13f774c
child 167
aed60ba37acf
     1.1 --- a/test/buffer_tests.c	Mon May 05 14:52:40 2014 +0200
     1.2 +++ b/test/buffer_tests.c	Mon May 05 15:56:39 2014 +0200
     1.3 @@ -34,15 +34,50 @@
     1.4   *  
     1.5   * ucx_buffer_extend
     1.6   * ucx_buffer_extract
     1.7 - * ucx_buffer_free
     1.8 - * ucx_buffer_getc
     1.9 - * ucx_buffer_new
    1.10 - * ucx_buffer_puts
    1.11 - * ucx_buffer_read
    1.12   * ucx_buffer_write
    1.13   * 
    1.14   */
    1.15  
    1.16 +UCX_TEST(test_ucx_buffer_new) {
    1.17 +    UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND);
    1.18 +    UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    1.19 +    UCX_TEST_BEGIN
    1.20 +
    1.21 +    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    1.22 +    UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity");
    1.23 +    
    1.24 +    UCX_TEST_ASSERT(b->size==0, "wrong size");
    1.25 +    UCX_TEST_ASSERT(b2->size==0, "wrong size");
    1.26 +    
    1.27 +    UCX_TEST_ASSERT(b->pos==0, "wrong position");
    1.28 +    UCX_TEST_ASSERT(b2->pos==0, "wrong position");
    1.29 +    
    1.30 +    UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE),
    1.31 +        "wrong flags for autoextending buffer");
    1.32 +    UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE,
    1.33 +        "wrong flags for default bufer");
    1.34 +    
    1.35 +    UCX_TEST_END
    1.36 +    ucx_buffer_free(b2);
    1.37 +    ucx_buffer_free(b);
    1.38 +}
    1.39 +
    1.40 +UCX_TEST(test_ucx_buffer_new_prealloc) {
    1.41 +    char* test = (char*) malloc(16);
    1.42 +    UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    1.43 +    UCX_TEST_BEGIN
    1.44 +
    1.45 +    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    1.46 +    UCX_TEST_ASSERT(b->size==0, "wrong size");    
    1.47 +    UCX_TEST_ASSERT(b->pos==0, "wrong position");
    1.48 +    
    1.49 +    UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared");
    1.50 +    
    1.51 +    UCX_TEST_END
    1.52 +    free(test);
    1.53 +    ucx_buffer_free(b);
    1.54 +}
    1.55 +
    1.56  UCX_TEST(test_ucx_buffer_eof) {
    1.57      char *test = "0123456789ABCDEF";
    1.58      UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    1.59 @@ -315,15 +350,19 @@
    1.60  
    1.61      char rb[16];
    1.62      for (size_t i = 0 ; i < 16 ; i++) {
    1.63 -        UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop");
    1.64 -        UCX_TEST_ASSERT(!ucx_buffer_eof(b),
    1.65 -                "EOF shall not be set during read loop");
    1.66 +        UCX_TEST_ASSERT(b->pos == i, "wrong position");
    1.67 +        UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive");
    1.68          rb[i] = ucx_buffer_getc(b);
    1.69      }
    1.70 -    UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
    1.71 -    UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
    1.72      UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
    1.73 -            "read data incorrect");
    1.74 +        "read data incorrect");
    1.75 +        
    1.76 +    UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read");
    1.77 +    UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF");
    1.78 +    
    1.79 +    UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF,
    1.80 +        "out of bounds read does not return EOF");
    1.81 +    UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read");
    1.82  
    1.83      UCX_TEST_END
    1.84  
    1.85 @@ -399,6 +438,8 @@
    1.86  
    1.87  UCX_TEST(test_ucx_buffer_read) {
    1.88      UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
    1.89 +    ucx_buffer_write("01234567", 1, 8, b);
    1.90 +    b->pos = 0;
    1.91      
    1.92      char buf[32];
    1.93      memset(buf, 'X', 32);
    1.94 @@ -406,28 +447,50 @@
    1.95      
    1.96      UCX_TEST_BEGIN
    1.97      
    1.98 +    ucx_buffer_seek(b, 2, SEEK_SET);
    1.99 +    r = ucx_buffer_read(buf, 1, 2, b);
   1.100 +    UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)");
   1.101 +    UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X',
   1.102 +            "buffer incorrect after read");
   1.103 +    UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)");
   1.104 +    
   1.105 +    UCX_TEST_END
   1.106 +    
   1.107 +    ucx_buffer_free(b);
   1.108 +}
   1.109 +
   1.110 +UCX_TEST(test_ucx_buffer_read_oob) {
   1.111 +    UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
   1.112      ucx_buffer_write("01234567", 1, 8, b);
   1.113 -    UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled");
   1.114 +    
   1.115 +    char buf[32];
   1.116 +    memset(buf, 'X', 32);
   1.117 +    int r;
   1.118 +    
   1.119 +    UCX_TEST_BEGIN
   1.120 +
   1.121 +    b->pos = 2;
   1.122 +    r = ucx_buffer_read(buf + 2, 1, 8, b);
   1.123 +    UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)");
   1.124 +    UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0,
   1.125 +            "buffer incorrect after read");
   1.126 +    UCX_TEST_ASSERT(b->pos == 8,
   1.127 +        "wrong position after read (8 items out of bound)");
   1.128 +
   1.129      b->pos = 0;
   1.130 +    memset(buf, 'X', 32);
   1.131      
   1.132 -    r = ucx_buffer_read(buf, 1, 2, b);
   1.133 -    UCX_TEST_ASSERT(r == 2, "wrong number of bytes read");
   1.134 -    UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X',
   1.135 -            "buffer incorrect after first read");
   1.136 -    
   1.137 -    r = ucx_buffer_read(buf + 2, 1, 8, b);
   1.138 -    UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)");
   1.139 -    UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
   1.140 -            "buffer incorrect after second read");
   1.141 -    
   1.142 -    memset(buf, 'X', 32);
   1.143 -    ucx_buffer_seek(b, 0, SEEK_SET);
   1.144      r = ucx_buffer_read(buf, 3, 3, b);
   1.145      
   1.146      UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
   1.147      UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
   1.148 -            "buffer incorrect after three byte read");
   1.149 +            "buffer incorrect after block out of bounds read");
   1.150      
   1.151 +    r = ucx_buffer_read(buf+6, 1, 5, b);
   1.152 +    
   1.153 +    UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read");
   1.154 +    UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
   1.155 +            "buffer incorrect after remaining byte read");
   1.156      
   1.157      UCX_TEST_END
   1.158      

mercurial