string_ref_test.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <string.h>
  19. #include <gtest/gtest.h>
  20. #include <grpcpp/support/string_ref.h>
  21. #include "test/core/util/test_config.h"
  22. namespace grpc {
  23. namespace {
  24. const char kTestString[] = "blah";
  25. const char kTestStringWithEmbeddedNull[] = "blah\0foo";
  26. const size_t kTestStringWithEmbeddedNullLength = 8;
  27. const char kTestUnrelatedString[] = "foo";
  28. class StringRefTest : public ::testing::Test {};
  29. TEST_F(StringRefTest, Empty) {
  30. string_ref s;
  31. EXPECT_EQ(0U, s.length());
  32. EXPECT_EQ(nullptr, s.data());
  33. }
  34. TEST_F(StringRefTest, FromCString) {
  35. string_ref s(kTestString);
  36. EXPECT_EQ(strlen(kTestString), s.length());
  37. EXPECT_EQ(kTestString, s.data());
  38. }
  39. TEST_F(StringRefTest, FromCStringWithLength) {
  40. string_ref s(kTestString, 2);
  41. EXPECT_EQ(2U, s.length());
  42. EXPECT_EQ(kTestString, s.data());
  43. }
  44. TEST_F(StringRefTest, FromString) {
  45. string copy(kTestString);
  46. string_ref s(copy);
  47. EXPECT_EQ(copy.data(), s.data());
  48. EXPECT_EQ(copy.length(), s.length());
  49. }
  50. TEST_F(StringRefTest, CopyConstructor) {
  51. string_ref s1(kTestString);
  52. ;
  53. const string_ref& s2(s1);
  54. EXPECT_EQ(s1.length(), s2.length());
  55. EXPECT_EQ(s1.data(), s2.data());
  56. }
  57. TEST_F(StringRefTest, FromStringWithEmbeddedNull) {
  58. string copy(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
  59. string_ref s(copy);
  60. EXPECT_EQ(copy.data(), s.data());
  61. EXPECT_EQ(copy.length(), s.length());
  62. EXPECT_EQ(kTestStringWithEmbeddedNullLength, s.length());
  63. }
  64. TEST_F(StringRefTest, Assignment) {
  65. string_ref s1(kTestString);
  66. ;
  67. string_ref s2;
  68. EXPECT_EQ(nullptr, s2.data());
  69. s2 = s1;
  70. EXPECT_EQ(s1.length(), s2.length());
  71. EXPECT_EQ(s1.data(), s2.data());
  72. }
  73. TEST_F(StringRefTest, Iterator) {
  74. string_ref s(kTestString);
  75. size_t i = 0;
  76. for (auto it = s.cbegin(); it != s.cend(); ++it) {
  77. auto val = kTestString[i++];
  78. EXPECT_EQ(val, *it);
  79. }
  80. EXPECT_EQ(strlen(kTestString), i);
  81. }
  82. TEST_F(StringRefTest, ReverseIterator) {
  83. string_ref s(kTestString);
  84. size_t i = strlen(kTestString);
  85. for (auto rit = s.crbegin(); rit != s.crend(); ++rit) {
  86. auto val = kTestString[--i];
  87. EXPECT_EQ(val, *rit);
  88. }
  89. EXPECT_EQ(0U, i);
  90. }
  91. TEST_F(StringRefTest, Capacity) {
  92. string_ref empty;
  93. EXPECT_EQ(0U, empty.length());
  94. EXPECT_EQ(0U, empty.size());
  95. EXPECT_EQ(0U, empty.max_size());
  96. EXPECT_TRUE(empty.empty());
  97. string_ref s(kTestString);
  98. EXPECT_EQ(strlen(kTestString), s.length());
  99. EXPECT_EQ(s.length(), s.size());
  100. EXPECT_EQ(s.max_size(), s.length());
  101. EXPECT_FALSE(s.empty());
  102. }
  103. TEST_F(StringRefTest, Compare) {
  104. string_ref s1(kTestString);
  105. string s1_copy(kTestString);
  106. string_ref s2(kTestUnrelatedString);
  107. string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
  108. EXPECT_EQ(0, s1.compare(s1_copy));
  109. EXPECT_NE(0, s1.compare(s2));
  110. EXPECT_NE(0, s1.compare(s3));
  111. }
  112. TEST_F(StringRefTest, StartsWith) {
  113. string_ref s1(kTestString);
  114. string_ref s2(kTestUnrelatedString);
  115. string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
  116. EXPECT_TRUE(s1.starts_with(s1));
  117. EXPECT_FALSE(s1.starts_with(s2));
  118. EXPECT_FALSE(s2.starts_with(s1));
  119. EXPECT_FALSE(s1.starts_with(s3));
  120. EXPECT_TRUE(s3.starts_with(s1));
  121. }
  122. TEST_F(StringRefTest, Endswith) {
  123. string_ref s1(kTestString);
  124. string_ref s2(kTestUnrelatedString);
  125. string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
  126. EXPECT_TRUE(s1.ends_with(s1));
  127. EXPECT_FALSE(s1.ends_with(s2));
  128. EXPECT_FALSE(s2.ends_with(s1));
  129. EXPECT_FALSE(s2.ends_with(s3));
  130. EXPECT_TRUE(s3.ends_with(s2));
  131. }
  132. TEST_F(StringRefTest, Find) {
  133. string_ref s1(kTestString);
  134. string_ref s2(kTestUnrelatedString);
  135. string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
  136. EXPECT_EQ(0U, s1.find(s1));
  137. EXPECT_EQ(0U, s2.find(s2));
  138. EXPECT_EQ(0U, s3.find(s3));
  139. EXPECT_EQ(string_ref::npos, s1.find(s2));
  140. EXPECT_EQ(string_ref::npos, s2.find(s1));
  141. EXPECT_EQ(string_ref::npos, s1.find(s3));
  142. EXPECT_EQ(0U, s3.find(s1));
  143. EXPECT_EQ(5U, s3.find(s2));
  144. EXPECT_EQ(string_ref::npos, s1.find('z'));
  145. EXPECT_EQ(1U, s2.find('o'));
  146. }
  147. TEST_F(StringRefTest, SubString) {
  148. string_ref s(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
  149. string_ref sub1 = s.substr(0, 4);
  150. EXPECT_EQ(string_ref(kTestString), sub1);
  151. string_ref sub2 = s.substr(5);
  152. EXPECT_EQ(string_ref(kTestUnrelatedString), sub2);
  153. }
  154. TEST_F(StringRefTest, ComparisonOperators) {
  155. string_ref s1(kTestString);
  156. string_ref s2(kTestUnrelatedString);
  157. string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
  158. EXPECT_EQ(s1, s1);
  159. EXPECT_EQ(s2, s2);
  160. EXPECT_EQ(s3, s3);
  161. EXPECT_GE(s1, s1);
  162. EXPECT_GE(s2, s2);
  163. EXPECT_GE(s3, s3);
  164. EXPECT_LE(s1, s1);
  165. EXPECT_LE(s2, s2);
  166. EXPECT_LE(s3, s3);
  167. EXPECT_NE(s1, s2);
  168. EXPECT_NE(s1, s3);
  169. EXPECT_NE(s2, s3);
  170. EXPECT_GT(s3, s1);
  171. EXPECT_LT(s1, s3);
  172. }
  173. } // namespace
  174. } // namespace grpc
  175. int main(int argc, char** argv) {
  176. grpc::testing::TestEnvironment env(argc, argv);
  177. ::testing::InitGoogleTest(&argc, argv);
  178. return RUN_ALL_TESTS();
  179. }