test/buffer_tests.c

Mon, 05 May 2014 13:53:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 May 2014 13:53:27 +0200
changeset 164
1fa3f13f774c
parent 163
5ec9a2ca6328
child 166
350a0e3898bd
permissions
-rw-r--r--

added allocator tests + started refactoring UcxBuffer tests (HINT: don't fix issues yet, complete tests first)

     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 /*
    33  * TODO: refactor tests
    34  *  
    35  * ucx_buffer_extend
    36  * ucx_buffer_extract
    37  * ucx_buffer_free
    38  * ucx_buffer_getc
    39  * ucx_buffer_new
    40  * ucx_buffer_puts
    41  * ucx_buffer_read
    42  * ucx_buffer_write
    43  * 
    44  */
    46 UCX_TEST(test_ucx_buffer_eof) {
    47     char *test = "0123456789ABCDEF";
    48     UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    49     UCX_TEST_BEGIN
    50     b->pos = 9; b->size = 10;
    51     UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive");
    52     b->pos = 10; b->size = 10;
    53     UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF");
    54     b->pos = 11; b->size = 10;
    55     UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative");
    56     UCX_TEST_END
    57 }
    59 UCX_TEST(test_ucx_buffer_seek_overflow) {
    60     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    61     b->size = 32;
    62     int r;
    64     UCX_TEST_BEGIN
    65     size_t bigpos = SIZE_MAX - 5000;
    66     b->pos = bigpos;
    67     r = ucx_buffer_seek(b, 5016, SEEK_CUR);
    68     UCX_TEST_ASSERT(r != 0, "seek cur overflow");
    69     UCX_TEST_ASSERT(b->pos == bigpos,
    70             "failed seek shall leave pos unchanged");
    72     b->pos = 0;
    73     b->size = SIZE_MAX / 2 + 32;
    74     off_t bigoff = SIZE_MAX / 2 - 16;
    75     r = ucx_buffer_seek(b, -bigoff, SEEK_CUR);
    76     UCX_TEST_ASSERT(r != 0, "seek cur underflow");
    77     UCX_TEST_ASSERT(b->pos == 0,
    78             "failed seek shall leave pos unchanged");
    80     UCX_TEST_END
    82     ucx_buffer_free(b);
    83 }
    85 UCX_TEST(test_ucx_buffer_seek_invalid) {
    86     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    87     b->pos = 7;
    88     int r;
    90     UCX_TEST_BEGIN
    91     r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END));
    92     UCX_TEST_ASSERT(r != 0, "invalid whence shall fail");
    93     UCX_TEST_ASSERT(b->pos == 7,
    94             "failed seek shall leave pos unchanged");
    95     UCX_TEST_END
    97     ucx_buffer_free(b);
    98 }
   100 UCX_TEST(test_ucx_buffer_seek_oob) {
   101     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   102     b->size = 16; // less than capacity
   103     b->pos = 7;
   104     int r;
   106     UCX_TEST_BEGIN
   108     r = ucx_buffer_seek(b, -1, SEEK_SET);
   109     UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail");
   110     UCX_TEST_ASSERT(b->pos == 7,
   111             "failed seek shall leave pos unchanged");
   113     r = ucx_buffer_seek(b, 16, SEEK_SET);
   114     UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail");
   115     UCX_TEST_ASSERT(b->pos == 7,
   116             "failed seek shall leave pos unchanged");
   118     r = ucx_buffer_seek(b, -8, SEEK_CUR);
   119     UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail");
   120     UCX_TEST_ASSERT(b->pos == 7,
   121             "failed seek shall leave pos unchanged");
   123     r = ucx_buffer_seek(b, 9, SEEK_CUR);
   124     UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail");
   125     UCX_TEST_ASSERT(b->pos == 7,
   126             "failed seek shall leave pos unchanged");
   128     r = ucx_buffer_seek(b, -17, SEEK_END);
   129     UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail");
   130     UCX_TEST_ASSERT(b->pos == 7,
   131             "failed seek shall leave pos unchanged");
   133     r = ucx_buffer_seek(b, 1, SEEK_END);
   134     UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail");
   135     UCX_TEST_ASSERT(b->pos == 7,
   136             "failed seek shall leave pos unchanged");
   138     UCX_TEST_END
   140     ucx_buffer_free(b);
   141 }
   143 UCX_TEST(test_ucx_buffer_seek_set) {
   144     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   145     b->size = 16;
   146     int r;
   148     UCX_TEST_BEGIN
   150     r = ucx_buffer_seek(b, 5, SEEK_SET);
   151     UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
   152     UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
   155     r = ucx_buffer_seek(b, 10, SEEK_SET);
   156     UCX_TEST_ASSERT(r == 0, "seek SET+10 failed");
   157     UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position");
   159     UCX_TEST_END
   161     ucx_buffer_free(b);
   162 }
   164 UCX_TEST(test_ucx_buffer_seek_cur) {
   165     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   166     b->size = 16;
   167     int r;
   169     UCX_TEST_BEGIN
   171     b->pos = 7;
   172     r = ucx_buffer_seek(b, 5, SEEK_CUR);
   173     UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
   174     UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position");
   176     UCX_TEST_END
   178     ucx_buffer_free(b);
   179 }
   181 UCX_TEST(test_ucx_buffer_seek_end) {
   182     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   183     b->size = 16;
   184     int r;
   186     UCX_TEST_BEGIN
   188     r = ucx_buffer_seek(b, -5, SEEK_END);
   189     UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
   190     UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");
   193     UCX_TEST_END
   195     ucx_buffer_free(b);
   196 }
   198 UCX_TEST(test_ucx_buffer_putc) {
   199     char *buffer = (char*) malloc(16);
   200     memset(buffer, 32, 16);
   202     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   203     b->size = b->capacity;
   205     UCX_TEST_BEGIN
   207     ucx_buffer_seek(b, 0, SEEK_SET);
   208     UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0',
   209             "putc shall return (arg & 0xFF)");
   210     ucx_buffer_putc(b, '0');
   211     ucx_buffer_putc(b, '0');
   213     UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
   214     ucx_buffer_seek(b, 10, SEEK_CUR);
   216     ucx_buffer_putc(b, '0');
   217     ucx_buffer_putc(b, '0');
   218     ucx_buffer_putc(b, '0');
   220     UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
   222     UCX_TEST_ASSERT(!memcmp(b->space, "000          000", 16),
   223             "buffer content wrong")
   224     UCX_TEST_END
   225     ucx_buffer_free(b);
   226     free(buffer);
   227 }
   229 UCX_TEST(test_ucx_buffer_putc_oob) {
   230     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT);
   232     UCX_TEST_BEGIN
   233     b->pos = b->size = b->capacity = 1;
   234     b->space[1] = 'X';
   236     UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF "
   237             "when buffer is full and auto extend is disabled");
   238     UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2),
   239             "wrong buffer content after failed putc");
   241     UCX_TEST_END
   243     ucx_buffer_free(b);
   244 }
   247 UCX_TEST(test_ucx_buffer_putc_ae) {
   248     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   249     ucx_buffer_putc(b, '0');
   250     ucx_buffer_putc(b, '1');
   252     UCX_TEST_BEGIN
   254     UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
   255     UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts");
   256     UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
   257     UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content");
   259     UCX_TEST_END
   261     ucx_buffer_free(b);
   262 }
   264 UCX_TEST(test_ucx_buffer_putc_oobae) {
   265     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   266     ucx_buffer_putc(b, '0');
   267     ucx_buffer_putc(b, '1');
   269     UCX_TEST_BEGIN
   271     ucx_buffer_putc(b, 'a');
   273     UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put");
   274     UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
   275     UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
   277     UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content");
   279     UCX_TEST_END
   281     ucx_buffer_free(b);
   282 }
   284 UCX_TEST(test_ucx_buffer_putc_size) {
   285     UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT);
   287     UCX_TEST_BEGIN
   289     UCX_TEST_ASSERT(b->size == 0, "wrong initial size");
   290     ucx_buffer_putc(b, 'a');
   291     ucx_buffer_putc(b, 'b');
   292     ucx_buffer_putc(b, 'c');
   293     UCX_TEST_ASSERT(b->size == 3, "size does not increase");
   294     ucx_buffer_seek(b, 1, SEEK_SET);
   295     ucx_buffer_putc(b, 'd');
   296     UCX_TEST_ASSERT(b->size == 3, "size shall not decrease");
   297     UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc");
   299     UCX_TEST_END
   301     ucx_buffer_free(b);
   302 }
   304 UCX_TEST(test_ucx_buffer_getc) {
   305     char *buffer = (char*) malloc(16);
   306     memset(buffer, 32, 8);
   307     for (int i = 8; i < 16 ; i++) {
   308         buffer[i] = 40+i;
   309     }
   311     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   312     b->size = b->capacity;
   314     UCX_TEST_BEGIN
   316     char rb[16];
   317     for (size_t i = 0 ; i < 16 ; i++) {
   318         UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop");
   319         UCX_TEST_ASSERT(!ucx_buffer_eof(b),
   320                 "EOF shall not be set during read loop");
   321         rb[i] = ucx_buffer_getc(b);
   322     }
   323     UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
   324     UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
   325     UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
   326             "read data incorrect");
   328     UCX_TEST_END
   330     ucx_buffer_free(b);
   331     free(buffer);
   332 }
   334 UCX_TEST(test_ucx_buffer_write) {
   335     char *buffer = (char*) malloc(16);
   336     memset(buffer, 32, 8);
   337     for (int i = 8; i < 16 ; i++) {
   338         buffer[i] = 40+i;
   339     }
   341     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   342     int r;
   344     UCX_TEST_BEGIN
   346     const char* teststring = "this is way too much";
   347     r = ucx_buffer_write((void*)teststring, 1, 20, b);
   348     UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
   349     UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
   350             "buffer data incorrect");
   351     UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
   353     ucx_buffer_seek(b, 8, SEEK_SET);
   354     r = ucx_buffer_write((void*)"not", 1, 3, b);
   355     UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
   356     UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
   357             "modified buffer is incorrect");
   359     const char* threebytestring = "  t  h  r  e  e   ";
   360     memset(buffer, 49, 16);
   361     ucx_buffer_seek(b, 0, SEEK_SET);
   362     r = ucx_buffer_write((void*)threebytestring, 3, 6, b);
   363     UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed");
   364     UCX_TEST_ASSERT(b->pos == 15,
   365             "position after write of three byte string incorrect");
   366     UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
   367     UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
   368                 "bufer is incorrect after three byte string has been written");
   370     UCX_TEST_END
   372     ucx_buffer_free(b);
   373     free(buffer);
   374 }
   376 UCX_TEST(test_ucx_buffer_write_ax) {
   377     char *buffer = (char*) malloc(16);
   379     UcxBuffer *b = ucx_buffer_new(buffer, 16,
   380             UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE);
   381     int r;
   383     UCX_TEST_BEGIN
   385     const char* teststring = "this is way too much";
   386     r = ucx_buffer_write((void*)teststring, 1, 20, b);
   387     buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/
   388     UCX_TEST_ASSERT(r == 20, "not all characters written");
   389     UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended");
   390     UCX_TEST_ASSERT(b->pos == 20, "position incorrect");
   391     UCX_TEST_ASSERT(memcmp(buffer,
   392             "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0,
   393             "incorrect buffer content");
   395     UCX_TEST_END
   397     ucx_buffer_free(b);
   398 }
   400 UCX_TEST(test_ucx_buffer_read) {
   401     UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
   403     char buf[32];
   404     memset(buf, 'X', 32);
   405     int r;
   407     UCX_TEST_BEGIN
   409     ucx_buffer_write("01234567", 1, 8, b);
   410     UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled");
   411     b->pos = 0;
   413     r = ucx_buffer_read(buf, 1, 2, b);
   414     UCX_TEST_ASSERT(r == 2, "wrong number of bytes read");
   415     UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X',
   416             "buffer incorrect after first read");
   418     r = ucx_buffer_read(buf + 2, 1, 8, b);
   419     UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)");
   420     UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
   421             "buffer incorrect after second read");
   423     memset(buf, 'X', 32);
   424     ucx_buffer_seek(b, 0, SEEK_SET);
   425     r = ucx_buffer_read(buf, 3, 3, b);
   427     UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
   428     UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
   429             "buffer incorrect after three byte read");
   432     UCX_TEST_END
   434     ucx_buffer_free(b);
   435 }
   437 UCX_TEST(test_ucx_buffer_extract) {
   438     char *buffer = (char*) malloc(16);
   439     strcpy(buffer, "this is a test!");
   441     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   442     src->size = 15;
   443     UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT);
   445     UCX_TEST_BEGIN
   446     UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
   448     UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE,
   449             "autofree flag shall be enforced");
   450     UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
   451     char rb[5];
   452     ucx_buffer_read(rb, 1, 5, dst);
   453     UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
   454             "new buffer has incorrect content");
   456     UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL,
   457             "extract shall fail on invalid bounds");
   459     UCX_TEST_END
   461     ucx_buffer_free(dst);
   462     ucx_buffer_free(src);
   463 }
   465 UCX_TEST(test_ucx_stream_copy) {
   466     UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
   467     UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   469     UCX_TEST_BEGIN
   471     ucx_buffer_write("01234567", 1, 8, b1);
   472     ucx_buffer_write("abcdefgh", 1, 8, b1);
   473     UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
   474     ucx_buffer_seek(b1, 0, SEEK_SET);
   476     size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
   477     UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
   478     UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
   479     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   480             "b1 and b2 have not the same content");
   482     memset(b2->space, 0, b2->capacity);
   483     b2->pos = 0;
   484     b2->size = 0;
   485     ucx_buffer_seek(b1, 0, SEEK_SET);
   487     FILE *file = tmpfile();
   488     UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
   490     ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
   491     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
   493     fseek(file, 0, SEEK_SET);
   495     ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
   496     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
   498     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   499             "b1 and b2 content mismatch");
   501     fclose(file);
   503     ucx_buffer_clear(b1);
   504     ucx_buffer_seek(b2, 0, SEEK_SET);
   505     ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
   506     UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
   507     UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
   508         "content wrong after ncopy");
   510     UCX_TEST_END
   512     ucx_buffer_free(b1);
   513     ucx_buffer_free(b2);
   514 }

mercurial