openat: don’t close (-1)
[gnulib.git] / tests / test-string-buffer.c
blob2bbb47cff213ad5992809bf59b9a765212bcaba1
1 /* Test of buffer that accumulates a string by piecewise concatenation.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
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 2, or (at your option)
7 any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2021. */
19 #include <config.h>
21 #include "string-buffer.h"
23 #include <errno.h>
24 #include <string.h>
25 #include <wchar.h>
27 #include "macros.h"
29 static int
30 my_appendf (struct string_buffer *buffer, const char *formatstring, ...)
32 va_list args;
34 va_start (args, formatstring);
35 int ret = sb_appendvf (buffer, formatstring, args);
36 va_end (args);
38 return ret;
41 char invalid_format_string_1[] = "%&";
42 char invalid_format_string_2[] = "%^";
44 int
45 main ()
47 /* Test accumulation. */
49 struct string_buffer buffer;
51 sb_init (&buffer);
52 sb_append1 (&buffer, 'x');
53 sb_append1 (&buffer, '\377');
54 char *s = sb_dupfree_c (&buffer);
55 ASSERT (s != NULL && strcmp (s, "x\377") == 0);
56 free (s);
59 struct string_buffer buffer;
61 sb_init (&buffer);
62 sb_append1 (&buffer, 'x');
63 sb_append1 (&buffer, '\377');
65 string_desc_t sd = sb_contents (&buffer);
66 ASSERT (string_desc_length (sd) == 2);
67 ASSERT (string_desc_char_at (sd, 0) == 'x');
68 ASSERT (string_desc_char_at (sd, 1) == '\377');
70 sb_append1 (&buffer, '\0');
71 sb_append1 (&buffer, 'z');
73 string_desc_t sd = sb_contents (&buffer);
74 ASSERT (string_desc_length (sd) == 4);
75 ASSERT (string_desc_char_at (sd, 0) == 'x');
76 ASSERT (string_desc_char_at (sd, 1) == '\377');
77 ASSERT (string_desc_char_at (sd, 2) == '\0');
78 ASSERT (string_desc_char_at (sd, 3) == 'z');
80 char *s = sb_dupfree_c (&buffer);
81 ASSERT (s != NULL && memcmp (s, "x\377\0z\0", 5) == 0);
82 free (s);
85 /* Test simple string concatenation. */
87 struct string_buffer buffer;
89 sb_init (&buffer);
90 char *s = sb_dupfree_c (&buffer);
91 ASSERT (s != NULL && strcmp (s, "") == 0);
92 free (s);
96 struct string_buffer buffer;
98 sb_init (&buffer);
99 sb_append_c (&buffer, "abc");
100 sb_append_c (&buffer, "");
101 sb_append_c (&buffer, "defg");
102 char *s = sb_dupfree_c (&buffer);
103 ASSERT (s != NULL && strcmp (s, "abcdefg") == 0);
104 free (s);
108 struct string_buffer buffer;
110 sb_init (&buffer);
111 sb_append_c (&buffer, "abc");
112 sb_append_desc (&buffer, string_desc_new_addr (5, "de\0fg"));
113 sb_append_c (&buffer, "hij");
114 char *s = sb_dupfree_c (&buffer);
115 ASSERT (s != NULL && memcmp (s, "abcde\0fghij", 12) == 0);
116 free (s);
119 /* Test printf-like formatting. */
121 struct string_buffer buffer;
123 sb_init (&buffer);
124 sb_append_c (&buffer, "<");
125 sb_appendf (&buffer, "%x", 3735928559U);
126 sb_append_c (&buffer, ">");
127 char *s = sb_dupfree_c (&buffer);
128 ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
129 free (s);
132 /* Test vprintf-like formatting. */
134 struct string_buffer buffer;
136 sb_init (&buffer);
137 sb_append_c (&buffer, "<");
138 my_appendf (&buffer, "%x", 3735928559U);
139 sb_append_c (&buffer, ">");
140 char *s = sb_dupfree_c (&buffer);
141 ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
142 free (s);
145 /* Test printf-like formatting failure.
146 On all systems except AIX, trying to convert the wide-character 0x76543210
147 to a multibyte string (in the "C" locale) fails.
148 On all systems, invalid format directives make the vsnzprintf() call
149 fail. */
151 struct string_buffer buffer;
152 int ret;
154 sb_init (&buffer);
155 sb_append_c (&buffer, "<");
157 ret = sb_appendf (&buffer, "%lc", (wint_t) 0x76543210);
158 #if !(defined _AIX || (defined _WIN32 && !defined __CYGWIN__))
159 ASSERT (ret < 0);
160 ASSERT (errno == EILSEQ);
161 #endif
163 sb_append_c (&buffer, "|");
165 ret = sb_appendf (&buffer, invalid_format_string_1, 1);
166 ASSERT (ret < 0);
167 ASSERT (errno == EINVAL);
169 sb_append_c (&buffer, "|");
171 ret = sb_appendf (&buffer, invalid_format_string_2, 2);
172 ASSERT (ret < 0);
173 ASSERT (errno == EINVAL);
175 sb_append_c (&buffer, ">");
176 char *s = sb_dupfree_c (&buffer);
177 ASSERT (s == NULL);
180 return test_exit_status;