Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

testFilesystem.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <chrono>
  6. #include <thread>
  7. #include <unordered_set>
  8. #include "CodeDweller/filesystem.hpp"
  9. ////////////////////////////////////////////////////////////////////////////////
  10. // Configuration ///////////////////////////////////////////////////////////////
  11. ////////////////////////////////////////////////////////////////////////////////
  12. /// Test directory name.
  13. const std::string testDirName("testDir");
  14. /// Test file name.
  15. const std::string testFileName("testFile.txt");
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // End of configuration ////////////////////////////////////////////////////////
  18. ////////////////////////////////////////////////////////////////////////////////
  19. int nTotalTests = 0;
  20. int nPass = 0;
  21. int nFail = 0;
  22. bool result;
  23. #define NO_EXCEPTION_TERM(msg) \
  24. std::cout \
  25. << msg << " failed to throw exception at line " \
  26. << __LINE__ << "." << std::endl
  27. #define EXCEPTION_TERM(msg) \
  28. std::cout \
  29. << msg << " threw unexpected exception: " << e.what() << std::endl
  30. #define RETURN_FALSE(msg) \
  31. std::cout \
  32. << msg << " at line " << __LINE__ << std::endl; \
  33. return false;
  34. #define RUN_TEST(test) \
  35. std::cout << " " #test ": "; \
  36. std::cout.flush(); \
  37. result = test(); \
  38. std::cout << (result ? "ok" : "fail") << std::endl; \
  39. nTotalTests++; \
  40. if (result) nPass++; else nFail++;
  41. #define SUMMARY \
  42. std::cout \
  43. << "\nPass: " << nPass \
  44. << ", Fail: " << nFail \
  45. << ", Total: " << nTotalTests << std::endl
  46. ////////////////////////////////////////////////////////////////////////////////
  47. // Tests ///////////////////////////////////////////////////////////////////////
  48. ////////////////////////////////////////////////////////////////////////////////
  49. bool testFilePathJoin() {
  50. char const dirSep = CodeDweller::FilePath::DirectorySeparator;
  51. std::vector<std::string> comp0 = {"comp0", "comp1", "comp2"};
  52. std::string expectedComp;
  53. // Test normal joining.
  54. for (auto &comp : comp0) {
  55. expectedComp += comp + dirSep;
  56. }
  57. expectedComp.pop_back();
  58. if (expectedComp !=
  59. CodeDweller::FilePath::join({comp0[0], comp0[1], comp0[2]})) {
  60. RETURN_FALSE("join() failure");
  61. }
  62. // Test null component.
  63. if (expectedComp !=
  64. CodeDweller::FilePath::join({comp0[0], comp0[1], "", comp0[2]})) {
  65. RETURN_FALSE("join() failure");
  66. }
  67. if (expectedComp !=
  68. CodeDweller::FilePath::join({"", comp0[0], comp0[1], "", comp0[2]})) {
  69. RETURN_FALSE("join() failure");
  70. }
  71. if (expectedComp !=
  72. CodeDweller::FilePath::join({"", comp0[0], comp0[1], "", comp0[2], ""})) {
  73. RETURN_FALSE("join() failure");
  74. }
  75. // Test single input.
  76. expectedComp = "test";
  77. if (expectedComp != CodeDweller::FilePath::join({expectedComp})) {
  78. RETURN_FALSE("join() failure");
  79. }
  80. if (expectedComp != CodeDweller::FilePath::join({"", expectedComp})) {
  81. RETURN_FALSE("join() failure");
  82. }
  83. if (expectedComp != CodeDweller::FilePath::join({"", expectedComp, ""})) {
  84. RETURN_FALSE("join() failure");
  85. }
  86. if (expectedComp != CodeDweller::FilePath::join({"", expectedComp, "", ""})) {
  87. RETURN_FALSE("join() failure");
  88. }
  89. if (expectedComp != CodeDweller::FilePath::join({"", "", expectedComp, ""})) {
  90. RETURN_FALSE("join() failure");
  91. }
  92. }
  93. long createTestFile(std::string fileName) {
  94. std::ofstream out(fileName.c_str());
  95. std::string contents = "Content";
  96. out << contents;
  97. out.close();
  98. return contents.size();
  99. }
  100. bool testFileReferenceFile() {
  101. try {
  102. size_t expectedFileSize = createTestFile(testFileName);
  103. CodeDweller::FileReference fileRef(testFileName);
  104. if (expectedFileSize != fileRef.Size()) {
  105. RETURN_FALSE("Size() failure");
  106. }
  107. if (!fileRef.exists()) {
  108. RETURN_FALSE("exists() failure");
  109. }
  110. if (fileRef.isDirectory()) {
  111. RETURN_FALSE("isDirectory() failure");
  112. }
  113. std::string fullPath = fileRef.FullPath();
  114. if (fullPath.find(testFileName) == std::string::npos) {
  115. RETURN_FALSE("FullPath() failure");
  116. }
  117. // Test timestamp change.
  118. size_t timestamp0 = fileRef.ModTimestamp();
  119. std::this_thread::sleep_for(std::chrono::seconds(5));
  120. (void) createTestFile(testFileName);
  121. fileRef.refresh();
  122. size_t diffTimestamp = fileRef.ModTimestamp() - timestamp0;
  123. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  124. RETURN_FALSE("ModTimestamp() failure");
  125. }
  126. } catch (std::exception &e) {
  127. EXCEPTION_TERM("FileReference()");
  128. return false;
  129. }
  130. return true;
  131. }
  132. bool testFileReferenceNoFile() {
  133. try {
  134. std::remove(testFileName.c_str());
  135. CodeDweller::FileReference fileRef(testFileName);
  136. if (0 != fileRef.Size()) {
  137. RETURN_FALSE("Size() failure");
  138. }
  139. if (fileRef.exists()) {
  140. RETURN_FALSE("exists() failure");
  141. }
  142. if (fileRef.isDirectory()) {
  143. RETURN_FALSE("isDirectory() failure");
  144. }
  145. std::string fullPath = fileRef.FullPath();
  146. if (!fullPath.empty()) {
  147. RETURN_FALSE("FullPath() failure");
  148. }
  149. if (0 != fileRef.ModTimestamp()) {
  150. RETURN_FALSE("ModTimestamp() failure");
  151. }
  152. // Create file.
  153. size_t expectedFileSize = createTestFile(testFileName);
  154. fileRef.refresh();
  155. if (expectedFileSize != fileRef.Size()) {
  156. std::cout << "expected: " << expectedFileSize << ", fileRef: "
  157. << fileRef.Size() << "\n";
  158. RETURN_FALSE("Size() failure");
  159. }
  160. if (!fileRef.exists()) {
  161. RETURN_FALSE("exists() failure");
  162. }
  163. if (fileRef.isDirectory()) {
  164. RETURN_FALSE("isDirectory() failure");
  165. }
  166. fullPath = fileRef.FullPath();
  167. if (fullPath.find(testFileName) == std::string::npos) {
  168. RETURN_FALSE("FullPath() failure");
  169. }
  170. } catch (std::exception &e) {
  171. EXCEPTION_TERM("FileReference()");
  172. return false;
  173. }
  174. return true;
  175. }
  176. bool testFileReferenceDir() {
  177. try {
  178. std::string fileName = testDirName + "/" + testFileName;
  179. std::remove(fileName.c_str());
  180. (void) createTestFile(fileName);
  181. std::remove(fileName.c_str());
  182. CodeDweller::FileReference fileRef(testDirName);
  183. if (!fileRef.exists()) {
  184. RETURN_FALSE("exists() failure");
  185. }
  186. if (!fileRef.isDirectory()) {
  187. RETURN_FALSE("isDirectory() failure");
  188. }
  189. std::string fullPath = fileRef.FullPath();
  190. if (fullPath.find(testDirName) == std::string::npos) {
  191. RETURN_FALSE("FullPath() failure");
  192. }
  193. // Test timestamp change.
  194. size_t timestamp0 = fileRef.ModTimestamp();
  195. std::this_thread::sleep_for(std::chrono::seconds(5));
  196. (void) createTestFile(fileName);
  197. fileRef.refresh();
  198. size_t timestamp1 = fileRef.ModTimestamp();
  199. size_t diffTimestamp = timestamp1 - timestamp0;
  200. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  201. RETURN_FALSE("ModTimestamp() failure");
  202. }
  203. } catch (std::exception &e) {
  204. EXCEPTION_TERM("FileReference()");
  205. return false;
  206. }
  207. return true;
  208. }
  209. // Filter for files whose name contains "f1".
  210. bool filter(std::string name) {
  211. if (name.find("f1") != std::string::npos) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. bool testDirectoryReferenceFiles() {
  217. char const dirSep = CodeDweller::FilePath::DirectorySeparator;
  218. std::unordered_set<std::string> fileNames;
  219. std::string filteredFileName = testDirName + dirSep + "f1.txt";
  220. fileNames.emplace(testDirName + dirSep + testFileName);
  221. fileNames.emplace(filteredFileName);
  222. fileNames.emplace(testDirName + dirSep + "f2.txt");
  223. fileNames.emplace(testDirName + dirSep + "f3.txt");
  224. try {
  225. for (auto &name : fileNames) {
  226. std::remove(name.c_str());
  227. }
  228. CodeDweller::DirectoryReference dirRef(testDirName);
  229. if (dirRef.size() != 2) {
  230. RETURN_FALSE("Incorrect number of files were found by "
  231. "DirectoryReference");
  232. }
  233. for (auto &name : fileNames) {
  234. (void) createTestFile(name);
  235. }
  236. dirRef.refresh();
  237. for (auto &name : fileNames) {
  238. bool foundFile;
  239. foundFile = false;
  240. for (auto &fileRef : dirRef) {
  241. if (fileRef.FullPath().find(name) != std::string::npos) {
  242. foundFile = true;
  243. break;
  244. }
  245. }
  246. if (!foundFile) {
  247. RETURN_FALSE("Not all files were found by DirectoryReference");
  248. }
  249. if (dirRef.size() != fileNames.size() + 2) {
  250. RETURN_FALSE("Incorrect number of files were found by "
  251. "DirectoryReference");
  252. }
  253. }
  254. CodeDweller::DirectoryReference dirRefFilter(testDirName, filter);
  255. bool foundFile = false;
  256. for (auto &fileRef : dirRefFilter) {
  257. if (fileRef.FullPath().find(filteredFileName) != std::string::npos) {
  258. foundFile = true;
  259. break;
  260. }
  261. if (!foundFile) {
  262. RETURN_FALSE("Filtered file was not found by DirectoryReference");
  263. }
  264. if (dirRefFilter.size() != 3) {
  265. RETURN_FALSE("Incorrect number of filtered files were found by "
  266. "DirectoryReference");
  267. }
  268. }
  269. } catch (std::exception &e) {
  270. EXCEPTION_TERM("FileReference()");
  271. return false;
  272. }
  273. #if 0
  274. for (auto &name : fileNames) {
  275. std::remove(name.c_str());
  276. }
  277. #endif
  278. return true;
  279. }
  280. bool testDirectoryReferenceNoDir() {
  281. try {
  282. CodeDweller::DirectoryReference dirRef("Doesntexist");
  283. NO_EXCEPTION_TERM("DirectoryReference with non-existant directory");
  284. return false;
  285. } catch (std::exception &e) {
  286. }
  287. return true;
  288. }
  289. ////////////////////////////////////////////////////////////////////////////////
  290. // End of tests ////////////////////////////////////////////////////////////////
  291. ////////////////////////////////////////////////////////////////////////////////
  292. int main()
  293. {
  294. std::cout << "CodeDweller::Filesystem unit tests" << std::endl << std::endl;
  295. RUN_TEST(testFilePathJoin);
  296. RUN_TEST(testFileReferenceFile);
  297. RUN_TEST(testFileReferenceNoFile);
  298. RUN_TEST(testFileReferenceDir);
  299. RUN_TEST(testDirectoryReferenceFiles);
  300. RUN_TEST(testDirectoryReferenceNoDir);
  301. SUMMARY;
  302. return 0;
  303. }