OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <grpc++/support/string_ref.h> |
| 35 |
| 36 #include <string.h> |
| 37 |
| 38 #include <gtest/gtest.h> |
| 39 |
| 40 namespace grpc { |
| 41 namespace { |
| 42 |
| 43 const char kTestString[] = "blah"; |
| 44 const char kTestStringWithEmbeddedNull[] = "blah\0foo"; |
| 45 const size_t kTestStringWithEmbeddedNullLength = 8; |
| 46 const char kTestUnrelatedString[] = "foo"; |
| 47 |
| 48 class StringRefTest : public ::testing::Test {}; |
| 49 |
| 50 TEST_F(StringRefTest, Empty) { |
| 51 string_ref s; |
| 52 EXPECT_EQ(0U, s.length()); |
| 53 EXPECT_EQ(nullptr, s.data()); |
| 54 } |
| 55 |
| 56 TEST_F(StringRefTest, FromCString) { |
| 57 string_ref s(kTestString); |
| 58 EXPECT_EQ(strlen(kTestString), s.length()); |
| 59 EXPECT_EQ(kTestString, s.data()); |
| 60 } |
| 61 |
| 62 TEST_F(StringRefTest, FromCStringWithLength) { |
| 63 string_ref s(kTestString, 2); |
| 64 EXPECT_EQ(2U, s.length()); |
| 65 EXPECT_EQ(kTestString, s.data()); |
| 66 } |
| 67 |
| 68 TEST_F(StringRefTest, FromString) { |
| 69 string copy(kTestString); |
| 70 string_ref s(copy); |
| 71 EXPECT_EQ(copy.data(), s.data()); |
| 72 EXPECT_EQ(copy.length(), s.length()); |
| 73 } |
| 74 |
| 75 TEST_F(StringRefTest, CopyConstructor) { |
| 76 string_ref s1(kTestString); |
| 77 ; |
| 78 string_ref s2(s1); |
| 79 EXPECT_EQ(s1.length(), s2.length()); |
| 80 EXPECT_EQ(s1.data(), s2.data()); |
| 81 } |
| 82 |
| 83 TEST_F(StringRefTest, FromStringWithEmbeddedNull) { |
| 84 string copy(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength); |
| 85 string_ref s(copy); |
| 86 EXPECT_EQ(copy.data(), s.data()); |
| 87 EXPECT_EQ(copy.length(), s.length()); |
| 88 EXPECT_EQ(kTestStringWithEmbeddedNullLength, s.length()); |
| 89 } |
| 90 |
| 91 TEST_F(StringRefTest, Assignment) { |
| 92 string_ref s1(kTestString); |
| 93 ; |
| 94 string_ref s2; |
| 95 EXPECT_EQ(nullptr, s2.data()); |
| 96 s2 = s1; |
| 97 EXPECT_EQ(s1.length(), s2.length()); |
| 98 EXPECT_EQ(s1.data(), s2.data()); |
| 99 } |
| 100 |
| 101 TEST_F(StringRefTest, Iterator) { |
| 102 string_ref s(kTestString); |
| 103 size_t i = 0; |
| 104 for (auto it = s.cbegin(); it != s.cend(); ++it) { |
| 105 auto val = kTestString[i++]; |
| 106 EXPECT_EQ(val, *it); |
| 107 } |
| 108 EXPECT_EQ(strlen(kTestString), i); |
| 109 } |
| 110 |
| 111 TEST_F(StringRefTest, ReverseIterator) { |
| 112 string_ref s(kTestString); |
| 113 size_t i = strlen(kTestString); |
| 114 for (auto rit = s.crbegin(); rit != s.crend(); ++rit) { |
| 115 auto val = kTestString[--i]; |
| 116 EXPECT_EQ(val, *rit); |
| 117 } |
| 118 EXPECT_EQ(0U, i); |
| 119 } |
| 120 |
| 121 TEST_F(StringRefTest, Capacity) { |
| 122 string_ref empty; |
| 123 EXPECT_EQ(0U, empty.length()); |
| 124 EXPECT_EQ(0U, empty.size()); |
| 125 EXPECT_EQ(0U, empty.max_size()); |
| 126 EXPECT_TRUE(empty.empty()); |
| 127 |
| 128 string_ref s(kTestString); |
| 129 EXPECT_EQ(strlen(kTestString), s.length()); |
| 130 EXPECT_EQ(s.length(), s.size()); |
| 131 EXPECT_EQ(s.max_size(), s.length()); |
| 132 EXPECT_FALSE(s.empty()); |
| 133 } |
| 134 |
| 135 TEST_F(StringRefTest, Compare) { |
| 136 string_ref s1(kTestString); |
| 137 string s1_copy(kTestString); |
| 138 string_ref s2(kTestUnrelatedString); |
| 139 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength); |
| 140 EXPECT_EQ(0, s1.compare(s1_copy)); |
| 141 EXPECT_NE(0, s1.compare(s2)); |
| 142 EXPECT_NE(0, s1.compare(s3)); |
| 143 } |
| 144 |
| 145 TEST_F(StringRefTest, StartsWith) { |
| 146 string_ref s1(kTestString); |
| 147 string_ref s2(kTestUnrelatedString); |
| 148 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength); |
| 149 EXPECT_TRUE(s1.starts_with(s1)); |
| 150 EXPECT_FALSE(s1.starts_with(s2)); |
| 151 EXPECT_FALSE(s2.starts_with(s1)); |
| 152 EXPECT_FALSE(s1.starts_with(s3)); |
| 153 EXPECT_TRUE(s3.starts_with(s1)); |
| 154 } |
| 155 |
| 156 TEST_F(StringRefTest, Endswith) { |
| 157 string_ref s1(kTestString); |
| 158 string_ref s2(kTestUnrelatedString); |
| 159 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength); |
| 160 EXPECT_TRUE(s1.ends_with(s1)); |
| 161 EXPECT_FALSE(s1.ends_with(s2)); |
| 162 EXPECT_FALSE(s2.ends_with(s1)); |
| 163 EXPECT_FALSE(s2.ends_with(s3)); |
| 164 EXPECT_TRUE(s3.ends_with(s2)); |
| 165 } |
| 166 |
| 167 TEST_F(StringRefTest, Find) { |
| 168 string_ref s1(kTestString); |
| 169 string_ref s2(kTestUnrelatedString); |
| 170 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength); |
| 171 EXPECT_EQ(0U, s1.find(s1)); |
| 172 EXPECT_EQ(0U, s2.find(s2)); |
| 173 EXPECT_EQ(0U, s3.find(s3)); |
| 174 EXPECT_EQ(string_ref::npos, s1.find(s2)); |
| 175 EXPECT_EQ(string_ref::npos, s2.find(s1)); |
| 176 EXPECT_EQ(string_ref::npos, s1.find(s3)); |
| 177 EXPECT_EQ(0U, s3.find(s1)); |
| 178 EXPECT_EQ(5U, s3.find(s2)); |
| 179 EXPECT_EQ(string_ref::npos, s1.find('z')); |
| 180 EXPECT_EQ(1U, s2.find('o')); |
| 181 } |
| 182 |
| 183 TEST_F(StringRefTest, SubString) { |
| 184 string_ref s(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength); |
| 185 string_ref sub1 = s.substr(0, 4); |
| 186 EXPECT_EQ(string_ref(kTestString), sub1); |
| 187 string_ref sub2 = s.substr(5); |
| 188 EXPECT_EQ(string_ref(kTestUnrelatedString), sub2); |
| 189 } |
| 190 |
| 191 TEST_F(StringRefTest, ComparisonOperators) { |
| 192 string_ref s1(kTestString); |
| 193 string_ref s2(kTestUnrelatedString); |
| 194 string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength); |
| 195 EXPECT_EQ(s1, s1); |
| 196 EXPECT_EQ(s2, s2); |
| 197 EXPECT_EQ(s3, s3); |
| 198 EXPECT_GE(s1, s1); |
| 199 EXPECT_GE(s2, s2); |
| 200 EXPECT_GE(s3, s3); |
| 201 EXPECT_LE(s1, s1); |
| 202 EXPECT_LE(s2, s2); |
| 203 EXPECT_LE(s3, s3); |
| 204 EXPECT_NE(s1, s2); |
| 205 EXPECT_NE(s1, s3); |
| 206 EXPECT_NE(s2, s3); |
| 207 EXPECT_GT(s3, s1); |
| 208 EXPECT_LT(s1, s3); |
| 209 } |
| 210 |
| 211 } // namespace |
| 212 } // namespace grpc |
| 213 |
| 214 int main(int argc, char** argv) { |
| 215 ::testing::InitGoogleTest(&argc, argv); |
| 216 return RUN_ALL_TESTS(); |
| 217 } |
OLD | NEW |