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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <chrono>
  6. #include <thread>
  7. #include "CodeDweller/filesystem.hpp"
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Configuration ///////////////////////////////////////////////////////////////
  10. ////////////////////////////////////////////////////////////////////////////////
  11. /// Test directory name.
  12. const std::string testDirName("testDir");
  13. /// Test file name.
  14. const std::string testFileName("testFile.txt");
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // End of configuration ////////////////////////////////////////////////////////
  17. ////////////////////////////////////////////////////////////////////////////////
  18. int nTotalTests = 0;
  19. int nPass = 0;
  20. int nFail = 0;
  21. bool result;
  22. #define NO_EXCEPTION_TERM(msg) \
  23. std::cout \
  24. << msg << " failed to throw exception at line " \
  25. << __LINE__ << "." << std::endl
  26. #define EXCEPTION_TERM(msg) \
  27. std::cout \
  28. << msg << " threw unexpected exception: " << e.what() << std::endl
  29. #define RETURN_FALSE(msg) \
  30. std::cout \
  31. << msg << " at line " << __LINE__ << std::endl; \
  32. return false;
  33. #define RUN_TEST(test) \
  34. std::cout << " " #test ": "; \
  35. std::cout.flush(); \
  36. result = test(); \
  37. std::cout << (result ? "ok" : "fail") << std::endl; \
  38. nTotalTests++; \
  39. if (result) nPass++; else nFail++;
  40. #define SUMMARY \
  41. std::cout \
  42. << "\nPass: " << nPass \
  43. << ", Fail: " << nFail \
  44. << ", Total: " << nTotalTests << std::endl
  45. ////////////////////////////////////////////////////////////////////////////////
  46. // Tests ///////////////////////////////////////////////////////////////////////
  47. ////////////////////////////////////////////////////////////////////////////////
  48. long createTestFile(std::string fileName) {
  49. std::ofstream out(fileName.c_str());
  50. std::string contents = "Content";
  51. out << contents;
  52. out.close();
  53. return contents.size();
  54. }
  55. bool
  56. 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
  89. testFileReferenceNoFile() {
  90. try {
  91. std::remove(testFileName.c_str());
  92. CodeDweller::FileReference fileRef(testFileName);
  93. if (0 != fileRef.Size()) {
  94. RETURN_FALSE("Size() failure");
  95. }
  96. if (fileRef.exists()) {
  97. RETURN_FALSE("exists() failure");
  98. }
  99. if (fileRef.isDirectory()) {
  100. RETURN_FALSE("isDirectory() failure");
  101. }
  102. std::string fullPath = fileRef.FullPath();
  103. if (!fullPath.empty()) {
  104. RETURN_FALSE("FullPath() failure");
  105. }
  106. if (0 != fileRef.ModTimestamp()) {
  107. RETURN_FALSE("ModTimestamp() failure");
  108. }
  109. // Create file.
  110. size_t expectedFileSize = createTestFile(testFileName);
  111. fileRef.refresh();
  112. if (expectedFileSize != fileRef.Size()) {
  113. std::cout << "expected: " << expectedFileSize << ", fileRef: "
  114. << fileRef.Size() << "\n";
  115. RETURN_FALSE("Size() failure");
  116. }
  117. if (!fileRef.exists()) {
  118. RETURN_FALSE("exists() failure");
  119. }
  120. if (fileRef.isDirectory()) {
  121. RETURN_FALSE("isDirectory() failure");
  122. }
  123. fullPath = fileRef.FullPath();
  124. if (fullPath.find(testFileName) == std::string::npos) {
  125. RETURN_FALSE("FullPath() failure");
  126. }
  127. } catch (std::exception &e) {
  128. EXCEPTION_TERM("FileReference()");
  129. return false;
  130. }
  131. return true;
  132. }
  133. bool
  134. testFileReferenceDir() {
  135. try {
  136. std::string fileName = testDirName + "/" + testFileName;
  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 diffTimestamp = fileRef.ModTimestamp() - timestamp0;
  155. if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
  156. RETURN_FALSE("ModTimestamp() failure");
  157. }
  158. } catch (std::exception &e) {
  159. EXCEPTION_TERM("FileReference()");
  160. return false;
  161. }
  162. return true;
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. // End of tests ////////////////////////////////////////////////////////////////
  166. ////////////////////////////////////////////////////////////////////////////////
  167. int main()
  168. {
  169. std::cout << "CodeDweller::Filesystem unit tests" << std::endl << std::endl;
  170. RUN_TEST(testFileReferenceFile);
  171. RUN_TEST(testFileReferenceNoFile);
  172. RUN_TEST(testFileReferenceDir);
  173. SUMMARY;
  174. return 0;
  175. }