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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. #ifdef _WIN32
  155. std::string absComponent = "\\abs";
  156. #else
  157. std::string absComponent = "/abs";
  158. #endif
  159. try {
  160. (void) CodeDweller::FilePath::join({"rel", absComponent});
  161. NO_EXCEPTION_TERM("join()");
  162. return false;
  163. } catch (std::exception &e) {
  164. }
  165. try {
  166. (void) CodeDweller::FilePath::join({"rel", absComponent, "otherrel"});
  167. NO_EXCEPTION_TERM("join()");
  168. return false;
  169. } catch (std::exception &e) {
  170. }
  171. return true;
  172. }
  173. long createTestFile(std::string fileName) {
  174. std::ofstream out(fileName.c_str());
  175. std::string contents = "Content";
  176. out << contents;
  177. out.close();
  178. return contents.size();
  179. }
  180. bool testFileReferenceFile() {
  181. try {
  182. size_t expectedFileSize = createTestFile(testFileName);
  183. CodeDweller::FileReference fileRef(testFileName);
  184. if (expectedFileSize != fileRef.Size()) {
  185. RETURN_FALSE("Size() failure");
  186. }
  187. if (!fileRef.exists()) {
  188. RETURN_FALSE("exists() failure");
  189. }
  190. if (fileRef.isDirectory()) {
  191. RETURN_FALSE("isDirectory() failure");
  192. }
  193. std::string fullPath = fileRef.FullPath();
  194. if (fullPath.find(testFileName) == std::string::npos) {
  195. RETURN_FALSE("FullPath() failure");
  196. }
  197. // Test timestamp change.
  198. size_t timestamp0 = fileRef.ModTimestamp();
  199. std::this_thread::sleep_for(std::chrono::seconds(5));
  200. (void) createTestFile(testFileName);
  201. fileRef.refresh();
  202. size_t diffTimestamp = fileRef.ModTimestamp() - timestamp0;
  203. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  204. RETURN_FALSE("ModTimestamp() failure");
  205. }
  206. } catch (std::exception &e) {
  207. EXCEPTION_TERM("FileReference()");
  208. return false;
  209. }
  210. return true;
  211. }
  212. bool testFileReferenceNoFile() {
  213. try {
  214. std::remove(testFileName.c_str());
  215. CodeDweller::FileReference fileRef(testFileName);
  216. if (0 != fileRef.Size()) {
  217. RETURN_FALSE("Size() failure");
  218. }
  219. if (fileRef.exists()) {
  220. RETURN_FALSE("exists() failure");
  221. }
  222. if (fileRef.isDirectory()) {
  223. RETURN_FALSE("isDirectory() failure");
  224. }
  225. std::string fullPath = fileRef.FullPath();
  226. if (!fullPath.empty()) {
  227. RETURN_FALSE("FullPath() failure");
  228. }
  229. if (0 != fileRef.ModTimestamp()) {
  230. RETURN_FALSE("ModTimestamp() failure");
  231. }
  232. // Create file.
  233. size_t expectedFileSize = createTestFile(testFileName);
  234. fileRef.refresh();
  235. if (expectedFileSize != fileRef.Size()) {
  236. std::cout << "expected: " << expectedFileSize << ", fileRef: "
  237. << fileRef.Size() << "\n";
  238. RETURN_FALSE("Size() failure");
  239. }
  240. if (!fileRef.exists()) {
  241. RETURN_FALSE("exists() failure");
  242. }
  243. if (fileRef.isDirectory()) {
  244. RETURN_FALSE("isDirectory() failure");
  245. }
  246. fullPath = fileRef.FullPath();
  247. if (fullPath.find(testFileName) == std::string::npos) {
  248. RETURN_FALSE("FullPath() failure");
  249. }
  250. } catch (std::exception &e) {
  251. EXCEPTION_TERM("FileReference()");
  252. return false;
  253. }
  254. return true;
  255. }
  256. bool testFileReferenceDir() {
  257. try {
  258. std::string fileName = testDirName + "/" + testFileName;
  259. std::remove(fileName.c_str());
  260. (void) createTestFile(fileName);
  261. std::remove(fileName.c_str());
  262. CodeDweller::FileReference fileRef(testDirName);
  263. if (!fileRef.exists()) {
  264. RETURN_FALSE("exists() failure");
  265. }
  266. if (!fileRef.isDirectory()) {
  267. RETURN_FALSE("isDirectory() failure");
  268. }
  269. std::string fullPath = fileRef.FullPath();
  270. if (fullPath.find(testDirName) == std::string::npos) {
  271. RETURN_FALSE("FullPath() failure");
  272. }
  273. // Test timestamp change.
  274. size_t timestamp0 = fileRef.ModTimestamp();
  275. std::this_thread::sleep_for(std::chrono::seconds(5));
  276. (void) createTestFile(fileName);
  277. fileRef.refresh();
  278. size_t timestamp1 = fileRef.ModTimestamp();
  279. size_t diffTimestamp = timestamp1 - timestamp0;
  280. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  281. RETURN_FALSE("ModTimestamp() failure");
  282. }
  283. } catch (std::exception &e) {
  284. EXCEPTION_TERM("FileReference()");
  285. return false;
  286. }
  287. return true;
  288. }
  289. // Filter for files whose name contains "f1".
  290. bool filter(std::string name) {
  291. if (name.find("f1") != std::string::npos) {
  292. return true;
  293. }
  294. return false;
  295. }
  296. bool testDirectoryReferenceFiles() {
  297. char const dirSep = CodeDweller::FilePath::DirectorySeparator;
  298. std::unordered_set<std::string> fileNames;
  299. std::string filteredFileName = testDirName + dirSep + "f1.txt";
  300. fileNames.emplace(testDirName + dirSep + testFileName);
  301. fileNames.emplace(filteredFileName);
  302. fileNames.emplace(testDirName + dirSep + "f2.txt");
  303. fileNames.emplace(testDirName + dirSep + "f3.txt");
  304. try {
  305. for (auto &name : fileNames) {
  306. std::remove(name.c_str());
  307. }
  308. CodeDweller::DirectoryReference dirRef(testDirName);
  309. if (dirRef.size() != 2) {
  310. RETURN_FALSE("Incorrect number of files were found by "
  311. "DirectoryReference");
  312. }
  313. for (auto &name : fileNames) {
  314. (void) createTestFile(name);
  315. }
  316. dirRef.refresh();
  317. for (auto &name : fileNames) {
  318. bool foundFile;
  319. foundFile = false;
  320. for (auto &fileRef : dirRef) {
  321. if (fileRef.FullPath().find(name) != std::string::npos) {
  322. foundFile = true;
  323. break;
  324. }
  325. }
  326. if (!foundFile) {
  327. RETURN_FALSE("Not all files were found by DirectoryReference");
  328. }
  329. if (dirRef.size() != fileNames.size() + 2) {
  330. RETURN_FALSE("Incorrect number of files were found by "
  331. "DirectoryReference");
  332. }
  333. }
  334. CodeDweller::DirectoryReference dirRefFilter(testDirName, filter);
  335. bool foundFile = false;
  336. for (auto &fileRef : dirRefFilter) {
  337. if (fileRef.FullPath().find(filteredFileName) != std::string::npos) {
  338. foundFile = true;
  339. break;
  340. }
  341. if (!foundFile) {
  342. RETURN_FALSE("Filtered file was not found by DirectoryReference");
  343. }
  344. if (dirRefFilter.size() != 3) {
  345. RETURN_FALSE("Incorrect number of filtered files were found by "
  346. "DirectoryReference");
  347. }
  348. }
  349. } catch (std::exception &e) {
  350. EXCEPTION_TERM("FileReference()");
  351. return false;
  352. }
  353. #if 0
  354. for (auto &name : fileNames) {
  355. std::remove(name.c_str());
  356. }
  357. #endif
  358. return true;
  359. }
  360. bool testDirectoryReferenceNoDir() {
  361. try {
  362. CodeDweller::DirectoryReference dirRef("Doesntexist");
  363. NO_EXCEPTION_TERM("DirectoryReference with non-existant directory");
  364. return false;
  365. } catch (std::exception &e) {
  366. }
  367. return true;
  368. }
  369. ////////////////////////////////////////////////////////////////////////////////
  370. // End of tests ////////////////////////////////////////////////////////////////
  371. ////////////////////////////////////////////////////////////////////////////////
  372. int main()
  373. {
  374. std::cout << "CodeDweller::Filesystem unit tests" << std::endl << std::endl;
  375. RUN_TEST(testFilePathIsAbsolute);
  376. RUN_TEST(testFilePathJoin);
  377. RUN_TEST(testFileReferenceFile);
  378. RUN_TEST(testFileReferenceNoFile);
  379. RUN_TEST(testFileReferenceDir);
  380. RUN_TEST(testDirectoryReferenceFiles);
  381. RUN_TEST(testDirectoryReferenceNoDir);
  382. SUMMARY;
  383. return 0;
  384. }