You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testFilesystem.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. long createTestFile(std::string fileName) {
  50. std::ofstream out(fileName.c_str());
  51. std::string contents = "Content";
  52. out << contents;
  53. out.close();
  54. return contents.size();
  55. }
  56. bool testFileReferenceFile() {
  57. try {
  58. size_t expectedFileSize = createTestFile(testFileName);
  59. CodeDweller::FileReference fileRef(testFileName);
  60. if (expectedFileSize != fileRef.Size()) {
  61. RETURN_FALSE("Size() failure");
  62. }
  63. if (!fileRef.exists()) {
  64. RETURN_FALSE("exists() failure");
  65. }
  66. if (fileRef.isDirectory()) {
  67. RETURN_FALSE("isDirectory() failure");
  68. }
  69. std::string fullPath = fileRef.FullPath();
  70. if (fullPath.find(testFileName) == std::string::npos) {
  71. RETURN_FALSE("FullPath() failure");
  72. }
  73. // Test timestamp change.
  74. size_t timestamp0 = fileRef.ModTimestamp();
  75. std::this_thread::sleep_for(std::chrono::seconds(5));
  76. (void) createTestFile(testFileName);
  77. fileRef.refresh();
  78. size_t diffTimestamp = fileRef.ModTimestamp() - timestamp0;
  79. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  80. RETURN_FALSE("ModTimestamp() failure");
  81. }
  82. } catch (std::exception &e) {
  83. EXCEPTION_TERM("FileReference()");
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool testFileReferenceNoFile() {
  89. try {
  90. std::remove(testFileName.c_str());
  91. CodeDweller::FileReference fileRef(testFileName);
  92. if (0 != fileRef.Size()) {
  93. RETURN_FALSE("Size() failure");
  94. }
  95. if (fileRef.exists()) {
  96. RETURN_FALSE("exists() failure");
  97. }
  98. if (fileRef.isDirectory()) {
  99. RETURN_FALSE("isDirectory() failure");
  100. }
  101. std::string fullPath = fileRef.FullPath();
  102. if (!fullPath.empty()) {
  103. RETURN_FALSE("FullPath() failure");
  104. }
  105. if (0 != fileRef.ModTimestamp()) {
  106. RETURN_FALSE("ModTimestamp() failure");
  107. }
  108. // Create file.
  109. size_t expectedFileSize = createTestFile(testFileName);
  110. fileRef.refresh();
  111. if (expectedFileSize != fileRef.Size()) {
  112. std::cout << "expected: " << expectedFileSize << ", fileRef: "
  113. << fileRef.Size() << "\n";
  114. RETURN_FALSE("Size() failure");
  115. }
  116. if (!fileRef.exists()) {
  117. RETURN_FALSE("exists() failure");
  118. }
  119. if (fileRef.isDirectory()) {
  120. RETURN_FALSE("isDirectory() failure");
  121. }
  122. fullPath = fileRef.FullPath();
  123. if (fullPath.find(testFileName) == std::string::npos) {
  124. RETURN_FALSE("FullPath() failure");
  125. }
  126. } catch (std::exception &e) {
  127. EXCEPTION_TERM("FileReference()");
  128. return false;
  129. }
  130. return true;
  131. }
  132. bool testFileReferenceDir() {
  133. try {
  134. std::string fileName = testDirName + "/" + testFileName;
  135. std::remove(fileName.c_str());
  136. (void) createTestFile(fileName);
  137. std::remove(fileName.c_str());
  138. CodeDweller::FileReference fileRef(testDirName);
  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.find(testDirName) == std::string::npos) {
  147. RETURN_FALSE("FullPath() failure");
  148. }
  149. // Test timestamp change.
  150. size_t timestamp0 = fileRef.ModTimestamp();
  151. std::this_thread::sleep_for(std::chrono::seconds(5));
  152. (void) createTestFile(fileName);
  153. fileRef.refresh();
  154. size_t timestamp1 = fileRef.ModTimestamp();
  155. size_t diffTimestamp = timestamp1 - timestamp0;
  156. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  157. RETURN_FALSE("ModTimestamp() failure");
  158. }
  159. } catch (std::exception &e) {
  160. EXCEPTION_TERM("FileReference()");
  161. return false;
  162. }
  163. return true;
  164. }
  165. // Filter for files whose name contains "f1".
  166. bool filter(std::string name) {
  167. if (name.find("f1") != std::string::npos) {
  168. return true;
  169. }
  170. return false;
  171. }
  172. bool testDirectoryReferenceFiles() {
  173. std::unordered_set<std::string> fileNames;
  174. std::string filteredFileName = testDirName + "/f1.txt";
  175. fileNames.emplace(testDirName + "/" + testFileName);
  176. fileNames.emplace(filteredFileName);
  177. fileNames.emplace(testDirName + "/f2.txt");
  178. fileNames.emplace(testDirName + "/f3.txt");
  179. try {
  180. for (auto &name : fileNames) {
  181. std::remove(name.c_str());
  182. }
  183. CodeDweller::DirectoryReference dirRef(testDirName);
  184. if (dirRef.size() != 2) {
  185. RETURN_FALSE("Incorrect number of files were found by "
  186. "DirectoryReference");
  187. }
  188. for (auto &name : fileNames) {
  189. (void) createTestFile(name);
  190. }
  191. dirRef.refresh();
  192. for (auto &name : fileNames) {
  193. bool foundFile;
  194. foundFile = false;
  195. for (auto &fileRef : dirRef) {
  196. if (fileRef.FullPath().find(name) != std::string::npos) {
  197. foundFile = true;
  198. break;
  199. }
  200. }
  201. if (!foundFile) {
  202. RETURN_FALSE("Not all files were found by DirectoryReference");
  203. }
  204. if (dirRef.size() != fileNames.size() + 2) {
  205. RETURN_FALSE("Incorrect number of files were found by "
  206. "DirectoryReference");
  207. }
  208. }
  209. CodeDweller::DirectoryReference dirRefFilter(testDirName, filter);
  210. bool foundFile = false;
  211. for (auto &fileRef : dirRefFilter) {
  212. if (fileRef.FullPath().find(filteredFileName) != std::string::npos) {
  213. foundFile = true;
  214. break;
  215. }
  216. if (!foundFile) {
  217. RETURN_FALSE("Filtered file was not found by DirectoryReference");
  218. }
  219. if (dirRefFilter.size() != 3) {
  220. RETURN_FALSE("Incorrect number of filtered files were found by "
  221. "DirectoryReference");
  222. }
  223. }
  224. } catch (std::exception &e) {
  225. EXCEPTION_TERM("FileReference()");
  226. return false;
  227. }
  228. for (auto &name : fileNames) {
  229. std::remove(name.c_str());
  230. }
  231. return true;
  232. }
  233. bool testDirectoryReferenceNoDir() {
  234. try {
  235. CodeDweller::DirectoryReference dirRef("Doesntexist");
  236. NO_EXCEPTION_TERM("DirectoryReference with non-existant directory");
  237. return false;
  238. } catch (std::exception &e) {
  239. }
  240. return true;
  241. }
  242. ////////////////////////////////////////////////////////////////////////////////
  243. // End of tests ////////////////////////////////////////////////////////////////
  244. ////////////////////////////////////////////////////////////////////////////////
  245. int main()
  246. {
  247. std::cout << "CodeDweller::Filesystem unit tests" << std::endl << std::endl;
  248. RUN_TEST(testFileReferenceFile);
  249. RUN_TEST(testFileReferenceNoFile);
  250. RUN_TEST(testFileReferenceDir);
  251. RUN_TEST(testDirectoryReferenceFiles);
  252. RUN_TEST(testDirectoryReferenceNoDir);
  253. SUMMARY;
  254. return 0;
  255. }