summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2021-02-21 21:41:53 +0100
committerBruno Haible <bruno@clisp.org>2021-02-21 21:41:53 +0100
commitc9b44f214c7c798c7701c7a281584e262b263655 (patch)
tree5f3b5b41710a22884358bf621aeca83ef5e9c73b
parent75461c74dca5e5fa9c9f2540d7328a38e59a7b9c (diff)
downloadgnulib-c9b44f214c7c798c7701c7a281584e262b263655.tar.gz
gnulib-c9b44f214c7c798c7701c7a281584e262b263655.tar.bz2
string-buffer: Add tests.
* tests/test-string-buffer.c: New file. * modules/string-buffer-tests: New file.
-rw-r--r--ChangeLog4
-rw-r--r--modules/string-buffer-tests11
-rw-r--r--tests/test-string-buffer.c108
3 files changed, 123 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 9c0e9b8a47..bbc8adf4be 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
2021-02-21 Bruno Haible <bruno@clisp.org>
+ string-buffer: Add tests.
+ * tests/test-string-buffer.c: New file.
+ * modules/string-buffer-tests: New file.
+
string-buffer: New module.
* lib/string-buffer.h: New file.
* lib/string-buffer.c: New file.
diff --git a/modules/string-buffer-tests b/modules/string-buffer-tests
new file mode 100644
index 0000000000..38473fdba3
--- /dev/null
+++ b/modules/string-buffer-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-string-buffer.c
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-string-buffer
+check_PROGRAMS += test-string-buffer
diff --git a/tests/test-string-buffer.c b/tests/test-string-buffer.c
new file mode 100644
index 0000000000..797355945b
--- /dev/null
+++ b/tests/test-string-buffer.c
@@ -0,0 +1,108 @@
+/* Test of buffer that accumulates a string by piecewise concatenation.
+ Copyright (C) 2021 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2021. */
+
+#include <config.h>
+
+#include "string-buffer.h"
+
+#include <string.h>
+
+#include "macros.h"
+
+static int
+my_appendf (struct string_buffer *buffer, const char *formatstring, ...)
+{
+ va_list args;
+
+ va_start (args, formatstring);
+ int ret = sb_appendvf (buffer, formatstring, args);
+ va_end (args);
+
+ return ret;
+}
+
+char invalid_format_string_1[] = "%&";
+char invalid_format_string_2[] = "%^";
+
+int
+main ()
+{
+ /* Test simple string concatenation. */
+ {
+ struct string_buffer buffer;
+
+ sb_init (&buffer);
+ char *s = sb_dupfree (&buffer);
+ ASSERT (s != NULL && strcmp (s, "") == 0);
+ free (s);
+ }
+
+ {
+ struct string_buffer buffer;
+
+ sb_init (&buffer);
+ sb_append (&buffer, "abc");
+ sb_append (&buffer, "");
+ sb_append (&buffer, "defg");
+ char *s = sb_dupfree (&buffer);
+ ASSERT (s != NULL && strcmp (s, "abcdefg") == 0);
+ free (s);
+ }
+
+ /* Test printf-like formatting. */
+ {
+ struct string_buffer buffer;
+
+ sb_init (&buffer);
+ sb_append (&buffer, "<");
+ sb_appendf (&buffer, "%x", 3735928559U);
+ sb_append (&buffer, ">");
+ char *s = sb_dupfree (&buffer);
+ ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
+ free (s);
+ }
+
+ /* Test vprintf-like formatting. */
+ {
+ struct string_buffer buffer;
+
+ sb_init (&buffer);
+ sb_append (&buffer, "<");
+ my_appendf (&buffer, "%x", 3735928559U);
+ sb_append (&buffer, ">");
+ char *s = sb_dupfree (&buffer);
+ ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
+ free (s);
+ }
+
+ /* Test printf-like formatting failure. */
+ {
+ struct string_buffer buffer;
+
+ sb_init (&buffer);
+ sb_append (&buffer, "<");
+ sb_appendf (&buffer, invalid_format_string_1, 1);
+ sb_append (&buffer, "|");
+ sb_appendf (&buffer, invalid_format_string_2, 2);
+ sb_append (&buffer, ">");
+ char *s = sb_dupfree (&buffer);
+ ASSERT (s == NULL);
+ }
+
+ return 0;
+}

Return to:

Send suggestions and report system problems to the System administrator.