blob: 99dfd862923a9f3949351f069f37cedd292d56a6 [file] [log] [blame]
Tommifef05002018-02-27 12:51:081/*
2 * Copyright 2018 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "rtc_base/strings/string_builder.h"
12
Yves Gerey3e707812018-11-28 15:47:4913#include <string.h>
14
Tommifef05002018-02-27 12:51:0815#include "rtc_base/checks.h"
Karl Wiberg881f1682018-03-08 14:03:2316#include "test/gmock.h"
17#include "test/gtest.h"
Tommifef05002018-02-27 12:51:0818
19namespace rtc {
20
21TEST(SimpleStringBuilder, Limit) {
Karl Wiberg881f1682018-03-08 14:03:2322 char sb_buf[10];
23 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 12:51:0824 EXPECT_EQ(0u, strlen(sb.str()));
25
26 // Test that for a SSB with a buffer size of 10, that we can write 9 chars
27 // into it.
28 sb << "012345678"; // 9 characters + '\0'.
29 EXPECT_EQ(0, strcmp(sb.str(), "012345678"));
30}
31
32TEST(SimpleStringBuilder, NumbersAndChars) {
Karl Wiberg881f1682018-03-08 14:03:2333 char sb_buf[100];
34 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 12:51:0835 sb << 1 << ':' << 2.1 << ":" << 2.2f << ':' << 78187493520ll << ':'
36 << 78187493520ul;
Jonas Olsson88c99562018-05-03 09:45:3337 EXPECT_EQ(0, strcmp(sb.str(), "1:2.1:2.2:78187493520:78187493520"));
Tommifef05002018-02-27 12:51:0838}
39
40TEST(SimpleStringBuilder, Format) {
Karl Wiberg881f1682018-03-08 14:03:2341 char sb_buf[100];
42 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 12:51:0843 sb << "Here we go - ";
Karl Wiberg881f1682018-03-08 14:03:2344 sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL);
Tommifef05002018-02-27 12:51:0845 EXPECT_EQ(0,
46 strcmp(sb.str(),
47 "Here we go - This is a hex formatted value: 0xdeadbeef"));
48}
49
50TEST(SimpleStringBuilder, StdString) {
Karl Wiberg881f1682018-03-08 14:03:2351 char sb_buf[100];
52 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 12:51:0853 std::string str = "does this work?";
54 sb << str;
55 EXPECT_EQ(str, sb.str());
56}
57
Karl Wiberg881f1682018-03-08 14:03:2358// These tests are safe to run if we have death test support or if DCHECKs are
59// off.
60#if (GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)) || !RTC_DCHECK_IS_ON
61
Tommia5e07cc2020-05-26 19:40:3762TEST(SimpleStringBuilderDeathTest, BufferOverrunConstCharP) {
Karl Wiberg881f1682018-03-08 14:03:2363 char sb_buf[4];
64 SimpleStringBuilder sb(sb_buf);
65 const char* const msg = "This is just too much";
66#if RTC_DCHECK_IS_ON
67 EXPECT_DEATH(sb << msg, "");
68#else
69 sb << msg;
Mirko Bonadei6a489f22019-04-09 13:11:1270 EXPECT_THAT(sb.str(), ::testing::StrEq("Thi"));
Karl Wiberg881f1682018-03-08 14:03:2371#endif
72}
73
Tommia5e07cc2020-05-26 19:40:3774TEST(SimpleStringBuilderDeathTest, BufferOverrunStdString) {
Karl Wiberg881f1682018-03-08 14:03:2375 char sb_buf[4];
76 SimpleStringBuilder sb(sb_buf);
77 sb << 12;
78 const std::string msg = "Aw, come on!";
79#if RTC_DCHECK_IS_ON
80 EXPECT_DEATH(sb << msg, "");
81#else
82 sb << msg;
Mirko Bonadei6a489f22019-04-09 13:11:1283 EXPECT_THAT(sb.str(), ::testing::StrEq("12A"));
Karl Wiberg881f1682018-03-08 14:03:2384#endif
85}
86
Tommia5e07cc2020-05-26 19:40:3787TEST(SimpleStringBuilderDeathTest, BufferOverrunInt) {
Karl Wiberg881f1682018-03-08 14:03:2388 char sb_buf[4];
89 SimpleStringBuilder sb(sb_buf);
90 constexpr int num = -12345;
91#if RTC_DCHECK_IS_ON
92 EXPECT_DEATH(sb << num, "");
93#else
94 sb << num;
95 // If we run into the end of the buffer, resonable results are either that
96 // the append has no effect or that it's truncated at the point where the
97 // buffer ends.
98 EXPECT_THAT(sb.str(),
Mirko Bonadei6a489f22019-04-09 13:11:1299 ::testing::AnyOf(::testing::StrEq(""), ::testing::StrEq("-12")));
Karl Wiberg881f1682018-03-08 14:03:23100#endif
101}
102
Tommia5e07cc2020-05-26 19:40:37103TEST(SimpleStringBuilderDeathTest, BufferOverrunDouble) {
Karl Wiberg881f1682018-03-08 14:03:23104 char sb_buf[5];
105 SimpleStringBuilder sb(sb_buf);
106 constexpr double num = 123.456;
107#if RTC_DCHECK_IS_ON
108 EXPECT_DEATH(sb << num, "");
109#else
110 sb << num;
111 EXPECT_THAT(sb.str(),
Mirko Bonadei6a489f22019-04-09 13:11:12112 ::testing::AnyOf(::testing::StrEq(""), ::testing::StrEq("123.")));
Karl Wiberg881f1682018-03-08 14:03:23113#endif
114}
115
Tommia5e07cc2020-05-26 19:40:37116TEST(SimpleStringBuilderDeathTest, BufferOverrunConstCharPAlreadyFull) {
Karl Wiberg881f1682018-03-08 14:03:23117 char sb_buf[4];
118 SimpleStringBuilder sb(sb_buf);
119 sb << 123;
120 const char* const msg = "This is just too much";
121#if RTC_DCHECK_IS_ON
122 EXPECT_DEATH(sb << msg, "");
123#else
124 sb << msg;
Mirko Bonadei6a489f22019-04-09 13:11:12125 EXPECT_THAT(sb.str(), ::testing::StrEq("123"));
Karl Wiberg881f1682018-03-08 14:03:23126#endif
127}
128
Tommia5e07cc2020-05-26 19:40:37129TEST(SimpleStringBuilderDeathTest, BufferOverrunIntAlreadyFull) {
Karl Wiberg881f1682018-03-08 14:03:23130 char sb_buf[4];
131 SimpleStringBuilder sb(sb_buf);
132 sb << "xyz";
133 constexpr int num = -12345;
134#if RTC_DCHECK_IS_ON
135 EXPECT_DEATH(sb << num, "");
136#else
137 sb << num;
Mirko Bonadei6a489f22019-04-09 13:11:12138 EXPECT_THAT(sb.str(), ::testing::StrEq("xyz"));
Karl Wiberg881f1682018-03-08 14:03:23139#endif
140}
141
142#endif
143
Jonas Olsson88e18482018-09-03 08:15:08144////////////////////////////////////////////////////////////////////////////////
145// StringBuilder.
146
147TEST(StringBuilder, Limit) {
148 StringBuilder sb;
149 EXPECT_EQ(0u, sb.str().size());
150
151 sb << "012345678";
152 EXPECT_EQ(sb.str(), "012345678");
153}
154
155TEST(StringBuilder, NumbersAndChars) {
156 StringBuilder sb;
157 sb << 1 << ":" << 2.1 << ":" << 2.2f << ":" << 78187493520ll << ":"
158 << 78187493520ul;
159 EXPECT_THAT(sb.str(),
Mirko Bonadei6a489f22019-04-09 13:11:12160 ::testing::MatchesRegex("1:2.10*:2.20*:78187493520:78187493520"));
Jonas Olsson88e18482018-09-03 08:15:08161}
162
163TEST(StringBuilder, Format) {
164 StringBuilder sb;
165 sb << "Here we go - ";
166 sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL);
167 EXPECT_EQ(sb.str(), "Here we go - This is a hex formatted value: 0xdeadbeef");
168}
169
170TEST(StringBuilder, StdString) {
171 StringBuilder sb;
172 std::string str = "does this work?";
173 sb << str;
174 EXPECT_EQ(str, sb.str());
175}
176
177TEST(StringBuilder, Release) {
178 StringBuilder sb;
179 std::string str =
180 "This string has to be of a moderate length, or we might "
181 "run into problems with small object optimizations.";
182 EXPECT_LT(sizeof(str), str.size());
183 sb << str;
184 EXPECT_EQ(str, sb.str());
185 const char* original_buffer = sb.str().c_str();
186 std::string moved = sb.Release();
187 EXPECT_TRUE(sb.str().empty());
188 EXPECT_EQ(str, moved);
189 EXPECT_EQ(original_buffer, moved.c_str());
190}
191
192TEST(StringBuilder, Reset) {
193 StringBuilder sb("abc");
194 sb << "def";
195 EXPECT_EQ("abcdef", sb.str());
196 sb.Clear();
197 EXPECT_TRUE(sb.str().empty());
198 sb << 123 << "!";
199 EXPECT_EQ("123!", sb.str());
200}
201
Tommifef05002018-02-27 12:51:08202} // namespace rtc