17 #include "test_snprintf.h"
52 int res = POSIX_EXIT_SUCCESS;
54 const char* expected1 =
"x1: 10, y2: 50%, eol";
55 const char* expected2 =
"Long int: 100000";
56 const char* expected3 =
"Unsigned long int: 3123456789";
57 const char* expected4 =
"Fields width padding: 2013-01-05";
58 const char* expected5 =
"Field width padding: -00100000";
59 const char* expected6 =
"Field width padding: -100000";
60 const char* expected7 =
"Zero field: 0";
61 const char* expected8 =
"Unsigned long int to hex: 0x8000100a";
62 const char* expected9 =
"Unsigned int to hex: 0x0A55";
63 const char* expected10 =
"AAA";
73 rv = posix_snprintf(buf, 128,
74 "x%s: %u, y%s: %d%%, eol",
79 res = POSIX_EXIT_FAILURE;
83 if(strlen(expected1) != rv)
86 res = POSIX_EXIT_FAILURE;
88 else if(strncmp(expected1, buf, 128))
91 res = POSIX_EXIT_FAILURE;
96 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
97 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected1);
101 if(POSIX_EXIT_SUCCESS == res)
107 rv = posix_snprintf(buf, 128,
"Long int: %ld", 100000L);
111 res = POSIX_EXIT_FAILURE;
115 if(strlen(expected2) != rv)
118 res = POSIX_EXIT_FAILURE;
120 else if(strncmp(expected2, buf, 128))
123 res = POSIX_EXIT_FAILURE;
128 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
129 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected2);
134 if(POSIX_EXIT_SUCCESS == res)
141 rv = posix_snprintf(buf, 128,
"Unsigned long int: %lu", 3123456789UL);
145 res = POSIX_EXIT_FAILURE;
149 if(strlen(expected3) != rv)
152 res = POSIX_EXIT_FAILURE;
154 else if(strncmp(expected3, buf, 128))
157 res = POSIX_EXIT_FAILURE;
162 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
163 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected3);
168 if(POSIX_EXIT_SUCCESS == res)
174 rv = posix_snprintf(buf, 128,
"Fields width padding: %04u-%02u-%02u",
179 res = POSIX_EXIT_FAILURE;
183 if(strlen(expected4) != rv)
186 res = POSIX_EXIT_FAILURE;
188 else if(strncmp(expected4, buf, 128))
191 res = POSIX_EXIT_FAILURE;
196 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
197 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected4);
202 if(POSIX_EXIT_SUCCESS == res)
208 rv = posix_snprintf(buf, 128,
"Field width padding: %09ld", -100000L);
212 res = POSIX_EXIT_FAILURE;
216 if(strlen(expected5) != rv)
219 res = POSIX_EXIT_FAILURE;
221 else if(strncmp(expected5, buf, 128))
224 res = POSIX_EXIT_FAILURE;
229 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
230 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected5);
235 if(POSIX_EXIT_SUCCESS == res)
241 rv = posix_snprintf(buf, 128,
"Field width padding: %9ld", -100000L);
245 res = POSIX_EXIT_FAILURE;
249 if(strlen(expected6) != rv)
252 res = POSIX_EXIT_FAILURE;
254 else if(strncmp(expected6, buf, 128))
257 res = POSIX_EXIT_FAILURE;
262 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
263 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected6);
268 if(POSIX_EXIT_SUCCESS == res)
275 rv = posix_snprintf(buf, 128,
"Zero field: %d", 0);
279 res = POSIX_EXIT_FAILURE;
283 if(strlen(expected7) != rv)
286 res = POSIX_EXIT_FAILURE;
288 else if(strncmp(expected7, buf, 128))
291 res = POSIX_EXIT_FAILURE;
296 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
297 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected7);
301 rv = posix_snprintf(buf, 128,
"Zero field: %u", 0U);
302 if(strlen(expected7) != rv)
305 res = POSIX_EXIT_FAILURE;
307 else if(strncmp(expected7, buf, 128))
310 res = POSIX_EXIT_FAILURE;
315 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
316 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected7);
322 if(POSIX_EXIT_SUCCESS == res)
329 rv = posix_snprintf(buf, 128,
"Unsigned long int to hex: 0x%lx",
334 res = POSIX_EXIT_FAILURE;
338 if(strlen(expected8) != rv)
341 res = POSIX_EXIT_FAILURE;
343 else if(strncmp(expected8, buf, 128))
346 res = POSIX_EXIT_FAILURE;
351 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
352 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected8);
357 if(POSIX_EXIT_SUCCESS == res)
364 rv = posix_snprintf(buf, 128,
"Unsigned int to hex: 0x%04X", 0xA55U);
368 res = POSIX_EXIT_FAILURE;
372 if(strlen(expected9) != rv)
375 res = POSIX_EXIT_FAILURE;
377 else if(strncmp(expected9, buf, 128))
380 res = POSIX_EXIT_FAILURE;
385 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
386 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected9);
391 if(POSIX_EXIT_SUCCESS == res)
397 rv = posix_snprintf(buf, 4,
"AAAbbb (truncation)");
401 res = POSIX_EXIT_FAILURE;
408 res = POSIX_EXIT_FAILURE;
410 else if(strncmp(expected10, buf, 128))
413 res = POSIX_EXIT_FAILURE;
418 fprintf(stderr,
TEST_TAB "Result is: \"%s\"\n", buf);
419 fprintf(stderr,
TEST_TAB "Should be: \"%s\"\n", expected10);
424 if(POSIX_EXIT_SUCCESS == res)
431 rv = posix_snprintf(buf, 0,
"AAAbbb (truncation)");
435 res = POSIX_EXIT_FAILURE;
442 res = POSIX_EXIT_FAILURE;
447 if(POSIX_EXIT_SUCCESS == res)
454 rv = posix_snprintf(NULL, 0,
"AAAbbb (truncation)");
458 res = POSIX_EXIT_FAILURE;
465 res = POSIX_EXIT_FAILURE;