2
* Copyright © 2017 Thomas Helland
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29
#include <gtest/gtest.h>
30
#include "util/string_buffer.h"
33
* \file string_buffer_test.cpp
35
* Test the string buffer implementation
38
#define INITIAL_BUF_SIZE 6
39
class string_buffer : public ::testing::Test {
42
struct _mesa_string_buffer *buf;
50
virtual void TearDown();
54
string_buffer::SetUp()
59
buf = _mesa_string_buffer_create(NULL, INITIAL_BUF_SIZE);
63
string_buffer::TearDown()
65
/* Finally, clean up after us */
66
_mesa_string_buffer_destroy(buf);
70
space_left_in_buffer(_mesa_string_buffer *buf)
72
return buf->capacity - buf->length - 1;
75
TEST_F(string_buffer, string_buffer_tests)
77
/* The string terminator needs one byte, so there should one "missing" */
78
EXPECT_TRUE(space_left_in_buffer(buf) == INITIAL_BUF_SIZE - 1);
80
/* Start by appending str1 */
81
EXPECT_TRUE(_mesa_string_buffer_append(buf, str1));
82
EXPECT_TRUE(space_left_in_buffer(buf) ==
83
INITIAL_BUF_SIZE - strlen(str1) - 1);
84
EXPECT_TRUE(strcmp(buf->buf, str1) == 0);
86
/* Add more, so that the string is resized */
87
EXPECT_TRUE(_mesa_string_buffer_append(buf, str2));
89
/* The string should now be equal to str3 */
90
EXPECT_TRUE(strcmp(buf->buf, str3) == 0);
92
/* Check that the length of the string is reset when clearing */
93
_mesa_string_buffer_clear(buf);
94
EXPECT_TRUE(buf->length == 0);
95
EXPECT_TRUE(strlen(buf->buf) == 0);
97
/* Test a string with some formatting */
98
snprintf(str4, sizeof(str4), "Testing formatting %d, %f", 100, 1.0);
99
EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %f", 100, 1.0));
100
EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
102
/* Compile a string with some other formatting */
103
snprintf(str5, sizeof(str5), "Testing formatting %d, %x", 100, 0xDEADBEAF);
105
/* Concatenate str5 to str4 */
106
strncat(str4, str5, sizeof(str5));
108
/* Now use the formatted append function again */
109
EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %x", 100, 0xDEADBEAF));
111
/* The string buffer should now be equal to str4 */
112
EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
114
_mesa_string_buffer_clear(buf);
116
/* Test appending by one char at a time */
117
EXPECT_TRUE(_mesa_string_buffer_append_char(buf, 'a'));
118
EXPECT_TRUE(buf->length == 1);
119
EXPECT_TRUE(strcmp(buf->buf, "a") == 0);
120
EXPECT_TRUE(_mesa_string_buffer_append_char(buf, 'a'));
121
EXPECT_TRUE(strcmp(buf->buf, "aa") == 0);