Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 testFilePathIsAbsolute() {
  50. char const dirSep = CodeDweller::FilePath::DirectorySeparator;
  51. std::string testPath;
  52. // Test absolute paths.
  53. testPath = dirSep;
  54. testPath += "filename";
  55. if (!CodeDweller::FilePath::isAbsolute(testPath)) {
  56. RETURN_FALSE("isAbsolute() failure");
  57. }
  58. testPath += dirSep;
  59. testPath += "pathcomponent";
  60. if (!CodeDweller::FilePath::isAbsolute(testPath)) {
  61. RETURN_FALSE("isAbsolute() failure");
  62. }
  63. testPath += dirSep;
  64. if (!CodeDweller::FilePath::isAbsolute(testPath)) {
  65. RETURN_FALSE("isAbsolute() failure");
  66. }
  67. #ifdef _WIN32
  68. if (!CodeDweller::FilePath::isAbsolute("x:sll")) {
  69. RETURN_FALSE("isAbsolute() failure");
  70. }
  71. if (!CodeDweller::FilePath::isAbsolute("x:\\sll")) {
  72. RETURN_FALSE("isAbsolute() failure");
  73. }
  74. if (!CodeDweller::FilePath::isAbsolute("x:\\sll\\")) {
  75. RETURN_FALSE("isAbsolute() failure");
  76. }
  77. if (!CodeDweller::FilePath::isAbsolute("x:\\sll\lll")) {
  78. RETURN_FALSE("isAbsolute() failure");
  79. }
  80. if (!CodeDweller::FilePath::isAbsolute("\\sll")) {
  81. RETURN_FALSE("isAbsolute() failure");
  82. }
  83. if (!CodeDweller::FilePath::isAbsolute("\\sll\\")) {
  84. RETURN_FALSE("isAbsolute() failure");
  85. }
  86. if (!CodeDweller::FilePath::isAbsolute("\\sll\lll")) {
  87. RETURN_FALSE("isAbsolute() failure");
  88. }
  89. #endif
  90. // Test relative paths.
  91. if (CodeDweller::FilePath::isAbsolute("")) {
  92. RETURN_FALSE("isAbsolute() failure");
  93. }
  94. testPath = "Hello";
  95. if (CodeDweller::FilePath::isAbsolute(testPath)) {
  96. RETURN_FALSE("isAbsolute() failure");
  97. }
  98. testPath = "Hello";
  99. testPath += dirSep;
  100. if (CodeDweller::FilePath::isAbsolute(testPath)) {
  101. RETURN_FALSE("isAbsolute() failure");
  102. }
  103. testPath = "Hello";
  104. testPath += dirSep;
  105. testPath += "file";
  106. if (CodeDweller::FilePath::isAbsolute(testPath)) {
  107. RETURN_FALSE("isAbsolute() failure");
  108. }
  109. return true;
  110. }
  111. bool testFilePathJoin() {
  112. char const dirSep = CodeDweller::FilePath::DirectorySeparator;
  113. std::vector<std::string> comp0 = {"comp0", "comp1", "comp2"};
  114. std::string expectedComp;
  115. // Test normal joining.
  116. for (auto &comp : comp0) {
  117. expectedComp += comp + dirSep;
  118. }
  119. expectedComp.pop_back();
  120. if (expectedComp !=
  121. CodeDweller::FilePath::join({comp0[0], comp0[1], comp0[2]})) {
  122. RETURN_FALSE("join() failure");
  123. }
  124. // Test null component.
  125. if (expectedComp !=
  126. CodeDweller::FilePath::join({comp0[0], comp0[1], "", comp0[2]})) {
  127. RETURN_FALSE("join() failure");
  128. }
  129. if (expectedComp !=
  130. CodeDweller::FilePath::join({"", comp0[0], comp0[1], "", comp0[2]})) {
  131. RETURN_FALSE("join() failure");
  132. }
  133. if (expectedComp !=
  134. CodeDweller::FilePath::join({"", comp0[0], comp0[1], "", comp0[2], ""})) {
  135. RETURN_FALSE("join() failure");
  136. }
  137. // Test single input.
  138. expectedComp = "test";
  139. if (expectedComp != CodeDweller::FilePath::join({expectedComp})) {
  140. RETURN_FALSE("join() failure");
  141. }
  142. if (expectedComp != CodeDweller::FilePath::join({"", expectedComp})) {
  143. RETURN_FALSE("join() failure");
  144. }
  145. if (expectedComp != CodeDweller::FilePath::join({"", expectedComp, ""})) {
  146. RETURN_FALSE("join() failure");
  147. }
  148. if (expectedComp != CodeDweller::FilePath::join({"", expectedComp, "", ""})) {
  149. RETURN_FALSE("join() failure");
  150. }
  151. if (expectedComp != CodeDweller::FilePath::join({"", "", expectedComp, ""})) {
  152. RETURN_FALSE("join() failure");
  153. }
  154. try {
  155. (void) CodeDweller::FilePath::join({"rel", "/abs"});
  156. NO_EXCEPTION_TERM("join()");
  157. return false;
  158. } catch (std::exception &e) {
  159. }
  160. try {
  161. (void) CodeDweller::FilePath::join({"rel", "/abs", "otherrel"});
  162. NO_EXCEPTION_TERM("join()");
  163. return false;
  164. } catch (std::exception &e) {
  165. }
  166. return true;
  167. }
  168. long createTestFile(std::string fileName) {
  169. std::ofstream out(fileName.c_str());
  170. std::string contents = "Content";
  171. out << contents;
  172. out.close();
  173. return contents.size();
  174. }
  175. bool testFileReferenceFile() {
  176. try {
  177. size_t expectedFileSize = createTestFile(testFileName);
  178. CodeDweller::FileReference fileRef(testFileName);
  179. if (expectedFileSize != fileRef.Size()) {
  180. RETURN_FALSE("Size() failure");
  181. }
  182. if (!fileRef.exists()) {
  183. RETURN_FALSE("exists() failure");
  184. }
  185. if (fileRef.isDirectory()) {
  186. RETURN_FALSE("isDirectory() failure");
  187. }
  188. std::string fullPath = fileRef.FullPath();
  189. if (fullPath.find(testFileName) == std::string::npos) {
  190. RETURN_FALSE("FullPath() failure");
  191. }
  192. // Test timestamp change.
  193. size_t timestamp0 = fileRef.ModTimestamp();
  194. std::this_thread::sleep_for(std::chrono::seconds(5));
  195. (void) createTestFile(testFileName);
  196. fileRef.refresh();
  197. size_t diffTimestamp = fileRef.ModTimestamp() - timestamp0;
  198. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  199. RETURN_FALSE("ModTimestamp() failure");
  200. }
  201. } catch (std::exception &e) {
  202. EXCEPTION_TERM("FileReference()");
  203. return false;
  204. }
  205. return true;
  206. }
  207. bool testFileReferenceNoFile() {
  208. try {
  209. std::remove(testFileName.c_str());
  210. CodeDweller::FileReference fileRef(testFileName);
  211. if (0 != fileRef.Size()) {
  212. RETURN_FALSE("Size() failure");
  213. }
  214. if (fileRef.exists()) {
  215. RETURN_FALSE("exists() failure");
  216. }
  217. if (fileRef.isDirectory()) {
  218. RETURN_FALSE("isDirectory() failure");
  219. }
  220. std::string fullPath = fileRef.FullPath();
  221. if (!fullPath.empty()) {
  222. RETURN_FALSE("FullPath() failure");
  223. }
  224. if (0 != fileRef.ModTimestamp()) {
  225. RETURN_FALSE("ModTimestamp() failure");
  226. }
  227. // Create file.
  228. size_t expectedFileSize = createTestFile(testFileName);
  229. fileRef.refresh();
  230. if (expectedFileSize != fileRef.Size()) {
  231. std::cout << "expected: " << expectedFileSize << ", fileRef: "
  232. << fileRef.Size() << "\n";
  233. RETURN_FALSE("Size() failure");
  234. }
  235. if (!fileRef.exists()) {
  236. RETURN_FALSE("exists() failure");
  237. }
  238. if (fileRef.isDirectory()) {
  239. RETURN_FALSE("isDirectory() failure");
  240. }
  241. fullPath = fileRef.FullPath();
  242. if (fullPath.find(testFileName) == std::string::npos) {
  243. RETURN_FALSE("FullPath() failure");
  244. }
  245. } catch (std::exception &e) {
  246. EXCEPTION_TERM("FileReference()");
  247. return false;
  248. }
  249. return true;
  250. }
  251. bool testFileReferenceDir() {
  252. try {
  253. std::string fileName = testDirName + "/" + testFileName;
  254. std::remove(fileName.c_str());
  255. (void) createTestFile(fileName);
  256. std::remove(fileName.c_str());
  257. CodeDweller::FileReference fileRef(testDirName);
  258. if (!fileRef.exists()) {
  259. RETURN_FALSE("exists() failure");
  260. }
  261. if (!fileRef.isDirectory()) {
  262. RETURN_FALSE("isDirectory() failure");
  263. }
  264. std::string fullPath = fileRef.FullPath();
  265. if (fullPath.find(testDirName) == std::string::npos) {
  266. RETURN_FALSE("FullPath() failure");
  267. }
  268. // Test timestamp change.
  269. size_t timestamp0 = fileRef.ModTimestamp();
  270. std::this_thread::sleep_for(std::chrono::seconds(5));
  271. (void) createTestFile(fileName);
  272. fileRef.refresh();
  273. size_t timestamp1 = fileRef.ModTimestamp();
  274. size_t diffTimestamp = timestamp1 - timestamp0;
  275. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  276. RETURN_FALSE("ModTimestamp() failure");
  277. }
  278. } catch (std::exception &e) {
  279. EXCEPTION_TERM("FileReference()");
  280. return false;
  281. }
  282. return true;
  283. }
  284. // Filter for files whose name contains "f1".
  285. bool filter(std::string name) {
  286. if (name.find("f1") != std::string::npos) {
  287. return true;
  288. }
  289. return false;
  290. }
  291. bool testDirectoryReferenceFiles() {
  292. char const dirSep = CodeDweller::FilePath::DirectorySeparator;
  293. std::unordered_set<std::string> fileNames;
  294. std::string filteredFileName = testDirName + dirSep + "f1.txt";
  295. fileNames.emplace(testDirName + dirSep + testFileName);
  296. fileNames.emplace(filteredFileName);
  297. fileNames.emplace(testDirName + dirSep + "f2.txt");
  298. fileNames.emplace(testDirName + dirSep + "f3.txt");
  299. try {
  300. for (auto &name : fileNames) {
  301. std::remove(name.c_str());
  302. }
  303. CodeDweller::DirectoryReference dirRef(testDirName);
  304. if (dirRef.size() != 2) {
  305. RETURN_FALSE("Incorrect number of files were found by "
  306. "DirectoryReference");
  307. }
  308. for (auto &name : fileNames) {
  309. (void) createTestFile(name);
  310. }
  311. dirRef.refresh();
  312. for (auto &name : fileNames) {
  313. bool foundFile;
  314. foundFile = false;
  315. for (auto &fileRef : dirRef) {
  316. if (fileRef.FullPath().find(name) != std::string::npos) {
  317. foundFile = true;
  318. break;
  319. }
  320. }
  321. if (!foundFile) {
  322. RETURN_FALSE("Not all files were found by DirectoryReference");
  323. }
  324. if (dirRef.size() != fileNames.size() + 2) {
  325. RETURN_FALSE("Incorrect number of files were found by "
  326. "DirectoryReference");
  327. }
  328. }
  329. CodeDweller::DirectoryReference dirRefFilter(testDirName, filter);
  330. bool foundFile = false;
  331. for (auto &fileRef : dirRefFilter) {
  332. if (fileRef.FullPath().find(filteredFileName) != std::string::npos) {
  333. foundFile = true;
  334. break;
  335. }
  336. if (!foundFile) {
  337. RETURN_FALSE("Filtered file was not found by DirectoryReference");
  338. }
  339. if (dirRefFilter.size() != 3) {
  340. RETURN_FALSE("Incorrect number of filtered files were found by "
  341. "DirectoryReference");
  342. }
  343. }
  344. } catch (std::exception &e) {
  345. EXCEPTION_TERM("FileReference()");
  346. return false;
  347. }
  348. #if 0
  349. for (auto &name : fileNames) {
  350. std::remove(name.c_str());
  351. }
  352. #endif
  353. return true;
  354. }
  355. bool testDirectoryReferenceNoDir() {
  356. try {
  357. CodeDweller::DirectoryReference dirRef("Doesntexist");
  358. NO_EXCEPTION_TERM("DirectoryReference with non-existant directory");
  359. return false;
  360. } catch (std::exception &e) {
  361. }
  362. return true;
  363. }
  364. ////////////////////////////////////////////////////////////////////////////////
  365. // End of tests ////////////////////////////////////////////////////////////////
  366. ////////////////////////////////////////////////////////////////////////////////
  367. int main()
  368. {
  369. std::cout << "CodeDweller::Filesystem unit tests" << std::endl << std::endl;
  370. RUN_TEST(testFilePathIsAbsolute);
  371. RUN_TEST(testFilePathJoin);
  372. RUN_TEST(testFileReferenceFile);
  373. RUN_TEST(testFileReferenceNoFile);
  374. RUN_TEST(testFileReferenceDir);
  375. RUN_TEST(testDirectoryReferenceFiles);
  376. RUN_TEST(testDirectoryReferenceNoDir);
  377. SUMMARY;
  378. return 0;
  379. }