| 26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
27 */ |
| 28 |
28 |
| 29 #include "cx/test.h" |
29 #include "cx/test.h" |
| 30 #include "util_allocator.h" |
30 #include "util_allocator.h" |
| |
31 #include <errno.h> |
| 31 |
32 |
| 32 #include "cx/buffer.h" |
33 #include "cx/buffer.h" |
| |
34 |
| |
35 #ifdef _WIN32 |
| |
36 #include <Windows.h> |
| |
37 #include <sysinfoapi.h> |
| |
38 static unsigned long system_page_size() { |
| |
39 static unsigned long ps = 0; |
| |
40 if (ps == 0) { |
| |
41 SYSTEM_INFO sysinfo; |
| |
42 GetSystemInfo(&sysinfo); |
| |
43 ps = sysinfo.dwPageSize; |
| |
44 } |
| |
45 return ps; |
| |
46 } |
| |
47 #else |
| |
48 #include <unistd.h> |
| |
49 static unsigned long system_page_size() { |
| |
50 static unsigned long ps = 0; |
| |
51 if (ps == 0) { |
| |
52 long sc = sysconf(_SC_PAGESIZE); |
| |
53 if (sc < 0) { |
| |
54 // fallback for systems which do not report a value here |
| |
55 ps = 4096; // LCOV_EXCL_LINE |
| |
56 } else { |
| |
57 ps = (unsigned long) sc; |
| |
58 } |
| |
59 } |
| |
60 return ps; |
| |
61 } |
| |
62 #endif |
| 33 |
63 |
| 34 CX_TEST(test_buffer_init_wrap_space) { |
64 CX_TEST(test_buffer_init_wrap_space) { |
| 35 CxTestingAllocator talloc; |
65 CxTestingAllocator talloc; |
| 36 cx_testing_allocator_init(&talloc); |
66 cx_testing_allocator_init(&talloc); |
| 37 CxAllocator *alloc = &talloc.base; |
67 CxAllocator *alloc = &talloc.base; |
| 145 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
175 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 146 } |
176 } |
| 147 cx_testing_allocator_destroy(&talloc); |
177 cx_testing_allocator_destroy(&talloc); |
| 148 } |
178 } |
| 149 |
179 |
| |
180 CX_TEST(test_buffer_create_defaulted_allocator) { |
| |
181 CX_TEST_DO { |
| |
182 CxBuffer *buf; |
| |
183 buf = cxBufferCreate(NULL, 16, NULL, 0); |
| |
184 CX_TEST_ASSERT(buf != NULL); |
| |
185 CX_TEST_ASSERT(buf->flush == NULL); |
| |
186 CX_TEST_ASSERT(buf->space != NULL); |
| |
187 CX_TEST_ASSERT((buf->flags & CX_BUFFER_AUTO_EXTEND) == 0); |
| |
188 CX_TEST_ASSERT((buf->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS); |
| |
189 CX_TEST_ASSERT(buf->pos == 0); |
| |
190 CX_TEST_ASSERT(buf->size == 0); |
| |
191 CX_TEST_ASSERT(buf->capacity == 16); |
| |
192 CX_TEST_ASSERT(buf->allocator == cxDefaultAllocator); |
| |
193 cxBufferFree(buf); |
| |
194 } |
| |
195 } |
| |
196 |
| 150 CX_TEST(test_buffer_minimum_capacity_sufficient) { |
197 CX_TEST(test_buffer_minimum_capacity_sufficient) { |
| 151 CxTestingAllocator talloc; |
198 CxTestingAllocator talloc; |
| 152 cx_testing_allocator_init(&talloc); |
199 cx_testing_allocator_init(&talloc); |
| 153 CxAllocator *alloc = &talloc.base; |
200 CxAllocator *alloc = &talloc.base; |
| 154 CX_TEST_DO { |
201 CX_TEST_DO { |
| 179 buf.size = 8; |
226 buf.size = 8; |
| 180 cxBufferMinimumCapacity(&buf, 16); |
227 cxBufferMinimumCapacity(&buf, 16); |
| 181 CX_TEST_ASSERT(buf.capacity == 16); |
228 CX_TEST_ASSERT(buf.capacity == 16); |
| 182 CX_TEST_ASSERT(buf.size == 8); |
229 CX_TEST_ASSERT(buf.size == 8); |
| 183 CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); |
230 CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); |
| |
231 |
| |
232 // below page size, new capacity should be power of two |
| |
233 cxBufferMinimumCapacity(&buf, 200); |
| |
234 CX_TEST_ASSERT(buf.capacity == 256); |
| |
235 |
| |
236 // greater than page size, new capacity should be multiple of pagesize |
| |
237 cxBufferMinimumCapacity(&buf, system_page_size() + 200); |
| |
238 CX_TEST_ASSERT(buf.capacity == system_page_size()*2); |
| |
239 |
| 184 cxBufferDestroy(&buf); |
240 cxBufferDestroy(&buf); |
| 185 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
241 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 186 } |
242 } |
| 187 cx_testing_allocator_destroy(&talloc); |
243 cx_testing_allocator_destroy(&talloc); |
| 188 } |
244 } |
| 685 CX_TEST_ASSERT(buf.pos == 7); |
741 CX_TEST_ASSERT(buf.pos == 7); |
| 686 CX_TEST_ASSERT(buf.size == 7); |
742 CX_TEST_ASSERT(buf.size == 7); |
| 687 CX_TEST_ASSERT(memcmp(original, "test____XXXXXXXX", 16) == 0); |
743 CX_TEST_ASSERT(memcmp(original, "test____XXXXXXXX", 16) == 0); |
| 688 CX_TEST_ASSERT(memcmp(buf.space, "testest", 7) == 0); |
744 CX_TEST_ASSERT(memcmp(buf.space, "testest", 7) == 0); |
| 689 cxFree(buf.allocator, original); |
745 cxFree(buf.allocator, original); |
| |
746 TEST_BUFFER_SHIFT_TEARDOWN(buf); |
| |
747 } |
| |
748 } |
| |
749 |
| |
750 CX_TEST(test_buffer_shift_right_overflow) { |
| |
751 TEST_BUFFER_SHIFT_SETUP(buf); |
| |
752 CX_TEST_DO { |
| |
753 errno = 0; |
| |
754 int ret = cxBufferShiftRight(&buf, SIZE_MAX - 2); |
| |
755 CX_TEST_ASSERT(ret != 0); |
| |
756 CX_TEST_ASSERT(errno == EOVERFLOW); |
| 690 TEST_BUFFER_SHIFT_TEARDOWN(buf); |
757 TEST_BUFFER_SHIFT_TEARDOWN(buf); |
| 691 } |
758 } |
| 692 } |
759 } |
| 693 |
760 |
| 694 static size_t mock_write_limited_rate( |
761 static size_t mock_write_limited_rate( |
| 1501 char target[4]; |
1568 char target[4]; |
| 1502 size_t read = cxBufferRead(&target, 1, 4, &buf); |
1569 size_t read = cxBufferRead(&target, 1, 4, &buf); |
| 1503 CX_TEST_ASSERT(read == 4); |
1570 CX_TEST_ASSERT(read == 4); |
| 1504 CX_TEST_ASSERT(0 == memcmp(&target, "me d", 4)); |
1571 CX_TEST_ASSERT(0 == memcmp(&target, "me d", 4)); |
| 1505 CX_TEST_ASSERT(buf.pos == 6); |
1572 CX_TEST_ASSERT(buf.pos == 6); |
| |
1573 |
| |
1574 // overflow |
| |
1575 errno = 0; |
| |
1576 read = cxBufferRead(&target, 4, SIZE_MAX/2, &buf); |
| |
1577 CX_TEST_ASSERT(read == 0); |
| |
1578 CX_TEST_ASSERT(errno == EOVERFLOW); |
| 1506 } |
1579 } |
| 1507 cxBufferDestroy(&buf); |
1580 cxBufferDestroy(&buf); |
| 1508 } |
1581 } |
| 1509 |
1582 |
| 1510 CX_TEST(test_buffer_read_oob) { |
1583 CX_TEST(test_buffer_read_oob) { |
| 1563 cx_test_register(suite, test_buffer_init_wrap_space); |
1636 cx_test_register(suite, test_buffer_init_wrap_space); |
| 1564 cx_test_register(suite, test_buffer_init_wrap_space_auto_extend); |
1637 cx_test_register(suite, test_buffer_init_wrap_space_auto_extend); |
| 1565 cx_test_register(suite, test_buffer_init_wrap_space_auto_free); |
1638 cx_test_register(suite, test_buffer_init_wrap_space_auto_free); |
| 1566 cx_test_register(suite, test_buffer_init_fresh_space); |
1639 cx_test_register(suite, test_buffer_init_fresh_space); |
| 1567 cx_test_register(suite, test_buffer_init_on_heap); |
1640 cx_test_register(suite, test_buffer_init_on_heap); |
| |
1641 cx_test_register(suite, test_buffer_create_defaulted_allocator); |
| 1568 cx_test_register(suite, test_buffer_minimum_capacity_sufficient); |
1642 cx_test_register(suite, test_buffer_minimum_capacity_sufficient); |
| 1569 cx_test_register(suite, test_buffer_minimum_capacity_extend); |
1643 cx_test_register(suite, test_buffer_minimum_capacity_extend); |
| 1570 cx_test_register(suite, test_buffer_shrink); |
1644 cx_test_register(suite, test_buffer_shrink); |
| 1571 cx_test_register(suite, test_buffer_shrink_copy_on_write); |
1645 cx_test_register(suite, test_buffer_shrink_copy_on_write); |
| 1572 cx_test_register(suite, test_buffer_shrink_copy_on_extend); |
1646 cx_test_register(suite, test_buffer_shrink_copy_on_extend); |
| 1599 cx_test_register(suite, test_buffer_shift_right_standard); |
1673 cx_test_register(suite, test_buffer_shift_right_standard); |
| 1600 cx_test_register(suite, test_buffer_shift_right_overshift_discard); |
1674 cx_test_register(suite, test_buffer_shift_right_overshift_discard); |
| 1601 cx_test_register(suite, test_buffer_shift_right_overshift_extend); |
1675 cx_test_register(suite, test_buffer_shift_right_overshift_extend); |
| 1602 cx_test_register(suite, test_buffer_shift_right_offset_interface); |
1676 cx_test_register(suite, test_buffer_shift_right_offset_interface); |
| 1603 cx_test_register(suite, test_buffer_shift_right_copy_on_write); |
1677 cx_test_register(suite, test_buffer_shift_right_copy_on_write); |
| |
1678 cx_test_register(suite, test_buffer_shift_right_overflow); |
| 1604 cx_test_register(suite, test_buffer_write_size_one_fit); |
1679 cx_test_register(suite, test_buffer_write_size_one_fit); |
| 1605 cx_test_register(suite, test_buffer_write_size_one_discard); |
1680 cx_test_register(suite, test_buffer_write_size_one_discard); |
| 1606 cx_test_register(suite, test_buffer_write_size_one_extend); |
1681 cx_test_register(suite, test_buffer_write_size_one_extend); |
| 1607 cx_test_register(suite, test_buffer_write_multibyte_fit); |
1682 cx_test_register(suite, test_buffer_write_multibyte_fit); |
| 1608 cx_test_register(suite, test_buffer_write_multibyte_discard); |
1683 cx_test_register(suite, test_buffer_write_multibyte_discard); |