Make SimpleStringBuilder into a non-template
So that future CLs can de-inline its methods. We do this by asking the caller to allocate the buffer instead of having it as a data member. Bug: webrtc:8982 Change-Id: I246b0973e54510fdd880c3b6875336c31334d008 Reviewed-on: https://webrtc-review.googlesource.com/60000 Commit-Queue: Karl Wiberg <kwiberg@webrtc.org> Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org> Reviewed-by: Tommi <tommi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#22355}
This commit is contained in:
@ -11,13 +11,15 @@
|
||||
#include "rtc_base/strings/string_builder.h"
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/gunit.h"
|
||||
#include "rtc_base/stringutils.h"
|
||||
#include "test/gmock.h"
|
||||
#include "test/gtest.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
TEST(SimpleStringBuilder, Limit) {
|
||||
SimpleStringBuilder<10> sb;
|
||||
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
|
||||
@ -27,26 +29,115 @@ TEST(SimpleStringBuilder, Limit) {
|
||||
}
|
||||
|
||||
TEST(SimpleStringBuilder, NumbersAndChars) {
|
||||
SimpleStringBuilder<100> sb;
|
||||
char sb_buf[100];
|
||||
SimpleStringBuilder sb(sb_buf);
|
||||
sb << 1 << ':' << 2.1 << ":" << 2.2f << ':' << 78187493520ll << ':'
|
||||
<< 78187493520ul;
|
||||
EXPECT_EQ(0, strcmp(sb.str(), "1:2.100000:2.200000:78187493520:78187493520"));
|
||||
}
|
||||
|
||||
TEST(SimpleStringBuilder, Format) {
|
||||
SimpleStringBuilder<100> sb;
|
||||
char sb_buf[100];
|
||||
SimpleStringBuilder sb(sb_buf);
|
||||
sb << "Here we go - ";
|
||||
sb.AppendFormat("This is a hex formatted value: 0x%08x", 3735928559);
|
||||
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) {
|
||||
SimpleStringBuilder<100> sb;
|
||||
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(SimpleStringBuilder, 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(SimpleStringBuilder, 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(SimpleStringBuilder, 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(SimpleStringBuilder, 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(SimpleStringBuilder, 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(SimpleStringBuilder, 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
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
Reference in New Issue
Block a user