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

Nicolai Hähnle nhaehnle at gmail.com
Tue Sep 26 10:47:32 UTC 2017


On 21.09.2017 21:13, Thomas Helland wrote:
> Fixed the missing newline at the end of this cpp file locally.
> This is the only patch left in the series without an RB.
> If there's no objections I plan on pushing this once I get an RB on this.
> Someone mind having a look at it?

Sorry, I forgot to send out the R-b. So:

Reviewed-by: Nicolai Hähnle <nicolai.haehnle at amd.com>


> 
> 2017-09-11 22:21 GMT+02:00 Thomas Helland <thomashelland90 at gmail.com>:
>> 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
>>
>> V3: Port to gtest
>>
>> V4: Fix test makefile
>>      Fix copyright header
>>      Fix missing extern C
>>      Use more appropriate name for C-file
>>      Add tests for append_char
>> ---
>>   configure.ac                                       |   1 +
>>   src/util/Makefile.am                               |   5 +-
>>   src/util/tests/string_buffer/Makefile.am           |  40 +++++++
>>   .../tests/string_buffer/string_buffer_test.cpp     | 119 +++++++++++++++++++++
>>   4 files changed, 164 insertions(+), 1 deletion(-)
>>   create mode 100644 src/util/tests/string_buffer/Makefile.am
>>   create mode 100644 src/util/tests/string_buffer/string_buffer_test.cpp
>>
>> diff --git a/configure.ac b/configure.ac
>> index d0d4c0dfd1..20727c7bb4 100644
>> --- a/configure.ac
>> +++ b/configure.ac
>> @@ -2924,6 +2924,7 @@ AC_CONFIG_FILES([Makefile
>>                    src/mesa/state_tracker/tests/Makefile
>>                    src/util/Makefile
>>                    src/util/tests/hash_table/Makefile
>> +                 src/util/tests/string_buffer/Makefile
>>                    src/util/xmlpool/Makefile
>>                    src/vulkan/Makefile])
>>
>> diff --git a/src/util/Makefile.am b/src/util/Makefile.am
>> index 4512dc99d5..2b47143ad7 100644
>> --- a/src/util/Makefile.am
>> +++ b/src/util/Makefile.am
>> @@ -19,7 +19,10 @@
>>   # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
>>   # IN THE SOFTWARE.
>>
>> -SUBDIRS = xmlpool . tests/hash_table
>> +SUBDIRS = . \
>> +       xmlpool \
>> +       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..bd04d86349
>> --- /dev/null
>> +++ b/src/util/tests/string_buffer/Makefile.am
>> @@ -0,0 +1,40 @@
>> +# 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.
>> +
>> +AM_CPPFLAGS = \
>> +       -I$(top_srcdir)/src \
>> +       -I$(top_srcdir)/include \
>> +       -I$(top_srcdir)/src/gtest/include \
>> +       $(PTHREAD_CFLAGS) \
>> +       $(DEFINES)
>> +
>> +TESTS = string_buffer_test
>> +
>> +check_PROGRAMS = $(TESTS)
>> +
>> +string_buffer_test_SOURCES = \
>> +       string_buffer_test.cpp
>> +
>> +string_buffer_test_LDADD = \
>> +       $(top_builddir)/src/gtest/libgtest.la \
>> +       $(top_builddir)/src/util/libmesautil.la \
>> +       $(PTHREAD_LIBS) \
>> +       $(DLOPEN_LIBS)
>> diff --git a/src/util/tests/string_buffer/string_buffer_test.cpp b/src/util/tests/string_buffer/string_buffer_test.cpp
>> new file mode 100644
>> index 0000000000..e80ee8b135
>> --- /dev/null
>> +++ b/src/util/tests/string_buffer/string_buffer_test.cpp
>> @@ -0,0 +1,119 @@
>> +/*
>> + * 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 <gtest/gtest.h>
>> +#include "util/string_buffer.h"
>> +
>> +/**
>> + * \file string_buffer_test.cpp
>> + *
>> + * Test the string buffer implementation
>> + */
>> +
>> +#define INITIAL_BUF_SIZE 6
>> +class string_buffer : public ::testing::Test {
>> +public:
>> +
>> +   struct _mesa_string_buffer *buf;
>> +   const char *str1 = "test1";
>> +   const char *str2 = "test2";
>> +   const char *str3 = "test1test2";
>> +   char str4[80];
>> +   char str5[40];
>> +
>> +   virtual void SetUp();
>> +   virtual void TearDown();
>> +};
>> +
>> +void
>> +string_buffer::SetUp()
>> +{
>> +   buf = _mesa_string_buffer_create(NULL, INITIAL_BUF_SIZE);
>> +}
>> +
>> +void
>> +string_buffer::TearDown()
>> +{
>> +   /* Finally, clean up after us */
>> +   _mesa_string_buffer_destroy(buf);
>> +}
>> +
>> +static uint32_t
>> +space_left_in_buffer(_mesa_string_buffer *buf)
>> +{
>> +   return buf->capacity - buf->length - 1;
>> +}
>> +
>> +TEST_F(string_buffer, string_buffer_tests)
>> +{
>> +   /* The string terminator needs one byte, so there should one "missing" */
>> +   EXPECT_TRUE(space_left_in_buffer(buf) == INITIAL_BUF_SIZE - 1);
>> +
>> +   /* Start by appending str1 */
>> +   EXPECT_TRUE(_mesa_string_buffer_append(buf, str1));
>> +   EXPECT_TRUE(space_left_in_buffer(buf) ==
>> +               INITIAL_BUF_SIZE - strlen(str1) - 1);
>> +   EXPECT_TRUE(strcmp(buf->buf, str1) == 0);
>> +
>> +   /* Add more, so that the string is resized */
>> +   EXPECT_TRUE(_mesa_string_buffer_append(buf, str2));
>> +
>> +   /* The string should now be equal to str3 */
>> +   EXPECT_TRUE(strcmp(buf->buf, str3) == 0);
>> +
>> +   /* Check that the length of the string is reset when clearing */
>> +   _mesa_string_buffer_clear(buf);
>> +   EXPECT_TRUE(buf->length == 0);
>> +   EXPECT_TRUE(strlen(buf->buf) == 0);
>> +
>> +   /* Test a string with some formatting */
>> +   sprintf(str4, "Testing formatting %d, %f", 100, 1.0);
>> +   EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %f", 100, 1.0));
>> +   EXPECT_TRUE(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 again */
>> +   EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %x", 100, 0xDEADBEAF));
>> +
>> +   /* The string buffer should now be equal to str4 */
>> +   EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
>> +
>> +   _mesa_string_buffer_clear(buf);
>> +
>> +   /* Test appending by one char at a time */
>> +   EXPECT_TRUE(_mesa_string_buffer_append_char(buf, 'a'));
>> +   EXPECT_TRUE(buf->length == 1);
>> +   EXPECT_TRUE(strcmp(buf->buf, "a") == 0);
>> +   EXPECT_TRUE(_mesa_string_buffer_append_char(buf, 'a'));
>> +   EXPECT_TRUE(strcmp(buf->buf, "aa") == 0);
>> +}
>> \ No newline at end of file
>> --
>> 2.13.3
>>


-- 
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.


More information about the mesa-dev mailing list