blob: 878e128b191d71c87d3ec63bc498679504c04e71 [file] [log] [blame]
Tommifef05002018-02-27 13:51:08 +01001/*
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
13#include "rtc_base/checks.h"
Tommifef05002018-02-27 13:51:08 +010014#include "rtc_base/stringutils.h"
Karl Wiberg881f1682018-03-08 15:03:23 +010015#include "test/gmock.h"
16#include "test/gtest.h"
Tommifef05002018-02-27 13:51:08 +010017
18namespace rtc {
19
20TEST(SimpleStringBuilder, Limit) {
Karl Wiberg881f1682018-03-08 15:03:23 +010021 char sb_buf[10];
22 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 13:51:08 +010023 EXPECT_EQ(0u, strlen(sb.str()));
24
25 // Test that for a SSB with a buffer size of 10, that we can write 9 chars
26 // into it.
27 sb << "012345678"; // 9 characters + '\0'.
28 EXPECT_EQ(0, strcmp(sb.str(), "012345678"));
29}
30
31TEST(SimpleStringBuilder, NumbersAndChars) {
Karl Wiberg881f1682018-03-08 15:03:23 +010032 char sb_buf[100];
33 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 13:51:08 +010034 sb << 1 << ':' << 2.1 << ":" << 2.2f << ':' << 78187493520ll << ':'
35 << 78187493520ul;
Jonas Olsson88c99562018-05-03 11:45:33 +020036 EXPECT_EQ(0, strcmp(sb.str(), "1:2.1:2.2:78187493520:78187493520"));
Tommifef05002018-02-27 13:51:08 +010037}
38
39TEST(SimpleStringBuilder, Format) {
Karl Wiberg881f1682018-03-08 15:03:23 +010040 char sb_buf[100];
41 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 13:51:08 +010042 sb << "Here we go - ";
Karl Wiberg881f1682018-03-08 15:03:23 +010043 sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL);
Tommifef05002018-02-27 13:51:08 +010044 EXPECT_EQ(0,
45 strcmp(sb.str(),
46 "Here we go - This is a hex formatted value: 0xdeadbeef"));
47}
48
49TEST(SimpleStringBuilder, StdString) {
Karl Wiberg881f1682018-03-08 15:03:23 +010050 char sb_buf[100];
51 SimpleStringBuilder sb(sb_buf);
Tommifef05002018-02-27 13:51:08 +010052 std::string str = "does this work?";
53 sb << str;
54 EXPECT_EQ(str, sb.str());
55}
56
Karl Wiberg881f1682018-03-08 15:03:23 +010057// These tests are safe to run if we have death test support or if DCHECKs are
58// off.
59#if (GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)) || !RTC_DCHECK_IS_ON
60
61TEST(SimpleStringBuilder, BufferOverrunConstCharP) {
62 char sb_buf[4];
63 SimpleStringBuilder sb(sb_buf);
64 const char* const msg = "This is just too much";
65#if RTC_DCHECK_IS_ON
66 EXPECT_DEATH(sb << msg, "");
67#else
68 sb << msg;
69 EXPECT_THAT(sb.str(), testing::StrEq("Thi"));
70#endif
71}
72
73TEST(SimpleStringBuilder, BufferOverrunStdString) {
74 char sb_buf[4];
75 SimpleStringBuilder sb(sb_buf);
76 sb << 12;
77 const std::string msg = "Aw, come on!";
78#if RTC_DCHECK_IS_ON
79 EXPECT_DEATH(sb << msg, "");
80#else
81 sb << msg;
82 EXPECT_THAT(sb.str(), testing::StrEq("12A"));
83#endif
84}
85
86TEST(SimpleStringBuilder, BufferOverrunInt) {
87 char sb_buf[4];
88 SimpleStringBuilder sb(sb_buf);
89 constexpr int num = -12345;
90#if RTC_DCHECK_IS_ON
91 EXPECT_DEATH(sb << num, "");
92#else
93 sb << num;
94 // If we run into the end of the buffer, resonable results are either that
95 // the append has no effect or that it's truncated at the point where the
96 // buffer ends.
97 EXPECT_THAT(sb.str(),
98 testing::AnyOf(testing::StrEq(""), testing::StrEq("-12")));
99#endif
100}
101
102TEST(SimpleStringBuilder, BufferOverrunDouble) {
103 char sb_buf[5];
104 SimpleStringBuilder sb(sb_buf);
105 constexpr double num = 123.456;
106#if RTC_DCHECK_IS_ON
107 EXPECT_DEATH(sb << num, "");
108#else
109 sb << num;
110 EXPECT_THAT(sb.str(),
111 testing::AnyOf(testing::StrEq(""), testing::StrEq("123.")));
112#endif
113}
114
115TEST(SimpleStringBuilder, BufferOverrunConstCharPAlreadyFull) {
116 char sb_buf[4];
117 SimpleStringBuilder sb(sb_buf);
118 sb << 123;
119 const char* const msg = "This is just too much";
120#if RTC_DCHECK_IS_ON
121 EXPECT_DEATH(sb << msg, "");
122#else
123 sb << msg;
124 EXPECT_THAT(sb.str(), testing::StrEq("123"));
125#endif
126}
127
128TEST(SimpleStringBuilder, BufferOverrunIntAlreadyFull) {
129 char sb_buf[4];
130 SimpleStringBuilder sb(sb_buf);
131 sb << "xyz";
132 constexpr int num = -12345;
133#if RTC_DCHECK_IS_ON
134 EXPECT_DEATH(sb << num, "");
135#else
136 sb << num;
137 EXPECT_THAT(sb.str(), testing::StrEq("xyz"));
138#endif
139}
140
141#endif
142
Jonas Olsson88e18482018-09-03 10:15:08 +0200143////////////////////////////////////////////////////////////////////////////////
144// StringBuilder.
145
146TEST(StringBuilder, Limit) {
147 StringBuilder sb;
148 EXPECT_EQ(0u, sb.str().size());
149
150 sb << "012345678";
151 EXPECT_EQ(sb.str(), "012345678");
152}
153
154TEST(StringBuilder, NumbersAndChars) {
155 StringBuilder sb;
156 sb << 1 << ":" << 2.1 << ":" << 2.2f << ":" << 78187493520ll << ":"
157 << 78187493520ul;
158 EXPECT_THAT(sb.str(),
159 testing::MatchesRegex("1:2.10*:2.20*:78187493520:78187493520"));
160}
161
162TEST(StringBuilder, Format) {
163 StringBuilder sb;
164 sb << "Here we go - ";
165 sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL);
166 EXPECT_EQ(sb.str(), "Here we go - This is a hex formatted value: 0xdeadbeef");
167}
168
169TEST(StringBuilder, StdString) {
170 StringBuilder sb;
171 std::string str = "does this work?";
172 sb << str;
173 EXPECT_EQ(str, sb.str());
174}
175
176TEST(StringBuilder, Release) {
177 StringBuilder sb;
178 std::string str =
179 "This string has to be of a moderate length, or we might "
180 "run into problems with small object optimizations.";
181 EXPECT_LT(sizeof(str), str.size());
182 sb << str;
183 EXPECT_EQ(str, sb.str());
184 const char* original_buffer = sb.str().c_str();
185 std::string moved = sb.Release();
186 EXPECT_TRUE(sb.str().empty());
187 EXPECT_EQ(str, moved);
188 EXPECT_EQ(original_buffer, moved.c_str());
189}
190
191TEST(StringBuilder, Reset) {
192 StringBuilder sb("abc");
193 sb << "def";
194 EXPECT_EQ("abcdef", sb.str());
195 sb.Clear();
196 EXPECT_TRUE(sb.str().empty());
197 sb << 123 << "!";
198 EXPECT_EQ("123!", sb.str());
199}
200
Tommifef05002018-02-27 13:51:08 +0100201} // namespace rtc