FileTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 
  2. #define NO_IMPLICIT_PATH_SYNTAX
  3. #include "../testTools.h"
  4. #include "../../DFPSR/api/fileAPI.h"
  5. START_TEST(File)
  6. { // Combining paths
  7. ASSERT_EQUAL(file_combinePaths(U"", U"myProgram.exe", PathSyntax::Windows), U"myProgram.exe");
  8. ASSERT_EQUAL(file_combinePaths(U"C:", U"myProgram.exe", PathSyntax::Windows), U"C:\\myProgram.exe");
  9. ASSERT_EQUAL(file_combinePaths(U"C:\\windows", U"myProgram.exe", PathSyntax::Windows), U"C:\\windows\\myProgram.exe");
  10. ASSERT_EQUAL(file_combinePaths(U"C:\\windows\\", U"myProgram.exe", PathSyntax::Windows), U"C:\\windows\\myProgram.exe");
  11. ASSERT_EQUAL(file_combinePaths(U"", U"myProgram", PathSyntax::Posix), U"myProgram");
  12. ASSERT_EQUAL(file_combinePaths(U"/", U"myProgram", PathSyntax::Posix), U"/myProgram");
  13. ASSERT_EQUAL(file_combinePaths(U"/home", U"me", PathSyntax::Posix), U"/home/me");
  14. ASSERT_EQUAL(file_combinePaths(U"/home/", U"me", PathSyntax::Posix), U"/home/me");
  15. }
  16. { // Optimizing paths
  17. // Preserving leading separators
  18. ASSERT_EQUAL(file_optimizePath(U"myProgram", PathSyntax::Windows), U"myProgram"); // Relative path
  19. ASSERT_EQUAL(file_optimizePath(U"\\myProgram", PathSyntax::Windows), U"\\myProgram"); // Implicit drive
  20. ASSERT_EQUAL(file_optimizePath(U"\\\\myProgram", PathSyntax::Windows), U"\\\\myProgram");
  21. ASSERT_EQUAL(file_optimizePath(U"\\\\\\myProgram", PathSyntax::Windows), U"\\\\\\myProgram");
  22. ASSERT_EQUAL(file_optimizePath(U"myProgram", PathSyntax::Posix), U"myProgram"); // Relative path
  23. ASSERT_EQUAL(file_optimizePath(U"/home", PathSyntax::Posix), U"/home"); // Root path
  24. ASSERT_EQUAL(file_optimizePath(U"//network", PathSyntax::Posix), U"//network"); // Special path
  25. ASSERT_EQUAL(file_optimizePath(U"///myProgram", PathSyntax::Posix), U"///myProgram");
  26. // Preserving drive letters
  27. ASSERT_EQUAL(file_optimizePath(U"C:\\myProgram", PathSyntax::Windows), U"C:\\myProgram");
  28. // Reducing redundancy
  29. ASSERT_EQUAL(file_optimizePath(U"/home/user", PathSyntax::Posix), U"/home/user");
  30. ASSERT_EQUAL(file_optimizePath(U"/home/user/", PathSyntax::Posix), U"/home/user");
  31. ASSERT_EQUAL(file_optimizePath(U"/home/user//", PathSyntax::Posix), U"/home/user");
  32. ASSERT_EQUAL(file_optimizePath(U"/home/user///", PathSyntax::Posix), U"/home/user");
  33. ASSERT_EQUAL(file_optimizePath(U"/home/user/.", PathSyntax::Posix), U"/home/user");
  34. ASSERT_EQUAL(file_optimizePath(U"/home/user/./", PathSyntax::Posix), U"/home/user");
  35. ASSERT_EQUAL(file_optimizePath(U"/home/user/.//", PathSyntax::Posix), U"/home/user");
  36. ASSERT_EQUAL(file_optimizePath(U"/home/user/..", PathSyntax::Posix), U"/home");
  37. ASSERT_EQUAL(file_optimizePath(U"/home/user/../", PathSyntax::Posix), U"/home");
  38. ASSERT_EQUAL(file_optimizePath(U"/home/user/..//", PathSyntax::Posix), U"/home");
  39. ASSERT_EQUAL(file_optimizePath(U"/cars/oldCars/veteranCars/../././../newCars/", PathSyntax::Posix), U"/cars/newCars");
  40. ASSERT_EQUAL(file_optimizePath(U"C:\\cars\\oldCars\\veteranCars\\..\\..\\newCars\\", PathSyntax::Windows), U"C:\\cars\\newCars");
  41. // Error handling
  42. ASSERT_EQUAL(file_optimizePath(U"C:\\..", PathSyntax::Windows), U"?"); // Can't go outside of C: drive
  43. ASSERT_EQUAL(file_optimizePath(U"\\..", PathSyntax::Windows), U"?"); // Can't go outside of current drive root
  44. ASSERT_EQUAL(file_optimizePath(U"..", PathSyntax::Windows), U".."); // Can go outside of the relative path
  45. ASSERT_EQUAL(file_optimizePath(U"/..", PathSyntax::Posix), U"?"); // Can't go outside of system root
  46. ASSERT_EQUAL(file_optimizePath(U"..", PathSyntax::Posix), U".."); // Can go outside of the relative path
  47. }
  48. { // Absolute canonical paths
  49. ASSERT_EQUAL(file_getTheoreticalAbsolutePath(U"mediaFolder\\myFile.txt", U"C:\\folder\\anotherFolder", PathSyntax::Windows), U"C:\\folder\\anotherFolder\\mediaFolder\\myFile.txt");
  50. ASSERT_EQUAL(file_getTheoreticalAbsolutePath(U"mediaFolder\\myFile.txt", U"C:\\folder\\anotherFolder\\", PathSyntax::Windows), U"C:\\folder\\anotherFolder\\mediaFolder\\myFile.txt");
  51. ASSERT_EQUAL(file_getTheoreticalAbsolutePath(U"myFile.txt", U"C:\\folder", PathSyntax::Windows), U"C:\\folder\\myFile.txt");
  52. ASSERT_EQUAL(file_getTheoreticalAbsolutePath(U"\\myFile.txt", U"C:\\folder", PathSyntax::Windows), U"C:\\myFile.txt"); // To the root of the current drive C:
  53. ASSERT_EQUAL(file_getTheoreticalAbsolutePath(U"", U"C:\\folder", PathSyntax::Windows), U"C:\\folder");
  54. ASSERT_EQUAL(file_getTheoreticalAbsolutePath(U"mediaFolder\\..\\myFile.txt", U"C:\\folder\\anotherFolder", PathSyntax::Windows), U"C:\\folder\\anotherFolder\\myFile.txt");
  55. }
  56. { // Parent folders
  57. ASSERT_EQUAL(file_getRelativeParentFolder(U"mediaFolder\\..\\myFile.txt", PathSyntax::Windows), U"");
  58. ASSERT_EQUAL(file_getTheoreticalAbsoluteParentFolder(U"mediaFolder\\..\\myFile.txt", U"C:\\folder\\anotherFolder", PathSyntax::Windows), U"C:\\folder\\anotherFolder");
  59. }
  60. { // Path removal
  61. ASSERT_EQUAL(file_getPathlessName(U"C:\\..\\folder\\file.txt"), U"file.txt");
  62. ASSERT_EQUAL(file_getPathlessName(U"C:\\..\\folder\\"), U"");
  63. ASSERT_EQUAL(file_getPathlessName(U"C:\\..\\folder"), U"folder");
  64. ASSERT_EQUAL(file_getPathlessName(U"C:\\..\\"), U"");
  65. ASSERT_EQUAL(file_getPathlessName(U"C:\\.."), U"..");
  66. ASSERT_EQUAL(file_getPathlessName(U"C:\\"), U"");
  67. ASSERT_EQUAL(file_getPathlessName(U"C:"), U"C:");
  68. ASSERT_EQUAL(file_getPathlessName(U"/folder/file.h"), U"file.h");
  69. ASSERT_EQUAL(file_getPathlessName(U"/folder/"), U"");
  70. ASSERT_EQUAL(file_getPathlessName(U"/folder"), U"folder");
  71. ASSERT_EQUAL(file_getPathlessName(U"/"), U"");
  72. }
  73. { // Extension removal
  74. ASSERT_EQUAL(file_getExtensionless(U"C:\\..\\folder\\file.txt"), U"C:\\..\\folder\\file");
  75. ASSERT_EQUAL(file_getExtensionless(U"C:\\folder\\file.h"), U"C:\\folder\\file");
  76. ASSERT_EQUAL(file_getExtensionless(U"C:\\file."), U"C:\\file");
  77. ASSERT_EQUAL(file_getExtensionless(U"\\file."), U"\\file");
  78. ASSERT_EQUAL(file_getExtensionless(U"file"), U"file");
  79. ASSERT_EQUAL(file_getExtensionless(U""), U"");
  80. ASSERT_EQUAL(file_getExtensionless(U"/folder/./file.txt"), U"/folder/./file");
  81. ASSERT_EQUAL(file_getExtensionless(U"/folder/file.h"), U"/folder/file");
  82. ASSERT_EQUAL(file_getExtensionless(U"/folder/../file.h"), U"/folder/../file");
  83. ASSERT_EQUAL(file_getExtensionless(U"/file."), U"/file");
  84. ASSERT_EQUAL(file_getExtensionless(U"file"), U"file");
  85. ASSERT_EQUAL(file_getExtension(U"C:\\..\\folder\\file.txt"), U"txt");
  86. ASSERT_EQUAL(file_getExtension(U"C:\\..\\folder\\file.foo.txt"), U"txt");
  87. ASSERT_EQUAL(file_getExtension(U"C:\\..\\folder\\file.foo_bar.txt"), U"txt");
  88. ASSERT_EQUAL(file_getExtension(U"C:\\..\\folder\\file.foo.bar_txt"), U"bar_txt");
  89. ASSERT_EQUAL(file_getExtension(U"C:\\folder\\file.h"), U"h");
  90. ASSERT_EQUAL(file_getExtension(U"C:\\file."), U"");
  91. ASSERT_EQUAL(file_getExtension(U"\\file."), U"");
  92. ASSERT_EQUAL(file_getExtension(U"file"), U"");
  93. ASSERT_EQUAL(file_getExtension(U""), U"");
  94. ASSERT_EQUAL(file_getExtension(U"/folder/com.dawoodoz.www/file.txt"), U"txt");
  95. ASSERT_EQUAL(file_getExtension(U"/folder/./file.txt"), U"txt");
  96. ASSERT_EQUAL(file_getExtension(U"/folder/file.h"), U"h");
  97. ASSERT_EQUAL(file_getExtension(U"/folder/../file.h"), U"h");
  98. ASSERT_EQUAL(file_getExtension(U"/file."), U"");
  99. ASSERT_EQUAL(file_getExtension(U"file"), U"");
  100. ASSERT_EQUAL(file_hasExtension(U"/folder/./file.txt"), true);
  101. ASSERT_EQUAL(file_hasExtension(U"/../folder/file.h"), true);
  102. ASSERT_EQUAL(file_hasExtension(U"/folder/file."), true); // Not a named extension, but ending with a dot is not a pure extensionless path either.
  103. ASSERT_EQUAL(file_hasExtension(U"/folder/file"), false);
  104. }
  105. { // Folder creation and removal
  106. // Prepare by removing any old folder from aborted tests.
  107. if (file_getEntryType(U"FooBarTestFolder") == EntryType::Folder) file_removeEmptyFolder(U"FooBarTestFolder");
  108. // The boolean results are compared with true rather than not-false, because it should still work if someone does that by mistake in the real program.
  109. // All booleans returned from system API calls in fileAPI are normalized to 1 and 0 using either direct assignments or comparisons.
  110. // Normalization using x == 0 or x != 0 is guaranteed to return precisely 1 or 0 according to the C++ standard.
  111. // Check that the folder does not exist.
  112. ASSERT_EQUAL(file_getEntryType(U"FooBarTestFolder"), EntryType::NotFound);
  113. // Create the folder.
  114. ASSERT_EQUAL(file_createFolder(U"FooBarTestFolder"), true);
  115. // Check that the folder does exist.
  116. ASSERT_EQUAL(file_getEntryType(U"FooBarTestFolder"), EntryType::Folder);
  117. // Remove the folder.
  118. ASSERT_EQUAL(file_removeEmptyFolder(U"FooBarTestFolder"), true);
  119. // Check that the folder does not exist.
  120. ASSERT_EQUAL(file_getEntryType(U"FooBarTestFolder"), EntryType::NotFound);
  121. }
  122. { // Nested creation and removal
  123. String childPathA = file_combinePaths(U"FooBarParent", U"FooBarChildA", LOCAL_PATH_SYNTAX);
  124. String childPathB = file_combinePaths(U"FooBarParent", U"FooBarChildB", LOCAL_PATH_SYNTAX);
  125. String filePathC = file_combinePaths(childPathA, U"testC.txt", LOCAL_PATH_SYNTAX);
  126. // Prepare by removing any old folder from aborted tests.
  127. if (file_getEntryType(filePathC) == EntryType::File) file_removeFile(filePathC);
  128. if (file_getEntryType(childPathA) == EntryType::Folder) file_removeEmptyFolder(childPathA);
  129. if (file_getEntryType(childPathB) == EntryType::Folder) file_removeEmptyFolder(childPathB);
  130. if (file_getEntryType(U"FooBarParent") == EntryType::Folder) file_removeEmptyFolder(U"FooBarParent");
  131. // Check that the folder does not exist.
  132. ASSERT_EQUAL(file_getEntryType(U"FooBarParent"), EntryType::NotFound);
  133. // Create the folder.
  134. ASSERT_EQUAL(file_createFolder(U"FooBarParent"), true);
  135. // Check that the folder does exist.
  136. ASSERT_EQUAL(file_getEntryType(U"FooBarParent"), EntryType::Folder);
  137. // Create child folders.
  138. ASSERT_EQUAL(file_getEntryType(childPathA), EntryType::NotFound);
  139. ASSERT_EQUAL(file_getEntryType(childPathB), EntryType::NotFound);
  140. ASSERT_EQUAL(file_createFolder(childPathA), true);
  141. ASSERT_EQUAL(file_getEntryType(childPathA), EntryType::Folder);
  142. ASSERT_EQUAL(file_getEntryType(childPathB), EntryType::NotFound);
  143. ASSERT_EQUAL(file_createFolder(childPathB), true);
  144. ASSERT_EQUAL(file_getEntryType(childPathA), EntryType::Folder);
  145. ASSERT_EQUAL(file_getEntryType(childPathB), EntryType::Folder);
  146. // Create a file in the FooBarParent/FooBarChildA folder.
  147. ASSERT_EQUAL(string_save(filePathC, U"Testing", CharacterEncoding::Raw_Latin1), true);
  148. ASSERT_EQUAL(file_getEntryType(filePathC), EntryType::File);
  149. ASSERT_EQUAL(string_load(filePathC, false), U"Testing");
  150. ASSERT_EQUAL(file_getFileSize(filePathC), 7);
  151. ASSERT_EQUAL(string_save(filePathC, U"Test", CharacterEncoding::Raw_Latin1), true);
  152. ASSERT_EQUAL(string_load(filePathC, false), U"Test");
  153. ASSERT_EQUAL(file_getFileSize(filePathC), 4);
  154. ASSERT_EQUAL(file_removeEmptyFolder(childPathA), false); // Trying to remove FooBarParent/FooBarChildA now should fail.
  155. // Remove the file.
  156. ASSERT_EQUAL(file_removeFile(filePathC), true);
  157. ASSERT_EQUAL(file_getEntryType(filePathC), EntryType::NotFound);
  158. // Remove the child folders.
  159. ASSERT_EQUAL(file_removeEmptyFolder(U"FooBarParent"), false); // Trying to remove the parent now should fail.
  160. ASSERT_EQUAL(file_removeEmptyFolder(childPathA), true);
  161. ASSERT_EQUAL(file_getEntryType(childPathA), EntryType::NotFound);
  162. ASSERT_EQUAL(file_getEntryType(childPathB), EntryType::Folder);
  163. ASSERT_EQUAL(file_removeEmptyFolder(U"FooBarParent"), false); // Trying to remove the parent now should fail.
  164. ASSERT_EQUAL(file_removeEmptyFolder(childPathB), true);
  165. ASSERT_EQUAL(file_getEntryType(childPathA), EntryType::NotFound);
  166. ASSERT_EQUAL(file_getEntryType(childPathB), EntryType::NotFound);
  167. // Remove the parent folder.
  168. ASSERT_EQUAL(file_removeEmptyFolder(U"FooBarParent"), true); // Trying to remove the parent now should succeed now that it's empty.
  169. ASSERT_EQUAL(file_getEntryType(U"FooBarParent"), EntryType::NotFound); // Now the parent folder should no longer exist.
  170. }
  171. END_TEST