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
--- 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 */
--- 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);
 }
+
--- 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
--- 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__)
-
 /**
  * <code>va_list</code> 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 <code>printf()</code> like function which writes the output to an
  * UcxBuffer.

mercurial