blob: 048a9746b070529a0a8fd51a5b3e807d6c996533 [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;
33 for (StringPiece tok : WordScanner("foo bar baz")) {
34 ss.push_back(tok);
35 }
36 assert(ss.size() == 3LU);
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 Hamaji63e68fc2015-06-16 17:36:53 +090040}
41
Shinichiro Hamajif91d6822015-06-16 17:51:23 +090042void TestHasPrefix() {
43 assert(HasPrefix("foo", "foo"));
44 assert(HasPrefix("foo", "fo"));
45 assert(HasPrefix("foo", ""));
46 assert(!HasPrefix("foo", "fooo"));
47}
48
49void TestHasSuffix() {
50 assert(HasSuffix("bar", "bar"));
51 assert(HasSuffix("bar", "ar"));
52 assert(HasSuffix("bar", ""));
53 assert(!HasSuffix("bar", "bbar"));
54}
55
56string SubstPattern(StringPiece str, StringPiece pat, StringPiece subst) {
57 string r;
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090058 Pattern(pat).AppendSubst(str, subst, &r);
Shinichiro Hamajif91d6822015-06-16 17:51:23 +090059 return r;
60}
61
62void TestSubstPattern() {
Shinichiro Hamajib3591ed2015-08-10 20:06:50 +090063 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 Hamajif91d6822015-06-16 17:51:23 +090071}
72
Shinichiro Hamaji2b674c42015-06-24 17:49:35 +090073void TestNoLineBreak() {
74 assert(NoLineBreak("a\nb") == "a\\nb");
75 assert(NoLineBreak("a\nb\nc") == "a\\nb\\nc");
76}
77
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +090078void 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 Hamajib3591ed2015-08-10 20:06:50 +090090static string NormalizePath(string s) {
91 ::NormalizePath(&s);
92 return s;
93}
94
95void 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 Hamaji63e68fc2015-06-16 17:36:53 +0900116int main() {
117 TestWordScanner();
Shinichiro Hamajif91d6822015-06-16 17:51:23 +0900118 TestHasPrefix();
119 TestHasSuffix();
120 TestSubstPattern();
Shinichiro Hamaji2b674c42015-06-24 17:49:35 +0900121 TestNoLineBreak();
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900122 TestHasWord();
Shinichiro Hamajib3591ed2015-08-10 20:06:50 +0900123 TestNormalizePath();
124 assert(!g_failed);
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +0900125}