This source file includes following definitions.
- my_xasprintf
- test_xvasprintf
- test_xasprintf
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
21 # pragma GCC diagnostic ignored "-Wformat-zero-length"
22 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
23 # pragma GCC diagnostic ignored "-Wformat-security"
24 #endif
25
26 #include <config.h>
27
28 #include "xvasprintf.h"
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "macros.h"
35
36 static char *
37 my_xasprintf (const char *format, ...)
38 {
39 va_list args;
40 char *ret;
41
42 va_start (args, format);
43 ret = xvasprintf (format, args);
44 va_end (args);
45 return ret;
46 }
47
48 static void
49 test_xvasprintf (void)
50 {
51 int repeat;
52 char *result;
53
54 for (repeat = 0; repeat <= 8; repeat++)
55 {
56 result = my_xasprintf ("%d", 12345);
57 ASSERT (result != NULL);
58 ASSERT (strcmp (result, "12345") == 0);
59 free (result);
60 }
61
62 {
63
64 const char *empty = "";
65 result = my_xasprintf (empty);
66 ASSERT (result != NULL);
67 ASSERT (strcmp (result, "") == 0);
68 free (result);
69 }
70
71 result = my_xasprintf ("%s", "foo");
72 ASSERT (result != NULL);
73 ASSERT (strcmp (result, "foo") == 0);
74 free (result);
75
76 result = my_xasprintf ("%s%s", "foo", "bar");
77 ASSERT (result != NULL);
78 ASSERT (strcmp (result, "foobar") == 0);
79 free (result);
80
81 result = my_xasprintf ("%s%sbaz", "foo", "bar");
82 ASSERT (result != NULL);
83 ASSERT (strcmp (result, "foobarbaz") == 0);
84 free (result);
85 }
86
87 static void
88 test_xasprintf (void)
89 {
90 int repeat;
91 char *result;
92
93 for (repeat = 0; repeat <= 8; repeat++)
94 {
95 result = xasprintf ("%d", 12345);
96 ASSERT (result != NULL);
97 ASSERT (strcmp (result, "12345") == 0);
98 free (result);
99 }
100
101 {
102
103
104
105 const char *empty = "";
106 result = xasprintf (empty, empty);
107 ASSERT (result != NULL);
108 ASSERT (strcmp (result, "") == 0);
109 free (result);
110 }
111
112 result = xasprintf ("%s", "foo");
113 ASSERT (result != NULL);
114 ASSERT (strcmp (result, "foo") == 0);
115 free (result);
116
117 result = xasprintf ("%s%s", "foo", "bar");
118 ASSERT (result != NULL);
119 ASSERT (strcmp (result, "foobar") == 0);
120 free (result);
121
122 result = my_xasprintf ("%s%sbaz", "foo", "bar");
123 ASSERT (result != NULL);
124 ASSERT (strcmp (result, "foobarbaz") == 0);
125 free (result);
126 }
127
128 int
129 main (_GL_UNUSED int argc, char *argv[])
130 {
131 test_xvasprintf ();
132 test_xasprintf ();
133
134 return 0;
135 }