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
;
43 const char *str1
= "test1";
44 const char *str2
= "test2";
45 const char *str3
= "test1test2";
50 virtual void TearDown();
54 string_buffer::SetUp()
56 buf
= _mesa_string_buffer_create(NULL
, INITIAL_BUF_SIZE
);
60 string_buffer::TearDown()
62 /* Finally, clean up after us */
63 _mesa_string_buffer_destroy(buf
);
67 space_left_in_buffer(_mesa_string_buffer
*buf
)
69 return buf
->capacity
- buf
->length
- 1;
72 TEST_F(string_buffer
, string_buffer_tests
)
74 /* The string terminator needs one byte, so there should one "missing" */
75 EXPECT_TRUE(space_left_in_buffer(buf
) == INITIAL_BUF_SIZE
- 1);
77 /* Start by appending str1 */
78 EXPECT_TRUE(_mesa_string_buffer_append(buf
, str1
));
79 EXPECT_TRUE(space_left_in_buffer(buf
) ==
80 INITIAL_BUF_SIZE
- strlen(str1
) - 1);
81 EXPECT_TRUE(strcmp(buf
->buf
, str1
) == 0);
83 /* Add more, so that the string is resized */
84 EXPECT_TRUE(_mesa_string_buffer_append(buf
, str2
));
86 /* The string should now be equal to str3 */
87 EXPECT_TRUE(strcmp(buf
->buf
, str3
) == 0);
89 /* Check that the length of the string is reset when clearing */
90 _mesa_string_buffer_clear(buf
);
91 EXPECT_TRUE(buf
->length
== 0);
92 EXPECT_TRUE(strlen(buf
->buf
) == 0);
94 /* Test a string with some formatting */
95 sprintf(str4
, "Testing formatting %d, %f", 100, 1.0);
96 EXPECT_TRUE(_mesa_string_buffer_printf(buf
, "Testing formatting %d, %f", 100, 1.0));
97 EXPECT_TRUE(strcmp(buf
->buf
, str4
) == 0);
99 /* Compile a string with some other formatting */
100 sprintf(str5
, "Testing formatting %d, %x", 100, 0xDEADBEAF);
102 /* Concatenate str5 to str4 */
105 /* Now use the formatted append function again */
106 EXPECT_TRUE(_mesa_string_buffer_printf(buf
, "Testing formatting %d, %x", 100, 0xDEADBEAF));
108 /* The string buffer should now be equal to str4 */
109 EXPECT_TRUE(strcmp(buf
->buf
, str4
) == 0);
111 _mesa_string_buffer_clear(buf
);
113 /* Test appending by one char at a time */
114 EXPECT_TRUE(_mesa_string_buffer_append_char(buf
, 'a'));
115 EXPECT_TRUE(buf
->length
== 1);
116 EXPECT_TRUE(strcmp(buf
->buf
, "a") == 0);
117 EXPECT_TRUE(_mesa_string_buffer_append_char(buf
, 'a'));
118 EXPECT_TRUE(strcmp(buf
->buf
, "aa") == 0);