--- a/test/buffer_tests.c Wed May 09 15:04:15 2018 +0200 +++ b/test/buffer_tests.c Wed May 09 20:15:10 2018 +0200 @@ -625,3 +625,114 @@ ucx_buffer_free(b); } + +UCX_TEST(test_ucx_buffer_shl) { + + const char* hw = "Shift the World!"; + + UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT); + ucx_buffer_puts(b, hw); + b->pos = 5; + + UCX_TEST_BEGIN + char* expected; + + ucx_buffer_shift_left(b, 2); + expected = "ift the World!"; + + UCX_TEST_ASSERT(b->pos == 3, "position after normal shl wrong"); + UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shl wrong"); + UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), + "contents after normal shl wrong"); + + + ucx_buffer_shift_left(b, 5); + expected = "he World!"; + + UCX_TEST_ASSERT(b->pos == 0, "position after overshift left wrong"); + UCX_TEST_ASSERT(b->size == strlen(expected), + "size after overshift left wrong"); + UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), + "contents after overshift left wrong"); + + ucx_buffer_shift_left(b, 10); + UCX_TEST_ASSERT(b->pos == 0, "position after 'shl everything away' wrong"); + UCX_TEST_ASSERT(b->size == 0, "size after 'shl everything away' wrong"); + + UCX_TEST_END + + ucx_buffer_free(b); +} + +UCX_TEST(test_ucx_buffer_shr) { + + const char* hw = "Shift the World!"; + + UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT); + ucx_buffer_puts(b, hw); + b->pos = 12; + + UCX_TEST_BEGIN + char* expected; + + ucx_buffer_shift_right(b, 2); + expected = "ShShift the World!"; + + UCX_TEST_ASSERT(b->pos == 14, "position after normal shr wrong"); + UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shr wrong"); + UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), + "contents after normal shr wrong"); + + + ucx_buffer_shift_right(b, 5); + expected = "ShShiShShift the Wor"; + UCX_TEST_ASSERT(strlen(expected) == b->capacity, + "Test data is wrong, please fix the test."); + + UCX_TEST_ASSERT(b->pos == 19, + "position after overshift right w/o auto-extend wrong"); + UCX_TEST_ASSERT(b->size == 20, + "size after overshift right w/o auto-extend wrong"); + UCX_TEST_ASSERT(b->capacity == 20, + "capacity after overshift right w/o auto-extend wrong"); + UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), + "contents after overshift right w/o auto-extend wrong"); + + ucx_buffer_shift_right(b, 15); + UCX_TEST_ASSERT(b->pos == b->capacity, "position after 'shr to eof' wrong"); + UCX_TEST_ASSERT(b->size == b->capacity, "size after 'shr to eof' wrong"); + UCX_TEST_ASSERT(ucx_buffer_eof(b), "buffer not eof after 'shr to eof'"); + + UCX_TEST_END + + ucx_buffer_free(b); +} + +UCX_TEST(test_ucx_buffer_shr_ax) { + + const char* hw = "Shift the World!"; + + UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_AUTOEXTEND); + ucx_buffer_puts(b, hw); + b->pos = 12; + + UCX_TEST_BEGIN + + const char* expected = "Shift the Shift the World!"; + + ucx_buffer_shift_right(b, 10); + UCX_TEST_ASSERT(b->pos == 22, "position after shr w/ auto-extend wrong"); + UCX_TEST_ASSERT(b->size == strlen(expected), + "size after shr w/ auto-extend wrong"); + UCX_TEST_ASSERT(b->capacity >= b->size, + "auto-extension of capacity after shr w/ auto-extend failed"); + UCX_TEST_ASSERT(!ucx_buffer_eof(b), + "buffer should not be eof after shr w/ auto-extend"); + UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size), + "contents wrong after shr w/ auto-extend"); + + UCX_TEST_END + + ucx_buffer_free(b); +} +