Tue, 06 May 2014 10:56:54 +0200
ucx_buffer_extract tests
test/buffer_tests.c | file | annotate | diff | comparison | revisions | |
test/buffer_tests.h | file | annotate | diff | comparison | revisions | |
test/main.c | file | annotate | diff | comparison | revisions |
1.1 --- a/test/buffer_tests.c Mon May 05 15:56:39 2014 +0200 1.2 +++ b/test/buffer_tests.c Tue May 06 10:56:54 2014 +0200 1.3 @@ -503,25 +503,57 @@ 1.4 1.5 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); 1.6 src->size = 15; 1.7 - UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); 1.8 + UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND); 1.9 1.10 UCX_TEST_BEGIN 1.11 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); 1.12 - 1.13 - UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE, 1.14 + UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE), 1.15 "autofree flag shall be enforced"); 1.16 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); 1.17 + UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer"); 1.18 + UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer"); 1.19 char rb[5]; 1.20 ucx_buffer_read(rb, 1, 5, dst); 1.21 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, 1.22 "new buffer has incorrect content"); 1.23 1.24 - UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL, 1.25 - "extract shall fail on invalid bounds"); 1.26 + UCX_TEST_END 1.27 + 1.28 + ucx_buffer_free(dst); 1.29 + ucx_buffer_free(src); 1.30 +} 1.31 + 1.32 +UCX_TEST(test_ucx_buffer_extract_oob) { 1.33 + char *buffer = (char*) malloc(16); 1.34 + strcpy(buffer, "this is a test!"); 1.35 + 1.36 + UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); 1.37 + UCX_TEST_BEGIN 1.38 + 1.39 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL, 1.40 + "extract shall fail on zero length"); 1.41 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL, 1.42 + "extract shall fail on invalid bounds (size exceeds limits)"); 1.43 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL, 1.44 + "extract shall fail on invalid bounds (start exceeds limits)"); 1.45 1.46 UCX_TEST_END 1.47 1.48 - ucx_buffer_free(dst); 1.49 + ucx_buffer_free(src); 1.50 +} 1.51 + 1.52 +UCX_TEST(test_ucx_buffer_extract_overflow) { 1.53 + char *buffer = (char*) malloc(16); 1.54 + strcpy(buffer, "this is a test!"); 1.55 + 1.56 + UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); 1.57 + UCX_TEST_BEGIN 1.58 + 1.59 + UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4, 1.60 + UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow"); 1.61 + 1.62 + UCX_TEST_END 1.63 + 1.64 ucx_buffer_free(src); 1.65 } 1.66
2.1 --- a/test/buffer_tests.h Mon May 05 15:56:39 2014 +0200 2.2 +++ b/test/buffer_tests.h Tue May 06 10:56:54 2014 +0200 2.3 @@ -53,11 +53,12 @@ 2.4 UCX_TEST(test_ucx_buffer_getc); 2.5 UCX_TEST(test_ucx_buffer_read); 2.6 UCX_TEST(test_ucx_buffer_read_oob); 2.7 +UCX_TEST(test_ucx_buffer_extract); 2.8 +UCX_TEST(test_ucx_buffer_extract_oob); 2.9 +UCX_TEST(test_ucx_buffer_extract_overflow); 2.10 2.11 UCX_TEST(test_ucx_buffer_write); 2.12 UCX_TEST(test_ucx_buffer_write_ax); 2.13 - 2.14 -UCX_TEST(test_ucx_buffer_extract); 2.15 UCX_TEST(test_ucx_stream_copy); 2.16 2.17 #ifdef __cplusplus
3.1 --- a/test/main.c Mon May 05 15:56:39 2014 +0200 3.2 +++ b/test/main.c Tue May 06 10:56:54 2014 +0200 3.3 @@ -194,9 +194,11 @@ 3.4 ucx_test_register(suite, test_ucx_buffer_getc); 3.5 ucx_test_register(suite, test_ucx_buffer_read); 3.6 ucx_test_register(suite, test_ucx_buffer_read_oob); 3.7 + ucx_test_register(suite, test_ucx_buffer_extract); 3.8 + ucx_test_register(suite, test_ucx_buffer_extract_oob); 3.9 + ucx_test_register(suite, test_ucx_buffer_extract_overflow); 3.10 ucx_test_register(suite, test_ucx_buffer_write); 3.11 ucx_test_register(suite, test_ucx_buffer_write_ax); 3.12 - ucx_test_register(suite, test_ucx_buffer_extract); 3.13 ucx_test_register(suite, test_ucx_stream_copy); 3.14 3.15 /* Utils Tests*/