1 /* 2 * Copyright (C) 2015 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 <algorithm> 18 #include <gtest/gtest.h> 19 #include <string> 20 #include <vector> 21 22 #include "util/StringPiece.h" 23 24 namespace aapt { 25 26 TEST(StringPieceTest, CompareNonNullTerminatedPiece) { 27 StringPiece a("hello world", 5); 28 StringPiece b("hello moon", 5); 29 EXPECT_EQ(a, b); 30 31 StringPiece16 a16(u"hello world", 5); 32 StringPiece16 b16(u"hello moon", 5); 33 EXPECT_EQ(a16, b16); 34 } 35 36 TEST(StringPieceTest, PiecesHaveCorrectSortOrder) { 37 std::u16string testing(u"testing"); 38 std::u16string banana(u"banana"); 39 std::u16string car(u"car"); 40 41 EXPECT_TRUE(StringPiece16(testing) > banana); 42 EXPECT_TRUE(StringPiece16(testing) > car); 43 EXPECT_TRUE(StringPiece16(banana) < testing); 44 EXPECT_TRUE(StringPiece16(banana) < car); 45 EXPECT_TRUE(StringPiece16(car) < testing); 46 EXPECT_TRUE(StringPiece16(car) > banana); 47 } 48 49 TEST(StringPieceTest, PiecesHaveCorrectSortOrderUtf8) { 50 std::string testing("testing"); 51 std::string banana("banana"); 52 std::string car("car"); 53 54 EXPECT_TRUE(StringPiece(testing) > banana); 55 EXPECT_TRUE(StringPiece(testing) > car); 56 EXPECT_TRUE(StringPiece(banana) < testing); 57 EXPECT_TRUE(StringPiece(banana) < car); 58 EXPECT_TRUE(StringPiece(car) < testing); 59 EXPECT_TRUE(StringPiece(car) > banana); 60 } 61 62 TEST(StringPieceTest, ContainsOtherStringPiece) { 63 StringPiece text("I am a leaf on the wind."); 64 StringPiece startNeedle("I am"); 65 StringPiece endNeedle("wind."); 66 StringPiece middleNeedle("leaf"); 67 StringPiece emptyNeedle(""); 68 StringPiece missingNeedle("soar"); 69 StringPiece longNeedle("This string is longer than the text."); 70 71 EXPECT_TRUE(text.contains(startNeedle)); 72 EXPECT_TRUE(text.contains(endNeedle)); 73 EXPECT_TRUE(text.contains(middleNeedle)); 74 EXPECT_TRUE(text.contains(emptyNeedle)); 75 EXPECT_FALSE(text.contains(missingNeedle)); 76 EXPECT_FALSE(text.contains(longNeedle)); 77 78 StringPiece16 text16(u"I am a leaf on the wind."); 79 StringPiece16 startNeedle16(u"I am"); 80 StringPiece16 endNeedle16(u"wind."); 81 StringPiece16 middleNeedle16(u"leaf"); 82 StringPiece16 emptyNeedle16(u""); 83 StringPiece16 missingNeedle16(u"soar"); 84 StringPiece16 longNeedle16(u"This string is longer than the text."); 85 86 EXPECT_TRUE(text16.contains(startNeedle16)); 87 EXPECT_TRUE(text16.contains(endNeedle16)); 88 EXPECT_TRUE(text16.contains(middleNeedle16)); 89 EXPECT_TRUE(text16.contains(emptyNeedle16)); 90 EXPECT_FALSE(text16.contains(missingNeedle16)); 91 EXPECT_FALSE(text16.contains(longNeedle16)); 92 } 93 94 } // namespace aapt 95