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 
57 }  // namespace
58 }  // namespace libtextclassifier3
59