BsFileSystemTestSuite.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "UnitTests/BsFileSystemTestSuite.h"
  4. #include "Debug/BsDebug.h"
  5. #include "Error/BsException.h"
  6. #include "FileSystem/BsFileSystem.h"
  7. #include <algorithm>
  8. #include <fstream>
  9. namespace bs
  10. {
  11. const String testDirectoryName = "FileSystemTestDirectory/";
  12. void createFile(Path path, String content)
  13. {
  14. std::ofstream fs;
  15. fs.open(path.toPlatformString().c_str());
  16. fs << content;
  17. fs.close();
  18. }
  19. void createEmptyFile(Path path)
  20. {
  21. createFile(path, "");
  22. }
  23. String readFile(Path path)
  24. {
  25. String content;
  26. std::ifstream fs;
  27. fs.open(path.toPlatformString().c_str());
  28. fs >> content;
  29. fs.close();
  30. return content;
  31. }
  32. void FileSystemTestSuite::startUp()
  33. {
  34. mTestDirectory = FileSystem::getWorkingDirectoryPath() + testDirectoryName;
  35. if (FileSystem::exists(mTestDirectory))
  36. {
  37. BS_EXCEPT(InternalErrorException,
  38. String("Directory '") + testDirectoryName
  39. + "' should not already exist; you should remove it manually.");
  40. }
  41. else
  42. {
  43. FileSystem::createDir(mTestDirectory);
  44. BS_TEST_ASSERT_MSG(FileSystem::exists(mTestDirectory), "FileSystemTestSuite::startUp(): test directory creation failed");
  45. }
  46. }
  47. void FileSystemTestSuite::shutDown()
  48. {
  49. FileSystem::remove(mTestDirectory, true);
  50. if (FileSystem::exists(mTestDirectory))
  51. {
  52. LOGERR("FileSystemTestSuite failed to delete '" + mTestDirectory.toString()
  53. + "', you should remove it manually.");
  54. }
  55. }
  56. FileSystemTestSuite::FileSystemTestSuite()
  57. {
  58. BS_ADD_TEST(FileSystemTestSuite::testExists_yes_file);
  59. BS_ADD_TEST(FileSystemTestSuite::testExists_yes_dir);
  60. BS_ADD_TEST(FileSystemTestSuite::testExists_no);
  61. BS_ADD_TEST(FileSystemTestSuite::testGetFileSize_zero);
  62. BS_ADD_TEST(FileSystemTestSuite::testGetFileSize_not_zero);
  63. BS_ADD_TEST(FileSystemTestSuite::testIsFile_yes);
  64. BS_ADD_TEST(FileSystemTestSuite::testIsFile_no);
  65. BS_ADD_TEST(FileSystemTestSuite::testIsDirectory_yes);
  66. BS_ADD_TEST(FileSystemTestSuite::testIsDirectory_no);
  67. BS_ADD_TEST(FileSystemTestSuite::testRemove_file);
  68. BS_ADD_TEST(FileSystemTestSuite::testRemove_directory);
  69. BS_ADD_TEST(FileSystemTestSuite::testMove);
  70. BS_ADD_TEST(FileSystemTestSuite::testMove_overwrite_existing);
  71. BS_ADD_TEST(FileSystemTestSuite::testMove_no_overwrite_existing);
  72. BS_ADD_TEST(FileSystemTestSuite::testCopy);
  73. BS_ADD_TEST(FileSystemTestSuite::testCopy_overwrite_existing);
  74. BS_ADD_TEST(FileSystemTestSuite::testCopy_no_overwrite_existing);
  75. BS_ADD_TEST(FileSystemTestSuite::testGetChildren);
  76. BS_ADD_TEST(FileSystemTestSuite::testGetLastModifiedTime);
  77. BS_ADD_TEST(FileSystemTestSuite::testGetTempDirectoryPath);
  78. }
  79. void FileSystemTestSuite::testExists_yes_file()
  80. {
  81. Path path = mTestDirectory + "plop";
  82. createEmptyFile(path);
  83. BS_TEST_ASSERT(FileSystem::exists(path));
  84. FileSystem::remove(path);
  85. }
  86. void FileSystemTestSuite::testExists_yes_dir()
  87. {
  88. Path path = mTestDirectory + "plop/";
  89. FileSystem::createDir(path);
  90. BS_TEST_ASSERT(FileSystem::exists(path));
  91. FileSystem::remove(path);
  92. }
  93. void FileSystemTestSuite::testExists_no()
  94. {
  95. BS_TEST_ASSERT(!FileSystem::exists(Path("this-file-does-not-exist")));
  96. }
  97. void FileSystemTestSuite::testGetFileSize_zero()
  98. {
  99. Path path = mTestDirectory + "file-size-test-1";
  100. createEmptyFile(path);
  101. BS_TEST_ASSERT(FileSystem::getFileSize(path) == 0);
  102. FileSystem::remove(path);
  103. }
  104. void FileSystemTestSuite::testGetFileSize_not_zero()
  105. {
  106. Path path = mTestDirectory + "file-size-test-2";
  107. createFile(path, "0123456789");
  108. BS_TEST_ASSERT(FileSystem::getFileSize(path) == 10);
  109. FileSystem::remove(path);
  110. }
  111. void FileSystemTestSuite::testIsFile_yes()
  112. {
  113. Path path = mTestDirectory + "some-file-1";
  114. createEmptyFile(path);
  115. BS_TEST_ASSERT(FileSystem::isFile(path));
  116. }
  117. void FileSystemTestSuite::testIsFile_no()
  118. {
  119. Path path = mTestDirectory + "some-directory-1/";
  120. FileSystem::createDir(path);
  121. BS_TEST_ASSERT(!FileSystem::isFile(path));
  122. }
  123. void FileSystemTestSuite::testIsDirectory_yes()
  124. {
  125. Path path = mTestDirectory + "some-directory-2/";
  126. FileSystem::createDir(path);
  127. BS_TEST_ASSERT(FileSystem::isDirectory(path));
  128. }
  129. void FileSystemTestSuite::testIsDirectory_no()
  130. {
  131. Path path = mTestDirectory + "some-file-2";
  132. createEmptyFile(path);
  133. BS_TEST_ASSERT(!FileSystem::isDirectory(path));
  134. }
  135. void FileSystemTestSuite::testRemove_file()
  136. {
  137. Path path = mTestDirectory + "file-to-remove";
  138. createEmptyFile(path);
  139. BS_TEST_ASSERT(FileSystem::exists(path));
  140. FileSystem::remove(path);
  141. BS_TEST_ASSERT(!FileSystem::exists(path));
  142. }
  143. void FileSystemTestSuite::testRemove_directory()
  144. {
  145. Path path = mTestDirectory + "directory-to-remove/";
  146. FileSystem::createDir(path);
  147. BS_TEST_ASSERT(FileSystem::exists(path));
  148. FileSystem::remove(path, true);
  149. BS_TEST_ASSERT(!FileSystem::exists(path));
  150. }
  151. void FileSystemTestSuite::testMove()
  152. {
  153. Path source = mTestDirectory + "move-source-1";
  154. Path destination = mTestDirectory + "move-destination-1";
  155. createFile(source, "move-data-source-1");
  156. BS_TEST_ASSERT(FileSystem::exists(source));
  157. BS_TEST_ASSERT(!FileSystem::exists(destination));
  158. FileSystem::move(source, destination);
  159. BS_TEST_ASSERT(!FileSystem::exists(source));
  160. BS_TEST_ASSERT(FileSystem::exists(destination));
  161. BS_TEST_ASSERT(readFile(destination) == "move-data-source-1");
  162. }
  163. void FileSystemTestSuite::testMove_overwrite_existing()
  164. {
  165. Path source = mTestDirectory + "move-source-2";
  166. Path destination = mTestDirectory + "move-destination-2";
  167. createFile(source, "move-data-source-2");
  168. createFile(destination, "move-data-destination-2");
  169. BS_TEST_ASSERT(FileSystem::exists(source));
  170. BS_TEST_ASSERT(FileSystem::exists(destination));
  171. FileSystem::move(source, destination, true);
  172. BS_TEST_ASSERT(!FileSystem::exists(source));
  173. BS_TEST_ASSERT(FileSystem::exists(destination));
  174. BS_TEST_ASSERT(readFile(destination) == "move-data-source-2");
  175. }
  176. void FileSystemTestSuite::testMove_no_overwrite_existing()
  177. {
  178. Path source = mTestDirectory + "move-source-3";
  179. Path destination = mTestDirectory + "move-destination-3";
  180. createFile(source, "move-data-source-3");
  181. createFile(destination, "move-data-destination-3");
  182. BS_TEST_ASSERT(FileSystem::exists(source));
  183. BS_TEST_ASSERT(FileSystem::exists(destination));
  184. FileSystem::move(source, destination, false);
  185. BS_TEST_ASSERT(FileSystem::exists(source));
  186. BS_TEST_ASSERT(FileSystem::exists(destination));
  187. BS_TEST_ASSERT(readFile(destination) == "move-data-destination-3");
  188. }
  189. void FileSystemTestSuite::testCopy()
  190. {
  191. Path source = mTestDirectory + "copy-source-1";
  192. Path destination = mTestDirectory + "copy-destination-1";
  193. createFile(source, "copy-data-source-1");
  194. BS_TEST_ASSERT(FileSystem::exists(source));
  195. BS_TEST_ASSERT(!FileSystem::exists(destination));
  196. FileSystem::copy(source, destination);
  197. BS_TEST_ASSERT(FileSystem::exists(source));
  198. BS_TEST_ASSERT(FileSystem::exists(destination));
  199. BS_TEST_ASSERT(readFile(source) == "copy-data-source-1");
  200. BS_TEST_ASSERT(readFile(destination) == "copy-data-source-1");
  201. }
  202. void FileSystemTestSuite::testCopy_overwrite_existing()
  203. {
  204. Path source = mTestDirectory + "copy-source-2";
  205. Path destination = mTestDirectory + "copy-destination-2";
  206. createFile(source, "copy-data-source-2");
  207. createFile(destination, "copy-data-destination-2");
  208. BS_TEST_ASSERT(FileSystem::exists(source));
  209. BS_TEST_ASSERT(FileSystem::exists(destination));
  210. FileSystem::copy(source, destination, true);
  211. BS_TEST_ASSERT(FileSystem::exists(source));
  212. BS_TEST_ASSERT(FileSystem::exists(destination));
  213. BS_TEST_ASSERT(readFile(source) == "copy-data-source-2");
  214. BS_TEST_ASSERT(readFile(destination) == "copy-data-source-2");
  215. }
  216. void FileSystemTestSuite::testCopy_no_overwrite_existing()
  217. {
  218. Path source = mTestDirectory + "copy-source-3";
  219. Path destination = mTestDirectory + "copy-destination-3";
  220. createFile(source, "copy-data-source-3");
  221. createFile(destination, "copy-data-destination-3");
  222. BS_TEST_ASSERT(FileSystem::exists(source));
  223. BS_TEST_ASSERT(FileSystem::exists(destination));
  224. FileSystem::copy(source, destination, false);
  225. BS_TEST_ASSERT(FileSystem::exists(source));
  226. BS_TEST_ASSERT(FileSystem::exists(destination));
  227. BS_TEST_ASSERT(readFile(source) == "copy-data-source-3");
  228. BS_TEST_ASSERT(readFile(destination) == "copy-data-destination-3");
  229. }
  230. #define CONTAINS(v, e) (std::find(v.begin(), v.end(), e) != v.end())
  231. void FileSystemTestSuite::testGetChildren()
  232. {
  233. Path path = mTestDirectory + "get-children-test/";
  234. FileSystem::createDir(path);
  235. FileSystem::createDir(path + "foo/");
  236. FileSystem::createDir(path + "bar/");
  237. FileSystem::createDir(path + "baz/");
  238. createEmptyFile(path + "ga");
  239. createEmptyFile(path + "bu");
  240. createEmptyFile(path + "zo");
  241. createEmptyFile(path + "meu");
  242. Vector<Path> files, directories;
  243. FileSystem::getChildren(path, files, directories);
  244. BS_TEST_ASSERT(files.size() == 4);
  245. BS_TEST_ASSERT(CONTAINS(files, path + "ga"));
  246. BS_TEST_ASSERT(CONTAINS(files, path + "bu"));
  247. BS_TEST_ASSERT(CONTAINS(files, path + "zo"));
  248. BS_TEST_ASSERT(CONTAINS(files, path + "meu"));
  249. BS_TEST_ASSERT(directories.size() == 3);
  250. BS_TEST_ASSERT(CONTAINS(directories, path + "foo"));
  251. BS_TEST_ASSERT(CONTAINS(directories, path + "bar"));
  252. BS_TEST_ASSERT(CONTAINS(directories, path + "baz"));
  253. }
  254. void FileSystemTestSuite::testGetLastModifiedTime()
  255. {
  256. std::time_t beforeTime;
  257. time(&beforeTime);
  258. Path path = mTestDirectory + "blah1234";
  259. createFile(path, "blah");
  260. std::time_t mtime = FileSystem::getLastModifiedTime(path);
  261. BS_TEST_ASSERT(mtime >= beforeTime);
  262. BS_TEST_ASSERT(mtime <= beforeTime + 10);
  263. }
  264. void FileSystemTestSuite::testGetTempDirectoryPath()
  265. {
  266. Path path = FileSystem::getTempDirectoryPath();
  267. /* No judging. */
  268. BS_TEST_ASSERT(!path.toString().empty());
  269. }
  270. }