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.3 kB

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