1 /** 2 * Copyright 2019 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include "minddata/dataset/util/path.h" 17 #include "common/common.h" 18 #include "gtest/gtest.h" 19 #include "utils/log_adapter.h" 20 #include <cstdio> 21 22 using namespace mindspore::dataset; 23 24 class MindDataTestPath : public UT::Common { 25 public: 26 MindDataTestPath() {} 27 }; 28 29 TEST_F(MindDataTestPath, Test1) { 30 Path f("/tmp"); 31 ASSERT_TRUE(f.Exists()); 32 ASSERT_TRUE(f.IsDirectory()); 33 ASSERT_EQ(f.ParentPath(), "/"); 34 // Print out the first few items in the directory 35 auto dir_it = Path::DirIterator::OpenDirectory(&f); 36 ASSERT_NE(dir_it.get(), nullptr); 37 int i = 0; 38 while (dir_it->HasNext()) { 39 Path v = dir_it->Next(); 40 MS_LOG(DEBUG) << v.ToString() << "\n"; 41 i++; 42 if (i == 10) { 43 break; 44 } 45 } 46 // Test extension. 47 Path g("file.jpeg"); 48 MS_LOG(DEBUG) << g.Extension() << "\n"; 49 ASSERT_EQ(g.Extension(), ".jpeg"); 50 } 51 52 TEST_F(MindDataTestPath, Test2) { 53 Path p("/tmp"); 54 Path p2(p); 55 ASSERT_TRUE(p2.Exists()); 56 ASSERT_TRUE(p2.IsDirectory()); 57 ASSERT_EQ(p2.ParentPath(), "/"); 58 59 p2 = p; 60 ASSERT_TRUE(p2.Exists()); 61 ASSERT_TRUE(p2.IsDirectory()); 62 ASSERT_EQ(p2.ParentPath(), "/"); 63 64 Path p3(""); 65 p3 = std::move(p2); 66 ASSERT_TRUE(p3.Exists()); 67 ASSERT_TRUE(p3.IsDirectory()); 68 ASSERT_EQ(p3.ParentPath(), "/"); 69 70 Path p4(std::move(p3)); 71 ASSERT_TRUE(p4.Exists()); 72 ASSERT_TRUE(p4.IsDirectory()); 73 ASSERT_EQ(p4.ParentPath(), "/"); 74 75 Path p5("/"); 76 std::string s = "tmp"; 77 ASSERT_TRUE((p5 + "tmp").Exists()); 78 ASSERT_TRUE((p5 + "tmp").IsDirectory()); 79 ASSERT_EQ((p5 + "tmp").ParentPath(), "/"); 80 81 ASSERT_TRUE((p5 / "tmp").Exists()); 82 ASSERT_TRUE((p5 / "tmp").IsDirectory()); 83 ASSERT_EQ((p5 / "tmp").ParentPath(), "/"); 84 85 ASSERT_TRUE((p5 + s).Exists()); 86 ASSERT_TRUE((p5 + s).IsDirectory()); 87 ASSERT_EQ((p5 + s).ParentPath(), "/"); 88 89 ASSERT_TRUE((p5 / s).Exists()); 90 ASSERT_TRUE((p5 / s).IsDirectory()); 91 ASSERT_EQ((p5 / s).ParentPath(), "/"); 92 93 Path p6("tmp"); 94 ASSERT_TRUE((p5 + p6).Exists()); 95 ASSERT_TRUE((p5 + p6).IsDirectory()); 96 ASSERT_EQ((p5 + p6).ParentPath(), "/"); 97 ASSERT_TRUE((p5 / p6).Exists()); 98 ASSERT_TRUE((p5 / p6).IsDirectory()); 99 ASSERT_EQ((p5 / p6).ParentPath(), "/"); 100 p5 += p6; 101 ASSERT_TRUE(p5.Exists()); 102 ASSERT_TRUE(p5.IsDirectory()); 103 ASSERT_EQ(p5.ParentPath(), "/"); 104 105 Path p7("/"); 106 Path p8(p7); 107 p7 += s; 108 p8 += "tmp"; 109 ASSERT_TRUE(p7.Exists()); 110 ASSERT_TRUE(p7.IsDirectory()); 111 ASSERT_EQ(p7.ParentPath(), "/"); 112 ASSERT_TRUE(p8.Exists()); 113 ASSERT_TRUE(p8.IsDirectory()); 114 ASSERT_EQ(p8.ParentPath(), "/"); 115 116 Path p9("/tmp/test_path"); 117 ASSERT_TRUE(p9.CreateDirectories().IsOk()); 118 ASSERT_EQ(remove("/tmp/test_path"), 0); 119 } 120