nstrftime, c-nstrftime tests: Avoid test failures on native Windows.
[gnulib.git] / tests / test-string-buffer.c
blob8538ac0cecbf461902c25702c14ce8c2c11866ed
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 <string.h>
24 #include <wchar.h>
26 #include "macros.h"
28 static int
29 my_appendf (struct string_buffer *buffer, const char *formatstring, ...)
31 va_list args;
33 va_start (args, formatstring);
34 int ret = sb_appendvf (buffer, formatstring, args);
35 va_end (args);
37 return ret;
40 char invalid_format_string_1[] = "%&";
41 char invalid_format_string_2[] = "%^";
43 int
44 main ()
46 /* Test simple string concatenation. */
48 struct string_buffer buffer;
50 sb_init (&buffer);
51 char *s = sb_dupfree (&buffer);
52 ASSERT (s != NULL && strcmp (s, "") == 0);
53 free (s);
57 struct string_buffer buffer;
59 sb_init (&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);
65 free (s);
68 /* Test printf-like formatting. */
70 struct string_buffer buffer;
72 sb_init (&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);
78 free (s);
81 /* Test vprintf-like formatting. */
83 struct string_buffer buffer;
85 sb_init (&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);
91 free (s);
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;
103 sb_init (&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);
112 ASSERT (s == NULL);
115 return test_exit_status;