Wed, 03 Jan 2024 22:17:40 +0100
migrate more buffer tests - relates to #342
only read and write tests are remaining now
universe@789 | 1 | /* |
universe@789 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@789 | 3 | * |
universe@789 | 4 | * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. |
universe@789 | 5 | * |
universe@789 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@789 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@789 | 8 | * |
universe@789 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@789 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@789 | 11 | * |
universe@789 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@789 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@789 | 14 | * documentation and/or other materials provided with the distribution. |
universe@789 | 15 | * |
universe@789 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@789 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@789 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@789 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@789 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@789 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@789 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@789 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@789 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@789 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@789 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@789 | 27 | */ |
universe@789 | 28 | |
universe@789 | 29 | #include "cx/test.h" |
universe@789 | 30 | #include "util_allocator.h" |
universe@789 | 31 | |
universe@789 | 32 | #include "cx/buffer.h" |
universe@789 | 33 | |
universe@789 | 34 | static CX_TEST_SUBROUTINE(expect_default_flush_config, CxBuffer *buf) { |
universe@789 | 35 | CX_TEST_ASSERT(buf->flush_blkmax == 0); |
universe@789 | 36 | CX_TEST_ASSERT(buf->flush_blksize == 4096); |
universe@789 | 37 | CX_TEST_ASSERT(buf->flush_threshold == SIZE_MAX); |
universe@789 | 38 | CX_TEST_ASSERT(buf->flush_func == NULL); |
universe@789 | 39 | CX_TEST_ASSERT(buf->flush_target == NULL); |
universe@789 | 40 | } |
universe@789 | 41 | |
universe@789 | 42 | CX_TEST(test_buffer_init_wrap_space) { |
universe@789 | 43 | CxTestingAllocator talloc; |
universe@789 | 44 | cx_testing_allocator_init(&talloc); |
universe@789 | 45 | CxAllocator *alloc = &talloc.base; |
universe@789 | 46 | CX_TEST_DO { |
universe@789 | 47 | CxBuffer buf; |
universe@789 | 48 | void *space = cxMalloc(alloc, 16); |
universe@789 | 49 | cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_DEFAULT); |
universe@789 | 50 | CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); |
universe@789 | 51 | CX_TEST_ASSERT(buf.space == space); |
universe@789 | 52 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); |
universe@789 | 53 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); |
universe@789 | 54 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 55 | CX_TEST_ASSERT(buf.size == 0); |
universe@789 | 56 | CX_TEST_ASSERT(buf.capacity == 16); |
universe@789 | 57 | CX_TEST_ASSERT(buf.allocator == alloc); |
universe@789 | 58 | cxBufferDestroy(&buf); |
universe@789 | 59 | CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); |
universe@789 | 60 | cxFree(alloc, space); |
universe@789 | 61 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@789 | 62 | } |
universe@789 | 63 | cx_testing_allocator_destroy(&talloc); |
universe@789 | 64 | } |
universe@789 | 65 | |
universe@789 | 66 | CX_TEST(test_buffer_init_wrap_space_auto_extend) { |
universe@789 | 67 | CxTestingAllocator talloc; |
universe@789 | 68 | cx_testing_allocator_init(&talloc); |
universe@789 | 69 | CxAllocator *alloc = &talloc.base; |
universe@789 | 70 | CX_TEST_DO { |
universe@789 | 71 | CxBuffer buf; |
universe@789 | 72 | void *space = cxMalloc(alloc, 16); |
universe@789 | 73 | cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_AUTO_EXTEND); |
universe@789 | 74 | CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); |
universe@789 | 75 | CX_TEST_ASSERT(buf.space == space); |
universe@789 | 76 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND); |
universe@789 | 77 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); |
universe@789 | 78 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 79 | CX_TEST_ASSERT(buf.size == 0); |
universe@789 | 80 | CX_TEST_ASSERT(buf.capacity == 16); |
universe@789 | 81 | CX_TEST_ASSERT(buf.allocator == alloc); |
universe@789 | 82 | cxBufferDestroy(&buf); |
universe@789 | 83 | CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); |
universe@789 | 84 | cxFree(alloc, space); |
universe@789 | 85 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@789 | 86 | } |
universe@789 | 87 | cx_testing_allocator_destroy(&talloc); |
universe@789 | 88 | } |
universe@789 | 89 | |
universe@789 | 90 | CX_TEST(test_buffer_init_wrap_space_auto_free) { |
universe@789 | 91 | CxTestingAllocator talloc; |
universe@789 | 92 | cx_testing_allocator_init(&talloc); |
universe@789 | 93 | CxAllocator *alloc = &talloc.base; |
universe@789 | 94 | CX_TEST_DO { |
universe@789 | 95 | CxBuffer buf; |
universe@789 | 96 | void *space = cxMalloc(alloc, 16); |
universe@789 | 97 | cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_FREE_CONTENTS); |
universe@789 | 98 | CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); |
universe@789 | 99 | CX_TEST_ASSERT(buf.space == space); |
universe@789 | 100 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); |
universe@789 | 101 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
universe@789 | 102 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 103 | CX_TEST_ASSERT(buf.size == 0); |
universe@789 | 104 | CX_TEST_ASSERT(buf.capacity == 16); |
universe@789 | 105 | CX_TEST_ASSERT(buf.allocator == alloc); |
universe@789 | 106 | CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); |
universe@789 | 107 | cxBufferDestroy(&buf); |
universe@789 | 108 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@789 | 109 | } |
universe@789 | 110 | cx_testing_allocator_destroy(&talloc); |
universe@789 | 111 | } |
universe@789 | 112 | |
universe@789 | 113 | CX_TEST(test_buffer_init_fresh_space) { |
universe@789 | 114 | CxTestingAllocator talloc; |
universe@789 | 115 | cx_testing_allocator_init(&talloc); |
universe@789 | 116 | CxAllocator *alloc = &talloc.base; |
universe@789 | 117 | CX_TEST_DO { |
universe@789 | 118 | CxBuffer buf; |
universe@789 | 119 | cxBufferInit(&buf, NULL, 8, alloc, CX_BUFFER_DEFAULT); |
universe@789 | 120 | CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, &buf); |
universe@789 | 121 | CX_TEST_ASSERT(buf.space != NULL); |
universe@789 | 122 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); |
universe@789 | 123 | CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
universe@789 | 124 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 125 | CX_TEST_ASSERT(buf.size == 0); |
universe@789 | 126 | CX_TEST_ASSERT(buf.capacity == 8); |
universe@789 | 127 | CX_TEST_ASSERT(buf.allocator == alloc); |
universe@789 | 128 | CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); // space is still allocated |
universe@789 | 129 | cxBufferDestroy(&buf); |
universe@789 | 130 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@789 | 131 | } |
universe@789 | 132 | cx_testing_allocator_destroy(&talloc); |
universe@789 | 133 | } |
universe@789 | 134 | |
universe@789 | 135 | CX_TEST(test_buffer_init_on_heap) { |
universe@789 | 136 | CxTestingAllocator talloc; |
universe@789 | 137 | cx_testing_allocator_init(&talloc); |
universe@789 | 138 | CxAllocator *alloc = &talloc.base; |
universe@789 | 139 | CX_TEST_DO { |
universe@789 | 140 | CxBuffer *buf; |
universe@789 | 141 | void *space = cxMalloc(alloc, 16); |
universe@789 | 142 | buf = cxBufferCreate(space, 16, alloc, CX_BUFFER_FREE_CONTENTS); |
universe@789 | 143 | CX_TEST_ASSERT(buf != NULL); |
universe@789 | 144 | CX_TEST_CALL_SUBROUTINE(expect_default_flush_config, buf); |
universe@789 | 145 | CX_TEST_ASSERT(buf->space == space); |
universe@789 | 146 | CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); |
universe@789 | 147 | CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
universe@789 | 148 | CX_TEST_ASSERT(buf->pos == 0); |
universe@789 | 149 | CX_TEST_ASSERT(buf->size == 0); |
universe@789 | 150 | CX_TEST_ASSERT(buf->capacity == 16); |
universe@789 | 151 | CX_TEST_ASSERT(buf->allocator == alloc); |
universe@789 | 152 | cxBufferFree(buf); |
universe@789 | 153 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@789 | 154 | } |
universe@789 | 155 | cx_testing_allocator_destroy(&talloc); |
universe@789 | 156 | } |
universe@789 | 157 | |
universe@789 | 158 | CX_TEST(test_buffer_minimum_capacity_sufficient) { |
universe@789 | 159 | CxTestingAllocator talloc; |
universe@789 | 160 | cx_testing_allocator_init(&talloc); |
universe@789 | 161 | CxAllocator *alloc = &talloc.base; |
universe@789 | 162 | CX_TEST_DO { |
universe@789 | 163 | void *space = cxMalloc(alloc, 8); |
universe@789 | 164 | CxBuffer buf; |
universe@789 | 165 | cxBufferInit(&buf, space, 8, alloc, CX_BUFFER_FREE_CONTENTS); |
universe@789 | 166 | memcpy(space, "Testing", 8); |
universe@789 | 167 | buf.size = 8; |
universe@789 | 168 | cxBufferMinimumCapacity(&buf, 6); |
universe@789 | 169 | CX_TEST_ASSERT(buf.capacity == 8); |
universe@789 | 170 | CX_TEST_ASSERT(buf.size == 8); |
universe@789 | 171 | CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); |
universe@789 | 172 | cxBufferDestroy(&buf); |
universe@789 | 173 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@789 | 174 | } |
universe@789 | 175 | cx_testing_allocator_destroy(&talloc); |
universe@789 | 176 | } |
universe@789 | 177 | |
universe@789 | 178 | CX_TEST(test_buffer_minimum_capacity_extend) { |
universe@789 | 179 | CxTestingAllocator talloc; |
universe@789 | 180 | cx_testing_allocator_init(&talloc); |
universe@789 | 181 | CxAllocator *alloc = &talloc.base; |
universe@789 | 182 | CX_TEST_DO { |
universe@789 | 183 | void *space = cxMalloc(alloc, 8); |
universe@789 | 184 | CxBuffer buf; |
universe@789 | 185 | cxBufferInit(&buf, space, 8, alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend! |
universe@789 | 186 | memcpy(space, "Testing", 8); |
universe@789 | 187 | buf.size = 8; |
universe@789 | 188 | cxBufferMinimumCapacity(&buf, 16); |
universe@789 | 189 | CX_TEST_ASSERT(buf.capacity == 16); |
universe@789 | 190 | CX_TEST_ASSERT(buf.size == 8); |
universe@789 | 191 | CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); |
universe@789 | 192 | cxBufferDestroy(&buf); |
universe@789 | 193 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
universe@789 | 194 | } |
universe@789 | 195 | cx_testing_allocator_destroy(&talloc); |
universe@789 | 196 | } |
universe@789 | 197 | |
universe@789 | 198 | CX_TEST(test_buffer_clear) { |
universe@789 | 199 | char space[16]; |
universe@789 | 200 | strcpy(space, "clear test"); |
universe@789 | 201 | CxBuffer buf; |
universe@789 | 202 | cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 203 | CX_TEST_DO { |
universe@789 | 204 | CX_TEST_ASSERT(buf.size == 0); |
universe@789 | 205 | // only clear the used part of the buffer |
universe@789 | 206 | cxBufferClear(&buf); |
universe@789 | 207 | CX_TEST_ASSERT(0 == memcmp(space, "clear test", 10)); |
universe@789 | 208 | buf.size = 5; |
universe@789 | 209 | buf.pos = 3; |
universe@789 | 210 | cxBufferClear(&buf); |
universe@789 | 211 | CX_TEST_ASSERT(0 == memcmp(space, "\0\0\0\0\0 test", 10)); |
universe@789 | 212 | CX_TEST_ASSERT(buf.size == 0); |
universe@789 | 213 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 214 | } |
universe@789 | 215 | cxBufferDestroy(&buf); |
universe@789 | 216 | } |
universe@789 | 217 | |
universe@789 | 218 | CX_TEST(test_buffer_reset) { |
universe@789 | 219 | char space[16]; |
universe@789 | 220 | strcpy(space, "reset test"); |
universe@789 | 221 | CxBuffer buf; |
universe@789 | 222 | cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 223 | CX_TEST_DO { |
universe@789 | 224 | buf.size = 5; |
universe@789 | 225 | buf.pos = 3; |
universe@789 | 226 | cxBufferReset(&buf); |
universe@789 | 227 | CX_TEST_ASSERT(0 == memcmp(space, "reset test", 10)); |
universe@789 | 228 | CX_TEST_ASSERT(buf.size == 0); |
universe@789 | 229 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 230 | } |
universe@789 | 231 | cxBufferDestroy(&buf); |
universe@789 | 232 | } |
universe@789 | 233 | |
universe@789 | 234 | CX_TEST(test_buffer_seek_set_zero) { |
universe@789 | 235 | CxBuffer buf; |
universe@789 | 236 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 237 | buf.size = 6; |
universe@789 | 238 | buf.pos = 3; |
universe@789 | 239 | CX_TEST_DO { |
universe@789 | 240 | int result = cxBufferSeek(&buf, 0, SEEK_SET); |
universe@789 | 241 | CX_TEST_ASSERT(result == 0); |
universe@789 | 242 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 243 | } |
universe@789 | 244 | cxBufferDestroy(&buf); |
universe@789 | 245 | } |
universe@789 | 246 | |
universe@789 | 247 | CX_TEST(test_buffer_seek_set_valid) { |
universe@789 | 248 | CxBuffer buf; |
universe@789 | 249 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 250 | buf.size = 6; |
universe@789 | 251 | buf.pos = 3; |
universe@789 | 252 | CX_TEST_DO { |
universe@789 | 253 | int result = cxBufferSeek(&buf, 5, SEEK_SET); |
universe@789 | 254 | CX_TEST_ASSERT(result == 0); |
universe@789 | 255 | CX_TEST_ASSERT(buf.pos == 5); |
universe@789 | 256 | } |
universe@789 | 257 | cxBufferDestroy(&buf); |
universe@789 | 258 | } |
universe@789 | 259 | |
universe@789 | 260 | CX_TEST(test_buffer_seek_set_invalid) { |
universe@789 | 261 | CxBuffer buf; |
universe@789 | 262 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 263 | buf.size = 6; |
universe@789 | 264 | buf.pos = 3; |
universe@789 | 265 | CX_TEST_DO { |
universe@789 | 266 | int result = cxBufferSeek(&buf, 6, SEEK_SET); |
universe@789 | 267 | CX_TEST_ASSERT(result != 0); |
universe@789 | 268 | CX_TEST_ASSERT(buf.pos == 3); |
universe@789 | 269 | } |
universe@789 | 270 | cxBufferDestroy(&buf); |
universe@789 | 271 | } |
universe@789 | 272 | |
universe@789 | 273 | CX_TEST(test_buffer_seek_cur_zero) { |
universe@789 | 274 | CxBuffer buf; |
universe@789 | 275 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 276 | buf.size = 6; |
universe@789 | 277 | buf.pos = 3; |
universe@789 | 278 | CX_TEST_DO { |
universe@789 | 279 | int result = cxBufferSeek(&buf, 0, SEEK_CUR); |
universe@789 | 280 | CX_TEST_ASSERT(result == 0); |
universe@789 | 281 | CX_TEST_ASSERT(buf.pos == 3); |
universe@789 | 282 | } |
universe@789 | 283 | cxBufferDestroy(&buf); |
universe@789 | 284 | } |
universe@789 | 285 | |
universe@789 | 286 | CX_TEST(test_buffer_seek_cur_valid_positive) { |
universe@789 | 287 | CxBuffer buf; |
universe@789 | 288 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 289 | buf.size = 6; |
universe@789 | 290 | buf.pos = 3; |
universe@789 | 291 | CX_TEST_DO { |
universe@789 | 292 | int result = cxBufferSeek(&buf, 2, SEEK_CUR); |
universe@789 | 293 | CX_TEST_ASSERT(result == 0); |
universe@789 | 294 | CX_TEST_ASSERT(buf.pos == 5); |
universe@789 | 295 | } |
universe@789 | 296 | cxBufferDestroy(&buf); |
universe@789 | 297 | } |
universe@789 | 298 | |
universe@789 | 299 | CX_TEST(test_buffer_seek_cur_valid_negative) { |
universe@789 | 300 | CxBuffer buf; |
universe@789 | 301 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 302 | buf.size = 6; |
universe@789 | 303 | buf.pos = 3; |
universe@789 | 304 | CX_TEST_DO { |
universe@789 | 305 | int result = cxBufferSeek(&buf, -3, SEEK_CUR); |
universe@789 | 306 | CX_TEST_ASSERT(result == 0); |
universe@789 | 307 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 308 | } |
universe@789 | 309 | cxBufferDestroy(&buf); |
universe@789 | 310 | } |
universe@789 | 311 | |
universe@789 | 312 | CX_TEST(test_buffer_seek_cur_invalid_positive) { |
universe@789 | 313 | CxBuffer buf; |
universe@789 | 314 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 315 | buf.size = 6; |
universe@789 | 316 | buf.pos = 3; |
universe@789 | 317 | CX_TEST_DO { |
universe@789 | 318 | int result = cxBufferSeek(&buf, 3, SEEK_CUR); |
universe@789 | 319 | CX_TEST_ASSERT(result != 0); |
universe@789 | 320 | CX_TEST_ASSERT(buf.pos == 3); |
universe@789 | 321 | } |
universe@789 | 322 | cxBufferDestroy(&buf); |
universe@789 | 323 | } |
universe@789 | 324 | |
universe@789 | 325 | CX_TEST(test_buffer_seek_cur_invalid_negative) { |
universe@789 | 326 | CxBuffer buf; |
universe@789 | 327 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 328 | buf.size = 6; |
universe@789 | 329 | buf.pos = 3; |
universe@789 | 330 | CX_TEST_DO { |
universe@789 | 331 | int result = cxBufferSeek(&buf, -4, SEEK_CUR); |
universe@789 | 332 | CX_TEST_ASSERT(result != 0); |
universe@789 | 333 | CX_TEST_ASSERT(buf.pos == 3); |
universe@789 | 334 | } |
universe@789 | 335 | cxBufferDestroy(&buf); |
universe@789 | 336 | } |
universe@789 | 337 | |
universe@789 | 338 | CX_TEST(test_buffer_seek_end_zero) { |
universe@789 | 339 | CxBuffer buf; |
universe@789 | 340 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 341 | buf.size = 6; |
universe@789 | 342 | buf.pos = 3; |
universe@789 | 343 | CX_TEST_DO { |
universe@789 | 344 | int result = cxBufferSeek(&buf, 0, SEEK_END); |
universe@789 | 345 | // the (past-the-)end position is always invalid |
universe@789 | 346 | CX_TEST_ASSERT(result != 0); |
universe@789 | 347 | CX_TEST_ASSERT(buf.pos == 3); |
universe@789 | 348 | } |
universe@789 | 349 | cxBufferDestroy(&buf); |
universe@789 | 350 | } |
universe@789 | 351 | |
universe@789 | 352 | CX_TEST(test_buffer_seek_end_valid) { |
universe@789 | 353 | CxBuffer buf; |
universe@789 | 354 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 355 | buf.size = 6; |
universe@789 | 356 | buf.pos = 3; |
universe@789 | 357 | CX_TEST_DO { |
universe@789 | 358 | int result = cxBufferSeek(&buf, -6, SEEK_END); |
universe@789 | 359 | CX_TEST_ASSERT(result == 0); |
universe@789 | 360 | CX_TEST_ASSERT(buf.pos == 0); |
universe@789 | 361 | } |
universe@789 | 362 | cxBufferDestroy(&buf); |
universe@789 | 363 | } |
universe@789 | 364 | |
universe@789 | 365 | CX_TEST(test_buffer_seek_end_invalid) { |
universe@789 | 366 | CxBuffer buf; |
universe@789 | 367 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 368 | buf.size = 6; |
universe@789 | 369 | buf.pos = 3; |
universe@789 | 370 | CX_TEST_DO { |
universe@789 | 371 | int result = cxBufferSeek(&buf, 1, SEEK_END); |
universe@789 | 372 | CX_TEST_ASSERT(result != 0); |
universe@789 | 373 | CX_TEST_ASSERT(buf.pos == 3); |
universe@789 | 374 | } |
universe@789 | 375 | cxBufferDestroy(&buf); |
universe@789 | 376 | } |
universe@789 | 377 | |
universe@789 | 378 | CX_TEST(test_buffer_seek_whence_invalid) { |
universe@789 | 379 | CxBuffer buf; |
universe@789 | 380 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@789 | 381 | buf.size = 6; |
universe@789 | 382 | buf.pos = 3; |
universe@789 | 383 | CX_TEST_DO { |
universe@789 | 384 | int result = cxBufferSeek(&buf, 2, 9000); |
universe@789 | 385 | CX_TEST_ASSERT(result != 0); |
universe@789 | 386 | CX_TEST_ASSERT(buf.size == 6); |
universe@789 | 387 | CX_TEST_ASSERT(buf.pos == 3); |
universe@789 | 388 | } |
universe@789 | 389 | cxBufferDestroy(&buf); |
universe@789 | 390 | } |
universe@789 | 391 | |
universe@792 | 392 | CX_TEST(test_buffer_eof_reached) { |
universe@792 | 393 | CxBuffer buf; |
universe@792 | 394 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@792 | 395 | buf.size = buf.pos = 3; |
universe@792 | 396 | CX_TEST_DO { |
universe@792 | 397 | CX_TEST_ASSERT(cxBufferEof(&buf)); |
universe@792 | 398 | buf.pos = buf.size - 1; |
universe@792 | 399 | CX_TEST_ASSERT(!cxBufferEof(&buf)); |
universe@792 | 400 | cxBufferPut(&buf, 'a'); |
universe@792 | 401 | CX_TEST_ASSERT(cxBufferEof(&buf)); |
universe@792 | 402 | } |
universe@792 | 403 | cxBufferDestroy(&buf); |
universe@792 | 404 | } |
universe@792 | 405 | |
universe@792 | 406 | CX_TEST(test_buffer_eof_not_reached) { |
universe@792 | 407 | CxBuffer buf; |
universe@792 | 408 | cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
universe@792 | 409 | buf.size = 6; |
universe@792 | 410 | CX_TEST_DO { |
universe@792 | 411 | buf.pos = buf.size - 1; |
universe@792 | 412 | CX_TEST_ASSERT(!cxBufferEof(&buf)); |
universe@792 | 413 | buf.pos = 0; |
universe@792 | 414 | cxBufferWrite("test", 1, 5, &buf); |
universe@792 | 415 | CX_TEST_ASSERT(!cxBufferEof(&buf)); |
universe@792 | 416 | } |
universe@792 | 417 | cxBufferDestroy(&buf); |
universe@792 | 418 | } |
universe@792 | 419 | |
universe@792 | 420 | #define TEST_BUFFER_SHIFT_SETUP(buf) \ |
universe@792 | 421 | CxTestingAllocator talloc; \ |
universe@792 | 422 | cx_testing_allocator_init(&talloc); \ |
universe@792 | 423 | CxAllocator *alloc = &talloc.base; \ |
universe@792 | 424 | CxBuffer buf; \ |
universe@792 | 425 | cxBufferInit(&buf, NULL, 16, alloc, CX_BUFFER_DEFAULT); \ |
universe@792 | 426 | memcpy(buf.space, "test____XXXXXXXX", 16); \ |
universe@792 | 427 | buf.capacity = 8; \ |
universe@792 | 428 | buf.pos = 4; \ |
universe@792 | 429 | buf.size = 4 |
universe@792 | 430 | #define TEST_BUFFER_SHIFT_TEARDOWN(buf) \ |
universe@792 | 431 | cxBufferDestroy(&buf); \ |
universe@792 | 432 | CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); \ |
universe@792 | 433 | cx_testing_allocator_destroy(&talloc); |
universe@792 | 434 | |
universe@792 | 435 | |
universe@792 | 436 | CX_TEST(test_buffer_shift_left_zero) { |
universe@792 | 437 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 438 | CX_TEST_DO { |
universe@792 | 439 | int ret = cxBufferShiftLeft(&buf, 0); |
universe@792 | 440 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 441 | CX_TEST_ASSERT(buf.pos == 4); |
universe@792 | 442 | CX_TEST_ASSERT(buf.size == 4); |
universe@792 | 443 | CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0); |
universe@792 | 444 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 445 | } |
universe@792 | 446 | } |
universe@792 | 447 | |
universe@792 | 448 | CX_TEST(test_buffer_shift_left_zero_offset_interface) { |
universe@792 | 449 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 450 | CX_TEST_DO { |
universe@792 | 451 | int ret = cxBufferShift(&buf, -0); |
universe@792 | 452 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 453 | CX_TEST_ASSERT(buf.pos == 4); |
universe@792 | 454 | CX_TEST_ASSERT(buf.size == 4); |
universe@792 | 455 | CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0); |
universe@792 | 456 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 457 | } |
universe@792 | 458 | } |
universe@792 | 459 | |
universe@792 | 460 | CX_TEST(test_buffer_shift_left_standard) { |
universe@792 | 461 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 462 | CX_TEST_DO { |
universe@792 | 463 | int ret = cxBufferShiftLeft(&buf, 2); |
universe@792 | 464 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 465 | CX_TEST_ASSERT(buf.pos == 2); |
universe@792 | 466 | CX_TEST_ASSERT(buf.size == 2); |
universe@792 | 467 | CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0); |
universe@792 | 468 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 469 | } |
universe@792 | 470 | } |
universe@792 | 471 | |
universe@792 | 472 | CX_TEST(test_buffer_shift_left_overshift) { |
universe@792 | 473 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 474 | CX_TEST_DO { |
universe@792 | 475 | int ret = cxBufferShiftLeft(&buf, 6); |
universe@792 | 476 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 477 | CX_TEST_ASSERT(buf.pos == 0); |
universe@792 | 478 | CX_TEST_ASSERT(buf.size == 0); |
universe@792 | 479 | CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0); |
universe@792 | 480 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 481 | } |
universe@792 | 482 | } |
universe@792 | 483 | |
universe@792 | 484 | CX_TEST(test_buffer_shift_left_overshift_pos_only) { |
universe@792 | 485 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 486 | buf.pos = 2; |
universe@792 | 487 | CX_TEST_DO { |
universe@792 | 488 | int ret = cxBufferShiftLeft(&buf, 3); |
universe@792 | 489 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 490 | CX_TEST_ASSERT(buf.pos == 0); |
universe@792 | 491 | CX_TEST_ASSERT(buf.size == 1); |
universe@792 | 492 | CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0); |
universe@792 | 493 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 494 | } |
universe@792 | 495 | } |
universe@792 | 496 | |
universe@792 | 497 | CX_TEST(test_buffer_shift_left_offset_interface) { |
universe@792 | 498 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 499 | buf.pos = 3; |
universe@792 | 500 | CX_TEST_DO { |
universe@792 | 501 | int ret = cxBufferShift(&buf, -2); |
universe@792 | 502 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 503 | CX_TEST_ASSERT(buf.pos == 1); |
universe@792 | 504 | CX_TEST_ASSERT(buf.size == 2); |
universe@792 | 505 | CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0); |
universe@792 | 506 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 507 | } |
universe@792 | 508 | } |
universe@792 | 509 | |
universe@792 | 510 | CX_TEST(test_buffer_shift_right_zero) { |
universe@792 | 511 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 512 | CX_TEST_DO { |
universe@792 | 513 | int ret = cxBufferShiftRight(&buf, 0); |
universe@792 | 514 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 515 | CX_TEST_ASSERT(buf.pos == 4); |
universe@792 | 516 | CX_TEST_ASSERT(buf.size == 4); |
universe@792 | 517 | CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0); |
universe@792 | 518 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 519 | } |
universe@792 | 520 | } |
universe@792 | 521 | |
universe@792 | 522 | CX_TEST(test_buffer_shift_right_zero_offset_interface) { |
universe@792 | 523 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 524 | CX_TEST_DO { |
universe@792 | 525 | int ret = cxBufferShift(&buf, +0); |
universe@792 | 526 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 527 | CX_TEST_ASSERT(buf.pos == 4); |
universe@792 | 528 | CX_TEST_ASSERT(buf.size == 4); |
universe@792 | 529 | CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0); |
universe@792 | 530 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 531 | } |
universe@792 | 532 | } |
universe@792 | 533 | |
universe@792 | 534 | CX_TEST(test_buffer_shift_right_standard) { |
universe@792 | 535 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 536 | CX_TEST_DO { |
universe@792 | 537 | int ret = cxBufferShiftRight(&buf, 3); |
universe@792 | 538 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 539 | CX_TEST_ASSERT(buf.pos == 7); |
universe@792 | 540 | CX_TEST_ASSERT(buf.size == 7); |
universe@792 | 541 | CX_TEST_ASSERT(memcmp(buf.space, "testest_XXXXXXXX", 16) == 0); |
universe@792 | 542 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 543 | } |
universe@792 | 544 | } |
universe@792 | 545 | |
universe@792 | 546 | CX_TEST(test_buffer_shift_right_overshift_discard) { |
universe@792 | 547 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 548 | CX_TEST_DO { |
universe@792 | 549 | int ret = cxBufferShiftRight(&buf, 6); |
universe@792 | 550 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 551 | CX_TEST_ASSERT(buf.pos == 8); |
universe@792 | 552 | CX_TEST_ASSERT(buf.size == 8); |
universe@792 | 553 | CX_TEST_ASSERT(buf.capacity == 8); |
universe@792 | 554 | CX_TEST_ASSERT(memcmp(buf.space, "test__teXXXXXXXX", 16) == 0); |
universe@792 | 555 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 556 | } |
universe@792 | 557 | } |
universe@792 | 558 | |
universe@792 | 559 | CX_TEST(test_buffer_shift_right_overshift_extend) { |
universe@792 | 560 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 561 | buf.flags |= CX_BUFFER_AUTO_EXTEND; |
universe@792 | 562 | CX_TEST_DO { |
universe@792 | 563 | int ret = cxBufferShiftRight(&buf, 6); |
universe@792 | 564 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 565 | CX_TEST_ASSERT(buf.pos == 10); |
universe@792 | 566 | CX_TEST_ASSERT(buf.size == 10); |
universe@792 | 567 | CX_TEST_ASSERT(buf.capacity >= 10); |
universe@792 | 568 | // cannot assert more than 10 bytes because |
universe@792 | 569 | // the buffer was required to reallocate the space |
universe@792 | 570 | CX_TEST_ASSERT(memcmp(buf.space, "test__test", 10) == 0); |
universe@792 | 571 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 572 | } |
universe@792 | 573 | } |
universe@792 | 574 | |
universe@792 | 575 | CX_TEST(test_buffer_shift_right_offset_interface) { |
universe@792 | 576 | TEST_BUFFER_SHIFT_SETUP(buf); |
universe@792 | 577 | buf.pos = 3; |
universe@792 | 578 | CX_TEST_DO { |
universe@792 | 579 | int ret = cxBufferShift(&buf, 2); |
universe@792 | 580 | CX_TEST_ASSERT(ret == 0); |
universe@792 | 581 | CX_TEST_ASSERT(buf.pos == 5); |
universe@792 | 582 | CX_TEST_ASSERT(buf.size == 6); |
universe@792 | 583 | CX_TEST_ASSERT(memcmp(buf.space, "tetest__XXXXXXXX", 16) == 0); |
universe@792 | 584 | TEST_BUFFER_SHIFT_TEARDOWN(buf); |
universe@792 | 585 | } |
universe@792 | 586 | } |
universe@792 | 587 | |
universe@789 | 588 | CxTestSuite *cx_test_suite_buffer(void) { |
universe@789 | 589 | CxTestSuite *suite = cx_test_suite_new("buffer"); |
universe@789 | 590 | |
universe@789 | 591 | cx_test_register(suite, test_buffer_init_wrap_space); |
universe@789 | 592 | cx_test_register(suite, test_buffer_init_wrap_space_auto_extend); |
universe@789 | 593 | cx_test_register(suite, test_buffer_init_wrap_space_auto_free); |
universe@789 | 594 | cx_test_register(suite, test_buffer_init_fresh_space); |
universe@789 | 595 | cx_test_register(suite, test_buffer_init_on_heap); |
universe@789 | 596 | cx_test_register(suite, test_buffer_minimum_capacity_sufficient); |
universe@789 | 597 | cx_test_register(suite, test_buffer_minimum_capacity_extend); |
universe@789 | 598 | cx_test_register(suite, test_buffer_clear); |
universe@789 | 599 | cx_test_register(suite, test_buffer_reset); |
universe@789 | 600 | cx_test_register(suite, test_buffer_seek_set_zero); |
universe@789 | 601 | cx_test_register(suite, test_buffer_seek_set_valid); |
universe@789 | 602 | cx_test_register(suite, test_buffer_seek_set_invalid); |
universe@789 | 603 | cx_test_register(suite, test_buffer_seek_cur_zero); |
universe@789 | 604 | cx_test_register(suite, test_buffer_seek_cur_valid_positive); |
universe@789 | 605 | cx_test_register(suite, test_buffer_seek_cur_valid_negative); |
universe@789 | 606 | cx_test_register(suite, test_buffer_seek_cur_invalid_positive); |
universe@789 | 607 | cx_test_register(suite, test_buffer_seek_cur_invalid_negative); |
universe@789 | 608 | cx_test_register(suite, test_buffer_seek_end_zero); |
universe@789 | 609 | cx_test_register(suite, test_buffer_seek_end_valid); |
universe@789 | 610 | cx_test_register(suite, test_buffer_seek_end_invalid); |
universe@789 | 611 | cx_test_register(suite, test_buffer_seek_whence_invalid); |
universe@792 | 612 | cx_test_register(suite, test_buffer_eof_reached); |
universe@792 | 613 | cx_test_register(suite, test_buffer_eof_not_reached); |
universe@792 | 614 | cx_test_register(suite, test_buffer_shift_left_zero); |
universe@792 | 615 | cx_test_register(suite, test_buffer_shift_left_zero_offset_interface); |
universe@792 | 616 | cx_test_register(suite, test_buffer_shift_left_standard); |
universe@792 | 617 | cx_test_register(suite, test_buffer_shift_left_overshift); |
universe@792 | 618 | cx_test_register(suite, test_buffer_shift_left_overshift_pos_only); |
universe@792 | 619 | cx_test_register(suite, test_buffer_shift_left_offset_interface); |
universe@792 | 620 | cx_test_register(suite, test_buffer_shift_right_zero); |
universe@792 | 621 | cx_test_register(suite, test_buffer_shift_right_zero_offset_interface); |
universe@792 | 622 | cx_test_register(suite, test_buffer_shift_right_standard); |
universe@792 | 623 | cx_test_register(suite, test_buffer_shift_right_overshift_discard); |
universe@792 | 624 | cx_test_register(suite, test_buffer_shift_right_overshift_extend); |
universe@792 | 625 | cx_test_register(suite, test_buffer_shift_right_offset_interface); |
universe@789 | 626 | |
universe@789 | 627 | return suite; |
universe@789 | 628 | } |