tests/test_printf.c

changeset 805
26500fc24058
parent 780
9965df621652
child 810
85859399a0cc
equal deleted inserted replaced
804:5136f2fc32ec 805:26500fc24058
62 } 62 }
63 cx_testing_allocator_destroy(&talloc); 63 cx_testing_allocator_destroy(&talloc);
64 } 64 }
65 65
66 CX_TEST(test_bprintf_large_string) { 66 CX_TEST(test_bprintf_large_string) {
67 unsigned len = cx_printf_sbo_size;
67 CxTestingAllocator talloc; 68 CxTestingAllocator talloc;
68 cx_testing_allocator_init(&talloc); 69 cx_testing_allocator_init(&talloc);
69 CxAllocator *alloc = &talloc.base; 70 CxAllocator *alloc = &talloc.base;
70 char *aaa = malloc(512); 71 char *aaa = malloc(len);
71 char *bbb = malloc(512); 72 char *bbb = malloc(len);
72 char *expected = malloc(1040); 73 char *expected = malloc(2*len+16);
73 memset(aaa, 'a', 511); 74 memset(aaa, 'a', len-1);
74 aaa[511] = 0; 75 aaa[len-1] = 0;
75 memset(bbb, 'b', 511); 76 memset(bbb, 'b', len-1);
76 bbb[511] = 0; 77 bbb[len-1] = 0;
77 sprintf(expected, "After %s comes %s.", aaa, bbb); 78 sprintf(expected, "After %s comes %s.", aaa, bbb);
78 CX_TEST_DO { 79 CX_TEST_DO {
79 CxBuffer buf; 80 CxBuffer buf;
80 cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND); 81 cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND);
81 size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb); 82 size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb);
82 CX_TEST_ASSERT(r == 1036); 83 size_t er = 2*len-2+14;
83 CX_TEST_ASSERT(buf.size == 1036); 84 CX_TEST_ASSERT(r == er);
85 CX_TEST_ASSERT(buf.size == er);
84 cxBufferPut(&buf, 0); 86 cxBufferPut(&buf, 0);
85 CX_TEST_ASSERT(0 == strcmp(expected, buf.space)); 87 CX_TEST_ASSERT(0 == strcmp(expected, buf.space));
86 cxBufferDestroy(&buf); 88 cxBufferDestroy(&buf);
87 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 89 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
88 } 90 }
269 } 271 }
270 cx_testing_allocator_destroy(&talloc); 272 cx_testing_allocator_destroy(&talloc);
271 } 273 }
272 274
273 CX_TEST(test_asprintf_large_string) { 275 CX_TEST(test_asprintf_large_string) {
274 char *aaa = malloc(512); 276 unsigned len = cx_printf_sbo_size;
275 char *bbb = malloc(512); 277 char *aaa = malloc(len);
276 char *expected = malloc(1040); 278 char *bbb = malloc(len);
277 memset(aaa, 'a', 511); 279 char *expected = malloc(2*len+16);
278 aaa[511] = 0; 280 memset(aaa, 'a', len-1);
279 memset(bbb, 'b', 511); 281 aaa[len-1] = 0;
280 bbb[511] = 0; 282 memset(bbb, 'b', len-1);
283 bbb[len-1] = 0;
281 sprintf(expected, "After %s comes %s.", aaa, bbb); 284 sprintf(expected, "After %s comes %s.", aaa, bbb);
282 CX_TEST_DO { 285 CX_TEST_DO {
283 cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb); 286 cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb);
284 CX_TEST_ASSERT(r.length == 1036); 287 CX_TEST_ASSERT(r.length == 2*len-2+14);
285 ASSERT_ZERO_TERMINATED(r); 288 ASSERT_ZERO_TERMINATED(r);
286 CX_TEST_ASSERT(0 == strcmp(r.ptr, expected)); 289 CX_TEST_ASSERT(0 == strcmp(r.ptr, expected));
287 cx_strfree(&r); 290 cx_strfree(&r);
288 } 291 }
289 free(aaa); 292 free(aaa);

mercurial