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

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