[Mesa-dev] [PATCH 2/8] util: Add tests for the string buffer

Thomas Helland thomashelland90 at gmail.com
Tue Aug 29 19:56:32 UTC 2017


More tests could probably be added, but this should cover
concatenation, resizing, clearing, formatted printing,
and checking the length, so it should be quite complete.

V2: Address review feedback from Timothy, plus fixes
   - Use a large enough char array
   - Actually test the formatted appending
   - Test that clear function resets string length
---
 configure.ac                                    |  2 +
 src/util/Makefile.am                            |  3 +-
 src/util/tests/string_buffer/Makefile.am        | 34 +++++++++
 src/util/tests/string_buffer/append_and_print.c | 99 +++++++++++++++++++++++++
 4 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 src/util/tests/string_buffer/Makefile.am
 create mode 100644 src/util/tests/string_buffer/append_and_print.c

diff --git a/configure.ac b/configure.ac
index 53d52f6d52..a0d7a67956 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2933,9 +2933,11 @@ AC_CONFIG_FILES([Makefile
                  src/mesa/main/tests/Makefile
                  src/util/Makefile
                  src/util/tests/hash_table/Makefile
+                 src/util/tests/string_buffer/Makefile
                  src/util/xmlpool/Makefile
                  src/vulkan/Makefile])
 
+
 AC_OUTPUT
 
 # Fix up dependencies in *.Plo files, where we changed the extension of a
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index 4512dc99d5..7000202a36 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -19,7 +19,8 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 # IN THE SOFTWARE.
 
-SUBDIRS = xmlpool . tests/hash_table
+SUBDIRS = . tests/hash_table \
+          . tests/string_buffer
 
 include Makefile.sources
 
diff --git a/src/util/tests/string_buffer/Makefile.am b/src/util/tests/string_buffer/Makefile.am
new file mode 100644
index 0000000000..60039a90d2
--- /dev/null
+++ b/src/util/tests/string_buffer/Makefile.am
@@ -0,0 +1,34 @@
+# Copyright © 2009 Intel Corporation
+#
+#  Permission is hereby granted, free of charge, to any person obtaining a
+#  copy of this software and associated documentation files (the "Software"),
+#  to deal in the Software without restriction, including without limitation
+#  on the rights to use, copy, modify, merge, publish, distribute, sub
+#  license, and/or sell copies of the Software, and to permit persons to whom
+#  the Software is furnished to do so, subject to the following conditions:
+#
+#  The above copyright notice and this permission notice (including the next
+#  paragraph) shall be included in all copies or substantial portions of the
+#  Software.
+#
+#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
+#  ADAM JACKSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+#  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+#  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+AM_CPPFLAGS = \
+	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/src/util \
+	$(DEFINES)
+
+LDADD = \
+	$(top_builddir)/src/util/libmesautil.la \
+	$(PTHREAD_LIBS) \
+	$(DLOPEN_LIBS)
+
+TESTS = append_and_print \
+	$()
+
+check_PROGRAMS = $(TESTS)
diff --git a/src/util/tests/string_buffer/append_and_print.c b/src/util/tests/string_buffer/append_and_print.c
new file mode 100644
index 0000000000..71b857892f
--- /dev/null
+++ b/src/util/tests/string_buffer/append_and_print.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright © 2017 Thomas Helland
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "string_buffer.h"
+
+int
+main(int argc, char **argv)
+{
+   (void) argc;
+   (void) argv;
+
+   struct _mesa_string_buffer *buf;
+   char *str1 = "test1";
+   char *str2 = "test2";
+   char *str3 = "test1test2";
+   char str4[80];
+   char str5[40];
+
+   buf = _mesa_string_buffer_create(NULL, 6);
+
+   /* The string terminator needs one byte, so there should be five left */
+   assert(_mesa_string_buffer_space_left(buf) == 5);
+
+   _mesa_string_buffer_append(buf, str1);
+
+   /* The string should now be full */
+   assert(_mesa_string_buffer_space_left(buf) == 0);
+
+   /* The content should be equal */
+   assert(strcmp(buf->buf, str1) == 0);
+
+   /* Add more, so that the string is resized */
+   _mesa_string_buffer_append(buf, str2);
+
+   /* The size of the buffer should have been doubled.
+    * We have 5 + 5 + 1 characters, so there should be one left.
+    */
+   assert(_mesa_string_buffer_space_left(buf) == 1);
+
+   /* The string should now be equal to str3 */
+   assert(strcmp(buf->buf, str3) == 0);
+
+   /* Compile a string with some formatting */
+   sprintf(str4, "Testing formatting %d, %f", 100, 1.0);
+
+   /* Clear the string buffer first */
+   _mesa_string_buffer_clear(buf);
+
+   /* Check that the length of the string is reset */
+   assert(buf->length == 0);
+
+   /* Then test printing some formatted text */
+   _mesa_string_buffer_printf(buf, "Testing formatting %d, %f", 100, 1.0);
+
+   /* The string should now be equal to str4 */
+   assert(strcmp(buf->buf, &str4) == 0);
+
+   /* Compile a string with some other formatting */
+   sprintf(str5, "Testing formatting %d, %x", 100, 0xDEADBEAF);
+
+   /* Concatenate str5 to str4 */
+   strcat(str4, str5);
+
+   /* Now use the formatted append function */
+   _mesa_string_buffer_printf(buf, "Testing formatting %d, %x", 100, 0xDEADBEAF);
+
+   /* The string buffer should now be equal to str4 */
+   assert(strcmp(buf->buf, &str4) == 0);
+
+   /* Finally, clean up after us */
+   _mesa_string_buffer_destroy(buf);
+
+   return 0;
+}
-- 
2.13.3



More information about the mesa-dev mailing list