add constant for reading out printf sbo size - relates to #343

Sun, 14 Jan 2024 13:13:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Jan 2024 13:13:12 +0100
changeset 805
26500fc24058
parent 804
5136f2fc32ec
child 806
e06249e09f99

add constant for reading out printf sbo size - relates to #343

src/cx/printf.h file | annotate | diff | comparison | revisions
src/printf.c file | annotate | diff | comparison | revisions
tests/test_printf.c file | annotate | diff | comparison | revisions
--- a/src/cx/printf.h	Sat Jan 13 17:51:42 2024 +0100
+++ b/src/cx/printf.h	Sun Jan 14 13:13:12 2024 +0100
@@ -44,6 +44,12 @@
 extern "C" {
 #endif
 
+
+/**
+ * The maximum string length that fits into stack memory.
+ */
+extern unsigned const cx_printf_sbo_size;
+
 /**
  * A \c fprintf like function which writes the output to a stream by
  * using a write_func.
--- a/src/printf.c	Sat Jan 13 17:51:42 2024 +0100
+++ b/src/printf.c	Sun Jan 14 13:13:12 2024 +0100
@@ -34,6 +34,7 @@
 #ifndef CX_PRINTF_SBO_SIZE
 #define CX_PRINTF_SBO_SIZE 512
 #endif
+unsigned const cx_printf_sbo_size = CX_PRINTF_SBO_SIZE;
 
 int cx_fprintf(
         void *stream,
@@ -85,9 +86,8 @@
         ...
 ) {
     va_list ap;
-    cxmutstr ret;
     va_start(ap, fmt);
-    ret = cx_vasprintf_a(allocator, fmt, ap);
+    cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap);
     va_end(ap);
     return ret;
 }
--- a/tests/test_printf.c	Sat Jan 13 17:51:42 2024 +0100
+++ b/tests/test_printf.c	Sun Jan 14 13:13:12 2024 +0100
@@ -64,23 +64,25 @@
 }
 
 CX_TEST(test_bprintf_large_string) {
+    unsigned len = cx_printf_sbo_size;
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
     CxAllocator *alloc = &talloc.base;
-    char *aaa = malloc(512);
-    char *bbb = malloc(512);
-    char *expected = malloc(1040);
-    memset(aaa, 'a', 511);
-    aaa[511] = 0;
-    memset(bbb, 'b', 511);
-    bbb[511] = 0;
+    char *aaa = malloc(len);
+    char *bbb = malloc(len);
+    char *expected = malloc(2*len+16);
+    memset(aaa, 'a', len-1);
+    aaa[len-1] = 0;
+    memset(bbb, 'b', len-1);
+    bbb[len-1] = 0;
     sprintf(expected, "After %s comes %s.", aaa, bbb);
     CX_TEST_DO {
         CxBuffer buf;
         cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND);
         size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb);
-        CX_TEST_ASSERT(r == 1036);
-        CX_TEST_ASSERT(buf.size == 1036);
+        size_t er = 2*len-2+14;
+        CX_TEST_ASSERT(r == er);
+        CX_TEST_ASSERT(buf.size == er);
         cxBufferPut(&buf, 0);
         CX_TEST_ASSERT(0 == strcmp(expected, buf.space));
         cxBufferDestroy(&buf);
@@ -271,17 +273,18 @@
 }
 
 CX_TEST(test_asprintf_large_string) {
-    char *aaa = malloc(512);
-    char *bbb = malloc(512);
-    char *expected = malloc(1040);
-    memset(aaa, 'a', 511);
-    aaa[511] = 0;
-    memset(bbb, 'b', 511);
-    bbb[511] = 0;
+    unsigned len = cx_printf_sbo_size;
+    char *aaa = malloc(len);
+    char *bbb = malloc(len);
+    char *expected = malloc(2*len+16);
+    memset(aaa, 'a', len-1);
+    aaa[len-1] = 0;
+    memset(bbb, 'b', len-1);
+    bbb[len-1] = 0;
     sprintf(expected, "After %s comes %s.", aaa, bbb);
     CX_TEST_DO {
         cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb);
-        CX_TEST_ASSERT(r.length == 1036);
+        CX_TEST_ASSERT(r.length == 2*len-2+14);
         ASSERT_ZERO_TERMINATED(r);
         CX_TEST_ASSERT(0 == strcmp(r.ptr, expected));
         cx_strfree(&r);

mercurial