blob: 069dc4d85d63e10552ee226aebcee3ebc39a2083 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090017#include "strutil.h"
18
19#include <assert.h>
20
21#include <string>
22#include <vector>
23
24#include "string_piece.h"
Shinichiro Hamajib3591ed2015-08-10 20:06:50 +090025#include "testutil.h"
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090026
27using namespace std;
28
Shinichiro Hamajib3591ed2015-08-10 20:06:50 +090029namespace {
30
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090031void TestWordScanner() {
32 vector<StringPiece> ss;
Shinichiro Hamaji096bb4e2016-02-03 13:24:34 +090033 for (StringPiece tok : WordScanner("foo bar baz hogeeeeeeeeeeeeeeee")) {
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090034 ss.push_back(tok);
35 }
Shinichiro Hamaji096bb4e2016-02-03 13:24:34 +090036 assert(ss.size() == 4LU);
Shinichiro Hamajib3591ed2015-08-10 20:06:50 +090037 ASSERT_EQ(ss[0], "foo");
38 ASSERT_EQ(ss[1], "bar");
39 ASSERT_EQ(ss[2], "baz");
Shinichiro Hamaji096bb4e2016-02-03 13:24:34 +090040 ASSERT_EQ(ss[3], "hogeeeeeeeeeeeeeeee");
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090041}
42
Shinichiro Hamajif91d6822015-06-16 17:51:23 +090043void TestHasPrefix() {
44 assert(HasPrefix("foo", "foo"));
45 assert(HasPrefix("foo", "fo"));
46 assert(HasPrefix("foo", ""));
47 assert(!HasPrefix("foo", "fooo"));
48}
49
50void TestHasSuffix() {
51 assert(HasSuffix("bar", "bar"));
52 assert(HasSuffix("bar", "ar"));
53 assert(HasSuffix("bar", ""));
54 assert(!HasSuffix("bar", "bbar"));
55}
56
57string SubstPattern(StringPiece str, StringPiece pat, StringPiece subst) {
58 string r;
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090059 Pattern(pat).AppendSubst(str, subst, &r);
Shinichiro Hamajif91d6822015-06-16 17:51:23 +090060 return r;
61}
62
63void TestSubstPattern() {
Shinichiro Hamajib3591ed2015-08-10 20:06:50 +090064 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 Hamajif91d6822015-06-16 17:51:23 +090072}
73
Shinichiro Hamaji2b674c42015-06-24 17:49:35 +090074void TestNoLineBreak() {
75 assert(NoLineBreak("a\nb") == "a\\nb");
76 assert(NoLineBreak("a\nb\nc") == "a\\nb\\nc");
77}
78
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +090079void 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 Hamajib3591ed2015-08-10 20:06:50 +090091static string NormalizePath(string s) {
92 ::NormalizePath(&s);
93 return s;
94}
95
96void 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 Hamaji14ea0f12015-10-13 17:04:34 +0900113 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 Hamajib3591ed2015-08-10 20:06:50 +0900117}
118
119} // namespace
120
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +0900121int main() {
122 TestWordScanner();
Shinichiro Hamajif91d6822015-06-16 17:51:23 +0900123 TestHasPrefix();
124 TestHasSuffix();
125 TestSubstPattern();
Shinichiro Hamaji2b674c42015-06-24 17:49:35 +0900126 TestNoLineBreak();
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900127 TestHasWord();
Shinichiro Hamajib3591ed2015-08-10 20:06:50 +0900128 TestNormalizePath();
129 assert(!g_failed);
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +0900130}