123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- #include <cstdio>
-
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <chrono>
- #include <thread>
-
- #include "CodeDweller/filesystem.hpp"
-
- ////////////////////////////////////////////////////////////////////////////////
- // Configuration ///////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- /// Test directory name.
- const std::string testDirName("testDir");
-
- /// Test file name.
- const std::string testFileName("testFile.txt");
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of configuration ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int nTotalTests = 0;
- int nPass = 0;
- int nFail = 0;
-
- bool result;
-
- #define NO_EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " failed to throw exception at line " \
- << __LINE__ << "." << std::endl
-
- #define EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " threw unexpected exception: " << e.what() << std::endl
-
- #define RETURN_FALSE(msg) \
- std::cout \
- << msg << " at line " << __LINE__ << std::endl; \
- return false;
-
- #define RUN_TEST(test) \
- std::cout << " " #test ": "; \
- std::cout.flush(); \
- result = test(); \
- std::cout << (result ? "ok" : "fail") << std::endl; \
- nTotalTests++; \
- if (result) nPass++; else nFail++;
-
- #define SUMMARY \
- std::cout \
- << "\nPass: " << nPass \
- << ", Fail: " << nFail \
- << ", Total: " << nTotalTests << std::endl
-
- ////////////////////////////////////////////////////////////////////////////////
- // Tests ///////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- long createTestFile(std::string fileName) {
- std::ofstream out(fileName.c_str());
- std::string contents = "Content";
-
- out << contents;
- out.close();
- return contents.size();
- }
-
- bool
- testFileReferenceFile() {
-
- try {
-
- size_t expectedFileSize = createTestFile(testFileName);
-
- CodeDweller::FileReference fileRef(testFileName);
-
- if (expectedFileSize != fileRef.Size()) {
- RETURN_FALSE("Size() failure");
- }
-
- if (!fileRef.exists()) {
- RETURN_FALSE("exists() failure");
- }
-
- if (fileRef.isDirectory()) {
- RETURN_FALSE("isDirectory() failure");
- }
-
- std::string fullPath = fileRef.FullPath();
- if (fullPath.find(testFileName) == std::string::npos) {
- RETURN_FALSE("FullPath() failure");
- }
-
- // Test timestamp change.
- size_t timestamp0 = fileRef.ModTimestamp();
-
- std::this_thread::sleep_for(std::chrono::seconds(5));
-
- (void) createTestFile(testFileName);
-
- fileRef.refresh();
-
- size_t diffTimestamp = fileRef.ModTimestamp() - timestamp0;
-
- if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
- RETURN_FALSE("ModTimestamp() failure");
- }
- } catch (std::exception &e) {
- EXCEPTION_TERM("FileReference()");
- return false;
- }
-
- return true;
-
- }
-
- bool
- testFileReferenceNoFile() {
-
- try {
-
- std::remove(testFileName.c_str());
-
- CodeDweller::FileReference fileRef(testFileName);
-
- if (0 != fileRef.Size()) {
- RETURN_FALSE("Size() failure");
- }
-
- if (fileRef.exists()) {
- RETURN_FALSE("exists() failure");
- }
-
- if (fileRef.isDirectory()) {
- RETURN_FALSE("isDirectory() failure");
- }
-
- std::string fullPath = fileRef.FullPath();
- if (!fullPath.empty()) {
- RETURN_FALSE("FullPath() failure");
- }
-
- if (0 != fileRef.ModTimestamp()) {
- RETURN_FALSE("ModTimestamp() failure");
- }
-
- // Create file.
- size_t expectedFileSize = createTestFile(testFileName);
-
- fileRef.refresh();
-
- if (expectedFileSize != fileRef.Size()) {
- std::cout << "expected: " << expectedFileSize << ", fileRef: "
- << fileRef.Size() << "\n";
- RETURN_FALSE("Size() failure");
- }
-
- if (!fileRef.exists()) {
- RETURN_FALSE("exists() failure");
- }
-
- if (fileRef.isDirectory()) {
- RETURN_FALSE("isDirectory() failure");
- }
-
- fullPath = fileRef.FullPath();
- if (fullPath.find(testFileName) == std::string::npos) {
- RETURN_FALSE("FullPath() failure");
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("FileReference()");
- return false;
- }
-
- return true;
-
- }
-
- bool
- testFileReferenceDir() {
-
- try {
-
- std::string fileName = testDirName + "/" + testFileName;
-
- std::remove(fileName.c_str());
- (void) createTestFile(fileName);
- std::remove(fileName.c_str());
-
- CodeDweller::FileReference fileRef(testDirName);
-
- if (!fileRef.exists()) {
- RETURN_FALSE("exists() failure");
- }
-
- if (!fileRef.isDirectory()) {
- RETURN_FALSE("isDirectory() failure");
- }
-
- std::string fullPath = fileRef.FullPath();
- if (fullPath.find(testDirName) == std::string::npos) {
- RETURN_FALSE("FullPath() failure");
- }
-
- // Test timestamp change.
- size_t timestamp0 = fileRef.ModTimestamp();
-
- std::this_thread::sleep_for(std::chrono::seconds(5));
-
- (void) createTestFile(fileName);
-
- fileRef.refresh();
-
- size_t timestamp1 = fileRef.ModTimestamp();
- size_t diffTimestamp = timestamp1 - timestamp0;
-
- if ((diffTimestamp < 4) || (6 < diffTimestamp)) {
- RETURN_FALSE("ModTimestamp() failure");
- }
- } catch (std::exception &e) {
- EXCEPTION_TERM("FileReference()");
- return false;
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of tests ////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int main()
- {
- std::cout << "CodeDweller::Filesystem unit tests" << std::endl << std::endl;
-
- RUN_TEST(testFileReferenceFile);
- RUN_TEST(testFileReferenceNoFile);
- RUN_TEST(testFileReferenceDir);
-
- SUMMARY;
-
- return 0;
- }
|