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"
29 my_appendf (struct string_buffer
*buffer
, const char *formatstring
, ...)
33 va_start (args
, formatstring
);
34 int ret
= sb_appendvf (buffer
, formatstring
, args
);
40 char invalid_format_string_1
[] = "%&";
41 char invalid_format_string_2
[] = "%^";
46 /* Test simple string concatenation. */
48 struct string_buffer buffer
;
51 char *s
= sb_dupfree (&buffer
);
52 ASSERT (s
!= NULL
&& strcmp (s
, "") == 0);
57 struct string_buffer buffer
;
60 sb_append (&buffer
, "abc");
61 sb_append (&buffer
, "");
62 sb_append (&buffer
, "defg");
63 char *s
= sb_dupfree (&buffer
);
64 ASSERT (s
!= NULL
&& strcmp (s
, "abcdefg") == 0);
68 /* Test printf-like formatting. */
70 struct string_buffer buffer
;
73 sb_append (&buffer
, "<");
74 sb_appendf (&buffer
, "%x", 3735928559U);
75 sb_append (&buffer
, ">");
76 char *s
= sb_dupfree (&buffer
);
77 ASSERT (s
!= NULL
&& strcmp (s
, "<deadbeef>") == 0);
81 /* Test vprintf-like formatting. */
83 struct string_buffer buffer
;
86 sb_append (&buffer
, "<");
87 my_appendf (&buffer
, "%x", 3735928559U);
88 sb_append (&buffer
, ">");
89 char *s
= sb_dupfree (&buffer
);
90 ASSERT (s
!= NULL
&& strcmp (s
, "<deadbeef>") == 0);
94 /* Test printf-like formatting failure.
95 On all systems except AIX, trying to convert the wide-character 0x76543210
96 to a multibyte string (in the "C" locale) fails.
97 On all systems where REPLACE_VSNPRINTF=1 (this includes AIX), i.e. where
98 the Gnulib implementation of vsnprintf() is used), invalid format
99 directives make the *printf call fail. */
101 struct string_buffer buffer
;
104 sb_append (&buffer
, "<");
105 sb_appendf (&buffer
, "%lc", (wint_t) 0x76543210);
106 sb_append (&buffer
, "|");
107 sb_appendf (&buffer
, invalid_format_string_1
, 1);
108 sb_append (&buffer
, "|");
109 sb_appendf (&buffer
, invalid_format_string_2
, 2);
110 sb_append (&buffer
, ">");
111 char *s
= sb_dupfree (&buffer
);
115 return test_exit_status
;