util: Add tests for the string buffer
[mesa.git] / src / util / tests / string_buffer / string_buffer_test.cpp
1 /*
2 * Copyright © 2017 Thomas Helland
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <gtest/gtest.h>
30 #include "util/string_buffer.h"
31
32 /**
33 * \file string_buffer_test.cpp
34 *
35 * Test the string buffer implementation
36 */
37
38 #define INITIAL_BUF_SIZE 6
39 class string_buffer : public ::testing::Test {
40 public:
41
42 struct _mesa_string_buffer *buf;
43 const char *str1 = "test1";
44 const char *str2 = "test2";
45 const char *str3 = "test1test2";
46 char str4[80];
47 char str5[40];
48
49 virtual void SetUp();
50 virtual void TearDown();
51 };
52
53 void
54 string_buffer::SetUp()
55 {
56 buf = _mesa_string_buffer_create(NULL, INITIAL_BUF_SIZE);
57 }
58
59 void
60 string_buffer::TearDown()
61 {
62 /* Finally, clean up after us */
63 _mesa_string_buffer_destroy(buf);
64 }
65
66 static uint32_t
67 space_left_in_buffer(_mesa_string_buffer *buf)
68 {
69 return buf->capacity - buf->length - 1;
70 }
71
72 TEST_F(string_buffer, string_buffer_tests)
73 {
74 /* The string terminator needs one byte, so there should one "missing" */
75 EXPECT_TRUE(space_left_in_buffer(buf) == INITIAL_BUF_SIZE - 1);
76
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);
82
83 /* Add more, so that the string is resized */
84 EXPECT_TRUE(_mesa_string_buffer_append(buf, str2));
85
86 /* The string should now be equal to str3 */
87 EXPECT_TRUE(strcmp(buf->buf, str3) == 0);
88
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);
93
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);
98
99 /* Compile a string with some other formatting */
100 sprintf(str5, "Testing formatting %d, %x", 100, 0xDEADBEAF);
101
102 /* Concatenate str5 to str4 */
103 strcat(str4, str5);
104
105 /* Now use the formatted append function again */
106 EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %x", 100, 0xDEADBEAF));
107
108 /* The string buffer should now be equal to str4 */
109 EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
110
111 _mesa_string_buffer_clear(buf);
112
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);
119 }