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
     1.1 --- a/src/cx/printf.h	Sat Jan 13 17:51:42 2024 +0100
     1.2 +++ b/src/cx/printf.h	Sun Jan 14 13:13:12 2024 +0100
     1.3 @@ -44,6 +44,12 @@
     1.4  extern "C" {
     1.5  #endif
     1.6  
     1.7 +
     1.8 +/**
     1.9 + * The maximum string length that fits into stack memory.
    1.10 + */
    1.11 +extern unsigned const cx_printf_sbo_size;
    1.12 +
    1.13  /**
    1.14   * A \c fprintf like function which writes the output to a stream by
    1.15   * using a write_func.
     2.1 --- a/src/printf.c	Sat Jan 13 17:51:42 2024 +0100
     2.2 +++ b/src/printf.c	Sun Jan 14 13:13:12 2024 +0100
     2.3 @@ -34,6 +34,7 @@
     2.4  #ifndef CX_PRINTF_SBO_SIZE
     2.5  #define CX_PRINTF_SBO_SIZE 512
     2.6  #endif
     2.7 +unsigned const cx_printf_sbo_size = CX_PRINTF_SBO_SIZE;
     2.8  
     2.9  int cx_fprintf(
    2.10          void *stream,
    2.11 @@ -85,9 +86,8 @@
    2.12          ...
    2.13  ) {
    2.14      va_list ap;
    2.15 -    cxmutstr ret;
    2.16      va_start(ap, fmt);
    2.17 -    ret = cx_vasprintf_a(allocator, fmt, ap);
    2.18 +    cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap);
    2.19      va_end(ap);
    2.20      return ret;
    2.21  }
     3.1 --- a/tests/test_printf.c	Sat Jan 13 17:51:42 2024 +0100
     3.2 +++ b/tests/test_printf.c	Sun Jan 14 13:13:12 2024 +0100
     3.3 @@ -64,23 +64,25 @@
     3.4  }
     3.5  
     3.6  CX_TEST(test_bprintf_large_string) {
     3.7 +    unsigned len = cx_printf_sbo_size;
     3.8      CxTestingAllocator talloc;
     3.9      cx_testing_allocator_init(&talloc);
    3.10      CxAllocator *alloc = &talloc.base;
    3.11 -    char *aaa = malloc(512);
    3.12 -    char *bbb = malloc(512);
    3.13 -    char *expected = malloc(1040);
    3.14 -    memset(aaa, 'a', 511);
    3.15 -    aaa[511] = 0;
    3.16 -    memset(bbb, 'b', 511);
    3.17 -    bbb[511] = 0;
    3.18 +    char *aaa = malloc(len);
    3.19 +    char *bbb = malloc(len);
    3.20 +    char *expected = malloc(2*len+16);
    3.21 +    memset(aaa, 'a', len-1);
    3.22 +    aaa[len-1] = 0;
    3.23 +    memset(bbb, 'b', len-1);
    3.24 +    bbb[len-1] = 0;
    3.25      sprintf(expected, "After %s comes %s.", aaa, bbb);
    3.26      CX_TEST_DO {
    3.27          CxBuffer buf;
    3.28          cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND);
    3.29          size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb);
    3.30 -        CX_TEST_ASSERT(r == 1036);
    3.31 -        CX_TEST_ASSERT(buf.size == 1036);
    3.32 +        size_t er = 2*len-2+14;
    3.33 +        CX_TEST_ASSERT(r == er);
    3.34 +        CX_TEST_ASSERT(buf.size == er);
    3.35          cxBufferPut(&buf, 0);
    3.36          CX_TEST_ASSERT(0 == strcmp(expected, buf.space));
    3.37          cxBufferDestroy(&buf);
    3.38 @@ -271,17 +273,18 @@
    3.39  }
    3.40  
    3.41  CX_TEST(test_asprintf_large_string) {
    3.42 -    char *aaa = malloc(512);
    3.43 -    char *bbb = malloc(512);
    3.44 -    char *expected = malloc(1040);
    3.45 -    memset(aaa, 'a', 511);
    3.46 -    aaa[511] = 0;
    3.47 -    memset(bbb, 'b', 511);
    3.48 -    bbb[511] = 0;
    3.49 +    unsigned len = cx_printf_sbo_size;
    3.50 +    char *aaa = malloc(len);
    3.51 +    char *bbb = malloc(len);
    3.52 +    char *expected = malloc(2*len+16);
    3.53 +    memset(aaa, 'a', len-1);
    3.54 +    aaa[len-1] = 0;
    3.55 +    memset(bbb, 'b', len-1);
    3.56 +    bbb[len-1] = 0;
    3.57      sprintf(expected, "After %s comes %s.", aaa, bbb);
    3.58      CX_TEST_DO {
    3.59          cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb);
    3.60 -        CX_TEST_ASSERT(r.length == 1036);
    3.61 +        CX_TEST_ASSERT(r.length == 2*len-2+14);
    3.62          ASSERT_ZERO_TERMINATED(r);
    3.63          CX_TEST_ASSERT(0 == strcmp(r.ptr, expected));
    3.64          cx_strfree(&r);

mercurial