Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 17 | #include "util/StringPiece.h" |
| 18 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 23 | #include "test/Test.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | TEST(StringPieceTest, CompareNonNullTerminatedPiece) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 28 | StringPiece a("hello world", 5); |
| 29 | StringPiece b("hello moon", 5); |
| 30 | EXPECT_EQ(a, b); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 32 | StringPiece16 a16(u"hello world", 5); |
| 33 | StringPiece16 b16(u"hello moon", 5); |
| 34 | EXPECT_EQ(a16, b16); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | TEST(StringPieceTest, PiecesHaveCorrectSortOrder) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 38 | std::string testing("testing"); |
| 39 | std::string banana("banana"); |
| 40 | std::string car("car"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 42 | EXPECT_TRUE(StringPiece(testing) > banana); |
| 43 | EXPECT_TRUE(StringPiece(testing) > car); |
| 44 | EXPECT_TRUE(StringPiece(banana) < testing); |
| 45 | EXPECT_TRUE(StringPiece(banana) < car); |
| 46 | EXPECT_TRUE(StringPiece(car) < testing); |
| 47 | EXPECT_TRUE(StringPiece(car) > banana); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | TEST(StringPieceTest, PiecesHaveCorrectSortOrderUtf8) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 51 | std::string testing("testing"); |
| 52 | std::string banana("banana"); |
| 53 | std::string car("car"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 55 | EXPECT_TRUE(StringPiece(testing) > banana); |
| 56 | EXPECT_TRUE(StringPiece(testing) > car); |
| 57 | EXPECT_TRUE(StringPiece(banana) < testing); |
| 58 | EXPECT_TRUE(StringPiece(banana) < car); |
| 59 | EXPECT_TRUE(StringPiece(car) < testing); |
| 60 | EXPECT_TRUE(StringPiece(car) > banana); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 63 | TEST(StringPieceTest, ContainsOtherStringPiece) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 64 | StringPiece text("I am a leaf on the wind."); |
| 65 | StringPiece start_needle("I am"); |
| 66 | StringPiece end_needle("wind."); |
| 67 | StringPiece middle_needle("leaf"); |
| 68 | StringPiece empty_needle(""); |
| 69 | StringPiece missing_needle("soar"); |
| 70 | StringPiece long_needle("This string is longer than the text."); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 71 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 72 | EXPECT_TRUE(text.contains(start_needle)); |
| 73 | EXPECT_TRUE(text.contains(end_needle)); |
| 74 | EXPECT_TRUE(text.contains(middle_needle)); |
| 75 | EXPECT_TRUE(text.contains(empty_needle)); |
| 76 | EXPECT_FALSE(text.contains(missing_needle)); |
| 77 | EXPECT_FALSE(text.contains(long_needle)); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 78 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 79 | StringPiece16 text16(u"I am a leaf on the wind."); |
| 80 | StringPiece16 start_needle16(u"I am"); |
| 81 | StringPiece16 end_needle16(u"wind."); |
| 82 | StringPiece16 middle_needle16(u"leaf"); |
| 83 | StringPiece16 empty_needle16(u""); |
| 84 | StringPiece16 missing_needle16(u"soar"); |
| 85 | StringPiece16 long_needle16(u"This string is longer than the text."); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 87 | EXPECT_TRUE(text16.contains(start_needle16)); |
| 88 | EXPECT_TRUE(text16.contains(end_needle16)); |
| 89 | EXPECT_TRUE(text16.contains(middle_needle16)); |
| 90 | EXPECT_TRUE(text16.contains(empty_needle16)); |
| 91 | EXPECT_FALSE(text16.contains(missing_needle16)); |
| 92 | EXPECT_FALSE(text16.contains(long_needle16)); |
Michael Wright | feaf99f | 2016-05-06 17:16:06 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 95 | } // namespace aapt |