You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

path_test.cc 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. using namespace mindspore::dataset;
  22. class MindDataTestPath : public UT::Common {
  23. public:
  24. MindDataTestPath() {}
  25. };
  26. TEST_F(MindDataTestPath, Test1) {
  27. Path f("/tmp");
  28. ASSERT_TRUE(f.Exists());
  29. ASSERT_TRUE(f.IsDirectory());
  30. ASSERT_EQ(f.ParentPath(), "/");
  31. // Print out the first few items in the directory
  32. auto dir_it = Path::DirIterator::OpenDirectory(&f);
  33. ASSERT_NE(dir_it.get(), nullptr);
  34. int i = 0;
  35. while (dir_it->hasNext()) {
  36. Path v = dir_it->next();
  37. MS_LOG(DEBUG) << v.toString() << "\n";
  38. i++;
  39. if (i == 10) {
  40. break;
  41. }
  42. }
  43. // Test extension.
  44. Path g("file.jpeg");
  45. MS_LOG(DEBUG) << g.Extension() << "\n";
  46. ASSERT_EQ(g.Extension(), ".jpeg");
  47. }
  48. TEST_F(MindDataTestPath, Test2) {
  49. Path p("/tmp");
  50. Path p2(p);
  51. ASSERT_TRUE(p2.Exists());
  52. ASSERT_TRUE(p2.IsDirectory());
  53. ASSERT_EQ(p2.ParentPath(), "/");
  54. p2 = p;
  55. ASSERT_TRUE(p2.Exists());
  56. ASSERT_TRUE(p2.IsDirectory());
  57. ASSERT_EQ(p2.ParentPath(), "/");
  58. Path p3("");
  59. p3 = std::move(p2);
  60. ASSERT_TRUE(p3.Exists());
  61. ASSERT_TRUE(p3.IsDirectory());
  62. ASSERT_EQ(p3.ParentPath(), "/");
  63. Path p4(std::move(p3));
  64. ASSERT_TRUE(p4.Exists());
  65. ASSERT_TRUE(p4.IsDirectory());
  66. ASSERT_EQ(p4.ParentPath(), "/");
  67. Path p5("/");
  68. std::string s = "tmp";
  69. ASSERT_TRUE((p5 + "tmp").Exists());
  70. ASSERT_TRUE((p5 + "tmp").IsDirectory());
  71. ASSERT_EQ((p5 + "tmp").ParentPath(), "/");
  72. ASSERT_TRUE((p5 / "tmp").Exists());
  73. ASSERT_TRUE((p5 / "tmp").IsDirectory());
  74. ASSERT_EQ((p5 / "tmp").ParentPath(), "/");
  75. ASSERT_TRUE((p5 + s).Exists());
  76. ASSERT_TRUE((p5 + s).IsDirectory());
  77. ASSERT_EQ((p5 + s).ParentPath(), "/");
  78. ASSERT_TRUE((p5 / s).Exists());
  79. ASSERT_TRUE((p5 / s).IsDirectory());
  80. ASSERT_EQ((p5 / s).ParentPath(), "/");
  81. Path p6("tmp");
  82. ASSERT_TRUE((p5 + p6).Exists());
  83. ASSERT_TRUE((p5 + p6).IsDirectory());
  84. ASSERT_EQ((p5 + p6).ParentPath(), "/");
  85. ASSERT_TRUE((p5 / p6).Exists());
  86. ASSERT_TRUE((p5 / p6).IsDirectory());
  87. ASSERT_EQ((p5 / p6).ParentPath(), "/");
  88. p5 += p6;
  89. ASSERT_TRUE(p5.Exists());
  90. ASSERT_TRUE(p5.IsDirectory());
  91. ASSERT_EQ(p5.ParentPath(), "/");
  92. Path p7("/");
  93. Path p8(p7);
  94. p7 += s;
  95. p8 += "tmp";
  96. ASSERT_TRUE(p7.Exists());
  97. ASSERT_TRUE(p7.IsDirectory());
  98. ASSERT_EQ(p7.ParentPath(), "/");
  99. ASSERT_TRUE(p8.Exists());
  100. ASSERT_TRUE(p8.IsDirectory());
  101. ASSERT_EQ(p8.ParentPath(), "/");
  102. Path p9("/tmp/test_path");
  103. ASSERT_TRUE(p9.CreateDirectories().IsOk());
  104. ASSERT_EQ(remove("/tmp/test_path"), 0);
  105. }