292 free(aaa); |
292 free(aaa); |
293 free(bbb); |
293 free(bbb); |
294 free(expected); |
294 free(expected); |
295 } |
295 } |
296 |
296 |
|
297 CX_TEST(test_sprintf_no_realloc) { |
|
298 char *buf = malloc(16); |
|
299 CxTestingAllocator talloc; |
|
300 cx_testing_allocator_init(&talloc); |
|
301 CxAllocator *alloc = &talloc.base; |
|
302 CX_TEST_DO { |
|
303 char *oldbuf = buf; |
|
304 size_t len = cx_sprintf_a(alloc, &buf, 16, "Test %d %s", 47, "string"); |
|
305 CX_TEST_ASSERT(oldbuf == buf); |
|
306 CX_TEST_ASSERT(len == 14); |
|
307 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15)); |
|
308 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
309 } |
|
310 cx_testing_allocator_destroy(&talloc); |
|
311 free(buf); |
|
312 } |
|
313 |
|
314 CX_TEST(test_sprintf_realloc) { |
|
315 CxTestingAllocator talloc; |
|
316 cx_testing_allocator_init(&talloc); |
|
317 CxAllocator *alloc = &talloc.base; |
|
318 char *buf = cxMalloc(alloc, 8); |
|
319 CX_TEST_DO { |
|
320 size_t len = cx_sprintf_a(alloc, &buf, 8, "Test %d %s", 47, "foobar"); |
|
321 CX_TEST_ASSERT(len == 14); |
|
322 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 foobar", 15)); |
|
323 cxFree(alloc, buf); |
|
324 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
325 } |
|
326 cx_testing_allocator_destroy(&talloc); |
|
327 } |
|
328 |
|
329 CX_TEST(test_sprintf_realloc_to_fit_terminator) { |
|
330 CxTestingAllocator talloc; |
|
331 cx_testing_allocator_init(&talloc); |
|
332 CxAllocator *alloc = &talloc.base; |
|
333 // make it so that only the zero-terminator does not fit |
|
334 char *buf = cxMalloc(alloc, 14); |
|
335 CX_TEST_DO { |
|
336 size_t len = cx_sprintf_a(alloc, &buf, 14, "Test %d %s", 13, "string"); |
|
337 CX_TEST_ASSERT(len == 14); |
|
338 CX_TEST_ASSERT(0 == memcmp(buf, "Test 13 string", 15)); |
|
339 cxFree(alloc, buf); |
|
340 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
341 } |
|
342 cx_testing_allocator_destroy(&talloc); |
|
343 } |
|
344 |
|
345 CX_TEST(test_sprintf_s_no_alloc) { |
|
346 char buf[16]; |
|
347 CxTestingAllocator talloc; |
|
348 cx_testing_allocator_init(&talloc); |
|
349 CxAllocator *alloc = &talloc.base; |
|
350 CX_TEST_DO { |
|
351 char *str; |
|
352 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Test %d %s", 47, "string"); |
|
353 CX_TEST_ASSERT(str == buf); |
|
354 CX_TEST_ASSERT(len == 14); |
|
355 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15)); |
|
356 CX_TEST_ASSERT(0 == memcmp(str, "Test 47 string", 15)); |
|
357 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
358 } |
|
359 cx_testing_allocator_destroy(&talloc); |
|
360 } |
|
361 |
|
362 CX_TEST(test_sprintf_s_alloc) { |
|
363 char buf[16]; |
|
364 memcpy(buf, "0123456789abcdef", 16); |
|
365 CxTestingAllocator talloc; |
|
366 cx_testing_allocator_init(&talloc); |
|
367 CxAllocator *alloc = &talloc.base; |
|
368 CX_TEST_DO { |
|
369 char *str; |
|
370 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 4711, "larger string"); |
|
371 CX_TEST_ASSERT(str != buf); |
|
372 CX_TEST_ASSERT(len == 24); |
|
373 CX_TEST_ASSERT(0 == memcmp(str, "Hello 4711 larger string", 25)); |
|
374 cxFree(alloc, str); |
|
375 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
376 } |
|
377 cx_testing_allocator_destroy(&talloc); |
|
378 } |
|
379 |
|
380 CX_TEST(test_sprintf_s_alloc_to_fit_terminator) { |
|
381 char buf[16]; |
|
382 memcpy(buf, "0123456789abcdef", 16); |
|
383 CxTestingAllocator talloc; |
|
384 cx_testing_allocator_init(&talloc); |
|
385 CxAllocator *alloc = &talloc.base; |
|
386 CX_TEST_DO { |
|
387 char *str; |
|
388 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 112, "string"); |
|
389 CX_TEST_ASSERT(str != buf); |
|
390 CX_TEST_ASSERT(len == 16); |
|
391 CX_TEST_ASSERT(0 == memcmp(str, "Hello 112 string", 17)); // include terminator |
|
392 cxFree(alloc, str); |
|
393 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
394 } |
|
395 cx_testing_allocator_destroy(&talloc); |
|
396 } |
|
397 |
297 CxTestSuite *cx_test_suite_printf(void) { |
398 CxTestSuite *cx_test_suite_printf(void) { |
298 CxTestSuite *suite = cx_test_suite_new("printf"); |
399 CxTestSuite *suite = cx_test_suite_new("printf"); |
299 |
400 |
300 cx_test_register(suite, test_bprintf); |
401 cx_test_register(suite, test_bprintf); |
301 cx_test_register(suite, test_bprintf_large_string); |
402 cx_test_register(suite, test_bprintf_large_string); |
302 cx_test_register(suite, test_bprintf_nocap); |
403 cx_test_register(suite, test_bprintf_nocap); |
303 cx_test_register(suite, test_fprintf); |
404 cx_test_register(suite, test_fprintf); |
304 cx_test_register(suite, test_asprintf); |
405 cx_test_register(suite, test_asprintf); |
305 cx_test_register(suite, test_asprintf_large_string); |
406 cx_test_register(suite, test_asprintf_large_string); |
|
407 cx_test_register(suite, test_sprintf_no_realloc); |
|
408 cx_test_register(suite, test_sprintf_realloc); |
|
409 cx_test_register(suite, test_sprintf_realloc_to_fit_terminator); |
|
410 cx_test_register(suite, test_sprintf_s_no_alloc); |
|
411 cx_test_register(suite, test_sprintf_s_alloc); |
|
412 cx_test_register(suite, test_sprintf_s_alloc_to_fit_terminator); |
306 |
413 |
307 return suite; |
414 return suite; |
308 } |
415 } |