LCOV - code coverage report
Current view: top level - test/cpp/util - string_ref_test.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 143 143 100.0 %
Date: 2015-10-10 Functions: 69 70 98.6 %

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

Generated by: LCOV version 1.10