Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 1 | /* |
| 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" |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 14 | #include "rtc_base/stringutils.h" |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 15 | #include "test/gmock.h" |
| 16 | #include "test/gtest.h" |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | |
| 20 | TEST(SimpleStringBuilder, Limit) { |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 21 | char sb_buf[10]; |
| 22 | SimpleStringBuilder sb(sb_buf); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 23 | 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 | |
| 31 | TEST(SimpleStringBuilder, NumbersAndChars) { |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 32 | char sb_buf[100]; |
| 33 | SimpleStringBuilder sb(sb_buf); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 34 | 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 | |
| 39 | TEST(SimpleStringBuilder, Format) { |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 40 | char sb_buf[100]; |
| 41 | SimpleStringBuilder sb(sb_buf); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 42 | sb << "Here we go - "; |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 43 | sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 44 | EXPECT_EQ(0, |
| 45 | strcmp(sb.str(), |
| 46 | "Here we go - This is a hex formatted value: 0xdeadbeef")); |
| 47 | } |
| 48 | |
| 49 | TEST(SimpleStringBuilder, StdString) { |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 50 | char sb_buf[100]; |
| 51 | SimpleStringBuilder sb(sb_buf); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 52 | std::string str = "does this work?"; |
| 53 | sb << str; |
| 54 | EXPECT_EQ(str, sb.str()); |
| 55 | } |
| 56 | |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 57 | // 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 | |
| 61 | TEST(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 | |
| 73 | TEST(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 | |
| 86 | TEST(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 | |
| 102 | TEST(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 | |
| 115 | TEST(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 | |
| 128 | TEST(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 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 143 | } // namespace rtc |