Filesystem.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <AnKi/Util/Filesystem.h>
  7. #include <AnKi/Util/File.h>
  8. ANKI_TEST(Util, FileExists)
  9. {
  10. // Create file
  11. File file;
  12. ANKI_TEST_EXPECT_NO_ERR(file.open("./tmp", FileOpenFlag::WRITE));
  13. file.close();
  14. // Check
  15. ANKI_TEST_EXPECT_EQ(fileExists("./tmp"), true);
  16. }
  17. ANKI_TEST(Util, Directory)
  18. {
  19. HeapAllocator<U8> alloc(allocAligned, nullptr);
  20. // Destroy previous
  21. if(directoryExists("./dir"))
  22. {
  23. ANKI_TEST_EXPECT_NO_ERR(removeDirectory("./dir", alloc));
  24. }
  25. // Create simple directory
  26. ANKI_TEST_EXPECT_NO_ERR(createDirectory("./dir"));
  27. File file;
  28. ANKI_TEST_EXPECT_NO_ERR(file.open("./dir/tmp", FileOpenFlag::WRITE));
  29. file.close();
  30. ANKI_TEST_EXPECT_EQ(fileExists("./dir/tmp"), true);
  31. ANKI_TEST_EXPECT_NO_ERR(removeDirectory("./dir", alloc));
  32. ANKI_TEST_EXPECT_EQ(fileExists("./dir/tmp"), false);
  33. ANKI_TEST_EXPECT_EQ(directoryExists("./dir"), false);
  34. // A bit more complex
  35. ANKI_TEST_EXPECT_NO_ERR(createDirectory("./dir"));
  36. ANKI_TEST_EXPECT_NO_ERR(createDirectory("./dir/rid"));
  37. ANKI_TEST_EXPECT_NO_ERR(file.open("./dir/rid/tmp", FileOpenFlag::WRITE));
  38. file.close();
  39. ANKI_TEST_EXPECT_EQ(fileExists("./dir/rid/tmp"), true);
  40. ANKI_TEST_EXPECT_NO_ERR(removeDirectory("./dir", alloc));
  41. ANKI_TEST_EXPECT_EQ(fileExists("./dir/rid/tmp"), false);
  42. ANKI_TEST_EXPECT_EQ(directoryExists("./dir/rid"), false);
  43. ANKI_TEST_EXPECT_EQ(directoryExists("./dir"), false);
  44. }
  45. ANKI_TEST(Util, HomeDir)
  46. {
  47. HeapAllocator<char> alloc(allocAligned, nullptr);
  48. StringAuto out(alloc);
  49. ANKI_TEST_EXPECT_NO_ERR(getHomeDirectory(out));
  50. printf("home dir %s\n", &out[0]);
  51. ANKI_TEST_EXPECT_GT(out.getLength(), 0);
  52. }
  53. ANKI_TEST(Util, WalkDir)
  54. {
  55. HeapAllocator<char> alloc(allocAligned, nullptr);
  56. class Path
  57. {
  58. public:
  59. CString m_path;
  60. Bool m_isDir;
  61. };
  62. class Ctx
  63. {
  64. public:
  65. Array<Path, 4> m_paths = {
  66. {{"./data", true}, {"./data/dir", true}, {"./data/file1", false}, {"./data/dir/file2", false}}};
  67. U32 m_foundMask = 0;
  68. HeapAllocator<char> m_alloc;
  69. } ctx;
  70. ctx.m_alloc = alloc;
  71. [[maybe_unused]] const Error err = removeDirectory("./data", alloc);
  72. // Create some dirs and some files
  73. for(U32 i = 0; i < ctx.m_paths.getSize(); ++i)
  74. {
  75. if(ctx.m_paths[i].m_isDir)
  76. {
  77. ANKI_TEST_EXPECT_NO_ERR(createDirectory(ctx.m_paths[i].m_path));
  78. }
  79. else
  80. {
  81. File file;
  82. ANKI_TEST_EXPECT_NO_ERR(file.open(ctx.m_paths[i].m_path, FileOpenFlag::WRITE));
  83. }
  84. }
  85. // Walk crnt dir
  86. ANKI_TEST_EXPECT_NO_ERR(walkDirectoryTree("./data", alloc, [&](const CString& fname, Bool isDir) -> Error {
  87. for(U32 i = 0; i < ctx.m_paths.getSize(); ++i)
  88. {
  89. StringAuto p(ctx.m_alloc);
  90. p.sprintf("./data/%s", fname.cstr());
  91. if(ctx.m_paths[i].m_path == p)
  92. {
  93. ANKI_TEST_EXPECT_EQ(ctx.m_paths[i].m_isDir, isDir);
  94. ctx.m_foundMask |= 1 << i;
  95. }
  96. }
  97. return Error::NONE;
  98. }));
  99. ANKI_TEST_EXPECT_EQ(ctx.m_foundMask, 0b1110);
  100. // Test error
  101. U32 count = 0;
  102. ANKI_TEST_EXPECT_ERR(
  103. walkDirectoryTree("./data///dir////", alloc,
  104. [&count]([[maybe_unused]] const CString& fname, [[maybe_unused]] Bool isDir) -> Error {
  105. ++count;
  106. return Error::FUNCTION_FAILED;
  107. }),
  108. Error::FUNCTION_FAILED);
  109. ANKI_TEST_EXPECT_EQ(count, 1);
  110. }