Home | History | Annotate | Download | only in util
      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 
     19 #include <grpcpp/support/string_ref.h>
     20 
     21 #include <string.h>
     22 
     23 #include <gtest/gtest.h>
     24 
     25 namespace grpc {
     26 namespace {
     27 
     28 const char kTestString[] = "blah";
     29 const char kTestStringWithEmbeddedNull[] = "blah\0foo";
     30 const size_t kTestStringWithEmbeddedNullLength = 8;
     31 const char kTestUnrelatedString[] = "foo";
     32 
     33 class StringRefTest : public ::testing::Test {};
     34 
     35 TEST_F(StringRefTest, Empty) {
     36   string_ref s;
     37   EXPECT_EQ(0U, s.length());
     38   EXPECT_EQ(nullptr, s.data());
     39 }
     40 
     41 TEST_F(StringRefTest, FromCString) {
     42   string_ref s(kTestString);
     43   EXPECT_EQ(strlen(kTestString), s.length());
     44   EXPECT_EQ(kTestString, s.data());
     45 }
     46 
     47 TEST_F(StringRefTest, FromCStringWithLength) {
     48   string_ref s(kTestString, 2);
     49   EXPECT_EQ(2U, s.length());
     50   EXPECT_EQ(kTestString, s.data());
     51 }
     52 
     53 TEST_F(StringRefTest, FromString) {
     54   string copy(kTestString);
     55   string_ref s(copy);
     56   EXPECT_EQ(copy.data(), s.data());
     57   EXPECT_EQ(copy.length(), s.length());
     58 }
     59 
     60 TEST_F(StringRefTest, CopyConstructor) {
     61   string_ref s1(kTestString);
     62   ;
     63   const string_ref& s2(s1);
     64   EXPECT_EQ(s1.length(), s2.length());
     65   EXPECT_EQ(s1.data(), s2.data());
     66 }
     67 
     68 TEST_F(StringRefTest, FromStringWithEmbeddedNull) {
     69   string copy(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
     70   string_ref s(copy);
     71   EXPECT_EQ(copy.data(), s.data());
     72   EXPECT_EQ(copy.length(), s.length());
     73   EXPECT_EQ(kTestStringWithEmbeddedNullLength, s.length());
     74 }
     75 
     76 TEST_F(StringRefTest, Assignment) {
     77   string_ref s1(kTestString);
     78   ;
     79   string_ref s2;
     80   EXPECT_EQ(nullptr, s2.data());
     81   s2 = s1;
     82   EXPECT_EQ(s1.length(), s2.length());
     83   EXPECT_EQ(s1.data(), s2.data());
     84 }
     85 
     86 TEST_F(StringRefTest, Iterator) {
     87   string_ref s(kTestString);
     88   size_t i = 0;
     89   for (auto it = s.cbegin(); it != s.cend(); ++it) {
     90     auto val = kTestString[i++];
     91     EXPECT_EQ(val, *it);
     92   }
     93   EXPECT_EQ(strlen(kTestString), i);
     94 }
     95 
     96 TEST_F(StringRefTest, ReverseIterator) {
     97   string_ref s(kTestString);
     98   size_t i = strlen(kTestString);
     99   for (auto rit = s.crbegin(); rit != s.crend(); ++rit) {
    100     auto val = kTestString[--i];
    101     EXPECT_EQ(val, *rit);
    102   }
    103   EXPECT_EQ(0U, i);
    104 }
    105 
    106 TEST_F(StringRefTest, Capacity) {
    107   string_ref empty;
    108   EXPECT_EQ(0U, empty.length());
    109   EXPECT_EQ(0U, empty.size());
    110   EXPECT_EQ(0U, empty.max_size());
    111   EXPECT_TRUE(empty.empty());
    112 
    113   string_ref s(kTestString);
    114   EXPECT_EQ(strlen(kTestString), s.length());
    115   EXPECT_EQ(s.length(), s.size());
    116   EXPECT_EQ(s.max_size(), s.length());
    117   EXPECT_FALSE(s.empty());
    118 }
    119 
    120 TEST_F(StringRefTest, Compare) {
    121   string_ref s1(kTestString);
    122   string s1_copy(kTestString);
    123   string_ref s2(kTestUnrelatedString);
    124   string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
    125   EXPECT_EQ(0, s1.compare(s1_copy));
    126   EXPECT_NE(0, s1.compare(s2));
    127   EXPECT_NE(0, s1.compare(s3));
    128 }
    129 
    130 TEST_F(StringRefTest, StartsWith) {
    131   string_ref s1(kTestString);
    132   string_ref s2(kTestUnrelatedString);
    133   string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
    134   EXPECT_TRUE(s1.starts_with(s1));
    135   EXPECT_FALSE(s1.starts_with(s2));
    136   EXPECT_FALSE(s2.starts_with(s1));
    137   EXPECT_FALSE(s1.starts_with(s3));
    138   EXPECT_TRUE(s3.starts_with(s1));
    139 }
    140 
    141 TEST_F(StringRefTest, Endswith) {
    142   string_ref s1(kTestString);
    143   string_ref s2(kTestUnrelatedString);
    144   string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
    145   EXPECT_TRUE(s1.ends_with(s1));
    146   EXPECT_FALSE(s1.ends_with(s2));
    147   EXPECT_FALSE(s2.ends_with(s1));
    148   EXPECT_FALSE(s2.ends_with(s3));
    149   EXPECT_TRUE(s3.ends_with(s2));
    150 }
    151 
    152 TEST_F(StringRefTest, Find) {
    153   string_ref s1(kTestString);
    154   string_ref s2(kTestUnrelatedString);
    155   string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
    156   EXPECT_EQ(0U, s1.find(s1));
    157   EXPECT_EQ(0U, s2.find(s2));
    158   EXPECT_EQ(0U, s3.find(s3));
    159   EXPECT_EQ(string_ref::npos, s1.find(s2));
    160   EXPECT_EQ(string_ref::npos, s2.find(s1));
    161   EXPECT_EQ(string_ref::npos, s1.find(s3));
    162   EXPECT_EQ(0U, s3.find(s1));
    163   EXPECT_EQ(5U, s3.find(s2));
    164   EXPECT_EQ(string_ref::npos, s1.find('z'));
    165   EXPECT_EQ(1U, s2.find('o'));
    166 }
    167 
    168 TEST_F(StringRefTest, SubString) {
    169   string_ref s(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
    170   string_ref sub1 = s.substr(0, 4);
    171   EXPECT_EQ(string_ref(kTestString), sub1);
    172   string_ref sub2 = s.substr(5);
    173   EXPECT_EQ(string_ref(kTestUnrelatedString), sub2);
    174 }
    175 
    176 TEST_F(StringRefTest, ComparisonOperators) {
    177   string_ref s1(kTestString);
    178   string_ref s2(kTestUnrelatedString);
    179   string_ref s3(kTestStringWithEmbeddedNull, kTestStringWithEmbeddedNullLength);
    180   EXPECT_EQ(s1, s1);
    181   EXPECT_EQ(s2, s2);
    182   EXPECT_EQ(s3, s3);
    183   EXPECT_GE(s1, s1);
    184   EXPECT_GE(s2, s2);
    185   EXPECT_GE(s3, s3);
    186   EXPECT_LE(s1, s1);
    187   EXPECT_LE(s2, s2);
    188   EXPECT_LE(s3, s3);
    189   EXPECT_NE(s1, s2);
    190   EXPECT_NE(s1, s3);
    191   EXPECT_NE(s2, s3);
    192   EXPECT_GT(s3, s1);
    193   EXPECT_LT(s1, s3);
    194 }
    195 
    196 }  // namespace
    197 }  // namespace grpc
    198 
    199 int main(int argc, char** argv) {
    200   ::testing::InitGoogleTest(&argc, argv);
    201   return RUN_ALL_TESTS();
    202 }
    203