1 /* Copyright 2015 The TensorFlow Authors. 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 ==============================================================================*/ 15 16 #include "tensorflow/core/lib/io/path.h" 17 #include "tensorflow/core/platform/test.h" 18 19 namespace tensorflow { 20 namespace io { 21 22 TEST(PathTest, JoinPath) { 23 EXPECT_EQ("/foo/bar", JoinPath("/foo", "bar")); 24 EXPECT_EQ("foo/bar", JoinPath("foo", "bar")); 25 EXPECT_EQ("foo/bar", JoinPath("foo", "/bar")); 26 EXPECT_EQ("/foo/bar", JoinPath("/foo", "/bar")); 27 28 EXPECT_EQ("/bar", JoinPath("", "/bar")); 29 EXPECT_EQ("bar", JoinPath("", "bar")); 30 EXPECT_EQ("/foo", JoinPath("/foo", "")); 31 32 EXPECT_EQ("/foo/bar/baz/blah/blink/biz", 33 JoinPath("/foo/bar/baz/", "/blah/blink/biz")); 34 EXPECT_EQ("/foo/bar/baz/blah", JoinPath("/foo", "bar", "baz", "blah")); 35 } 36 37 TEST(PathTest, IsAbsolutePath) { 38 EXPECT_FALSE(IsAbsolutePath("")); 39 EXPECT_FALSE(IsAbsolutePath("../foo")); 40 EXPECT_FALSE(IsAbsolutePath("foo")); 41 EXPECT_FALSE(IsAbsolutePath("./foo")); 42 EXPECT_FALSE(IsAbsolutePath("foo/bar/baz/")); 43 EXPECT_TRUE(IsAbsolutePath("/foo")); 44 EXPECT_TRUE(IsAbsolutePath("/foo/bar/../baz")); 45 } 46 47 TEST(PathTest, Dirname) { 48 EXPECT_EQ("hdfs://127.0.0.1:9000/", 49 Dirname("hdfs://127.0.0.1:9000/train.csv.tfrecords")); 50 EXPECT_EQ("/hello", Dirname("/hello/")); 51 EXPECT_EQ("/", Dirname("/hello")); 52 EXPECT_EQ("hello", Dirname("hello/world")); 53 EXPECT_EQ("hello", Dirname("hello/")); 54 EXPECT_EQ("", Dirname("world")); 55 EXPECT_EQ("/", Dirname("/")); 56 EXPECT_EQ("", Dirname("")); 57 } 58 59 TEST(PathTest, Basename) { 60 EXPECT_EQ("", Basename("/hello/")); 61 EXPECT_EQ("hello", Basename("/hello")); 62 EXPECT_EQ("world", Basename("hello/world")); 63 EXPECT_EQ("", Basename("hello/")); 64 EXPECT_EQ("world", Basename("world")); 65 EXPECT_EQ("", Basename("/")); 66 EXPECT_EQ("", Basename("")); 67 } 68 69 TEST(PathTest, Extension) { 70 EXPECT_EQ("gif", Extension("foo.gif")); 71 EXPECT_EQ("", Extension("foo.")); 72 EXPECT_EQ("", Extension("")); 73 EXPECT_EQ("", Extension("/")); 74 EXPECT_EQ("", Extension("foo")); 75 EXPECT_EQ("", Extension("foo/")); 76 EXPECT_EQ("gif", Extension("/a/path/to/foo.gif")); 77 EXPECT_EQ("html", Extension("/a/path.bar/to/foo.html")); 78 EXPECT_EQ("", Extension("/a/path.bar/to/foo")); 79 EXPECT_EQ("baz", Extension("/a/path.bar/to/foo.bar.baz")); 80 } 81 82 TEST(PathTest, CleanPath) { 83 EXPECT_EQ(".", CleanPath("")); 84 EXPECT_EQ("x", CleanPath("x")); 85 EXPECT_EQ("/a/b/c/d", CleanPath("/a/b/c/d")); 86 EXPECT_EQ("/a/b/c/d/*", CleanPath("/a/b/c/d/*")); 87 EXPECT_EQ("/a/b/c/d", CleanPath("/a/b/c/d/")); 88 EXPECT_EQ("/a/b", CleanPath("/a//b")); 89 EXPECT_EQ("/a/b", CleanPath("//a//b/")); 90 EXPECT_EQ("/", CleanPath("/..")); 91 EXPECT_EQ("/", CleanPath("/././././")); 92 EXPECT_EQ("/a", CleanPath("/a/b/..")); 93 EXPECT_EQ("/", CleanPath("/a/b/../../..")); 94 EXPECT_EQ("/", CleanPath("//a//b/..////../..//")); 95 EXPECT_EQ("/x", CleanPath("//a//../x//")); 96 EXPECT_EQ("x", CleanPath("x")); 97 EXPECT_EQ("../../a/c", CleanPath("../../a/b/../c")); 98 EXPECT_EQ("../..", CleanPath("../../a/b/../c/../..")); 99 EXPECT_EQ("../../bar", CleanPath("foo/../../../bar")); 100 } 101 102 #define EXPECT_PARSE_URI(uri, scheme, host, path) \ 103 do { \ 104 StringPiece u(uri); \ 105 StringPiece s, h, p; \ 106 ParseURI(u, &s, &h, &p); \ 107 EXPECT_EQ(scheme, s.ToString()); \ 108 EXPECT_EQ(host, h.ToString()); \ 109 EXPECT_EQ(path, p.ToString()); \ 110 EXPECT_EQ(uri, CreateURI(scheme, host, path)); \ 111 EXPECT_LE(u.begin(), s.begin()); \ 112 EXPECT_GE(u.end(), s.begin()); \ 113 EXPECT_LE(u.begin(), s.end()); \ 114 EXPECT_GE(u.end(), s.end()); \ 115 EXPECT_LE(u.begin(), h.begin()); \ 116 EXPECT_GE(u.end(), h.begin()); \ 117 EXPECT_LE(u.begin(), h.end()); \ 118 EXPECT_GE(u.end(), h.end()); \ 119 EXPECT_LE(u.begin(), p.begin()); \ 120 EXPECT_GE(u.end(), p.begin()); \ 121 EXPECT_LE(u.begin(), p.end()); \ 122 EXPECT_GE(u.end(), p.end()); \ 123 } while (0) 124 125 TEST(PathTest, CreateParseURI) { 126 EXPECT_PARSE_URI("http://foo", "http", "foo", ""); 127 EXPECT_PARSE_URI("/encrypted/://foo", "", "", "/encrypted/://foo"); 128 EXPECT_PARSE_URI("/usr/local/foo", "", "", "/usr/local/foo"); 129 EXPECT_PARSE_URI("file:///usr/local/foo", "file", "", "/usr/local/foo"); 130 EXPECT_PARSE_URI("local.file:///usr/local/foo", "local.file", "", 131 "/usr/local/foo"); 132 EXPECT_PARSE_URI("a-b:///foo", "", "", "a-b:///foo"); 133 EXPECT_PARSE_URI(":///foo", "", "", ":///foo"); 134 EXPECT_PARSE_URI("9dfd:///foo", "", "", "9dfd:///foo"); 135 EXPECT_PARSE_URI("file:", "", "", "file:"); 136 EXPECT_PARSE_URI("file:/", "", "", "file:/"); 137 EXPECT_PARSE_URI("hdfs://localhost:8020/path/to/file", "hdfs", 138 "localhost:8020", "/path/to/file"); 139 EXPECT_PARSE_URI("hdfs://localhost:8020", "hdfs", "localhost:8020", ""); 140 EXPECT_PARSE_URI("hdfs://localhost:8020/", "hdfs", "localhost:8020", "/"); 141 } 142 #undef EXPECT_PARSE_URI 143 144 } // namespace io 145 } // namespace tensorflow 146