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; |
| 33 | for (StringPiece tok : WordScanner("foo bar baz")) { |
| 34 | ss.push_back(tok); |
| 35 | } |
| 36 | assert(ss.size() == 3LU); |
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 | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 40 | } |
| 41 | |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 42 | void TestHasPrefix() { |
| 43 | assert(HasPrefix("foo", "foo")); |
| 44 | assert(HasPrefix("foo", "fo")); |
| 45 | assert(HasPrefix("foo", "")); |
| 46 | assert(!HasPrefix("foo", "fooo")); |
| 47 | } |
| 48 | |
| 49 | void TestHasSuffix() { |
| 50 | assert(HasSuffix("bar", "bar")); |
| 51 | assert(HasSuffix("bar", "ar")); |
| 52 | assert(HasSuffix("bar", "")); |
| 53 | assert(!HasSuffix("bar", "bbar")); |
| 54 | } |
| 55 | |
| 56 | string SubstPattern(StringPiece str, StringPiece pat, StringPiece subst) { |
| 57 | string r; |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 58 | Pattern(pat).AppendSubst(str, subst, &r); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 59 | return r; |
| 60 | } |
| 61 | |
| 62 | void TestSubstPattern() { |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 63 | ASSERT_EQ(SubstPattern("x.c", "%.c", "%.o"), "x.o"); |
| 64 | ASSERT_EQ(SubstPattern("c.x", "c.%", "o.%"), "o.x"); |
| 65 | ASSERT_EQ(SubstPattern("x.c.c", "%.c", "%.o"), "x.c.o"); |
| 66 | ASSERT_EQ(SubstPattern("x.x y.c", "%.c", "%.o"), "x.x y.o"); |
| 67 | ASSERT_EQ(SubstPattern("x.%.c", "%.%.c", "OK"), "OK"); |
| 68 | ASSERT_EQ(SubstPattern("x.c", "x.c", "OK"), "OK"); |
| 69 | ASSERT_EQ(SubstPattern("x.c.c", "x.c", "XX"), "x.c.c"); |
| 70 | ASSERT_EQ(SubstPattern("x.x.c", "x.c", "XX"), "x.x.c"); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 71 | } |
| 72 | |
Shinichiro Hamaji | 2b674c4 | 2015-06-24 17:49:35 +0900 | [diff] [blame] | 73 | void TestNoLineBreak() { |
| 74 | assert(NoLineBreak("a\nb") == "a\\nb"); |
| 75 | assert(NoLineBreak("a\nb\nc") == "a\\nb\\nc"); |
| 76 | } |
| 77 | |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 78 | void TestHasWord() { |
| 79 | assert(HasWord("foo bar baz", "bar")); |
| 80 | assert(HasWord("foo bar baz", "foo")); |
| 81 | assert(HasWord("foo bar baz", "baz")); |
| 82 | assert(!HasWord("foo bar baz", "oo")); |
| 83 | assert(!HasWord("foo bar baz", "ar")); |
| 84 | assert(!HasWord("foo bar baz", "ba")); |
| 85 | assert(!HasWord("foo bar baz", "az")); |
| 86 | assert(!HasWord("foo bar baz", "ba")); |
| 87 | assert(!HasWord("foo bar baz", "fo")); |
| 88 | } |
| 89 | |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 90 | static string NormalizePath(string s) { |
| 91 | ::NormalizePath(&s); |
| 92 | return s; |
| 93 | } |
| 94 | |
| 95 | void TestNormalizePath() { |
| 96 | ASSERT_EQ(NormalizePath(""), ""); |
| 97 | ASSERT_EQ(NormalizePath("."), ""); |
| 98 | ASSERT_EQ(NormalizePath("/"), "/"); |
| 99 | ASSERT_EQ(NormalizePath("/tmp"), "/tmp"); |
| 100 | ASSERT_EQ(NormalizePath("////tmp////"), "/tmp"); |
| 101 | ASSERT_EQ(NormalizePath("a////b"), "a/b"); |
| 102 | ASSERT_EQ(NormalizePath("a//.//b"), "a/b"); |
| 103 | ASSERT_EQ(NormalizePath("a////b//../c/////"), "a/c"); |
| 104 | ASSERT_EQ(NormalizePath("../foo"), "../foo"); |
| 105 | ASSERT_EQ(NormalizePath("./foo"), "foo"); |
| 106 | ASSERT_EQ(NormalizePath("x/y/..//../foo"), "foo"); |
| 107 | ASSERT_EQ(NormalizePath("x/../../foo"), "../foo"); |
| 108 | ASSERT_EQ(NormalizePath("/../foo"), "/foo"); |
| 109 | ASSERT_EQ(NormalizePath("/../../foo"), "/foo"); |
| 110 | ASSERT_EQ(NormalizePath("/a/../../foo"), "/foo"); |
| 111 | ASSERT_EQ(NormalizePath("/a/b/.."), "/a"); |
| 112 | } |
| 113 | |
| 114 | } // namespace |
| 115 | |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 116 | int main() { |
| 117 | TestWordScanner(); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 118 | TestHasPrefix(); |
| 119 | TestHasSuffix(); |
| 120 | TestSubstPattern(); |
Shinichiro Hamaji | 2b674c4 | 2015-06-24 17:49:35 +0900 | [diff] [blame] | 121 | TestNoLineBreak(); |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 122 | TestHasWord(); |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 123 | TestNormalizePath(); |
| 124 | assert(!g_failed); |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 125 | } |