Added few more stubs so that control reaches to DestroyDevice().
[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;
44 const char *str2;
45 const char *str3;
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 str1 = "test1";
57 str2 = "test2";
58 str3 = "test1test2";
59 buf = _mesa_string_buffer_create(NULL, INITIAL_BUF_SIZE);
60 }
61
62 void
63 string_buffer::TearDown()
64 {
65 /* Finally, clean up after us */
66 _mesa_string_buffer_destroy(buf);
67 }
68
69 static uint32_t
70 space_left_in_buffer(_mesa_string_buffer *buf)
71 {
72 return buf->capacity - buf->length - 1;
73 }
74
75 TEST_F(string_buffer, string_buffer_tests)
76 {
77 /* The string terminator needs one byte, so there should one "missing" */
78 EXPECT_TRUE(space_left_in_buffer(buf) == INITIAL_BUF_SIZE - 1);
79
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);
85
86 /* Add more, so that the string is resized */
87 EXPECT_TRUE(_mesa_string_buffer_append(buf, str2));
88
89 /* The string should now be equal to str3 */
90 EXPECT_TRUE(strcmp(buf->buf, str3) == 0);
91
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);
96
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);
101
102 /* Compile a string with some other formatting */
103 snprintf(str5, sizeof(str5), "Testing formatting %d, %x", 100, 0xDEADBEAF);
104
105 /* Concatenate str5 to str4 */
106 strncat(str4, str5, sizeof(str5));
107
108 /* Now use the formatted append function again */
109 EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d, %x", 100, 0xDEADBEAF));
110
111 /* The string buffer should now be equal to str4 */
112 EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
113
114 _mesa_string_buffer_clear(buf);
115
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);
122 }