1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/strings/string_builder.h"
#include <string.h>
#include "rtc_base/checks.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace rtc {
TEST(SimpleStringBuilder, Limit) {
char sb_buf[10];
SimpleStringBuilder sb(sb_buf);
EXPECT_EQ(0u, strlen(sb.str()));
// Test that for a SSB with a buffer size of 10, that we can write 9 chars
// into it.
sb << "012345678"; // 9 characters + '\0'.
EXPECT_EQ(0, strcmp(sb.str(), "012345678"));
}
TEST(SimpleStringBuilder, NumbersAndChars) {
char sb_buf[100];
SimpleStringBuilder sb(sb_buf);
sb << 1 << ':' << 2.1 << ":" << 2.2f << ':' << 78187493520ll << ':'
<< 78187493520ul;
EXPECT_EQ(0, strcmp(sb.str(), "1:2.1:2.2:78187493520:78187493520"));
}
TEST(SimpleStringBuilder, Format) {
char sb_buf[100];
SimpleStringBuilder sb(sb_buf);
sb << "Here we go - ";
sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL);
EXPECT_EQ(0,
strcmp(sb.str(),
"Here we go - This is a hex formatted value: 0xdeadbeef"));
}
TEST(SimpleStringBuilder, StdString) {
char sb_buf[100];
SimpleStringBuilder sb(sb_buf);
std::string str = "does this work?";
sb << str;
EXPECT_EQ(str, sb.str());
}
// These tests are safe to run if we have death test support or if DCHECKs are
// off.
#if (GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)) || !RTC_DCHECK_IS_ON
TEST(SimpleStringBuilderDeathTest, BufferOverrunConstCharP) {
char sb_buf[4];
SimpleStringBuilder sb(sb_buf);
const char* const msg = "This is just too much";
#if RTC_DCHECK_IS_ON
EXPECT_DEATH(sb << msg, "");
#else
sb << msg;
EXPECT_THAT(sb.str(), ::testing::StrEq("Thi"));
#endif
}
TEST(SimpleStringBuilderDeathTest, BufferOverrunStdString) {
char sb_buf[4];
SimpleStringBuilder sb(sb_buf);
sb << 12;
const std::string msg = "Aw, come on!";
#if RTC_DCHECK_IS_ON
EXPECT_DEATH(sb << msg, "");
#else
sb << msg;
EXPECT_THAT(sb.str(), ::testing::StrEq("12A"));
#endif
}
TEST(SimpleStringBuilderDeathTest, BufferOverrunInt) {
char sb_buf[4];
SimpleStringBuilder sb(sb_buf);
constexpr int num = -12345;
#if RTC_DCHECK_IS_ON
EXPECT_DEATH(sb << num, "");
#else
sb << num;
// If we run into the end of the buffer, resonable results are either that
// the append has no effect or that it's truncated at the point where the
// buffer ends.
EXPECT_THAT(sb.str(),
::testing::AnyOf(::testing::StrEq(""), ::testing::StrEq("-12")));
#endif
}
TEST(SimpleStringBuilderDeathTest, BufferOverrunDouble) {
char sb_buf[5];
SimpleStringBuilder sb(sb_buf);
constexpr double num = 123.456;
#if RTC_DCHECK_IS_ON
EXPECT_DEATH(sb << num, "");
#else
sb << num;
EXPECT_THAT(sb.str(),
::testing::AnyOf(::testing::StrEq(""), ::testing::StrEq("123.")));
#endif
}
TEST(SimpleStringBuilderDeathTest, BufferOverrunConstCharPAlreadyFull) {
char sb_buf[4];
SimpleStringBuilder sb(sb_buf);
sb << 123;
const char* const msg = "This is just too much";
#if RTC_DCHECK_IS_ON
EXPECT_DEATH(sb << msg, "");
#else
sb << msg;
EXPECT_THAT(sb.str(), ::testing::StrEq("123"));
#endif
}
TEST(SimpleStringBuilderDeathTest, BufferOverrunIntAlreadyFull) {
char sb_buf[4];
SimpleStringBuilder sb(sb_buf);
sb << "xyz";
constexpr int num = -12345;
#if RTC_DCHECK_IS_ON
EXPECT_DEATH(sb << num, "");
#else
sb << num;
EXPECT_THAT(sb.str(), ::testing::StrEq("xyz"));
#endif
}
#endif
////////////////////////////////////////////////////////////////////////////////
// StringBuilder.
TEST(StringBuilder, Limit) {
StringBuilder sb;
EXPECT_EQ(0u, sb.str().size());
sb << "012345678";
EXPECT_EQ(sb.str(), "012345678");
}
TEST(StringBuilder, NumbersAndChars) {
StringBuilder sb;
sb << 1 << ":" << 2.1 << ":" << 2.2f << ":" << 78187493520ll << ":"
<< 78187493520ul;
EXPECT_THAT(sb.str(),
::testing::MatchesRegex("1:2.10*:2.20*:78187493520:78187493520"));
}
TEST(StringBuilder, Format) {
StringBuilder sb;
sb << "Here we go - ";
sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL);
EXPECT_EQ(sb.str(), "Here we go - This is a hex formatted value: 0xdeadbeef");
}
TEST(StringBuilder, StdString) {
StringBuilder sb;
std::string str = "does this work?";
sb << str;
EXPECT_EQ(str, sb.str());
}
TEST(StringBuilder, Release) {
StringBuilder sb;
std::string str =
"This string has to be of a moderate length, or we might "
"run into problems with small object optimizations.";
EXPECT_LT(sizeof(str), str.size());
sb << str;
EXPECT_EQ(str, sb.str());
const char* original_buffer = sb.str().c_str();
std::string moved = sb.Release();
EXPECT_TRUE(sb.str().empty());
EXPECT_EQ(str, moved);
EXPECT_EQ(original_buffer, moved.c_str());
}
TEST(StringBuilder, Reset) {
StringBuilder sb("abc");
sb << "def";
EXPECT_EQ("abcdef", sb.str());
sb.Clear();
EXPECT_TRUE(sb.str().empty());
sb << 123 << "!";
EXPECT_EQ("123!", sb.str());
}
} // namespace rtc
|