tests/test_printf.c

changeset 849
edb9f875b7f9
parent 810
85859399a0cc
child 890
54565fd74e74
equal deleted inserted replaced
848:6456036bbb37 849:edb9f875b7f9
299 CxTestingAllocator talloc; 299 CxTestingAllocator talloc;
300 cx_testing_allocator_init(&talloc); 300 cx_testing_allocator_init(&talloc);
301 CxAllocator *alloc = &talloc.base; 301 CxAllocator *alloc = &talloc.base;
302 CX_TEST_DO { 302 CX_TEST_DO {
303 char *oldbuf = buf; 303 char *oldbuf = buf;
304 size_t len = cx_sprintf_a(alloc, &buf, 16, "Test %d %s", 47, "string"); 304 size_t buflen = 16;
305 size_t len = cx_sprintf_a(alloc, &buf, &buflen, "Test %d %s", 47, "string");
305 CX_TEST_ASSERT(oldbuf == buf); 306 CX_TEST_ASSERT(oldbuf == buf);
306 CX_TEST_ASSERT(len == 14); 307 CX_TEST_ASSERT(len == 14);
308 CX_TEST_ASSERT(buflen == 16);
307 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15)); 309 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15));
308 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 310 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
309 } 311 }
310 cx_testing_allocator_destroy(&talloc); 312 cx_testing_allocator_destroy(&talloc);
311 free(buf); 313 free(buf);
315 CxTestingAllocator talloc; 317 CxTestingAllocator talloc;
316 cx_testing_allocator_init(&talloc); 318 cx_testing_allocator_init(&talloc);
317 CxAllocator *alloc = &talloc.base; 319 CxAllocator *alloc = &talloc.base;
318 char *buf = cxMalloc(alloc, 8); 320 char *buf = cxMalloc(alloc, 8);
319 CX_TEST_DO { 321 CX_TEST_DO {
320 size_t len = cx_sprintf_a(alloc, &buf, 8, "Test %d %s", 47, "foobar"); 322 size_t buflen = 8;
323 size_t len = cx_sprintf_a(alloc, &buf, &buflen, "Test %d %s", 47, "foobar");
321 CX_TEST_ASSERT(len == 14); 324 CX_TEST_ASSERT(len == 14);
325 CX_TEST_ASSERT(buflen == 15);
322 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 foobar", 15)); 326 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 foobar", 15));
323 cxFree(alloc, buf); 327 cxFree(alloc, buf);
324 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 328 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
325 } 329 }
326 cx_testing_allocator_destroy(&talloc); 330 cx_testing_allocator_destroy(&talloc);
331 cx_testing_allocator_init(&talloc); 335 cx_testing_allocator_init(&talloc);
332 CxAllocator *alloc = &talloc.base; 336 CxAllocator *alloc = &talloc.base;
333 // make it so that only the zero-terminator does not fit 337 // make it so that only the zero-terminator does not fit
334 char *buf = cxMalloc(alloc, 14); 338 char *buf = cxMalloc(alloc, 14);
335 CX_TEST_DO { 339 CX_TEST_DO {
336 size_t len = cx_sprintf_a(alloc, &buf, 14, "Test %d %s", 13, "string"); 340 size_t buflen = 14;
341 size_t len = cx_sprintf_a(alloc, &buf, &buflen, "Test %d %s", 13, "string");
337 CX_TEST_ASSERT(len == 14); 342 CX_TEST_ASSERT(len == 14);
343 CX_TEST_ASSERT(buflen == 15);
338 CX_TEST_ASSERT(0 == memcmp(buf, "Test 13 string", 15)); 344 CX_TEST_ASSERT(0 == memcmp(buf, "Test 13 string", 15));
339 cxFree(alloc, buf); 345 cxFree(alloc, buf);
340 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 346 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
341 } 347 }
342 cx_testing_allocator_destroy(&talloc); 348 cx_testing_allocator_destroy(&talloc);
347 CxTestingAllocator talloc; 353 CxTestingAllocator talloc;
348 cx_testing_allocator_init(&talloc); 354 cx_testing_allocator_init(&talloc);
349 CxAllocator *alloc = &talloc.base; 355 CxAllocator *alloc = &talloc.base;
350 CX_TEST_DO { 356 CX_TEST_DO {
351 char *str; 357 char *str;
352 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Test %d %s", 47, "string"); 358 size_t buflen = 16;
359 size_t len = cx_sprintf_sa(alloc, buf, &buflen, &str, "Test %d %s", 47, "string");
353 CX_TEST_ASSERT(str == buf); 360 CX_TEST_ASSERT(str == buf);
361 CX_TEST_ASSERT(buflen == 16);
354 CX_TEST_ASSERT(len == 14); 362 CX_TEST_ASSERT(len == 14);
355 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15)); 363 CX_TEST_ASSERT(0 == memcmp(buf, "Test 47 string", 15));
356 CX_TEST_ASSERT(0 == memcmp(str, "Test 47 string", 15)); 364 CX_TEST_ASSERT(0 == memcmp(str, "Test 47 string", 15));
357 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 365 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
358 } 366 }
365 CxTestingAllocator talloc; 373 CxTestingAllocator talloc;
366 cx_testing_allocator_init(&talloc); 374 cx_testing_allocator_init(&talloc);
367 CxAllocator *alloc = &talloc.base; 375 CxAllocator *alloc = &talloc.base;
368 CX_TEST_DO { 376 CX_TEST_DO {
369 char *str; 377 char *str;
370 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 4711, "larger string"); 378 size_t buflen = 16;
379 size_t len = cx_sprintf_sa(alloc, buf, &buflen, &str, "Hello %d %s", 4711, "larger string");
371 CX_TEST_ASSERT(str != buf); 380 CX_TEST_ASSERT(str != buf);
381 CX_TEST_ASSERT(buflen == 25);
372 CX_TEST_ASSERT(len == 24); 382 CX_TEST_ASSERT(len == 24);
373 CX_TEST_ASSERT(0 == memcmp(str, "Hello 4711 larger string", 25)); 383 CX_TEST_ASSERT(0 == memcmp(str, "Hello 4711 larger string", 25));
374 cxFree(alloc, str); 384 cxFree(alloc, str);
375 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 385 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
376 } 386 }
383 CxTestingAllocator talloc; 393 CxTestingAllocator talloc;
384 cx_testing_allocator_init(&talloc); 394 cx_testing_allocator_init(&talloc);
385 CxAllocator *alloc = &talloc.base; 395 CxAllocator *alloc = &talloc.base;
386 CX_TEST_DO { 396 CX_TEST_DO {
387 char *str; 397 char *str;
388 size_t len = cx_sprintf_sa(alloc, buf, 16, &str, "Hello %d %s", 112, "string"); 398 size_t buflen = 16;
399 size_t len = cx_sprintf_sa(alloc, buf,&buflen, &str, "Hello %d %s", 112, "string");
389 CX_TEST_ASSERT(str != buf); 400 CX_TEST_ASSERT(str != buf);
390 CX_TEST_ASSERT(len == 16); 401 CX_TEST_ASSERT(len == 16);
402 CX_TEST_ASSERT(buflen == 17);
391 CX_TEST_ASSERT(0 == memcmp(str, "Hello 112 string", 17)); // include terminator 403 CX_TEST_ASSERT(0 == memcmp(str, "Hello 112 string", 17)); // include terminator
392 cxFree(alloc, str); 404 cxFree(alloc, str);
393 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 405 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
394 } 406 }
395 cx_testing_allocator_destroy(&talloc); 407 cx_testing_allocator_destroy(&talloc);

mercurial