• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 
20 #include "utils/strings/stringpiece.h"
21 
22 namespace libtextclassifier3 {
23 namespace {
24 
TEST(StringPieceTest,EndsWith)25 TEST(StringPieceTest, EndsWith) {
26   EXPECT_TRUE(EndsWith("hello there!", "there!"));
27   EXPECT_TRUE(EndsWith("hello there!", "!"));
28   EXPECT_FALSE(EndsWith("hello there!", "there"));
29   EXPECT_FALSE(EndsWith("hello there!", " hello there!"));
30   EXPECT_TRUE(EndsWith("hello there!", ""));
31   EXPECT_FALSE(EndsWith("", "hello there!"));
32 }
33 
TEST(StringPieceTest,StartsWith)34 TEST(StringPieceTest, StartsWith) {
35   EXPECT_TRUE(StartsWith("hello there!", "hello"));
36   EXPECT_TRUE(StartsWith("hello there!", "hello "));
37   EXPECT_FALSE(StartsWith("hello there!", "there!"));
38   EXPECT_FALSE(StartsWith("hello there!", " hello there! "));
39   EXPECT_TRUE(StartsWith("hello there!", ""));
40   EXPECT_FALSE(StartsWith("", "hello there!"));
41 }
42 
TEST(StringPieceTest,ConsumePrefix)43 TEST(StringPieceTest, ConsumePrefix) {
44   StringPiece str("hello there!");
45   EXPECT_TRUE(ConsumePrefix(&str, "hello "));
46   EXPECT_EQ(str.ToString(), "there!");
47   EXPECT_TRUE(ConsumePrefix(&str, "there"));
48   EXPECT_EQ(str.ToString(), "!");
49   EXPECT_FALSE(ConsumePrefix(&str, "!!"));
50   EXPECT_TRUE(ConsumePrefix(&str, ""));
51   EXPECT_TRUE(ConsumePrefix(&str, "!"));
52   EXPECT_EQ(str.ToString(), "");
53   EXPECT_TRUE(ConsumePrefix(&str, ""));
54   EXPECT_FALSE(ConsumePrefix(&str, "!"));
55 }
56 
TEST(StringPieceTest,ConsumeSuffix)57 TEST(StringPieceTest, ConsumeSuffix) {
58   StringPiece str("hello there!");
59   EXPECT_TRUE(ConsumeSuffix(&str, "!"));
60   EXPECT_EQ(str.ToString(), "hello there");
61   EXPECT_TRUE(ConsumeSuffix(&str, " there"));
62   EXPECT_EQ(str.ToString(), "hello");
63   EXPECT_FALSE(ConsumeSuffix(&str, "!!"));
64   EXPECT_TRUE(ConsumeSuffix(&str, ""));
65   EXPECT_TRUE(ConsumeSuffix(&str, "hello"));
66   EXPECT_EQ(str.ToString(), "");
67   EXPECT_TRUE(ConsumeSuffix(&str, ""));
68   EXPECT_FALSE(ConsumeSuffix(&str, "!"));
69 }
70 
TEST(StringPieceTest,Find)71 TEST(StringPieceTest, Find) {
72   StringPiece str("<hello there!>");
73   EXPECT_EQ(str.find('<'), 0);
74   EXPECT_EQ(str.find('>'), str.length() - 1);
75   EXPECT_EQ(str.find('?'), StringPiece::npos);
76   EXPECT_EQ(str.find('<', str.length() - 1), StringPiece::npos);
77   EXPECT_EQ(str.find('<', 0), 0);
78   EXPECT_EQ(str.find('>', str.length() - 1), str.length() - 1);
79 }
80 
TEST(StringPieceTest,FindStringPiece)81 TEST(StringPieceTest, FindStringPiece) {
82   StringPiece str("<foo bar baz!>");
83   EXPECT_EQ(str.find("foo"), 1);
84   EXPECT_EQ(str.find("bar"), 5);
85   EXPECT_EQ(str.find("baz"), 9);
86   EXPECT_EQ(str.find("qux"), StringPiece::npos);
87   EXPECT_EQ(str.find("?"), StringPiece::npos);
88   EXPECT_EQ(str.find(">"), str.length() - 1);
89   EXPECT_EQ(str.find("<", str.length() - 1), StringPiece::npos);
90   EXPECT_EQ(str.find("<", 0), 0);
91   EXPECT_EQ(str.find(">", str.length() - 1), str.length() - 1);
92 }
93 
94 }  // namespace
95 }  // namespace libtextclassifier3
96