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

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

mercurial