test/buffer_tests.c

Thu, 17 Apr 2014 14:33:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 17 Apr 2014 14:33:06 +0200
changeset 163
5ec9a2ca6328
parent 147
1aa598f36872
child 164
1fa3f13f774c
permissions
-rw-r--r--

minor lexical and documentation fixes

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 Olaf Wintermann. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "buffer_tests.h"
    30 #include "ucx/utils.h"
    32 UCX_TEST(test_ucx_buffer_seek) {
    33     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    34     b->size = 16; // less than capacity
    35     int r;
    37     UCX_TEST_BEGIN
    39     r = ucx_buffer_seek(b, 5, SEEK_SET);
    40     UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
    41     UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
    43     r = ucx_buffer_seek(b, 20, SEEK_SET);
    44     UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
    45     UCX_TEST_ASSERT(b->pos == 5,
    46             "failed seek shall leave pos unchanged");
    48     r = ucx_buffer_seek(b, 5, SEEK_CUR);
    49     UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
    50     UCX_TEST_ASSERT(b->pos == 10, "seek CUR+5 set wrong position");
    52     r = ucx_buffer_seek(b, 10, SEEK_CUR);
    53     UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
    54     UCX_TEST_ASSERT(b->pos == 10,
    55             "failed seek shall leave pos unchanged");
    57     r = ucx_buffer_seek(b, -5, SEEK_END);
    58     UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
    59     UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");
    61     r = ucx_buffer_seek(b, -20, SEEK_END);
    62     UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
    63     UCX_TEST_ASSERT(b->pos == 11,
    64             "failed seek shall leave pos unchanged");
    66     UCX_TEST_END
    68     ucx_buffer_free(b);
    69 }
    71 UCX_TEST(test_ucx_buffer_putc) {
    72     char *buffer = (char*) malloc(16);
    73     memset(buffer, 32, 16);
    75     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
    76     b->size = b->capacity;
    78     UCX_TEST_BEGIN
    80     ucx_buffer_putc(b, '0');
    81     ucx_buffer_putc(b, '0');
    82     ucx_buffer_putc(b, '0');
    84     UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
    85     ucx_buffer_seek(b, 10, SEEK_CUR);
    87     ucx_buffer_putc(b, '0');
    88     ucx_buffer_putc(b, '0');
    89     ucx_buffer_putc(b, '0');
    91     UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
    92     UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set");
    93     UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF,
    94             "put shall return EOF when buffer is full");
    96     ucx_buffer_seek(b, 3, SEEK_SET);
    97     ucx_buffer_putc(b, 'a');
    98     ucx_buffer_putc(b, 'b');
    99     ucx_buffer_putc(b, 'c');
   101     UCX_TEST_ASSERT(b->size == 16, "wrong size after seek and puts");
   102     UCX_TEST_ASSERT(memcmp(buffer, "000abc       000", 16) == 0,
   103             "buffer contains incorrect content");
   105     UCX_TEST_END
   107     ucx_buffer_free(b);
   108     free(buffer);
   109 }
   111 UCX_TEST(test_ucx_buffer_putc_ax) {
   112     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   114     UCX_TEST_BEGIN
   116     ucx_buffer_putc(b, '0');
   117     ucx_buffer_putc(b, '1');
   119     UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
   120     UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
   122     ucx_buffer_putc(b, 'a');
   124     UCX_TEST_ASSERT(b->pos == 3, "pos wrong after 1 put");
   125     UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
   126     UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
   128     UCX_TEST_ASSERT(b->space[2] == 'a', "wrong content");
   130     UCX_TEST_END
   132     ucx_buffer_free(b);
   133 }
   135 UCX_TEST(test_ucx_buffer_getc) {
   136     char *buffer = (char*) malloc(16);
   137     memset(buffer, 32, 8);
   138     for (int i = 8; i < 16 ; i++) {
   139         buffer[i] = 40+i;
   140     }
   142     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   143     b->size = b->capacity;
   145     UCX_TEST_BEGIN
   147     char rb[16];
   148     for (size_t i = 0 ; i < 16 ; i++) {
   149         UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop");
   150         UCX_TEST_ASSERT(!ucx_buffer_eof(b),
   151                 "EOF shall not be set during read loop");
   152         rb[i] = ucx_buffer_getc(b);
   153     }
   154     UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
   155     UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
   156     UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
   157             "read data incorrect");
   159     UCX_TEST_END
   161     ucx_buffer_free(b);
   162     free(buffer);
   163 }
   165 UCX_TEST(test_ucx_buffer_write) {
   166     char *buffer = (char*) malloc(16);
   167     memset(buffer, 32, 8);
   168     for (int i = 8; i < 16 ; i++) {
   169         buffer[i] = 40+i;
   170     }
   172     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   173     int r;
   175     UCX_TEST_BEGIN
   177     const char* teststring = "this is way too much";
   178     r = ucx_buffer_write((void*)teststring, 1, 20, b);
   179     UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
   180     UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
   181             "buffer data incorrect");
   182     UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
   184     ucx_buffer_seek(b, 8, SEEK_SET);
   185     r = ucx_buffer_write((void*)"not", 1, 3, b);
   186     UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
   187     UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
   188             "modified buffer is incorrect");
   190     const char* threebytestring = "  t  h  r  e  e   ";
   191     memset(buffer, 49, 16);
   192     ucx_buffer_seek(b, 0, SEEK_SET);
   193     r = ucx_buffer_write((void*)threebytestring, 3, 6, b);
   194     UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed");
   195     UCX_TEST_ASSERT(b->pos == 15,
   196             "position after write of three byte string incorrect");
   197     UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
   198     UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
   199                 "bufer is incorrect after three byte string has been written");
   201     UCX_TEST_END
   203     ucx_buffer_free(b);
   204     free(buffer);
   205 }
   207 UCX_TEST(test_ucx_buffer_write_ax) {
   208     char *buffer = (char*) malloc(16);
   210     UcxBuffer *b = ucx_buffer_new(buffer, 16,
   211             UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE);
   212     int r;
   214     UCX_TEST_BEGIN
   216     const char* teststring = "this is way too much";
   217     r = ucx_buffer_write((void*)teststring, 1, 20, b);
   218     buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/
   219     UCX_TEST_ASSERT(r == 20, "not all characters written");
   220     UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended");
   221     UCX_TEST_ASSERT(b->pos == 20, "position incorrect");
   222     UCX_TEST_ASSERT(memcmp(buffer,
   223             "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0,
   224             "incorrect buffer content");
   226     UCX_TEST_END
   228     ucx_buffer_free(b);
   229 }
   231 UCX_TEST(test_ucx_buffer_read) {
   232     UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
   234     char buf[32];
   235     memset(buf, 'X', 32);
   236     int r;
   238     UCX_TEST_BEGIN
   240     ucx_buffer_write("01234567", 1, 8, b);
   241     UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled");
   242     b->pos = 0;
   244     r = ucx_buffer_read(buf, 1, 2, b);
   245     UCX_TEST_ASSERT(r == 2, "wrong number of bytes read");
   246     UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X',
   247             "buffer incorrect after first read");
   249     r = ucx_buffer_read(buf + 2, 1, 8, b);
   250     UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)");
   251     UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
   252             "buffer incorrect after second read");
   254     memset(buf, 'X', 32);
   255     ucx_buffer_seek(b, 0, SEEK_SET);
   256     r = ucx_buffer_read(buf, 3, 3, b);
   258     UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
   259     UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
   260             "buffer incorrect after three byte read");
   263     UCX_TEST_END
   265     ucx_buffer_free(b);
   266 }
   268 UCX_TEST(test_ucx_buffer_extract) {
   269     char *buffer = (char*) malloc(16);
   270     strcpy(buffer, "this is a test!");
   272     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   273     src->size = 15;
   274     UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT);
   276     UCX_TEST_BEGIN
   277     UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
   279     UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE,
   280             "autofree flag shall be enforced");
   281     UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
   282     char rb[5];
   283     ucx_buffer_read(rb, 1, 5, dst);
   284     UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
   285             "new buffer has incorrect content");
   287     UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL,
   288             "extract shall fail on invalid bounds");
   290     UCX_TEST_END
   292     ucx_buffer_free(dst);
   293     ucx_buffer_free(src);
   294 }
   296 UCX_TEST(test_ucx_stream_copy) {
   297     UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
   298     UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   300     UCX_TEST_BEGIN
   302     ucx_buffer_write("01234567", 1, 8, b1);
   303     ucx_buffer_write("abcdefgh", 1, 8, b1);
   304     UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
   305     ucx_buffer_seek(b1, 0, SEEK_SET);
   307     size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
   308     UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
   309     UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
   310     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   311             "b1 and b2 have not the same content");
   313     memset(b2->space, 0, b2->capacity);
   314     b2->pos = 0;
   315     b2->size = 0;
   316     ucx_buffer_seek(b1, 0, SEEK_SET);
   318     FILE *file = tmpfile();
   319     UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
   321     ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
   322     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
   324     fseek(file, 0, SEEK_SET);
   326     ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
   327     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
   329     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   330             "b1 and b2 content mismatch");
   332     fclose(file);
   334     ucx_buffer_clear(b1);
   335     ucx_buffer_seek(b2, 0, SEEK_SET);
   336     ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
   337     UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
   338     UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
   339         "content wrong after ncopy");
   341     UCX_TEST_END
   343     ucx_buffer_free(b1);
   344     ucx_buffer_free(b2);
   345 }

mercurial