# HG changeset patch # User Mike Becker # Date 1473244890 -7200 # Node ID e18884bbad481d7cf14c9bffa70c3f5ac641400c # Parent e0f850709a5cc9b799ac080ae07f3f8fe13f98aa fixes ucx_sprintf macro and adds tests for sprintf and bprintf diff -r e0f850709a5c -r e18884bbad48 test/main.c --- a/test/main.c Wed Sep 07 12:26:01 2016 +0200 +++ b/test/main.c Wed Sep 07 12:41:30 2016 +0200 @@ -219,6 +219,8 @@ /* Utils Tests*/ ucx_test_register(suite, test_ucx_fprintf); ucx_test_register(suite, test_ucx_asprintf); + ucx_test_register(suite, test_ucx_sprintf); + ucx_test_register(suite, test_ucx_bprintf); ucx_test_register(suite, test_ucx_stream_copy); /* AVL Tests */ diff -r e0f850709a5c -r e18884bbad48 test/utils_tests.c --- a/test/utils_tests.c Wed Sep 07 12:26:01 2016 +0200 +++ b/test/utils_tests.c Wed Sep 07 12:41:30 2016 +0200 @@ -91,6 +91,47 @@ free(teststr2); } +UCX_TEST(test_ucx_sprintf) { + UCX_TEST_BEGIN + + sstr_t s1 = ucx_sprintf("int: %d\nHello %s!", 123, "World"); + UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL"); + UCX_TEST_ASSERT(s1.length == 21, "wrong length"); + UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content"); + free(s1.ptr); + + sstr_t s2 = ucx_sprintf("Nothing to format!"); + UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL"); + UCX_TEST_ASSERT(s2.length == 18, "wrong length"); + UCX_TEST_ASSERT(!memcmp(s2.ptr, "Nothing to format!", 18), + "wrong string without format arguments"); + free(s2.ptr); + + UCX_TEST_END +} + +UCX_TEST(test_ucx_bprintf) { + UcxBuffer *b = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); + + UCX_TEST_BEGIN + + ucx_bprintf(b, "int: %d\nHello %s!", 123, "World"); + UCX_TEST_ASSERT(b->size == 21, "wrong length"); + UCX_TEST_ASSERT(!memcmp(b->space, "int: 123\nHello World!", 21), + "wrong content"); + + ucx_buffer_clear(b); + + ucx_bprintf(b, "Nothing to format!"); + UCX_TEST_ASSERT(b->size == 18, "wrong length"); + UCX_TEST_ASSERT(!memcmp(b->space, "Nothing to format!", 18), + "wrong string without format arguments"); + + UCX_TEST_END + + ucx_buffer_free(b); +} + UCX_TEST(test_ucx_stream_copy) { UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); @@ -143,3 +184,4 @@ ucx_buffer_free(b1); ucx_buffer_free(b2); } + diff -r e0f850709a5c -r e18884bbad48 test/utils_tests.h --- a/test/utils_tests.h Wed Sep 07 12:26:01 2016 +0200 +++ b/test/utils_tests.h Wed Sep 07 12:41:30 2016 +0200 @@ -38,6 +38,8 @@ UCX_TEST(test_ucx_fprintf); UCX_TEST(test_ucx_asprintf); +UCX_TEST(test_ucx_sprintf); +UCX_TEST(test_ucx_bprintf); UCX_TEST(test_ucx_stream_copy); #ifdef __cplusplus diff -r e0f850709a5c -r e18884bbad48 ucx/utils.h --- a/ucx/utils.h Wed Sep 07 12:26:01 2016 +0200 +++ b/ucx/utils.h Wed Sep 07 12:41:30 2016 +0200 @@ -243,10 +243,6 @@ */ sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); -/** Shortcut for ucx_asprintf() with default allocator. */ -#define ucx_sprintf(fmt, ...) \ - ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__) - /** * va_list version of ucx_asprintf(). * @@ -258,6 +254,10 @@ */ sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); +/** Shortcut for ucx_asprintf() with default allocator. */ +#define ucx_sprintf(...) \ + ucx_asprintf(ucx_default_allocator(), __VA_ARGS__) + /** * A printf() like function which writes the output to an * UcxBuffer.