1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Thomas Helland
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include <stdlib.h>
26bf215546Sopenharmony_ci#include <stdio.h>
27bf215546Sopenharmony_ci#include <string.h>
28bf215546Sopenharmony_ci#include <assert.h>
29bf215546Sopenharmony_ci#include <gtest/gtest.h>
30bf215546Sopenharmony_ci#include "util/string_buffer.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci/**
33bf215546Sopenharmony_ci * \file string_buffer_test.cpp
34bf215546Sopenharmony_ci *
35bf215546Sopenharmony_ci * Test the string buffer implementation
36bf215546Sopenharmony_ci */
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#define INITIAL_BUF_SIZE 6
39bf215546Sopenharmony_ciclass string_buffer : public ::testing::Test {
40bf215546Sopenharmony_cipublic:
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   struct _mesa_string_buffer *buf;
43bf215546Sopenharmony_ci   const char *str1;
44bf215546Sopenharmony_ci   const char *str2;
45bf215546Sopenharmony_ci   const char *str3;
46bf215546Sopenharmony_ci   char str4[80];
47bf215546Sopenharmony_ci   char str5[40];
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   virtual void SetUp();
50bf215546Sopenharmony_ci   virtual void TearDown();
51bf215546Sopenharmony_ci};
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_civoid
54bf215546Sopenharmony_cistring_buffer::SetUp()
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   str1 = "test1";
57bf215546Sopenharmony_ci   str2 = "test2";
58bf215546Sopenharmony_ci   str3 = "test1test2";
59bf215546Sopenharmony_ci   buf = _mesa_string_buffer_create(NULL, INITIAL_BUF_SIZE);
60bf215546Sopenharmony_ci}
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_civoid
63bf215546Sopenharmony_cistring_buffer::TearDown()
64bf215546Sopenharmony_ci{
65bf215546Sopenharmony_ci   /* Finally, clean up after us */
66bf215546Sopenharmony_ci   _mesa_string_buffer_destroy(buf);
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_cistatic uint32_t
70bf215546Sopenharmony_cispace_left_in_buffer(_mesa_string_buffer *buf)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   return buf->capacity - buf->length - 1;
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ciTEST_F(string_buffer, string_buffer_tests)
76bf215546Sopenharmony_ci{
77bf215546Sopenharmony_ci   /* The string terminator needs one byte, so there should one "missing" */
78bf215546Sopenharmony_ci   EXPECT_TRUE(space_left_in_buffer(buf) == INITIAL_BUF_SIZE - 1);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   /* Start by appending str1 */
81bf215546Sopenharmony_ci   EXPECT_TRUE(_mesa_string_buffer_append(buf, str1));
82bf215546Sopenharmony_ci   EXPECT_TRUE(space_left_in_buffer(buf) ==
83bf215546Sopenharmony_ci               INITIAL_BUF_SIZE - strlen(str1) - 1);
84bf215546Sopenharmony_ci   EXPECT_TRUE(strcmp(buf->buf, str1) == 0);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   /* Add more, so that the string is resized */
87bf215546Sopenharmony_ci   EXPECT_TRUE(_mesa_string_buffer_append(buf, str2));
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   /* The string should now be equal to str3 */
90bf215546Sopenharmony_ci   EXPECT_TRUE(strcmp(buf->buf, str3) == 0);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   /* Check that the length of the string is reset when clearing */
93bf215546Sopenharmony_ci   _mesa_string_buffer_clear(buf);
94bf215546Sopenharmony_ci   EXPECT_TRUE(buf->length == 0);
95bf215546Sopenharmony_ci   EXPECT_TRUE(strlen(buf->buf) == 0);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   /* Test a string with some formatting */
98bf215546Sopenharmony_ci   snprintf(str4, sizeof(str4), "Testing formatting %d, %f", 100, 1.0);
99bf215546Sopenharmony_ci   EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %f", 100, 1.0));
100bf215546Sopenharmony_ci   EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   /* Compile a string with some other formatting */
103bf215546Sopenharmony_ci   snprintf(str5, sizeof(str5), "Testing formatting %d, %x", 100, 0xDEADBEAF);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   /* Concatenate str5 to str4 */
106bf215546Sopenharmony_ci   strncat(str4, str5, sizeof(str5));
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   /* Now use the formatted append function again */
109bf215546Sopenharmony_ci   EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %x", 100, 0xDEADBEAF));
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   /* The string buffer should now be equal to str4 */
112bf215546Sopenharmony_ci   EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   _mesa_string_buffer_clear(buf);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   /* Test appending by one char at a time */
117bf215546Sopenharmony_ci   EXPECT_TRUE(_mesa_string_buffer_append_char(buf, 'a'));
118bf215546Sopenharmony_ci   EXPECT_TRUE(buf->length == 1);
119bf215546Sopenharmony_ci   EXPECT_TRUE(strcmp(buf->buf, "a") == 0);
120bf215546Sopenharmony_ci   EXPECT_TRUE(_mesa_string_buffer_append_char(buf, 'a'));
121bf215546Sopenharmony_ci   EXPECT_TRUE(strcmp(buf->buf, "aa") == 0);
122bf215546Sopenharmony_ci}
123