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)
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. */
21 #include "string-buffer.h"
30 my_appendf (struct string_buffer
*buffer
, const char *formatstring
, ...)
34 va_start (args
, formatstring
);
35 int ret
= sb_appendvf (buffer
, formatstring
, args
);
41 char invalid_format_string_1
[] = "%&";
42 char invalid_format_string_2
[] = "%^";
47 /* Test accumulation. */
49 struct string_buffer 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);
59 struct string_buffer 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);
85 /* Test simple string concatenation. */
87 struct string_buffer buffer
;
90 char *s
= sb_dupfree_c (&buffer
);
91 ASSERT (s
!= NULL
&& strcmp (s
, "") == 0);
96 struct string_buffer 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);
108 struct string_buffer 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);
119 /* Test printf-like formatting. */
121 struct string_buffer 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);
132 /* Test vprintf-like formatting. */
134 struct string_buffer 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);
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
151 struct string_buffer buffer
;
155 sb_append_c (&buffer
, "<");
157 ret
= sb_appendf (&buffer
, "%lc", (wint_t) 0x76543210);
158 #if !(defined _AIX || (defined _WIN32 && !defined __CYGWIN__))
160 ASSERT (errno
== EILSEQ
);
163 sb_append_c (&buffer
, "|");
165 ret
= sb_appendf (&buffer
, invalid_format_string_1
, 1);
167 ASSERT (errno
== EINVAL
);
169 sb_append_c (&buffer
, "|");
171 ret
= sb_appendf (&buffer
, invalid_format_string_2
, 2);
173 ASSERT (errno
== EINVAL
);
175 sb_append_c (&buffer
, ">");
176 char *s
= sb_dupfree_c (&buffer
);
180 return test_exit_status
;