Refactor common macros used in tests.
[gnulib.git] / tests / test-vasnprintf.c
1 /* Test of vasnprintf() and asnprintf() functions.
2    Copyright (C) 2007-2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "vasnprintf.h"
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "macros.h"
28
29 static char *
30 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
31 {
32   va_list args;
33   char *ret;
34
35   va_start (args, format);
36   ret = vasnprintf (resultbuf, lengthp, format, args);
37   va_end (args);
38   return ret;
39 }
40
41 static void
42 test_vasnprintf ()
43 {
44   char buf[8];
45   int size;
46
47   for (size = 0; size <= 8; size++)
48     {
49       size_t length = size;
50       char *result = my_asnprintf (NULL, &length, "%d", 12345);
51       ASSERT (result != NULL);
52       ASSERT (strcmp (result, "12345") == 0);
53       ASSERT (length == 5);
54       free (result);
55     }
56
57   for (size = 0; size <= 8; size++)
58     {
59       size_t length;
60       char *result;
61
62       memcpy (buf, "DEADBEEF", 8);
63       length = size;
64       result = my_asnprintf (buf, &length, "%d", 12345);
65       ASSERT (result != NULL);
66       ASSERT (strcmp (result, "12345") == 0);
67       ASSERT (length == 5);
68       if (size < 6)
69         ASSERT (result != buf);
70       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
71       if (result != buf)
72         free (result);
73     }
74 }
75
76 static void
77 test_asnprintf ()
78 {
79   char buf[8];
80   int size;
81
82   for (size = 0; size <= 8; size++)
83     {
84       size_t length = size;
85       char *result = asnprintf (NULL, &length, "%d", 12345);
86       ASSERT (result != NULL);
87       ASSERT (strcmp (result, "12345") == 0);
88       ASSERT (length == 5);
89       free (result);
90     }
91
92   for (size = 0; size <= 8; size++)
93     {
94       size_t length;
95       char *result;
96
97       memcpy (buf, "DEADBEEF", 8);
98       length = size;
99       result = asnprintf (buf, &length, "%d", 12345);
100       ASSERT (result != NULL);
101       ASSERT (strcmp (result, "12345") == 0);
102       ASSERT (length == 5);
103       if (size < 6)
104         ASSERT (result != buf);
105       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
106       if (result != buf)
107         free (result);
108     }
109 }
110
111 int
112 main (int argc, char *argv[])
113 {
114   test_vasnprintf ();
115   test_asnprintf ();
116   return 0;
117 }