1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tests/test_buffer.c Mon Jan 01 16:42:37 2024 +0100 1.3 @@ -0,0 +1,418 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#include "cx/test.h" 1.33 +#include "util_allocator.h" 1.34 + 1.35 +#include "cx/buffer.h" 1.36 + 1.37 +static CX_TEST_SUBROUTINE(expect_default_flush_config, CxBuffer *buf) { 1.38 + CX_TEST_ASSERT(buf->flush_blkmax == 0); 1.39 + CX_TEST_ASSERT(buf->flush_blksize == 4096); 1.40 + CX_TEST_ASSERT(buf->flush_threshold == SIZE_MAX); 1.41 + CX_TEST_ASSERT(buf->flush_func == NULL); 1.42 + CX_TEST_ASSERT(buf->flush_target == NULL); 1.43 +} 1.44 + 1.45 +CX_TEST(test_buffer_init_wrap_space) { 1.46 + CxTestingAllocator talloc; 1.47 + cx_testing_allocator_init(&talloc); 1.48 + CxAllocator *alloc = &talloc.base; 1.49 + CX_TEST_DO { 1.50 + CxBuffer buf; 1.51 + void *space = cxMalloc(alloc, 16); 1.52 + cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_DEFAULT); 1.53 + CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); 1.54 + CX_TEST_ASSERT(buf.space == space); 1.55 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); 1.56 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); 1.57 + CX_TEST_ASSERT(buf.pos == 0); 1.58 + CX_TEST_ASSERT(buf.size == 0); 1.59 + CX_TEST_ASSERT(buf.capacity == 16); 1.60 + CX_TEST_ASSERT(buf.allocator == alloc); 1.61 + cxBufferDestroy(&buf); 1.62 + CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1.63 + cxFree(alloc, space); 1.64 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.65 + } 1.66 + cx_testing_allocator_destroy(&talloc); 1.67 +} 1.68 + 1.69 +CX_TEST(test_buffer_init_wrap_space_auto_extend) { 1.70 + CxTestingAllocator talloc; 1.71 + cx_testing_allocator_init(&talloc); 1.72 + CxAllocator *alloc = &talloc.base; 1.73 + CX_TEST_DO { 1.74 + CxBuffer buf; 1.75 + void *space = cxMalloc(alloc, 16); 1.76 + cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_AUTO_EXTEND); 1.77 + CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); 1.78 + CX_TEST_ASSERT(buf.space == space); 1.79 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND); 1.80 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); 1.81 + CX_TEST_ASSERT(buf.pos == 0); 1.82 + CX_TEST_ASSERT(buf.size == 0); 1.83 + CX_TEST_ASSERT(buf.capacity == 16); 1.84 + CX_TEST_ASSERT(buf.allocator == alloc); 1.85 + cxBufferDestroy(&buf); 1.86 + CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1.87 + cxFree(alloc, space); 1.88 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.89 + } 1.90 + cx_testing_allocator_destroy(&talloc); 1.91 +} 1.92 + 1.93 +CX_TEST(test_buffer_init_wrap_space_auto_free) { 1.94 + CxTestingAllocator talloc; 1.95 + cx_testing_allocator_init(&talloc); 1.96 + CxAllocator *alloc = &talloc.base; 1.97 + CX_TEST_DO { 1.98 + CxBuffer buf; 1.99 + void *space = cxMalloc(alloc, 16); 1.100 + cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_FREE_CONTENTS); 1.101 + CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); 1.102 + CX_TEST_ASSERT(buf.space == space); 1.103 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); 1.104 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); 1.105 + CX_TEST_ASSERT(buf.pos == 0); 1.106 + CX_TEST_ASSERT(buf.size == 0); 1.107 + CX_TEST_ASSERT(buf.capacity == 16); 1.108 + CX_TEST_ASSERT(buf.allocator == alloc); 1.109 + CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1.110 + cxBufferDestroy(&buf); 1.111 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.112 + } 1.113 + cx_testing_allocator_destroy(&talloc); 1.114 +} 1.115 + 1.116 +CX_TEST(test_buffer_init_fresh_space) { 1.117 + CxTestingAllocator talloc; 1.118 + cx_testing_allocator_init(&talloc); 1.119 + CxAllocator *alloc = &talloc.base; 1.120 + CX_TEST_DO { 1.121 + CxBuffer buf; 1.122 + cxBufferInit(&buf, NULL, 8, alloc, CX_BUFFER_DEFAULT); 1.123 + CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); 1.124 + CX_TEST_ASSERT(buf.space != NULL); 1.125 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); 1.126 + CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); 1.127 + CX_TEST_ASSERT(buf.pos == 0); 1.128 + CX_TEST_ASSERT(buf.size == 0); 1.129 + CX_TEST_ASSERT(buf.capacity == 8); 1.130 + CX_TEST_ASSERT(buf.allocator == alloc); 1.131 + CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); // space is still allocated 1.132 + cxBufferDestroy(&buf); 1.133 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.134 + } 1.135 + cx_testing_allocator_destroy(&talloc); 1.136 +} 1.137 + 1.138 +CX_TEST(test_buffer_init_on_heap) { 1.139 + CxTestingAllocator talloc; 1.140 + cx_testing_allocator_init(&talloc); 1.141 + CxAllocator *alloc = &talloc.base; 1.142 + CX_TEST_DO { 1.143 + CxBuffer *buf; 1.144 + void *space = cxMalloc(alloc, 16); 1.145 + buf = cxBufferCreate(space, 16, alloc, CX_BUFFER_FREE_CONTENTS); 1.146 + CX_TEST_ASSERT(buf != NULL); 1.147 + CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, buf); 1.148 + CX_TEST_ASSERT(buf->space == space); 1.149 + CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); 1.150 + CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); 1.151 + CX_TEST_ASSERT(buf->pos == 0); 1.152 + CX_TEST_ASSERT(buf->size == 0); 1.153 + CX_TEST_ASSERT(buf->capacity == 16); 1.154 + CX_TEST_ASSERT(buf->allocator == alloc); 1.155 + cxBufferFree(buf); 1.156 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.157 + } 1.158 + cx_testing_allocator_destroy(&talloc); 1.159 +} 1.160 + 1.161 +CX_TEST(test_buffer_minimum_capacity_sufficient) { 1.162 + CxTestingAllocator talloc; 1.163 + cx_testing_allocator_init(&talloc); 1.164 + CxAllocator *alloc = &talloc.base; 1.165 + CX_TEST_DO { 1.166 + void *space = cxMalloc(alloc, 8); 1.167 + CxBuffer buf; 1.168 + cxBufferInit(&buf, space, 8, alloc, CX_BUFFER_FREE_CONTENTS); 1.169 + memcpy(space, "Testing", 8); 1.170 + buf.size = 8; 1.171 + cxBufferMinimumCapacity(&buf, 6); 1.172 + CX_TEST_ASSERT(buf.capacity == 8); 1.173 + CX_TEST_ASSERT(buf.size == 8); 1.174 + CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); 1.175 + cxBufferDestroy(&buf); 1.176 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.177 + } 1.178 + cx_testing_allocator_destroy(&talloc); 1.179 +} 1.180 + 1.181 +CX_TEST(test_buffer_minimum_capacity_extend) { 1.182 + CxTestingAllocator talloc; 1.183 + cx_testing_allocator_init(&talloc); 1.184 + CxAllocator *alloc = &talloc.base; 1.185 + CX_TEST_DO { 1.186 + void *space = cxMalloc(alloc, 8); 1.187 + CxBuffer buf; 1.188 + cxBufferInit(&buf, space, 8, alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend! 1.189 + memcpy(space, "Testing", 8); 1.190 + buf.size = 8; 1.191 + cxBufferMinimumCapacity(&buf, 16); 1.192 + CX_TEST_ASSERT(buf.capacity == 16); 1.193 + CX_TEST_ASSERT(buf.size == 8); 1.194 + CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); 1.195 + cxBufferDestroy(&buf); 1.196 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.197 + } 1.198 + cx_testing_allocator_destroy(&talloc); 1.199 +} 1.200 + 1.201 +CX_TEST(test_buffer_clear) { 1.202 + char space[16]; 1.203 + strcpy(space, "clear test"); 1.204 + CxBuffer buf; 1.205 + cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.206 + CX_TEST_DO { 1.207 + CX_TEST_ASSERT(buf.size == 0); 1.208 + // only clear the used part of the buffer 1.209 + cxBufferClear(&buf); 1.210 + CX_TEST_ASSERT(0 == memcmp(space, "clear test", 10)); 1.211 + buf.size = 5; 1.212 + buf.pos = 3; 1.213 + cxBufferClear(&buf); 1.214 + CX_TEST_ASSERT(0 == memcmp(space, "\0\0\0\0\0 test", 10)); 1.215 + CX_TEST_ASSERT(buf.size == 0); 1.216 + CX_TEST_ASSERT(buf.pos == 0); 1.217 + } 1.218 + cxBufferDestroy(&buf); 1.219 +} 1.220 + 1.221 +CX_TEST(test_buffer_reset) { 1.222 + char space[16]; 1.223 + strcpy(space, "reset test"); 1.224 + CxBuffer buf; 1.225 + cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.226 + CX_TEST_DO { 1.227 + buf.size = 5; 1.228 + buf.pos = 3; 1.229 + cxBufferReset(&buf); 1.230 + CX_TEST_ASSERT(0 == memcmp(space, "reset test", 10)); 1.231 + CX_TEST_ASSERT(buf.size == 0); 1.232 + CX_TEST_ASSERT(buf.pos == 0); 1.233 + } 1.234 + cxBufferDestroy(&buf); 1.235 +} 1.236 + 1.237 +CX_TEST(test_buffer_seek_set_zero) { 1.238 + CxBuffer buf; 1.239 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.240 + buf.size = 6; 1.241 + buf.pos = 3; 1.242 + CX_TEST_DO { 1.243 + int result = cxBufferSeek(&buf, 0, SEEK_SET); 1.244 + CX_TEST_ASSERT(result == 0); 1.245 + CX_TEST_ASSERT(buf.pos == 0); 1.246 + } 1.247 + cxBufferDestroy(&buf); 1.248 +} 1.249 + 1.250 +CX_TEST(test_buffer_seek_set_valid) { 1.251 + CxBuffer buf; 1.252 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.253 + buf.size = 6; 1.254 + buf.pos = 3; 1.255 + CX_TEST_DO { 1.256 + int result = cxBufferSeek(&buf, 5, SEEK_SET); 1.257 + CX_TEST_ASSERT(result == 0); 1.258 + CX_TEST_ASSERT(buf.pos == 5); 1.259 + } 1.260 + cxBufferDestroy(&buf); 1.261 +} 1.262 + 1.263 +CX_TEST(test_buffer_seek_set_invalid) { 1.264 + CxBuffer buf; 1.265 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.266 + buf.size = 6; 1.267 + buf.pos = 3; 1.268 + CX_TEST_DO { 1.269 + int result = cxBufferSeek(&buf, 6, SEEK_SET); 1.270 + CX_TEST_ASSERT(result != 0); 1.271 + CX_TEST_ASSERT(buf.pos == 3); 1.272 + } 1.273 + cxBufferDestroy(&buf); 1.274 +} 1.275 + 1.276 +CX_TEST(test_buffer_seek_cur_zero) { 1.277 + CxBuffer buf; 1.278 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.279 + buf.size = 6; 1.280 + buf.pos = 3; 1.281 + CX_TEST_DO { 1.282 + int result = cxBufferSeek(&buf, 0, SEEK_CUR); 1.283 + CX_TEST_ASSERT(result == 0); 1.284 + CX_TEST_ASSERT(buf.pos == 3); 1.285 + } 1.286 + cxBufferDestroy(&buf); 1.287 +} 1.288 + 1.289 +CX_TEST(test_buffer_seek_cur_valid_positive) { 1.290 + CxBuffer buf; 1.291 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.292 + buf.size = 6; 1.293 + buf.pos = 3; 1.294 + CX_TEST_DO { 1.295 + int result = cxBufferSeek(&buf, 2, SEEK_CUR); 1.296 + CX_TEST_ASSERT(result == 0); 1.297 + CX_TEST_ASSERT(buf.pos == 5); 1.298 + } 1.299 + cxBufferDestroy(&buf); 1.300 +} 1.301 + 1.302 +CX_TEST(test_buffer_seek_cur_valid_negative) { 1.303 + CxBuffer buf; 1.304 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.305 + buf.size = 6; 1.306 + buf.pos = 3; 1.307 + CX_TEST_DO { 1.308 + int result = cxBufferSeek(&buf, -3, SEEK_CUR); 1.309 + CX_TEST_ASSERT(result == 0); 1.310 + CX_TEST_ASSERT(buf.pos == 0); 1.311 + } 1.312 + cxBufferDestroy(&buf); 1.313 +} 1.314 + 1.315 +CX_TEST(test_buffer_seek_cur_invalid_positive) { 1.316 + CxBuffer buf; 1.317 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.318 + buf.size = 6; 1.319 + buf.pos = 3; 1.320 + CX_TEST_DO { 1.321 + int result = cxBufferSeek(&buf, 3, SEEK_CUR); 1.322 + CX_TEST_ASSERT(result != 0); 1.323 + CX_TEST_ASSERT(buf.pos == 3); 1.324 + } 1.325 + cxBufferDestroy(&buf); 1.326 +} 1.327 + 1.328 +CX_TEST(test_buffer_seek_cur_invalid_negative) { 1.329 + CxBuffer buf; 1.330 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.331 + buf.size = 6; 1.332 + buf.pos = 3; 1.333 + CX_TEST_DO { 1.334 + int result = cxBufferSeek(&buf, -4, SEEK_CUR); 1.335 + CX_TEST_ASSERT(result != 0); 1.336 + CX_TEST_ASSERT(buf.pos == 3); 1.337 + } 1.338 + cxBufferDestroy(&buf); 1.339 +} 1.340 + 1.341 +CX_TEST(test_buffer_seek_end_zero) { 1.342 + CxBuffer buf; 1.343 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.344 + buf.size = 6; 1.345 + buf.pos = 3; 1.346 + CX_TEST_DO { 1.347 + int result = cxBufferSeek(&buf, 0, SEEK_END); 1.348 + // the (past-the-)end position is always invalid 1.349 + CX_TEST_ASSERT(result != 0); 1.350 + CX_TEST_ASSERT(buf.pos == 3); 1.351 + } 1.352 + cxBufferDestroy(&buf); 1.353 +} 1.354 + 1.355 +CX_TEST(test_buffer_seek_end_valid) { 1.356 + CxBuffer buf; 1.357 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.358 + buf.size = 6; 1.359 + buf.pos = 3; 1.360 + CX_TEST_DO { 1.361 + int result = cxBufferSeek(&buf, -6, SEEK_END); 1.362 + CX_TEST_ASSERT(result == 0); 1.363 + CX_TEST_ASSERT(buf.pos == 0); 1.364 + } 1.365 + cxBufferDestroy(&buf); 1.366 +} 1.367 + 1.368 +CX_TEST(test_buffer_seek_end_invalid) { 1.369 + CxBuffer buf; 1.370 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.371 + buf.size = 6; 1.372 + buf.pos = 3; 1.373 + CX_TEST_DO { 1.374 + int result = cxBufferSeek(&buf, 1, SEEK_END); 1.375 + CX_TEST_ASSERT(result != 0); 1.376 + CX_TEST_ASSERT(buf.pos == 3); 1.377 + } 1.378 + cxBufferDestroy(&buf); 1.379 +} 1.380 + 1.381 +CX_TEST(test_buffer_seek_whence_invalid) { 1.382 + CxBuffer buf; 1.383 + cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); 1.384 + buf.size = 6; 1.385 + buf.pos = 3; 1.386 + CX_TEST_DO { 1.387 + int result = cxBufferSeek(&buf, 2, 9000); 1.388 + CX_TEST_ASSERT(result != 0); 1.389 + CX_TEST_ASSERT(buf.size == 6); 1.390 + CX_TEST_ASSERT(buf.pos == 3); 1.391 + } 1.392 + cxBufferDestroy(&buf); 1.393 +} 1.394 + 1.395 +CxTestSuite *cx_test_suite_buffer(void) { 1.396 + CxTestSuite *suite = cx_test_suite_new("buffer"); 1.397 + 1.398 + cx_test_register(suite, test_buffer_init_wrap_space); 1.399 + cx_test_register(suite, test_buffer_init_wrap_space_auto_extend); 1.400 + cx_test_register(suite, test_buffer_init_wrap_space_auto_free); 1.401 + cx_test_register(suite, test_buffer_init_fresh_space); 1.402 + cx_test_register(suite, test_buffer_init_on_heap); 1.403 + cx_test_register(suite, test_buffer_minimum_capacity_sufficient); 1.404 + cx_test_register(suite, test_buffer_minimum_capacity_extend); 1.405 + cx_test_register(suite, test_buffer_clear); 1.406 + cx_test_register(suite, test_buffer_reset); 1.407 + cx_test_register(suite, test_buffer_seek_set_zero); 1.408 + cx_test_register(suite, test_buffer_seek_set_valid); 1.409 + cx_test_register(suite, test_buffer_seek_set_invalid); 1.410 + cx_test_register(suite, test_buffer_seek_cur_zero); 1.411 + cx_test_register(suite, test_buffer_seek_cur_valid_positive); 1.412 + cx_test_register(suite, test_buffer_seek_cur_valid_negative); 1.413 + cx_test_register(suite, test_buffer_seek_cur_invalid_positive); 1.414 + cx_test_register(suite, test_buffer_seek_cur_invalid_negative); 1.415 + cx_test_register(suite, test_buffer_seek_end_zero); 1.416 + cx_test_register(suite, test_buffer_seek_end_valid); 1.417 + cx_test_register(suite, test_buffer_seek_end_invalid); 1.418 + cx_test_register(suite, test_buffer_seek_whence_invalid); 1.419 + 1.420 + return suite; 1.421 +}