test_snprintf.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Test of \c posix_snprintf() implementation
4  *
5  * Copyright (c) 2012-2020 by the developers. See the LICENSE file for details.
6  */
7 
8 
9 /* ========================================================================== */
10 /* Include headers */
11 
12 #include "posix.h" /* Include this first because of feature test macros */
13 
14 #include <stdio.h>
15 
16 #include "test.h"
17 #include "test_snprintf.h"
18 
19 
20 /* ========================================================================== */
21 /*! \addtogroup TEST */
22 /*! @{ */
23 
24 
25 /* ========================================================================== */
26 /*! \brief Test \c posix_snprintf() implementation
27  *
28  * The following cases are tested:
29  * - Literal percent conversion
30  * - Signed integer conversion with long modifier
31  * - Unsigned integer conversion with long modifier
32  * - Unsigned integer conversion to hex
33  * - Unsigned integer conversion to hex with long modifier
34  * - Minimum field width padding with unsigned integer conversion
35  * - Minimum field width zero padding with signed long integer conversion
36  * (on negative value)
37  * - Minimum field width padding with signed long integer conversion
38  * (on negative value)
39  * - Zero signed value without padding
40  * - Zero unsigned value without padding
41  * - Truncation of output
42  * - Zero length (C99/POSIX.1-2001/SUSv4 semantics)
43  * - Zero length and NULL pointer for buffer (C99/POSIX.1-2001/SUSv4 semantics)
44  *
45  * \return
46  * - \c EXIT_SUCCESS on success
47  * - \c EXIT_FAILURE on error
48  */
49 
50 int test_snprintf(void)
51 {
52  int res = POSIX_EXIT_SUCCESS;
53  char buf[128];
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"; /* truncated */
64  int rv;
65 
66  /*
67  * This test the following things:
68  * - String conversion
69  * - Signed integer conversion
70  * - Literal percent conversion
71  * - Intermediate text between conversions
72  */
73  rv = posix_snprintf(buf, 128,
74  "x%s: %u, y%s: %d%%, eol",
75  "1", 10U, "2", 50);
76  if (0 > rv)
77  {
78  print_error("Negative return value");
79  res = POSIX_EXIT_FAILURE;
80  }
81  else
82  {
83  if(strlen(expected1) != rv)
84  {
85  print_error("Length of result is not correct");
86  res = POSIX_EXIT_FAILURE;
87  }
88  else if(strncmp(expected1, buf, 128))
89  {
90  print_error("Result is not correct");
91  res = POSIX_EXIT_FAILURE;
92  }
93  if(res)
94  {
95  /* For debugging */
96  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
97  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected1);
98  }
99  }
100 
101  if(POSIX_EXIT_SUCCESS == res)
102  {
103  /*
104  * This test the following things:
105  * - Signed long integer conversion
106  */
107  rv = posix_snprintf(buf, 128, "Long int: %ld", 100000L);
108  if (0 > rv)
109  {
110  print_error("Negative return value");
111  res = POSIX_EXIT_FAILURE;
112  }
113  else
114  {
115  if(strlen(expected2) != rv)
116  {
117  print_error("Length of result is not correct");
118  res = POSIX_EXIT_FAILURE;
119  }
120  else if(strncmp(expected2, buf, 128))
121  {
122  print_error("Result is not correct");
123  res = POSIX_EXIT_FAILURE;
124  }
125  if(res)
126  {
127  /* For debugging */
128  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
129  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected2);
130  }
131  }
132  }
133 
134  if(POSIX_EXIT_SUCCESS == res)
135  {
136  /*
137  * This test the following things:
138  * - Unsigned long integer conversion
139  * The test value does not fit into a 32 Bit signed variable
140  */
141  rv = posix_snprintf(buf, 128, "Unsigned long int: %lu", 3123456789UL);
142  if (0 > rv)
143  {
144  print_error("Negative return value");
145  res = POSIX_EXIT_FAILURE;
146  }
147  else
148  {
149  if(strlen(expected3) != rv)
150  {
151  print_error("Length of result is not correct");
152  res = POSIX_EXIT_FAILURE;
153  }
154  else if(strncmp(expected3, buf, 128))
155  {
156  print_error("Result is not correct");
157  res = POSIX_EXIT_FAILURE;
158  }
159  if(res)
160  {
161  /* For debugging */
162  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
163  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected3);
164  }
165  }
166  }
167 
168  if(POSIX_EXIT_SUCCESS == res)
169  {
170  /*
171  * This test the following things:
172  * - Pad minimum field width with zeros
173  */
174  rv = posix_snprintf(buf, 128, "Fields width padding: %04u-%02u-%02u",
175  2013, 1, 5);
176  if (0 > rv)
177  {
178  print_error("Negative return value");
179  res = POSIX_EXIT_FAILURE;
180  }
181  else
182  {
183  if(strlen(expected4) != rv)
184  {
185  print_error("Length of result is not correct");
186  res = POSIX_EXIT_FAILURE;
187  }
188  else if(strncmp(expected4, buf, 128))
189  {
190  print_error("Result is not correct");
191  res = POSIX_EXIT_FAILURE;
192  }
193  if(res)
194  {
195  /* For debugging */
196  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
197  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected4);
198  }
199  }
200  }
201 
202  if(POSIX_EXIT_SUCCESS == res)
203  {
204  /*
205  * This test the following things:
206  * - Pad minimum field width with leading zeros on negative value
207  */
208  rv = posix_snprintf(buf, 128, "Field width padding: %09ld", -100000L);
209  if (0 > rv)
210  {
211  print_error("Negative return value");
212  res = POSIX_EXIT_FAILURE;
213  }
214  else
215  {
216  if(strlen(expected5) != rv)
217  {
218  print_error("Length of result is not correct");
219  res = POSIX_EXIT_FAILURE;
220  }
221  else if(strncmp(expected5, buf, 128))
222  {
223  print_error("Result is not correct");
224  res = POSIX_EXIT_FAILURE;
225  }
226  if(res)
227  {
228  /* For debugging */
229  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
230  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected5);
231  }
232  }
233  }
234 
235  if(POSIX_EXIT_SUCCESS == res)
236  {
237  /*
238  * This test the following things:
239  * - Pad minimum field width with spaces before sign on negative value
240  */
241  rv = posix_snprintf(buf, 128, "Field width padding: %9ld", -100000L);
242  if (0 > rv)
243  {
244  print_error("Negative return value");
245  res = POSIX_EXIT_FAILURE;
246  }
247  else
248  {
249  if(strlen(expected6) != rv)
250  {
251  print_error("Length of result is not correct");
252  res = POSIX_EXIT_FAILURE;
253  }
254  else if(strncmp(expected6, buf, 128))
255  {
256  print_error("Result is not correct");
257  res = POSIX_EXIT_FAILURE;
258  }
259  if(res)
260  {
261  /* For debugging */
262  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
263  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected6);
264  }
265  }
266  }
267 
268  if(POSIX_EXIT_SUCCESS == res)
269  {
270  /*
271  * This test the following things:
272  * - Zero signed value without padding
273  * - Zero unsigned value without padding
274  */
275  rv = posix_snprintf(buf, 128, "Zero field: %d", 0);
276  if (0 > rv)
277  {
278  print_error("Negative return value");
279  res = POSIX_EXIT_FAILURE;
280  }
281  else
282  {
283  if(strlen(expected7) != rv)
284  {
285  print_error("Length of result is not correct");
286  res = POSIX_EXIT_FAILURE;
287  }
288  else if(strncmp(expected7, buf, 128))
289  {
290  print_error("Result is not correct");
291  res = POSIX_EXIT_FAILURE;
292  }
293  if(res)
294  {
295  /* For debugging */
296  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
297  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected7);
298  }
299  if(res)
300  {
301  rv = posix_snprintf(buf, 128, "Zero field: %u", 0U);
302  if(strlen(expected7) != rv)
303  {
304  print_error("Length of result is not correct");
305  res = POSIX_EXIT_FAILURE;
306  }
307  else if(strncmp(expected7, buf, 128))
308  {
309  print_error("Result is not correct");
310  res = POSIX_EXIT_FAILURE;
311  }
312  if(res)
313  {
314  /* For debugging */
315  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
316  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected7);
317  }
318  }
319  }
320  }
321 
322  if(POSIX_EXIT_SUCCESS == res)
323  {
324  /*
325  * This test the following things:
326  * - Unsigned long integer conversion to hexadecimal
327  * The test value does not fit into a 32 Bit signed variable
328  */
329  rv = posix_snprintf(buf, 128, "Unsigned long int to hex: 0x%lx",
330  0x8000100AUL);
331  if (0 > rv)
332  {
333  print_error("Negative return value");
334  res = POSIX_EXIT_FAILURE;
335  }
336  else
337  {
338  if(strlen(expected8) != rv)
339  {
340  print_error("Length of result is not correct");
341  res = POSIX_EXIT_FAILURE;
342  }
343  else if(strncmp(expected8, buf, 128))
344  {
345  print_error("Result is not correct");
346  res = POSIX_EXIT_FAILURE;
347  }
348  if(res)
349  {
350  /* For debugging */
351  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
352  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected8);
353  }
354  }
355  }
356 
357  if(POSIX_EXIT_SUCCESS == res)
358  {
359  /*
360  * This test the following things:
361  * - Unsigned integer conversion to hexadecimal (with capital letters)
362  * - Pad minimum field width with leading zeros
363  */
364  rv = posix_snprintf(buf, 128, "Unsigned int to hex: 0x%04X", 0xA55U);
365  if (0 > rv)
366  {
367  print_error("Negative return value");
368  res = POSIX_EXIT_FAILURE;
369  }
370  else
371  {
372  if(strlen(expected9) != rv)
373  {
374  print_error("Length of result is not correct");
375  res = POSIX_EXIT_FAILURE;
376  }
377  else if(strncmp(expected9, buf, 128))
378  {
379  print_error("Result is not correct");
380  res = POSIX_EXIT_FAILURE;
381  }
382  if(res)
383  {
384  /* For debugging */
385  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
386  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected9);
387  }
388  }
389  }
390 
391  if(POSIX_EXIT_SUCCESS == res)
392  {
393  /*
394  * This test the following things:
395  * - Truncation of output
396  */
397  rv = posix_snprintf(buf, 4, "AAAbbb (truncation)");
398  if (0 > rv)
399  {
400  print_error("Negative return value");
401  res = POSIX_EXIT_FAILURE;
402  }
403  else
404  {
405  if(19 != rv)
406  {
407  print_error("Length of result is not correct");
408  res = POSIX_EXIT_FAILURE;
409  }
410  else if(strncmp(expected10, buf, 128))
411  {
412  print_error("Result is not correct");
413  res = POSIX_EXIT_FAILURE;
414  }
415  if(res)
416  {
417  /* For debugging */
418  fprintf(stderr, TEST_TAB "Result is: \"%s\"\n", buf);
419  fprintf(stderr, TEST_TAB "Should be: \"%s\"\n", expected10);
420  }
421  }
422  }
423 
424  if(POSIX_EXIT_SUCCESS == res)
425  {
426  /*
427  * This test the following things:
428  * - Truncation of output with zero buffer size
429  * Allowed with C99/POSIX.1-2001/SUSv4 semantics
430  */
431  rv = posix_snprintf(buf, 0, "AAAbbb (truncation)");
432  if (0 > rv)
433  {
434  print_error("Negative return value");
435  res = POSIX_EXIT_FAILURE;
436  }
437  else
438  {
439  if(19 != rv)
440  {
441  print_error("Length of result is not correct");
442  res = POSIX_EXIT_FAILURE;
443  }
444  }
445  }
446 
447  if(POSIX_EXIT_SUCCESS == res)
448  {
449  /*
450  * This test the following things:
451  * - Truncation of output with zero buffer size and NULL pointer
452  * Allowed with C99/POSIX.1-2001/SUSv4 semantics
453  */
454  rv = posix_snprintf(NULL, 0, "AAAbbb (truncation)");
455  if (0 > rv)
456  {
457  print_error("Negative return value");
458  res = POSIX_EXIT_FAILURE;
459  }
460  else
461  {
462  if(19 != rv)
463  {
464  print_error("Length of result is not correct");
465  res = POSIX_EXIT_FAILURE;
466  }
467  }
468  }
469 
470  return(res);
471 }
472 
473 
474 /*! @} */
475 
476 /* EOF */
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
test_snprintf
int test_snprintf(void)
Test posix_snprintf() implementation.
Definition: test_snprintf.c:50
print_error
void print_error(const char *)
Print error message.
Definition: main.cxx:276

Generated at 2024-04-27 using  doxygen