--- src/util/tests/string_buffer/append_and_print.c | 99 ---------- src/util/tests/string_buffer/append_and_print.cpp | 221 ++++++++++++++++++++++ 2 files changed, 221 insertions(+), 99 deletions(-) delete mode 100644 src/util/tests/string_buffer/append_and_print.c create mode 100644 src/util/tests/string_buffer/append_and_print.cpp
diff --git a/src/util/tests/string_buffer/append_and_print.c b/src/util/tests/string_buffer/append_and_print.c deleted file mode 100644 index 71b857892f..0000000000 --- a/src/util/tests/string_buffer/append_and_print.c +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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; -} diff --git a/src/util/tests/string_buffer/append_and_print.cpp b/src/util/tests/string_buffer/append_and_print.cpp new file mode 100644 index 0000000000..c3ccd8f9d1 --- /dev/null +++ b/src/util/tests/string_buffer/append_and_print.cpp @@ -0,0 +1,221 @@ +/* + * 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; +} + + + + +/* + * 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; + char *str1 = "test1"; + char *str2 = "test2"; + 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); +} + +TEST_F(string_buffer, string_buffer_tests) +{ + /* The string terminator needs one byte, so there should one "missing" */ + EXPECT_TRUE(_mesa_string_buffer_space_left(buf) == INITIAL_BUF_SIZE - 1); + + /* Start by appending str1 */ + _mesa_string_buffer_append(buf, str1); + + EXPECT_TRUE(_mesa_string_buffer_space_left(buf) == + INITIAL_BUF_SIZE - strlen(str1) - 1); + + /* The content should be equal */ + EXPECT_TRUE(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. + */ + EXPECT_TRUE(_mesa_string_buffer_space_left(buf) == + ((INITIAL_BUF_SIZE * 2) - strlen(str3) - 1)); + + /* The string should now be equal to str3 */ + EXPECT_TRUE(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 */ + EXPECT_TRUE(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 */ + 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 */ + _mesa_string_buffer_printf(buf, "Testing formatting %d, %x", 100, 0xDEADBEAF); + + /* The string buffer should now be equal to str4 */ + EXPQCT_TRUE(strcmp(buf->buf, &str4) == 0); +} \ No newline at end of file -- 2.13.3 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev