| 1570:8fd491bc2940 | 1571:25ead2ffb9b5 |
|---|---|
| 38 CxAllocator *alloc = &talloc.base; | 38 CxAllocator *alloc = &talloc.base; |
| 39 CX_TEST_DO { | 39 CX_TEST_DO { |
| 40 CxBuffer buf; | 40 CxBuffer buf; |
| 41 void *space = cxMalloc(alloc, 16); | 41 void *space = cxMalloc(alloc, 16); |
| 42 cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_DEFAULT); | 42 cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_DEFAULT); |
| 43 CX_TEST_ASSERT(buf.flush == NULL); | |
| 44 CX_TEST_ASSERT(buf.space == space); | 43 CX_TEST_ASSERT(buf.space == space); |
| 45 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); | 44 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); |
| 46 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); | 45 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); |
| 47 CX_TEST_ASSERT(buf.pos == 0); | 46 CX_TEST_ASSERT(buf.pos == 0); |
| 48 CX_TEST_ASSERT(buf.size == 0); | 47 CX_TEST_ASSERT(buf.size == 0); |
| 49 CX_TEST_ASSERT(buf.capacity == 16); | 48 CX_TEST_ASSERT(buf.capacity == 16); |
| 49 CX_TEST_ASSERT(buf.max_capacity == SIZE_MAX); | |
| 50 CX_TEST_ASSERT(buf.allocator == alloc); | 50 CX_TEST_ASSERT(buf.allocator == alloc); |
| 51 cxBufferDestroy(&buf); | 51 cxBufferDestroy(&buf); |
| 52 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); | 52 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); |
| 53 cxFree(alloc, space); | 53 cxFree(alloc, space); |
| 54 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); | 54 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 62 CxAllocator *alloc = &talloc.base; | 62 CxAllocator *alloc = &talloc.base; |
| 63 CX_TEST_DO { | 63 CX_TEST_DO { |
| 64 CxBuffer buf; | 64 CxBuffer buf; |
| 65 void *space = cxMalloc(alloc, 16); | 65 void *space = cxMalloc(alloc, 16); |
| 66 cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_AUTO_EXTEND); | 66 cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_AUTO_EXTEND); |
| 67 CX_TEST_ASSERT(buf.flush == NULL); | |
| 68 CX_TEST_ASSERT(buf.space == space); | 67 CX_TEST_ASSERT(buf.space == space); |
| 69 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND); | 68 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND); |
| 70 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); | 69 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == 0); |
| 71 CX_TEST_ASSERT(buf.pos == 0); | 70 CX_TEST_ASSERT(buf.pos == 0); |
| 72 CX_TEST_ASSERT(buf.size == 0); | 71 CX_TEST_ASSERT(buf.size == 0); |
| 73 CX_TEST_ASSERT(buf.capacity == 16); | 72 CX_TEST_ASSERT(buf.capacity == 16); |
| 73 CX_TEST_ASSERT(buf.max_capacity == SIZE_MAX); | |
| 74 CX_TEST_ASSERT(buf.allocator == alloc); | 74 CX_TEST_ASSERT(buf.allocator == alloc); |
| 75 cxBufferDestroy(&buf); | 75 cxBufferDestroy(&buf); |
| 76 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); | 76 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); |
| 77 cxFree(alloc, space); | 77 cxFree(alloc, space); |
| 78 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); | 78 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 86 CxAllocator *alloc = &talloc.base; | 86 CxAllocator *alloc = &talloc.base; |
| 87 CX_TEST_DO { | 87 CX_TEST_DO { |
| 88 CxBuffer buf; | 88 CxBuffer buf; |
| 89 void *space = cxMalloc(alloc, 16); | 89 void *space = cxMalloc(alloc, 16); |
| 90 cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_FREE_CONTENTS); | 90 cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_FREE_CONTENTS); |
| 91 CX_TEST_ASSERT(buf.flush == NULL); | |
| 92 CX_TEST_ASSERT(buf.space == space); | 91 CX_TEST_ASSERT(buf.space == space); |
| 93 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); | 92 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); |
| 94 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); | 93 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
| 95 CX_TEST_ASSERT(buf.pos == 0); | 94 CX_TEST_ASSERT(buf.pos == 0); |
| 96 CX_TEST_ASSERT(buf.size == 0); | 95 CX_TEST_ASSERT(buf.size == 0); |
| 97 CX_TEST_ASSERT(buf.capacity == 16); | 96 CX_TEST_ASSERT(buf.capacity == 16); |
| 97 CX_TEST_ASSERT(buf.max_capacity == SIZE_MAX); | |
| 98 CX_TEST_ASSERT(buf.allocator == alloc); | 98 CX_TEST_ASSERT(buf.allocator == alloc); |
| 99 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); | 99 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); |
| 100 cxBufferDestroy(&buf); | 100 cxBufferDestroy(&buf); |
| 101 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); | 101 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 102 } | 102 } |
| 108 cx_testing_allocator_init(&talloc); | 108 cx_testing_allocator_init(&talloc); |
| 109 CxAllocator *alloc = &talloc.base; | 109 CxAllocator *alloc = &talloc.base; |
| 110 CX_TEST_DO { | 110 CX_TEST_DO { |
| 111 CxBuffer buf; | 111 CxBuffer buf; |
| 112 cxBufferInit(&buf, NULL, 8, alloc, CX_BUFFER_DEFAULT); | 112 cxBufferInit(&buf, NULL, 8, alloc, CX_BUFFER_DEFAULT); |
| 113 CX_TEST_ASSERT(buf.flush == NULL); | |
| 114 CX_TEST_ASSERT(buf.space != NULL); | 113 CX_TEST_ASSERT(buf.space != NULL); |
| 115 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); | 114 CX_TEST_ASSERT((buf.flags & CX_BUFFER_AUTO_EXTEND) == 0); |
| 116 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); | 115 CX_TEST_ASSERT((buf.flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
| 117 CX_TEST_ASSERT(buf.pos == 0); | 116 CX_TEST_ASSERT(buf.pos == 0); |
| 118 CX_TEST_ASSERT(buf.size == 0); | 117 CX_TEST_ASSERT(buf.size == 0); |
| 119 CX_TEST_ASSERT(buf.capacity == 8); | 118 CX_TEST_ASSERT(buf.capacity == 8); |
| 119 CX_TEST_ASSERT(buf.max_capacity == SIZE_MAX); | |
| 120 CX_TEST_ASSERT(buf.allocator == alloc); | 120 CX_TEST_ASSERT(buf.allocator == alloc); |
| 121 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); // space is still allocated | 121 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); // space is still allocated |
| 122 cxBufferDestroy(&buf); | 122 cxBufferDestroy(&buf); |
| 123 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); | 123 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 124 } | 124 } |
| 132 CX_TEST_DO { | 132 CX_TEST_DO { |
| 133 CxBuffer *buf; | 133 CxBuffer *buf; |
| 134 void *space = cxMalloc(alloc, 16); | 134 void *space = cxMalloc(alloc, 16); |
| 135 buf = cxBufferCreate(space, 16, alloc, CX_BUFFER_FREE_CONTENTS); | 135 buf = cxBufferCreate(space, 16, alloc, CX_BUFFER_FREE_CONTENTS); |
| 136 CX_TEST_ASSERT(buf != NULL); | 136 CX_TEST_ASSERT(buf != NULL); |
| 137 CX_TEST_ASSERT(buf->flush == NULL); | |
| 138 CX_TEST_ASSERT(buf->space == space); | 137 CX_TEST_ASSERT(buf->space == space); |
| 139 CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); | 138 CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); |
| 140 CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); | 139 CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
| 141 CX_TEST_ASSERT(buf->pos == 0); | 140 CX_TEST_ASSERT(buf->pos == 0); |
| 142 CX_TEST_ASSERT(buf->size == 0); | 141 CX_TEST_ASSERT(buf->size == 0); |
| 143 CX_TEST_ASSERT(buf->capacity == 16); | 142 CX_TEST_ASSERT(buf->capacity == 16); |
| 143 CX_TEST_ASSERT(buf->max_capacity == SIZE_MAX); | |
| 144 CX_TEST_ASSERT(buf->allocator == alloc); | 144 CX_TEST_ASSERT(buf->allocator == alloc); |
| 145 cxBufferFree(buf); | 145 cxBufferFree(buf); |
| 146 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); | 146 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 147 } | 147 } |
| 148 cx_testing_allocator_destroy(&talloc); | 148 cx_testing_allocator_destroy(&talloc); |
| 151 CX_TEST(test_buffer_create_defaulted_allocator) { | 151 CX_TEST(test_buffer_create_defaulted_allocator) { |
| 152 CX_TEST_DO { | 152 CX_TEST_DO { |
| 153 CxBuffer *buf; | 153 CxBuffer *buf; |
| 154 buf = cxBufferCreate(NULL, 16, NULL, 0); | 154 buf = cxBufferCreate(NULL, 16, NULL, 0); |
| 155 CX_TEST_ASSERT(buf != NULL); | 155 CX_TEST_ASSERT(buf != NULL); |
| 156 CX_TEST_ASSERT(buf->flush == NULL); | |
| 157 CX_TEST_ASSERT(buf->space != NULL); | 156 CX_TEST_ASSERT(buf->space != NULL); |
| 158 CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); | 157 CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); |
| 159 CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); | 158 CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
| 160 CX_TEST_ASSERT(buf->pos == 0); | 159 CX_TEST_ASSERT(buf->pos == 0); |
| 161 CX_TEST_ASSERT(buf->size == 0); | 160 CX_TEST_ASSERT(buf->size == 0); |
| 162 CX_TEST_ASSERT(buf->capacity == 16); | 161 CX_TEST_ASSERT(buf->capacity == 16); |
| 162 CX_TEST_ASSERT(buf->max_capacity == SIZE_MAX); | |
| 163 CX_TEST_ASSERT(buf->allocator == cxDefaultAllocator); | 163 CX_TEST_ASSERT(buf->allocator == cxDefaultAllocator); |
| 164 cxBufferFree(buf); | 164 cxBufferFree(buf); |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 210 | 210 |
| 211 cxBufferDestroy(&buf); | 211 cxBufferDestroy(&buf); |
| 212 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); | 212 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 213 } | 213 } |
| 214 cx_testing_allocator_destroy(&talloc); | 214 cx_testing_allocator_destroy(&talloc); |
| 215 } | |
| 216 | |
| 217 CX_TEST(test_buffer_maximum_capacity) { | |
| 218 CxBuffer buf; | |
| 219 cxBufferInit(&buf, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND); | |
| 220 CX_TEST_DO { | |
| 221 // set maximum capacity | |
| 222 CX_TEST_ASSERT(0 == cxBufferMaximumCapacity(&buf, 512)); | |
| 223 CX_TEST_ASSERT(buf.capacity == 256); | |
| 224 CX_TEST_ASSERT(buf.max_capacity == 512); | |
| 225 // increase maximum capacity again | |
| 226 CX_TEST_ASSERT(0 == cxBufferMaximumCapacity(&buf, 1024)); | |
| 227 CX_TEST_ASSERT(buf.capacity == 256); | |
| 228 CX_TEST_ASSERT(buf.max_capacity == 1024); | |
| 229 // try to reduce maximum capacity below current capacity | |
| 230 CX_TEST_ASSERT(0 != cxBufferMaximumCapacity(&buf, 128)); | |
| 231 CX_TEST_ASSERT(buf.capacity == 256); | |
| 232 CX_TEST_ASSERT(buf.max_capacity == 1024); | |
| 233 | |
| 234 // try to reserve more than the maximum | |
| 235 CX_TEST_ASSERT(0 == cxBufferMaximumCapacity(&buf, 512)); | |
| 236 CX_TEST_ASSERT(0 != cxBufferReserve(&buf, 600)); | |
| 237 CX_TEST_ASSERT(buf.capacity == 256); | |
| 238 CX_TEST_ASSERT(buf.max_capacity == 512); | |
| 239 CX_TEST_ASSERT(0 == cxBufferReserve(&buf, 512)); | |
| 240 CX_TEST_ASSERT(buf.capacity == 512); | |
| 241 CX_TEST_ASSERT(buf.max_capacity == 512); | |
| 242 | |
| 243 // make sure that cxBufferMinimumCapacity() is capped at the limit | |
| 244 CX_TEST_ASSERT(0 == cxBufferMaximumCapacity(&buf, 777)); | |
| 245 CX_TEST_ASSERT(0 != cxBufferMinimumCapacity(&buf, 800)); | |
| 246 CX_TEST_ASSERT(buf.capacity == 512); | |
| 247 CX_TEST_ASSERT(buf.max_capacity == 777); | |
| 248 CX_TEST_ASSERT(0 == cxBufferMinimumCapacity(&buf, 700)); | |
| 249 CX_TEST_ASSERT(buf.capacity == 777); | |
| 250 CX_TEST_ASSERT(buf.max_capacity == 777); | |
| 251 } | |
| 252 cxBufferDestroy(&buf); | |
| 215 } | 253 } |
| 216 | 254 |
| 217 CX_TEST(test_buffer_shrink) { | 255 CX_TEST(test_buffer_shrink) { |
| 218 CxTestingAllocator talloc; | 256 CxTestingAllocator talloc; |
| 219 cx_testing_allocator_init(&talloc); | 257 cx_testing_allocator_init(&talloc); |
| 762 CX_TEST_ASSERT(errno == EOVERFLOW); | 800 CX_TEST_ASSERT(errno == EOVERFLOW); |
| 763 TEST_BUFFER_SHIFT_TEARDOWN(buf); | 801 TEST_BUFFER_SHIFT_TEARDOWN(buf); |
| 764 } | 802 } |
| 765 } | 803 } |
| 766 | 804 |
| 767 static size_t mock_write_limited_rate( | |
| 768 const void *ptr, | |
| 769 size_t size, | |
| 770 cx_attr_unused size_t nitems, | |
| 771 CxBuffer *buffer | |
| 772 ) { | |
| 773 return cxBufferWrite(ptr, size, nitems > 2 ? 2 : nitems, buffer); | |
| 774 } | |
| 775 | |
| 776 CX_TEST(test_buffer_write_size_one_fit) { | 805 CX_TEST(test_buffer_write_size_one_fit) { |
| 777 CxBuffer buf; | 806 CxBuffer buf; |
| 778 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); | 807 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
| 779 memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16); | 808 memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16); |
| 780 buf.capacity = 8; | 809 buf.capacity = 8; |
| 924 CX_TEST_ASSERT(buf.pos == 4); | 953 CX_TEST_ASSERT(buf.pos == 4); |
| 925 CX_TEST_ASSERT(buf.capacity >= 13); | 954 CX_TEST_ASSERT(buf.capacity >= 13); |
| 926 CX_TEST_ASSERT(0 == memcmp(buf.space, "prepXXtesting", 13)); | 955 CX_TEST_ASSERT(0 == memcmp(buf.space, "prepXXtesting", 13)); |
| 927 } | 956 } |
| 928 cxBufferDestroy(&buf); | 957 cxBufferDestroy(&buf); |
| 929 } | |
| 930 | |
| 931 CX_TEST(test_buffer_append_flush) { | |
| 932 CxBuffer buf, target; | |
| 933 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 934 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 935 memcpy(buf.space, "prepXXXX", 8); | |
| 936 buf.capacity = 8; | |
| 937 buf.size = 6; | |
| 938 buf.pos = 4; | |
| 939 CxBufferFlushConfig flush; | |
| 940 flush.threshold = 0; | |
| 941 flush.blksize = 4; | |
| 942 flush.blkmax = 1; | |
| 943 flush.target = ⌖ | |
| 944 flush.wfunc = cxBufferWriteFunc; | |
| 945 cxBufferEnableFlushing(&buf, flush); | |
| 946 CX_TEST_DO{ | |
| 947 size_t written = cxBufferAppend("testing", 1, 7, &buf); | |
| 948 CX_TEST_ASSERT(written == 7); | |
| 949 CX_TEST_ASSERT(buf.size == 7); | |
| 950 CX_TEST_ASSERTM(buf.pos == 0, "position not correctly reset"); | |
| 951 CX_TEST_ASSERT(buf.capacity == 8); | |
| 952 CX_TEST_ASSERT(target.size == 6); | |
| 953 CX_TEST_ASSERT(target.pos == 6); | |
| 954 CX_TEST_ASSERT(0 == memcmp(buf.space, "testing", 7)); | |
| 955 CX_TEST_ASSERT(0 == memcmp(target.space, "prepXX", 6)); | |
| 956 // second test - position only shifted by one block | |
| 957 buf.pos = 6; | |
| 958 written = cxBufferAppend("foo", 1, 3, &buf); | |
| 959 CX_TEST_ASSERT(written == 3); | |
| 960 CX_TEST_ASSERT(buf.size == 6); | |
| 961 CX_TEST_ASSERTM(buf.pos == 2, "position not correctly adjusted"); | |
| 962 CX_TEST_ASSERT(buf.capacity == 8); | |
| 963 CX_TEST_ASSERT(target.size == 10); | |
| 964 CX_TEST_ASSERT(target.pos == 10); | |
| 965 CX_TEST_ASSERT(0 == memcmp(buf.space, "ingfoo", 6)); | |
| 966 CX_TEST_ASSERT(0 == memcmp(target.space, "prepXXtest", 10)); | |
| 967 } | |
| 968 cxBufferDestroy(&buf); | |
| 969 cxBufferDestroy(&target); | |
| 970 } | 958 } |
| 971 | 959 |
| 972 CX_TEST(test_buffer_put_fit) { | 960 CX_TEST(test_buffer_put_fit) { |
| 973 CxBuffer buf; | 961 CxBuffer buf; |
| 974 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); | 962 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
| 1228 CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0", 5)); | 1216 CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0", 5)); |
| 1229 } | 1217 } |
| 1230 cxBufferDestroy(&buf); | 1218 cxBufferDestroy(&buf); |
| 1231 } | 1219 } |
| 1232 | 1220 |
| 1221 CX_TEST(test_buffer_write_maximum_capacity_exceeded) { | |
| 1222 CxBuffer buf; | |
| 1223 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1224 CX_TEST_DO { | |
| 1225 cxBufferMaximumCapacity(&buf, 30); | |
| 1226 size_t written = cxBufferPutString(&buf, "Hello, World!\nHello, Tester!"); | |
| 1227 CX_TEST_ASSERT(written == 28); | |
| 1228 CX_TEST_ASSERT(buf.capacity == 30); // would be 32 without limit! | |
| 1229 CX_TEST_ASSERT(buf.pos == 28); | |
| 1230 CX_TEST_ASSERT(buf.size == 28); | |
| 1231 cxBufferMaximumCapacity(&buf, 34); | |
| 1232 written = cxBufferPutString(&buf, "blubberbla"); | |
| 1233 CX_TEST_ASSERT(written == 6); | |
| 1234 CX_TEST_ASSERT(buf.capacity == 34); | |
| 1235 CX_TEST_ASSERT(buf.pos == 34); | |
| 1236 CX_TEST_ASSERT(buf.size == 34); | |
| 1237 CX_TEST_ASSERT(0 == memcmp(buf.space, "Hello, World!\nHello, Tester!blubbe", 34)); | |
| 1238 | |
| 1239 // test multi-byte as well | |
| 1240 cxBufferMaximumCapacity(&buf, 44); | |
| 1241 written = cxBufferWrite("1234abcdABCD", 4, 3, &buf); | |
| 1242 CX_TEST_ASSERT(written == 2); | |
| 1243 CX_TEST_ASSERT(buf.capacity == 44); | |
| 1244 CX_TEST_ASSERT(buf.pos == 42); | |
| 1245 CX_TEST_ASSERT(buf.size == 42); | |
| 1246 CX_TEST_ASSERT(0 == memcmp(buf.space, "Hello, World!\nHello, Tester!blubbe1234abcd", 42)); | |
| 1247 } | |
| 1248 cxBufferDestroy(&buf); | |
| 1249 } | |
| 1250 | |
| 1233 CX_TEST(test_buffer_write_only_overwrite) { | 1251 CX_TEST(test_buffer_write_only_overwrite) { |
| 1234 CxBuffer buf; | 1252 CxBuffer buf; |
| 1235 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); | 1253 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
| 1236 memcpy(buf.space, "preptest\0\0\0\0\0\0\0\0", 16); | 1254 memcpy(buf.space, "preptest\0\0\0\0\0\0\0\0", 16); |
| 1237 buf.capacity = 8; | 1255 buf.capacity = 8; |
| 1245 CX_TEST_ASSERT(buf.size == 8); | 1263 CX_TEST_ASSERT(buf.size == 8); |
| 1246 CX_TEST_ASSERT(buf.pos == 7); | 1264 CX_TEST_ASSERT(buf.pos == 7); |
| 1247 CX_TEST_ASSERT(0 == memcmp(buf.space, "preXXX\0t", 8)); | 1265 CX_TEST_ASSERT(0 == memcmp(buf.space, "preXXX\0t", 8)); |
| 1248 } | 1266 } |
| 1249 cxBufferDestroy(&buf); | 1267 cxBufferDestroy(&buf); |
| 1250 } | |
| 1251 | |
| 1252 CX_TEST(test_buffer_write_flush_at_capacity) { | |
| 1253 CxBuffer buf, target; | |
| 1254 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1255 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1256 memset(buf.space, 0, 8); | |
| 1257 cxBufferPutString(&buf, "prep"); | |
| 1258 CX_TEST_DO { | |
| 1259 CxBufferFlushConfig flush; | |
| 1260 flush.threshold = 0; | |
| 1261 flush.blksize = 32; | |
| 1262 flush.blkmax = 1; | |
| 1263 flush.target = ⌖ | |
| 1264 flush.wfunc = cxBufferWriteFunc; | |
| 1265 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1266 size_t written = cxBufferWrite("foo", 1, 3, &buf); | |
| 1267 CX_TEST_ASSERT(written == 3); | |
| 1268 CX_TEST_ASSERT(buf.pos == 7); | |
| 1269 CX_TEST_ASSERT(buf.size == 7); | |
| 1270 CX_TEST_ASSERT(target.pos == 0); | |
| 1271 CX_TEST_ASSERT(target.size == 0); | |
| 1272 written = cxBufferWrite("hello", 1, 5, &buf); | |
| 1273 CX_TEST_ASSERT(written == 5); | |
| 1274 CX_TEST_ASSERT(buf.pos == 5); | |
| 1275 CX_TEST_ASSERT(buf.size == 5); | |
| 1276 CX_TEST_ASSERT(buf.capacity == 8); | |
| 1277 CX_TEST_ASSERT(target.pos == 7); | |
| 1278 CX_TEST_ASSERT(target.size == 7); | |
| 1279 CX_TEST_ASSERT(0 == memcmp(buf.space, "hello", 5)); | |
| 1280 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoo", 7)); | |
| 1281 } | |
| 1282 cxBufferDestroy(&buf); | |
| 1283 cxBufferDestroy(&target); | |
| 1284 } | |
| 1285 | |
| 1286 CX_TEST(test_buffer_write_flush_at_threshold) { | |
| 1287 CxBuffer buf, target; | |
| 1288 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1289 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1290 cxBufferPutString(&buf, "prep"); | |
| 1291 CX_TEST_DO { | |
| 1292 CxBufferFlushConfig flush; | |
| 1293 flush.threshold = 16; | |
| 1294 flush.blksize = 32; | |
| 1295 flush.blkmax = 1; | |
| 1296 flush.target = ⌖ | |
| 1297 flush.wfunc = cxBufferWriteFunc; | |
| 1298 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1299 size_t written = cxBufferWrite("foobar", 1, 6, &buf); | |
| 1300 CX_TEST_ASSERT(written == 6); | |
| 1301 CX_TEST_ASSERT(buf.pos == 10); | |
| 1302 CX_TEST_ASSERT(buf.size == 10); | |
| 1303 CX_TEST_ASSERT(buf.capacity == 16); | |
| 1304 CX_TEST_ASSERT(target.pos == 0); | |
| 1305 CX_TEST_ASSERT(target.size == 0); | |
| 1306 written = cxBufferWrite("hello world", 1, 11, &buf); | |
| 1307 CX_TEST_ASSERT(written == 11); | |
| 1308 CX_TEST_ASSERT(buf.pos == 11); | |
| 1309 CX_TEST_ASSERT(buf.size == 11); | |
| 1310 CX_TEST_ASSERT(buf.capacity == 16); | |
| 1311 CX_TEST_ASSERT(target.pos == 10); | |
| 1312 CX_TEST_ASSERT(target.size == 10); | |
| 1313 CX_TEST_ASSERT(0 == memcmp(buf.space, "hello", 5)); | |
| 1314 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoobar", 10)); | |
| 1315 } | |
| 1316 cxBufferDestroy(&buf); | |
| 1317 cxBufferDestroy(&target); | |
| 1318 } | |
| 1319 | |
| 1320 CX_TEST(test_buffer_write_flush_rate_limited_and_buffer_too_small) { | |
| 1321 // the idea is that the target only accepts two bytes and | |
| 1322 // then gives up... accepts another two bytes, gives up, etc. | |
| 1323 // and at the same time, the written string is too large for | |
| 1324 // the buffer (buffer can take 8, we want to write 13) | |
| 1325 CxBuffer buf, target; | |
| 1326 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1327 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1328 cxBufferPutString(&buf, "prep"); | |
| 1329 CX_TEST_DO { | |
| 1330 CxBufferFlushConfig flush; | |
| 1331 flush.threshold = 0; | |
| 1332 flush.blksize = 32; | |
| 1333 flush.blkmax = 1; | |
| 1334 flush.target = ⌖ | |
| 1335 flush.wfunc = (cx_write_func)mock_write_limited_rate; | |
| 1336 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1337 size_t written = cxBufferWrite("foo", 1, 3, &buf); | |
| 1338 CX_TEST_ASSERT(written == 3); | |
| 1339 CX_TEST_ASSERT(buf.pos == 7); | |
| 1340 CX_TEST_ASSERT(buf.size == 7); | |
| 1341 CX_TEST_ASSERT(target.pos == 0); | |
| 1342 CX_TEST_ASSERT(target.size == 0); | |
| 1343 written = cxBufferWrite("hello, world!", 1, 13, &buf); | |
| 1344 // " world!" fits into this buffer, the remaining stuff is flushed out | |
| 1345 CX_TEST_ASSERT(written == 13); | |
| 1346 CX_TEST_ASSERT(buf.pos == 7); | |
| 1347 CX_TEST_ASSERT(buf.size == 7); | |
| 1348 CX_TEST_ASSERT(buf.capacity == 8); | |
| 1349 CX_TEST_ASSERT(0 == memcmp(buf.space, " world!", 7)); | |
| 1350 CX_TEST_ASSERT(target.pos == 13); | |
| 1351 CX_TEST_ASSERT(target.size == 13); | |
| 1352 CX_TEST_ASSERT(target.capacity >= 13); | |
| 1353 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoohello,", 13)); | |
| 1354 } | |
| 1355 cxBufferDestroy(&buf); | |
| 1356 cxBufferDestroy(&target); | |
| 1357 } | |
| 1358 | |
| 1359 CX_TEST(test_buffer_write_flush_multibyte) { | |
| 1360 // this test case tests that flushing works correctly even when the | |
| 1361 // contents in the buffer are currently not aligned with the item size | |
| 1362 CxBuffer buf, target; | |
| 1363 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1364 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1365 memset(buf.space, 0, 8); | |
| 1366 cxBufferPutString(&buf, "pre"); | |
| 1367 CX_TEST_DO { | |
| 1368 CxBufferFlushConfig flush; | |
| 1369 flush.threshold = 0; | |
| 1370 flush.blksize = 4; // test blksize that is not aligned | |
| 1371 flush.blkmax = 2; // test with two blocks | |
| 1372 flush.target = ⌖ | |
| 1373 flush.wfunc = cxBufferWriteFunc; | |
| 1374 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1375 // first case: string fits after flush | |
| 1376 size_t written = cxBufferWrite("foobar", 3, 2, &buf); | |
| 1377 CX_TEST_ASSERT(written == 2); | |
| 1378 CX_TEST_ASSERT(buf.pos == 6); | |
| 1379 CX_TEST_ASSERT(buf.size == 6); | |
| 1380 CX_TEST_ASSERT(target.pos == 3); | |
| 1381 CX_TEST_ASSERT(target.size == 3); | |
| 1382 CX_TEST_ASSERT(0 == memcmp(buf.space, "foobar", 6)); | |
| 1383 CX_TEST_ASSERT(0 == memcmp(target.space, "pre", 3)); | |
| 1384 // second case: string does not fit, data is relayed, but only two blocks! | |
| 1385 written = cxBufferWrite("bazfooBAR", 3, 3, &buf); | |
| 1386 CX_TEST_ASSERT(written == 3); | |
| 1387 CX_TEST_ASSERT(buf.pos == 3); | |
| 1388 CX_TEST_ASSERT(buf.size == 3); | |
| 1389 CX_TEST_ASSERT(target.pos == 15); | |
| 1390 CX_TEST_ASSERT(target.size == 15); | |
| 1391 CX_TEST_ASSERT(0 == memcmp(buf.space, "BAR", 3)); | |
| 1392 CX_TEST_ASSERT(0 == memcmp(target.space, "prefoobarbazfoo", 15)); | |
| 1393 // third case: everything can be relayed, block size is large enough | |
| 1394 buf.flush->blkmax = 3; | |
| 1395 written = cxBufferWrite("abcdef012", 3, 3, &buf); | |
| 1396 CX_TEST_ASSERT(written == 3); | |
| 1397 CX_TEST_ASSERT(buf.pos == 0); | |
| 1398 CX_TEST_ASSERT(buf.size == 0); | |
| 1399 CX_TEST_ASSERT(target.pos == 27); | |
| 1400 CX_TEST_ASSERT(target.size == 27); | |
| 1401 CX_TEST_ASSERT(0 == memcmp(target.space, "prefoobarbazfooBARabcdef012", 27)); | |
| 1402 } | |
| 1403 cxBufferDestroy(&buf); | |
| 1404 cxBufferDestroy(&target); | |
| 1405 } | |
| 1406 | |
| 1407 CX_TEST(test_buffer_write_flush_misaligned) { | |
| 1408 // this test case tests that flushing works correctly even when the | |
| 1409 // contents in the buffer are currently not aligned with the item size | |
| 1410 CxBuffer buf, target; | |
| 1411 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1412 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1413 cxBufferPutString(&buf, "prep"); | |
| 1414 CX_TEST_DO { | |
| 1415 CxBufferFlushConfig flush; | |
| 1416 flush.threshold = 0; | |
| 1417 flush.blksize = 32; | |
| 1418 flush.blkmax = 1; | |
| 1419 flush.target = ⌖ | |
| 1420 flush.wfunc = cxBufferWriteFunc; | |
| 1421 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1422 // first case: string fits after flush | |
| 1423 size_t written = cxBufferWrite("foobar", 3, 2, &buf); | |
| 1424 CX_TEST_ASSERT(written == 2); | |
| 1425 CX_TEST_ASSERT(buf.pos == 7); | |
| 1426 CX_TEST_ASSERT(buf.size == 7); | |
| 1427 CX_TEST_ASSERT(target.pos == 3); | |
| 1428 CX_TEST_ASSERT(target.size == 3); | |
| 1429 CX_TEST_ASSERT(0 == memcmp(buf.space, "pfoobar", 7)); | |
| 1430 CX_TEST_ASSERT(0 == memcmp(target.space, "pre", 3)); | |
| 1431 // second case: string does not fit, relaying not possible due to misalignment | |
| 1432 // string will be truncated (two items fit into buffer, the third does not) | |
| 1433 written = cxBufferWrite("bazfoobar", 3, 3, &buf); | |
| 1434 CX_TEST_ASSERT(written == 2); | |
| 1435 CX_TEST_ASSERT(buf.pos == 7); | |
| 1436 CX_TEST_ASSERT(buf.size == 7); | |
| 1437 CX_TEST_ASSERT(target.pos == 9); | |
| 1438 CX_TEST_ASSERT(target.size == 9); | |
| 1439 CX_TEST_ASSERT(0 == memcmp(buf.space, "rbazfoo", 7)); | |
| 1440 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfooba", 9)); | |
| 1441 } | |
| 1442 cxBufferDestroy(&buf); | |
| 1443 cxBufferDestroy(&target); | |
| 1444 } | |
| 1445 | |
| 1446 CX_TEST(test_buffer_write_flush_target_full) { | |
| 1447 CxBuffer buf, target; | |
| 1448 // target does NOT auto-extend and can get completely full | |
| 1449 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1450 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1451 cxBufferPutString(&buf, "prep"); | |
| 1452 CX_TEST_DO { | |
| 1453 CxBufferFlushConfig flush; | |
| 1454 flush.threshold = 0; | |
| 1455 flush.blksize = 32; | |
| 1456 flush.blkmax = 1; | |
| 1457 flush.target = ⌖ | |
| 1458 flush.wfunc = cxBufferWriteFunc; | |
| 1459 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1460 // step one - flush 4 existing bytes, write 6 new bytes | |
| 1461 size_t written = cxBufferWrite("foobar", 1, 6, &buf); | |
| 1462 CX_TEST_ASSERT(written == 6); | |
| 1463 CX_TEST_ASSERT(buf.pos == 6); | |
| 1464 CX_TEST_ASSERT(buf.size == 6); | |
| 1465 CX_TEST_ASSERT(target.pos == 4); | |
| 1466 CX_TEST_ASSERT(target.size == 4); | |
| 1467 CX_TEST_ASSERT(0 == memcmp(buf.space, "foobar", 6)); | |
| 1468 CX_TEST_ASSERT(0 == memcmp(target.space, "prep", 4)); | |
| 1469 // step two - can only flush 4 more bytes, but rest fits into buffer | |
| 1470 written = cxBufferWrite("xyz", 1, 3, &buf); | |
| 1471 CX_TEST_ASSERT(written == 3); | |
| 1472 CX_TEST_ASSERT(buf.pos == 5); | |
| 1473 CX_TEST_ASSERT(buf.size == 5); | |
| 1474 CX_TEST_ASSERT(target.pos == 8); | |
| 1475 CX_TEST_ASSERT(target.size == 8); | |
| 1476 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz", 5)); | |
| 1477 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); | |
| 1478 // step three - cannot flush more, but can write 3 more bytes | |
| 1479 written = cxBufferWrite("123456", 1, 6, &buf); | |
| 1480 CX_TEST_ASSERT(written == 3); | |
| 1481 CX_TEST_ASSERT(buf.pos == 8); | |
| 1482 CX_TEST_ASSERT(buf.size == 8); | |
| 1483 CX_TEST_ASSERT(target.pos == 8); | |
| 1484 CX_TEST_ASSERT(target.size == 8); | |
| 1485 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8)); | |
| 1486 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); | |
| 1487 // final test - cannot write anything more | |
| 1488 written = cxBufferWrite("baz", 1, 3, &buf); | |
| 1489 CX_TEST_ASSERT(written == 0); | |
| 1490 CX_TEST_ASSERT(buf.pos == 8); | |
| 1491 CX_TEST_ASSERT(buf.size == 8); | |
| 1492 CX_TEST_ASSERT(target.pos == 8); | |
| 1493 CX_TEST_ASSERT(target.size == 8); | |
| 1494 CX_TEST_ASSERT(0 == memcmp(buf.space, "arxyz123", 8)); | |
| 1495 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); | |
| 1496 } | |
| 1497 cxBufferDestroy(&buf); | |
| 1498 cxBufferDestroy(&target); | |
| 1499 } | |
| 1500 | |
| 1501 CX_TEST(test_buffer_write_flush_multibyte_target_full) { | |
| 1502 CxBuffer buf, target; | |
| 1503 cxBufferInit(&target, NULL, 12, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1504 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1505 CX_TEST_DO { | |
| 1506 CxBufferFlushConfig flush; | |
| 1507 flush.threshold = 12; | |
| 1508 flush.blksize = 8; | |
| 1509 flush.blkmax = 2; | |
| 1510 flush.target = ⌖ | |
| 1511 flush.wfunc = cxBufferWriteFunc; | |
| 1512 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1513 cxBufferPutString(&buf, "preparation"); | |
| 1514 size_t written = cxBufferWrite("teststring", 2, 5, &buf); | |
| 1515 CX_TEST_ASSERT(written == 5); | |
| 1516 CX_TEST_ASSERT(buf.pos == 11); | |
| 1517 CX_TEST_ASSERT(buf.size == 11); | |
| 1518 CX_TEST_ASSERT(target.pos == 10); | |
| 1519 CX_TEST_ASSERT(target.size == 10); | |
| 1520 CX_TEST_ASSERT(0 == memcmp(buf.space, "nteststring", 11)); | |
| 1521 CX_TEST_ASSERT(0 == memcmp(target.space, "preparatio", 10)); | |
| 1522 // pop the misaligned byte from the buffer | |
| 1523 cxBufferPop(&buf, 1, 1); | |
| 1524 // write three more items, but only one fits into the target and one more into the buffer | |
| 1525 written = cxBufferWrite("123456", 2, 3, &buf); | |
| 1526 CX_TEST_ASSERT(written == 2); | |
| 1527 CX_TEST_ASSERT(buf.pos == 12); | |
| 1528 CX_TEST_ASSERT(buf.size == 12); | |
| 1529 CX_TEST_ASSERT(target.pos == 12); | |
| 1530 CX_TEST_ASSERT(target.size == 12); | |
| 1531 CX_TEST_ASSERT(0 == memcmp(buf.space, "eststrin1234", 12)); | |
| 1532 CX_TEST_ASSERT(0 == memcmp(target.space, "preparationt", 12)); | |
| 1533 } | |
| 1534 cxBufferDestroy(&buf); | |
| 1535 cxBufferDestroy(&target); | |
| 1536 } | |
| 1537 | |
| 1538 CX_TEST(test_buffer_write_large_data_flush_target_full) { | |
| 1539 CxBuffer buf, target; | |
| 1540 cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1541 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1542 // simulate that the target is full: | |
| 1543 target.pos = target.size = 16; | |
| 1544 CX_TEST_DO { | |
| 1545 CxBufferFlushConfig flush; | |
| 1546 flush.threshold = 0; | |
| 1547 flush.blksize = 32; | |
| 1548 flush.blkmax = 1; | |
| 1549 flush.target = ⌖ | |
| 1550 flush.wfunc = cxBufferWriteFunc; | |
| 1551 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1552 // write more bytes than the buffer can take, but the target is full | |
| 1553 size_t written = cxBufferWrite("foobarfoobar", 1, 12, &buf); | |
| 1554 CX_TEST_ASSERT(written == 0); | |
| 1555 CX_TEST_ASSERT(buf.pos == 0); | |
| 1556 CX_TEST_ASSERT(buf.size == 0); | |
| 1557 CX_TEST_ASSERT(target.pos == 16); | |
| 1558 CX_TEST_ASSERT(target.size == 16); | |
| 1559 } | |
| 1560 cxBufferDestroy(&buf); | |
| 1561 cxBufferDestroy(&target); | |
| 1562 } | |
| 1563 | |
| 1564 CX_TEST(test_buffer_write_flush_at_threshold_target_full) { | |
| 1565 CxBuffer buf, target; | |
| 1566 // target does NOT auto-extend and can get completely full | |
| 1567 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1568 // source may auto-extend but flushes at a certain threshold | |
| 1569 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1570 cxBufferPutString(&buf, "prep"); | |
| 1571 CX_TEST_DO { | |
| 1572 CxBufferFlushConfig flush; | |
| 1573 flush.threshold = 16; | |
| 1574 flush.blksize = 4; | |
| 1575 flush.blkmax = 2; | |
| 1576 flush.target = ⌖ | |
| 1577 flush.wfunc = cxBufferWriteFunc; | |
| 1578 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1579 // step one - adding 6 bytes does not exceed the threshold | |
| 1580 size_t written = cxBufferWrite("foobar", 1, 6, &buf); | |
| 1581 CX_TEST_ASSERT(written == 6); | |
| 1582 CX_TEST_ASSERT(buf.pos == 10); | |
| 1583 CX_TEST_ASSERT(buf.size == 10); | |
| 1584 CX_TEST_ASSERT(target.pos == 0); | |
| 1585 CX_TEST_ASSERT(target.size == 0); | |
| 1586 CX_TEST_ASSERT(0 == memcmp(buf.space, "prepfoobar", 10)); | |
| 1587 // step two - adding 8 bytes is two too many, two blocks are flushed | |
| 1588 written = cxBufferWrite("12345678", 1, 8, &buf); | |
| 1589 CX_TEST_ASSERT(written == 8); | |
| 1590 CX_TEST_ASSERT(buf.pos == 10); | |
| 1591 CX_TEST_ASSERT(buf.size == 10); | |
| 1592 CX_TEST_ASSERT(target.pos == 8); | |
| 1593 CX_TEST_ASSERT(target.size == 8); | |
| 1594 CX_TEST_ASSERT(0 == memcmp(buf.space, "ar12345678", 10)); | |
| 1595 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); | |
| 1596 // step three - cannot flush more, but can write 6 more bytes | |
| 1597 written = cxBufferWrite("ABCDEFGH", 1, 8, &buf); | |
| 1598 CX_TEST_ASSERT(written == 6); | |
| 1599 CX_TEST_ASSERT(buf.pos == 16); | |
| 1600 CX_TEST_ASSERT(buf.size == 16); | |
| 1601 CX_TEST_ASSERT(target.pos == 8); | |
| 1602 CX_TEST_ASSERT(target.size == 8); | |
| 1603 CX_TEST_ASSERT(0 == memcmp(buf.space, "ar12345678ABCDEF", 16)); | |
| 1604 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); | |
| 1605 // final test - cannot write anything more | |
| 1606 written = cxBufferWrite("baz", 1, 3, &buf); | |
| 1607 CX_TEST_ASSERT(written == 0); | |
| 1608 CX_TEST_ASSERT(buf.pos == 16); | |
| 1609 CX_TEST_ASSERT(buf.size == 16); | |
| 1610 CX_TEST_ASSERT(target.pos == 8); | |
| 1611 CX_TEST_ASSERT(target.size == 8); | |
| 1612 CX_TEST_ASSERT(0 == memcmp(buf.space, "ar12345678ABCDEF", 16)); | |
| 1613 CX_TEST_ASSERT(0 == memcmp(target.space, "prepfoob", 8)); | |
| 1614 } | |
| 1615 cxBufferDestroy(&buf); | |
| 1616 cxBufferDestroy(&target); | |
| 1617 } | 1268 } |
| 1618 | 1269 |
| 1619 CX_TEST(test_buffer_pop) { | 1270 CX_TEST(test_buffer_pop) { |
| 1620 CxBuffer buf; | 1271 CxBuffer buf; |
| 1621 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); | 1272 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
| 1650 CX_TEST_ASSERT(buf.size == 0); | 1301 CX_TEST_ASSERT(buf.size == 0); |
| 1651 } | 1302 } |
| 1652 cxBufferDestroy(&buf); | 1303 cxBufferDestroy(&buf); |
| 1653 } | 1304 } |
| 1654 | 1305 |
| 1655 CX_TEST(test_buffer_flush) { | |
| 1656 CxBuffer buf, target; | |
| 1657 cxBufferInit(&target, NULL, 8, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); | |
| 1658 cxBufferInit(&buf, NULL, 8, cxDefaultAllocator, CX_BUFFER_DEFAULT); | |
| 1659 cxBufferPutString(&buf, "prepare"); | |
| 1660 CX_TEST_DO { | |
| 1661 CxBufferFlushConfig flush; | |
| 1662 flush.threshold = 0; | |
| 1663 flush.blksize = 2; | |
| 1664 flush.blkmax = 2; | |
| 1665 flush.target = ⌖ | |
| 1666 flush.wfunc = cxBufferWriteFunc; | |
| 1667 CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush)); | |
| 1668 CX_TEST_ASSERT(buf.size == 7); | |
| 1669 buf.pos = 5; | |
| 1670 size_t flushed = cxBufferFlush(&buf); | |
| 1671 CX_TEST_ASSERT(flushed == flush.blkmax * flush.blksize); | |
| 1672 CX_TEST_ASSERT(buf.pos == 1); | |
| 1673 CX_TEST_ASSERT(buf.size == 3); | |
| 1674 CX_TEST_ASSERT(target.pos == 4); | |
| 1675 CX_TEST_ASSERT(target.size == 4); | |
| 1676 CX_TEST_ASSERT(0 == memcmp(buf.space, "are", 3)); | |
| 1677 CX_TEST_ASSERT(0 == memcmp(target.space, "prep", 4)); | |
| 1678 flushed = cxBufferFlush(&buf); | |
| 1679 CX_TEST_ASSERT(flushed == 1); | |
| 1680 CX_TEST_ASSERT(buf.pos == 0); | |
| 1681 CX_TEST_ASSERT(buf.size == 2); | |
| 1682 CX_TEST_ASSERT(target.pos == 5); | |
| 1683 CX_TEST_ASSERT(target.size == 5); | |
| 1684 CX_TEST_ASSERT(0 == memcmp(buf.space, "re", 2)); | |
| 1685 CX_TEST_ASSERT(0 == memcmp(target.space, "prepa", 5)); | |
| 1686 } | |
| 1687 cxBufferDestroy(&buf); | |
| 1688 cxBufferDestroy(&target); | |
| 1689 } | |
| 1690 | |
| 1691 CX_TEST(test_buffer_get) { | 1306 CX_TEST(test_buffer_get) { |
| 1692 CxBuffer buf; | 1307 CxBuffer buf; |
| 1693 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); | 1308 cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
| 1694 memcpy(buf.space, "some data\0\0\0\0\0\0\0", 16); | 1309 memcpy(buf.space, "some data\0\0\0\0\0\0\0", 16); |
| 1695 buf.capacity = 12; | 1310 buf.capacity = 12; |
| 1799 cx_test_register(suite, test_buffer_init_fresh_space); | 1414 cx_test_register(suite, test_buffer_init_fresh_space); |
| 1800 cx_test_register(suite, test_buffer_init_on_heap); | 1415 cx_test_register(suite, test_buffer_init_on_heap); |
| 1801 cx_test_register(suite, test_buffer_create_defaulted_allocator); | 1416 cx_test_register(suite, test_buffer_create_defaulted_allocator); |
| 1802 cx_test_register(suite, test_buffer_minimum_capacity_sufficient); | 1417 cx_test_register(suite, test_buffer_minimum_capacity_sufficient); |
| 1803 cx_test_register(suite, test_buffer_minimum_capacity_extend); | 1418 cx_test_register(suite, test_buffer_minimum_capacity_extend); |
| 1419 cx_test_register(suite, test_buffer_maximum_capacity); | |
| 1804 cx_test_register(suite, test_buffer_shrink); | 1420 cx_test_register(suite, test_buffer_shrink); |
| 1805 cx_test_register(suite, test_buffer_shrink_copy_on_write); | 1421 cx_test_register(suite, test_buffer_shrink_copy_on_write); |
| 1806 cx_test_register(suite, test_buffer_shrink_copy_on_extend); | 1422 cx_test_register(suite, test_buffer_shrink_copy_on_extend); |
| 1807 cx_test_register(suite, test_buffer_reserve); | 1423 cx_test_register(suite, test_buffer_reserve); |
| 1808 cx_test_register(suite, test_buffer_clear); | 1424 cx_test_register(suite, test_buffer_clear); |
| 1843 cx_test_register(suite, test_buffer_write_multibyte_fit); | 1459 cx_test_register(suite, test_buffer_write_multibyte_fit); |
| 1844 cx_test_register(suite, test_buffer_write_multibyte_discard); | 1460 cx_test_register(suite, test_buffer_write_multibyte_discard); |
| 1845 cx_test_register(suite, test_buffer_write_multibyte_extend); | 1461 cx_test_register(suite, test_buffer_write_multibyte_extend); |
| 1846 cx_test_register(suite, test_buffer_write_copy_on_write); | 1462 cx_test_register(suite, test_buffer_write_copy_on_write); |
| 1847 cx_test_register(suite, test_buffer_append); | 1463 cx_test_register(suite, test_buffer_append); |
| 1848 cx_test_register(suite, test_buffer_append_flush); | |
| 1849 cx_test_register(suite, test_buffer_put_fit); | 1464 cx_test_register(suite, test_buffer_put_fit); |
| 1850 cx_test_register(suite, test_buffer_put_discard); | 1465 cx_test_register(suite, test_buffer_put_discard); |
| 1851 cx_test_register(suite, test_buffer_put_extend); | 1466 cx_test_register(suite, test_buffer_put_extend); |
| 1852 cx_test_register(suite, test_buffer_put_copy_on_write); | 1467 cx_test_register(suite, test_buffer_put_copy_on_write); |
| 1853 cx_test_register(suite, test_buffer_put_string_fit); | 1468 cx_test_register(suite, test_buffer_put_string_fit); |
| 1856 cx_test_register(suite, test_buffer_put_string_copy_on_extend); | 1471 cx_test_register(suite, test_buffer_put_string_copy_on_extend); |
| 1857 cx_test_register(suite, test_buffer_put_string_copy_on_write); | 1472 cx_test_register(suite, test_buffer_put_string_copy_on_write); |
| 1858 cx_test_register(suite, test_buffer_terminate); | 1473 cx_test_register(suite, test_buffer_terminate); |
| 1859 cx_test_register(suite, test_buffer_write_size_overflow); | 1474 cx_test_register(suite, test_buffer_write_size_overflow); |
| 1860 cx_test_register(suite, test_buffer_write_capacity_overflow); | 1475 cx_test_register(suite, test_buffer_write_capacity_overflow); |
| 1476 cx_test_register(suite, test_buffer_write_maximum_capacity_exceeded); | |
| 1861 cx_test_register(suite, test_buffer_write_only_overwrite); | 1477 cx_test_register(suite, test_buffer_write_only_overwrite); |
| 1862 cx_test_register(suite, test_buffer_write_flush_at_capacity); | |
| 1863 cx_test_register(suite, test_buffer_write_flush_at_threshold); | |
| 1864 cx_test_register(suite, test_buffer_write_flush_at_threshold_target_full); | |
| 1865 cx_test_register(suite, test_buffer_write_flush_rate_limited_and_buffer_too_small); | |
| 1866 cx_test_register(suite, test_buffer_write_flush_multibyte); | |
| 1867 cx_test_register(suite, test_buffer_write_flush_misaligned); | |
| 1868 cx_test_register(suite, test_buffer_write_flush_target_full); | |
| 1869 cx_test_register(suite, test_buffer_write_flush_multibyte_target_full); | |
| 1870 cx_test_register(suite, test_buffer_write_large_data_flush_target_full); | |
| 1871 cx_test_register(suite, test_buffer_pop); | 1478 cx_test_register(suite, test_buffer_pop); |
| 1872 cx_test_register(suite, test_buffer_flush); | |
| 1873 cx_test_register(suite, test_buffer_get); | 1479 cx_test_register(suite, test_buffer_get); |
| 1874 cx_test_register(suite, test_buffer_get_eof); | 1480 cx_test_register(suite, test_buffer_get_eof); |
| 1875 cx_test_register(suite, test_buffer_read); | 1481 cx_test_register(suite, test_buffer_read); |
| 1876 cx_test_register(suite, test_buffer_read_oob); | 1482 cx_test_register(suite, test_buffer_read_oob); |
| 1877 cx_test_register(suite, test_buffer_read_oob_multibyte); | 1483 cx_test_register(suite, test_buffer_read_oob_multibyte); |