Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 17 | #include "strutil.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "string_piece.h" |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 25 | #include "testutil.h" |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 26 | |
| 27 | using namespace std; |
| 28 | |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 29 | namespace { |
| 30 | |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 31 | void TestWordScanner() { |
| 32 | vector<StringPiece> ss; |
Shinichiro Hamaji | 096bb4e | 2016-02-03 13:24:34 +0900 | [diff] [blame^] | 33 | for (StringPiece tok : WordScanner("foo bar baz hogeeeeeeeeeeeeeeee")) { |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 34 | ss.push_back(tok); |
| 35 | } |
Shinichiro Hamaji | 096bb4e | 2016-02-03 13:24:34 +0900 | [diff] [blame^] | 36 | assert(ss.size() == 4LU); |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 37 | ASSERT_EQ(ss[0], "foo"); |
| 38 | ASSERT_EQ(ss[1], "bar"); |
| 39 | ASSERT_EQ(ss[2], "baz"); |
Shinichiro Hamaji | 096bb4e | 2016-02-03 13:24:34 +0900 | [diff] [blame^] | 40 | ASSERT_EQ(ss[3], "hogeeeeeeeeeeeeeeee"); |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 41 | } |
| 42 | |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 43 | void TestHasPrefix() { |
| 44 | assert(HasPrefix("foo", "foo")); |
| 45 | assert(HasPrefix("foo", "fo")); |
| 46 | assert(HasPrefix("foo", "")); |
| 47 | assert(!HasPrefix("foo", "fooo")); |
| 48 | } |
| 49 | |
| 50 | void TestHasSuffix() { |
| 51 | assert(HasSuffix("bar", "bar")); |
| 52 | assert(HasSuffix("bar", "ar")); |
| 53 | assert(HasSuffix("bar", "")); |
| 54 | assert(!HasSuffix("bar", "bbar")); |
| 55 | } |
| 56 | |
| 57 | string SubstPattern(StringPiece str, StringPiece pat, StringPiece subst) { |
| 58 | string r; |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 59 | Pattern(pat).AppendSubst(str, subst, &r); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 60 | return r; |
| 61 | } |
| 62 | |
| 63 | void TestSubstPattern() { |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 64 | ASSERT_EQ(SubstPattern("x.c", "%.c", "%.o"), "x.o"); |
| 65 | ASSERT_EQ(SubstPattern("c.x", "c.%", "o.%"), "o.x"); |
| 66 | ASSERT_EQ(SubstPattern("x.c.c", "%.c", "%.o"), "x.c.o"); |
| 67 | ASSERT_EQ(SubstPattern("x.x y.c", "%.c", "%.o"), "x.x y.o"); |
| 68 | ASSERT_EQ(SubstPattern("x.%.c", "%.%.c", "OK"), "OK"); |
| 69 | ASSERT_EQ(SubstPattern("x.c", "x.c", "OK"), "OK"); |
| 70 | ASSERT_EQ(SubstPattern("x.c.c", "x.c", "XX"), "x.c.c"); |
| 71 | ASSERT_EQ(SubstPattern("x.x.c", "x.c", "XX"), "x.x.c"); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Shinichiro Hamaji | 2b674c4 | 2015-06-24 17:49:35 +0900 | [diff] [blame] | 74 | void TestNoLineBreak() { |
| 75 | assert(NoLineBreak("a\nb") == "a\\nb"); |
| 76 | assert(NoLineBreak("a\nb\nc") == "a\\nb\\nc"); |
| 77 | } |
| 78 | |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 79 | void TestHasWord() { |
| 80 | assert(HasWord("foo bar baz", "bar")); |
| 81 | assert(HasWord("foo bar baz", "foo")); |
| 82 | assert(HasWord("foo bar baz", "baz")); |
| 83 | assert(!HasWord("foo bar baz", "oo")); |
| 84 | assert(!HasWord("foo bar baz", "ar")); |
| 85 | assert(!HasWord("foo bar baz", "ba")); |
| 86 | assert(!HasWord("foo bar baz", "az")); |
| 87 | assert(!HasWord("foo bar baz", "ba")); |
| 88 | assert(!HasWord("foo bar baz", "fo")); |
| 89 | } |
| 90 | |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 91 | static string NormalizePath(string s) { |
| 92 | ::NormalizePath(&s); |
| 93 | return s; |
| 94 | } |
| 95 | |
| 96 | void TestNormalizePath() { |
| 97 | ASSERT_EQ(NormalizePath(""), ""); |
| 98 | ASSERT_EQ(NormalizePath("."), ""); |
| 99 | ASSERT_EQ(NormalizePath("/"), "/"); |
| 100 | ASSERT_EQ(NormalizePath("/tmp"), "/tmp"); |
| 101 | ASSERT_EQ(NormalizePath("////tmp////"), "/tmp"); |
| 102 | ASSERT_EQ(NormalizePath("a////b"), "a/b"); |
| 103 | ASSERT_EQ(NormalizePath("a//.//b"), "a/b"); |
| 104 | ASSERT_EQ(NormalizePath("a////b//../c/////"), "a/c"); |
| 105 | ASSERT_EQ(NormalizePath("../foo"), "../foo"); |
| 106 | ASSERT_EQ(NormalizePath("./foo"), "foo"); |
| 107 | ASSERT_EQ(NormalizePath("x/y/..//../foo"), "foo"); |
| 108 | ASSERT_EQ(NormalizePath("x/../../foo"), "../foo"); |
| 109 | ASSERT_EQ(NormalizePath("/../foo"), "/foo"); |
| 110 | ASSERT_EQ(NormalizePath("/../../foo"), "/foo"); |
| 111 | ASSERT_EQ(NormalizePath("/a/../../foo"), "/foo"); |
| 112 | ASSERT_EQ(NormalizePath("/a/b/.."), "/a"); |
Shinichiro Hamaji | 14ea0f1 | 2015-10-13 17:04:34 +0900 | [diff] [blame] | 113 | ASSERT_EQ(NormalizePath("../../a/b"), "../../a/b"); |
| 114 | ASSERT_EQ(NormalizePath("../../../a/b"), "../../../a/b"); |
| 115 | ASSERT_EQ(NormalizePath(".././../a/b"), "../../a/b"); |
| 116 | ASSERT_EQ(NormalizePath("./../../a/b"), "../../a/b"); |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace |
| 120 | |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 121 | int main() { |
| 122 | TestWordScanner(); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 123 | TestHasPrefix(); |
| 124 | TestHasSuffix(); |
| 125 | TestSubstPattern(); |
Shinichiro Hamaji | 2b674c4 | 2015-06-24 17:49:35 +0900 | [diff] [blame] | 126 | TestNoLineBreak(); |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 127 | TestHasWord(); |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 128 | TestNormalizePath(); |
| 129 | assert(!g_failed); |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 130 | } |