fixes ucx_sprintf macro and adds tests for sprintf and bprintf

Wed, 07 Sep 2016 12:41:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 07 Sep 2016 12:41:30 +0200
changeset 223
e18884bbad48
parent 222
e0f850709a5c
child 224
f9ba63fc6a80

fixes ucx_sprintf macro and adds tests for sprintf and bprintf

test/main.c file | annotate | diff | comparison | revisions
test/utils_tests.c file | annotate | diff | comparison | revisions
test/utils_tests.h file | annotate | diff | comparison | revisions
ucx/utils.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/main.c	Wed Sep 07 12:26:01 2016 +0200
     1.2 +++ b/test/main.c	Wed Sep 07 12:41:30 2016 +0200
     1.3 @@ -219,6 +219,8 @@
     1.4          /* Utils Tests*/
     1.5          ucx_test_register(suite, test_ucx_fprintf);
     1.6          ucx_test_register(suite, test_ucx_asprintf);
     1.7 +        ucx_test_register(suite, test_ucx_sprintf);
     1.8 +        ucx_test_register(suite, test_ucx_bprintf);
     1.9          ucx_test_register(suite, test_ucx_stream_copy);
    1.10          
    1.11          /* AVL Tests */
     2.1 --- a/test/utils_tests.c	Wed Sep 07 12:26:01 2016 +0200
     2.2 +++ b/test/utils_tests.c	Wed Sep 07 12:41:30 2016 +0200
     2.3 @@ -91,6 +91,47 @@
     2.4      free(teststr2);
     2.5  }
     2.6  
     2.7 +UCX_TEST(test_ucx_sprintf) {
     2.8 +    UCX_TEST_BEGIN
     2.9 +    
    2.10 +    sstr_t s1 = ucx_sprintf("int: %d\nHello %s!", 123, "World");
    2.11 +    UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL");
    2.12 +    UCX_TEST_ASSERT(s1.length == 21, "wrong length");
    2.13 +    UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content");
    2.14 +    free(s1.ptr);
    2.15 +    
    2.16 +    sstr_t s2 = ucx_sprintf("Nothing to format!");
    2.17 +    UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL");
    2.18 +    UCX_TEST_ASSERT(s2.length == 18, "wrong length");
    2.19 +    UCX_TEST_ASSERT(!memcmp(s2.ptr, "Nothing to format!", 18),
    2.20 +            "wrong string without format arguments");
    2.21 +    free(s2.ptr);
    2.22 +    
    2.23 +    UCX_TEST_END
    2.24 +}
    2.25 +
    2.26 +UCX_TEST(test_ucx_bprintf) {
    2.27 +    UcxBuffer *b = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
    2.28 +    
    2.29 +    UCX_TEST_BEGIN
    2.30 +    
    2.31 +    ucx_bprintf(b, "int: %d\nHello %s!", 123, "World");
    2.32 +    UCX_TEST_ASSERT(b->size == 21, "wrong length");
    2.33 +    UCX_TEST_ASSERT(!memcmp(b->space, "int: 123\nHello World!", 21),
    2.34 +            "wrong content");
    2.35 +            
    2.36 +    ucx_buffer_clear(b);
    2.37 +            
    2.38 +    ucx_bprintf(b, "Nothing to format!");
    2.39 +    UCX_TEST_ASSERT(b->size == 18, "wrong length");
    2.40 +    UCX_TEST_ASSERT(!memcmp(b->space, "Nothing to format!", 18),
    2.41 +            "wrong string without format arguments");
    2.42 +            
    2.43 +    UCX_TEST_END
    2.44 +    
    2.45 +    ucx_buffer_free(b);
    2.46 +}
    2.47 +
    2.48  UCX_TEST(test_ucx_stream_copy) {
    2.49      UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
    2.50      UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
    2.51 @@ -143,3 +184,4 @@
    2.52      ucx_buffer_free(b1);
    2.53      ucx_buffer_free(b2);
    2.54  }
    2.55 +
     3.1 --- a/test/utils_tests.h	Wed Sep 07 12:26:01 2016 +0200
     3.2 +++ b/test/utils_tests.h	Wed Sep 07 12:41:30 2016 +0200
     3.3 @@ -38,6 +38,8 @@
     3.4  
     3.5  UCX_TEST(test_ucx_fprintf);
     3.6  UCX_TEST(test_ucx_asprintf);
     3.7 +UCX_TEST(test_ucx_sprintf);
     3.8 +UCX_TEST(test_ucx_bprintf);
     3.9  UCX_TEST(test_ucx_stream_copy);
    3.10  
    3.11  #ifdef	__cplusplus
     4.1 --- a/ucx/utils.h	Wed Sep 07 12:26:01 2016 +0200
     4.2 +++ b/ucx/utils.h	Wed Sep 07 12:41:30 2016 +0200
     4.3 @@ -243,10 +243,6 @@
     4.4   */
     4.5  sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...);
     4.6  
     4.7 -/** Shortcut for ucx_asprintf() with default allocator. */
     4.8 -#define ucx_sprintf(fmt, ...) \
     4.9 -    ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__)
    4.10 -
    4.11  /**
    4.12   * <code>va_list</code> version of ucx_asprintf().
    4.13   * 
    4.14 @@ -258,6 +254,10 @@
    4.15   */
    4.16  sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap);
    4.17  
    4.18 +/** Shortcut for ucx_asprintf() with default allocator. */
    4.19 +#define ucx_sprintf(...) \
    4.20 +    ucx_asprintf(ucx_default_allocator(), __VA_ARGS__)
    4.21 +
    4.22  /**
    4.23   * A <code>printf()</code> like function which writes the output to an
    4.24   * UcxBuffer.

mercurial