test/buffer_tests.c

Tue, 06 May 2014 10:56:54 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 06 May 2014 10:56:54 +0200
changeset 167
aed60ba37acf
parent 166
350a0e3898bd
child 168
24a012440dee
permissions
-rw-r--r--

ucx_buffer_extract tests

     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_write
    38  * 
    39  */
    41 UCX_TEST(test_ucx_buffer_new) {
    42     UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND);
    43     UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    44     UCX_TEST_BEGIN
    46     UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    47     UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity");
    49     UCX_TEST_ASSERT(b->size==0, "wrong size");
    50     UCX_TEST_ASSERT(b2->size==0, "wrong size");
    52     UCX_TEST_ASSERT(b->pos==0, "wrong position");
    53     UCX_TEST_ASSERT(b2->pos==0, "wrong position");
    55     UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE),
    56         "wrong flags for autoextending buffer");
    57     UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE,
    58         "wrong flags for default bufer");
    60     UCX_TEST_END
    61     ucx_buffer_free(b2);
    62     ucx_buffer_free(b);
    63 }
    65 UCX_TEST(test_ucx_buffer_new_prealloc) {
    66     char* test = (char*) malloc(16);
    67     UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    68     UCX_TEST_BEGIN
    70     UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    71     UCX_TEST_ASSERT(b->size==0, "wrong size");    
    72     UCX_TEST_ASSERT(b->pos==0, "wrong position");
    74     UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared");
    76     UCX_TEST_END
    77     free(test);
    78     ucx_buffer_free(b);
    79 }
    81 UCX_TEST(test_ucx_buffer_eof) {
    82     char *test = "0123456789ABCDEF";
    83     UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    84     UCX_TEST_BEGIN
    85     b->pos = 9; b->size = 10;
    86     UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive");
    87     b->pos = 10; b->size = 10;
    88     UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF");
    89     b->pos = 11; b->size = 10;
    90     UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative");
    91     UCX_TEST_END
    92 }
    94 UCX_TEST(test_ucx_buffer_seek_overflow) {
    95     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    96     b->size = 32;
    97     int r;
    99     UCX_TEST_BEGIN
   100     size_t bigpos = SIZE_MAX - 5000;
   101     b->pos = bigpos;
   102     r = ucx_buffer_seek(b, 5016, SEEK_CUR);
   103     UCX_TEST_ASSERT(r != 0, "seek cur overflow");
   104     UCX_TEST_ASSERT(b->pos == bigpos,
   105             "failed seek shall leave pos unchanged");
   107     b->pos = 0;
   108     b->size = SIZE_MAX / 2 + 32;
   109     off_t bigoff = SIZE_MAX / 2 - 16;
   110     r = ucx_buffer_seek(b, -bigoff, SEEK_CUR);
   111     UCX_TEST_ASSERT(r != 0, "seek cur underflow");
   112     UCX_TEST_ASSERT(b->pos == 0,
   113             "failed seek shall leave pos unchanged");
   115     UCX_TEST_END
   117     ucx_buffer_free(b);
   118 }
   120 UCX_TEST(test_ucx_buffer_seek_invalid) {
   121     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   122     b->pos = 7;
   123     int r;
   125     UCX_TEST_BEGIN
   126     r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END));
   127     UCX_TEST_ASSERT(r != 0, "invalid whence shall fail");
   128     UCX_TEST_ASSERT(b->pos == 7,
   129             "failed seek shall leave pos unchanged");
   130     UCX_TEST_END
   132     ucx_buffer_free(b);
   133 }
   135 UCX_TEST(test_ucx_buffer_seek_oob) {
   136     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   137     b->size = 16; // less than capacity
   138     b->pos = 7;
   139     int r;
   141     UCX_TEST_BEGIN
   143     r = ucx_buffer_seek(b, -1, SEEK_SET);
   144     UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail");
   145     UCX_TEST_ASSERT(b->pos == 7,
   146             "failed seek shall leave pos unchanged");
   148     r = ucx_buffer_seek(b, 16, SEEK_SET);
   149     UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail");
   150     UCX_TEST_ASSERT(b->pos == 7,
   151             "failed seek shall leave pos unchanged");
   153     r = ucx_buffer_seek(b, -8, SEEK_CUR);
   154     UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail");
   155     UCX_TEST_ASSERT(b->pos == 7,
   156             "failed seek shall leave pos unchanged");
   158     r = ucx_buffer_seek(b, 9, SEEK_CUR);
   159     UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail");
   160     UCX_TEST_ASSERT(b->pos == 7,
   161             "failed seek shall leave pos unchanged");
   163     r = ucx_buffer_seek(b, -17, SEEK_END);
   164     UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail");
   165     UCX_TEST_ASSERT(b->pos == 7,
   166             "failed seek shall leave pos unchanged");
   168     r = ucx_buffer_seek(b, 1, SEEK_END);
   169     UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail");
   170     UCX_TEST_ASSERT(b->pos == 7,
   171             "failed seek shall leave pos unchanged");
   173     UCX_TEST_END
   175     ucx_buffer_free(b);
   176 }
   178 UCX_TEST(test_ucx_buffer_seek_set) {
   179     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   180     b->size = 16;
   181     int r;
   183     UCX_TEST_BEGIN
   185     r = ucx_buffer_seek(b, 5, SEEK_SET);
   186     UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
   187     UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
   190     r = ucx_buffer_seek(b, 10, SEEK_SET);
   191     UCX_TEST_ASSERT(r == 0, "seek SET+10 failed");
   192     UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position");
   194     UCX_TEST_END
   196     ucx_buffer_free(b);
   197 }
   199 UCX_TEST(test_ucx_buffer_seek_cur) {
   200     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   201     b->size = 16;
   202     int r;
   204     UCX_TEST_BEGIN
   206     b->pos = 7;
   207     r = ucx_buffer_seek(b, 5, SEEK_CUR);
   208     UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
   209     UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position");
   211     UCX_TEST_END
   213     ucx_buffer_free(b);
   214 }
   216 UCX_TEST(test_ucx_buffer_seek_end) {
   217     UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   218     b->size = 16;
   219     int r;
   221     UCX_TEST_BEGIN
   223     r = ucx_buffer_seek(b, -5, SEEK_END);
   224     UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
   225     UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");
   228     UCX_TEST_END
   230     ucx_buffer_free(b);
   231 }
   233 UCX_TEST(test_ucx_buffer_putc) {
   234     char *buffer = (char*) malloc(16);
   235     memset(buffer, 32, 16);
   237     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   238     b->size = b->capacity;
   240     UCX_TEST_BEGIN
   242     ucx_buffer_seek(b, 0, SEEK_SET);
   243     UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0',
   244             "putc shall return (arg & 0xFF)");
   245     ucx_buffer_putc(b, '0');
   246     ucx_buffer_putc(b, '0');
   248     UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
   249     ucx_buffer_seek(b, 10, SEEK_CUR);
   251     ucx_buffer_putc(b, '0');
   252     ucx_buffer_putc(b, '0');
   253     ucx_buffer_putc(b, '0');
   255     UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
   257     UCX_TEST_ASSERT(!memcmp(b->space, "000          000", 16),
   258             "buffer content wrong")
   259     UCX_TEST_END
   260     ucx_buffer_free(b);
   261     free(buffer);
   262 }
   264 UCX_TEST(test_ucx_buffer_putc_oob) {
   265     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT);
   267     UCX_TEST_BEGIN
   268     b->pos = b->size = b->capacity = 1;
   269     b->space[1] = 'X';
   271     UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF "
   272             "when buffer is full and auto extend is disabled");
   273     UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2),
   274             "wrong buffer content after failed putc");
   276     UCX_TEST_END
   278     ucx_buffer_free(b);
   279 }
   282 UCX_TEST(test_ucx_buffer_putc_ae) {
   283     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   284     ucx_buffer_putc(b, '0');
   285     ucx_buffer_putc(b, '1');
   287     UCX_TEST_BEGIN
   289     UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
   290     UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts");
   291     UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
   292     UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content");
   294     UCX_TEST_END
   296     ucx_buffer_free(b);
   297 }
   299 UCX_TEST(test_ucx_buffer_putc_oobae) {
   300     UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   301     ucx_buffer_putc(b, '0');
   302     ucx_buffer_putc(b, '1');
   304     UCX_TEST_BEGIN
   306     ucx_buffer_putc(b, 'a');
   308     UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put");
   309     UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
   310     UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
   312     UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content");
   314     UCX_TEST_END
   316     ucx_buffer_free(b);
   317 }
   319 UCX_TEST(test_ucx_buffer_putc_size) {
   320     UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT);
   322     UCX_TEST_BEGIN
   324     UCX_TEST_ASSERT(b->size == 0, "wrong initial size");
   325     ucx_buffer_putc(b, 'a');
   326     ucx_buffer_putc(b, 'b');
   327     ucx_buffer_putc(b, 'c');
   328     UCX_TEST_ASSERT(b->size == 3, "size does not increase");
   329     ucx_buffer_seek(b, 1, SEEK_SET);
   330     ucx_buffer_putc(b, 'd');
   331     UCX_TEST_ASSERT(b->size == 3, "size shall not decrease");
   332     UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc");
   334     UCX_TEST_END
   336     ucx_buffer_free(b);
   337 }
   339 UCX_TEST(test_ucx_buffer_getc) {
   340     char *buffer = (char*) malloc(16);
   341     memset(buffer, 32, 8);
   342     for (int i = 8; i < 16 ; i++) {
   343         buffer[i] = 40+i;
   344     }
   346     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   347     b->size = b->capacity;
   349     UCX_TEST_BEGIN
   351     char rb[16];
   352     for (size_t i = 0 ; i < 16 ; i++) {
   353         UCX_TEST_ASSERT(b->pos == i, "wrong position");
   354         UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive");
   355         rb[i] = ucx_buffer_getc(b);
   356     }
   357     UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
   358         "read data incorrect");
   360     UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read");
   361     UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF");
   363     UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF,
   364         "out of bounds read does not return EOF");
   365     UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read");
   367     UCX_TEST_END
   369     ucx_buffer_free(b);
   370     free(buffer);
   371 }
   373 UCX_TEST(test_ucx_buffer_write) {
   374     char *buffer = (char*) malloc(16);
   375     memset(buffer, 32, 8);
   376     for (int i = 8; i < 16 ; i++) {
   377         buffer[i] = 40+i;
   378     }
   380     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   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     UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
   388     UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
   389             "buffer data incorrect");
   390     UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
   392     ucx_buffer_seek(b, 8, SEEK_SET);
   393     r = ucx_buffer_write((void*)"not", 1, 3, b);
   394     UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
   395     UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
   396             "modified buffer is incorrect");
   398     const char* threebytestring = "  t  h  r  e  e   ";
   399     memset(buffer, 49, 16);
   400     ucx_buffer_seek(b, 0, SEEK_SET);
   401     r = ucx_buffer_write((void*)threebytestring, 3, 6, b);
   402     UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed");
   403     UCX_TEST_ASSERT(b->pos == 15,
   404             "position after write of three byte string incorrect");
   405     UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
   406     UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
   407                 "bufer is incorrect after three byte string has been written");
   409     UCX_TEST_END
   411     ucx_buffer_free(b);
   412     free(buffer);
   413 }
   415 UCX_TEST(test_ucx_buffer_write_ax) {
   416     char *buffer = (char*) malloc(16);
   418     UcxBuffer *b = ucx_buffer_new(buffer, 16,
   419             UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE);
   420     int r;
   422     UCX_TEST_BEGIN
   424     const char* teststring = "this is way too much";
   425     r = ucx_buffer_write((void*)teststring, 1, 20, b);
   426     buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/
   427     UCX_TEST_ASSERT(r == 20, "not all characters written");
   428     UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended");
   429     UCX_TEST_ASSERT(b->pos == 20, "position incorrect");
   430     UCX_TEST_ASSERT(memcmp(buffer,
   431             "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0,
   432             "incorrect buffer content");
   434     UCX_TEST_END
   436     ucx_buffer_free(b);
   437 }
   439 UCX_TEST(test_ucx_buffer_read) {
   440     UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
   441     ucx_buffer_write("01234567", 1, 8, b);
   442     b->pos = 0;
   444     char buf[32];
   445     memset(buf, 'X', 32);
   446     int r;
   448     UCX_TEST_BEGIN
   450     ucx_buffer_seek(b, 2, SEEK_SET);
   451     r = ucx_buffer_read(buf, 1, 2, b);
   452     UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)");
   453     UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X',
   454             "buffer incorrect after read");
   455     UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)");
   457     UCX_TEST_END
   459     ucx_buffer_free(b);
   460 }
   462 UCX_TEST(test_ucx_buffer_read_oob) {
   463     UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
   464     ucx_buffer_write("01234567", 1, 8, b);
   466     char buf[32];
   467     memset(buf, 'X', 32);
   468     int r;
   470     UCX_TEST_BEGIN
   472     b->pos = 2;
   473     r = ucx_buffer_read(buf + 2, 1, 8, b);
   474     UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)");
   475     UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0,
   476             "buffer incorrect after read");
   477     UCX_TEST_ASSERT(b->pos == 8,
   478         "wrong position after read (8 items out of bound)");
   480     b->pos = 0;
   481     memset(buf, 'X', 32);
   483     r = ucx_buffer_read(buf, 3, 3, b);
   485     UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
   486     UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
   487             "buffer incorrect after block out of bounds read");
   489     r = ucx_buffer_read(buf+6, 1, 5, b);
   491     UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read");
   492     UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
   493             "buffer incorrect after remaining byte read");
   495     UCX_TEST_END
   497     ucx_buffer_free(b);
   498 }
   500 UCX_TEST(test_ucx_buffer_extract) {
   501     char *buffer = (char*) malloc(16);
   502     strcpy(buffer, "this is a test!");
   504     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   505     src->size = 15;
   506     UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND);
   508     UCX_TEST_BEGIN
   509     UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
   510     UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE),
   511             "autofree flag shall be enforced");
   512     UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
   513     UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer");
   514     UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer");
   515     char rb[5];
   516     ucx_buffer_read(rb, 1, 5, dst);
   517     UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
   518             "new buffer has incorrect content");
   520     UCX_TEST_END
   522     ucx_buffer_free(dst);
   523     ucx_buffer_free(src);
   524 }
   526 UCX_TEST(test_ucx_buffer_extract_oob) {
   527     char *buffer = (char*) malloc(16);
   528     strcpy(buffer, "this is a test!");
   530     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   531     UCX_TEST_BEGIN
   533     UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL,
   534             "extract shall fail on zero length");
   535     UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL,
   536             "extract shall fail on invalid bounds (size exceeds limits)");
   537     UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL,
   538             "extract shall fail on invalid bounds (start exceeds limits)");
   540     UCX_TEST_END
   542     ucx_buffer_free(src);
   543 }
   545 UCX_TEST(test_ucx_buffer_extract_overflow) {
   546     char *buffer = (char*) malloc(16);
   547     strcpy(buffer, "this is a test!");
   549     UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   550     UCX_TEST_BEGIN
   552     UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4,
   553         UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow");
   555     UCX_TEST_END
   557     ucx_buffer_free(src);
   558 }
   560 UCX_TEST(test_ucx_stream_copy) {
   561     UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
   562     UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   564     UCX_TEST_BEGIN
   566     ucx_buffer_write("01234567", 1, 8, b1);
   567     ucx_buffer_write("abcdefgh", 1, 8, b1);
   568     UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
   569     ucx_buffer_seek(b1, 0, SEEK_SET);
   571     size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
   572     UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
   573     UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
   574     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   575             "b1 and b2 have not the same content");
   577     memset(b2->space, 0, b2->capacity);
   578     b2->pos = 0;
   579     b2->size = 0;
   580     ucx_buffer_seek(b1, 0, SEEK_SET);
   582     FILE *file = tmpfile();
   583     UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
   585     ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
   586     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
   588     fseek(file, 0, SEEK_SET);
   590     ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
   591     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
   593     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   594             "b1 and b2 content mismatch");
   596     fclose(file);
   598     ucx_buffer_clear(b1);
   599     ucx_buffer_seek(b2, 0, SEEK_SET);
   600     ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
   601     UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
   602     UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
   603         "content wrong after ncopy");
   605     UCX_TEST_END
   607     ucx_buffer_free(b1);
   608     ucx_buffer_free(b2);
   609 }

mercurial