blob: 8d6312f9474c662ee442076121ce667e54ab5846 [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;
36 EXPECT_EQ(0, strcmp(sb.str(), "1:2.100000:2.200000:78187493520:78187493520"));
37}
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
Tommifef05002018-02-27 13:51:08 +0100143} // namespace rtc