FileTest.cpp 12 KB

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