123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- #include <cstdio>
-
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <chrono>
- #include <thread>
- #include <unordered_set>
-
- #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 ///////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- bool testFileOpsMoveFile() {
-
- std::string fromName = "from.txt";
- std::string toName = "to.txt";
- std::string const expectedContent = "fileContentForMoveTest";
- std::string content;
-
- // Test nominal case.
- std::ofstream out(fromName.c_str());
- out << expectedContent;
- out.close();
-
- CodeDweller::FileOps::moveFile(fromName, toName);
-
- std::ifstream in(toName.c_str());
- in >> content;
- in.close();
-
- if (expectedContent != content) {
- RETURN_FALSE("FileOps::moveFile() failure");
- }
-
- // Test moving to a file that exists.
- out.open(fromName.c_str());
- out << expectedContent;
- out.close();
-
- out.open(toName.c_str());
- out << "not" + expectedContent;
- out.close();
-
- CodeDweller::FileOps::moveFile(fromName, toName);
-
- in.open(toName.c_str());
- in >> content;
- in.close();
-
- if (expectedContent != content) {
- RETURN_FALSE("FileOps::moveFile() failure");
- }
-
- // Test moving file that doesn't exist.
- try {
-
- (void) CodeDweller::FileOps::moveFile("doesNotExist", "alsoDoesNotExist");
-
- NO_EXCEPTION_TERM("FileOps::moveFile()");
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- return true;
-
- }
-
- bool testFilePathIsAbsolute() {
-
- char const dirSep = CodeDweller::FilePath::DirectorySeparator;
- std::string testPath;
-
- // Test absolute paths.
- testPath = dirSep;
- testPath += "filename";
- if (!CodeDweller::FilePath::isAbsolute(testPath)) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- testPath += dirSep;
- testPath += "pathcomponent";
- if (!CodeDweller::FilePath::isAbsolute(testPath)) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- testPath += dirSep;
- if (!CodeDweller::FilePath::isAbsolute(testPath)) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- #ifdef _WIN32
- if (!CodeDweller::FilePath::isAbsolute("x:sll")) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- if (!CodeDweller::FilePath::isAbsolute("x:\\sll")) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- if (!CodeDweller::FilePath::isAbsolute("x:\\sll\\")) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- if (!CodeDweller::FilePath::isAbsolute("x:\\sll\\lll")) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- if (!CodeDweller::FilePath::isAbsolute("\\sll")) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- if (!CodeDweller::FilePath::isAbsolute("\\sll\\")) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- if (!CodeDweller::FilePath::isAbsolute("\\sll\\lll")) {
- RETURN_FALSE("isAbsolute() failure");
- }
- #endif
-
- // Test relative paths.
- if (CodeDweller::FilePath::isAbsolute("")) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- testPath = "Hello";
- if (CodeDweller::FilePath::isAbsolute(testPath)) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- testPath = "Hello";
- testPath += dirSep;
- if (CodeDweller::FilePath::isAbsolute(testPath)) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- testPath = "Hello";
- testPath += dirSep;
- testPath += "file";
- if (CodeDweller::FilePath::isAbsolute(testPath)) {
- RETURN_FALSE("isAbsolute() failure");
- }
-
- return true;
-
- }
-
- bool testFilePathJoin() {
-
- char const dirSep = CodeDweller::FilePath::DirectorySeparator;
- std::vector<std::string> comp0 = {"comp0", "comp1", "comp2"};
- std::string expectedComp;
-
- // Test normal joining.
- for (auto &comp : comp0) {
- expectedComp += comp + dirSep;
- }
- expectedComp.pop_back();
-
- if (expectedComp !=
- CodeDweller::FilePath::join({comp0[0], comp0[1], comp0[2]})) {
- RETURN_FALSE("join() failure");
- }
-
- // Test null component.
- if (expectedComp !=
- CodeDweller::FilePath::join({comp0[0], comp0[1], "", comp0[2]})) {
- RETURN_FALSE("join() failure");
- }
-
- if (expectedComp !=
- CodeDweller::FilePath::join({"", comp0[0], comp0[1], "", comp0[2]})) {
- RETURN_FALSE("join() failure");
- }
-
- if (expectedComp !=
- CodeDweller::FilePath::join({"", comp0[0], comp0[1], "", comp0[2], ""})) {
- RETURN_FALSE("join() failure");
- }
-
- // Test single input.
- expectedComp = "test";
- if (expectedComp != CodeDweller::FilePath::join({expectedComp})) {
- RETURN_FALSE("join() failure");
- }
-
- if (expectedComp != CodeDweller::FilePath::join({"", expectedComp})) {
- RETURN_FALSE("join() failure");
- }
-
- if (expectedComp != CodeDweller::FilePath::join({"", expectedComp, ""})) {
- RETURN_FALSE("join() failure");
- }
-
- if (expectedComp != CodeDweller::FilePath::join({"", expectedComp, "", ""})) {
- RETURN_FALSE("join() failure");
- }
-
- if (expectedComp != CodeDweller::FilePath::join({"", "", expectedComp, ""})) {
- RETURN_FALSE("join() failure");
- }
-
- #ifdef _WIN32
- std::string absComponent = "\\abs";
- #else
- std::string absComponent = "/abs";
- #endif
-
- try {
-
- (void) CodeDweller::FilePath::join({"rel", absComponent});
-
- NO_EXCEPTION_TERM("join()");
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- try {
-
- (void) CodeDweller::FilePath::join({"rel", absComponent, "otherrel"});
-
- NO_EXCEPTION_TERM("join()");
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- return true;
-
- }
-
- 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;
-
- }
-
- // Filter for files whose name contains "f1".
- bool filter(std::string name) {
- if (name.find("f1") != std::string::npos) {
- return true;
- }
- return false;
- }
-
- bool testDirectoryReferenceFiles() {
-
- char const dirSep = CodeDweller::FilePath::DirectorySeparator;
- std::unordered_set<std::string> fileNames;
- std::string filteredFileName = testDirName + dirSep + "f1.txt";
-
- fileNames.emplace(testDirName + dirSep + testFileName);
- fileNames.emplace(filteredFileName);
- fileNames.emplace(testDirName + dirSep + "f2.txt");
- fileNames.emplace(testDirName + dirSep + "f3.txt");
-
- try {
-
- for (auto &name : fileNames) {
- std::remove(name.c_str());
- }
-
- CodeDweller::DirectoryReference dirRef(testDirName);
-
- if (dirRef.size() != 2) {
- RETURN_FALSE("Incorrect number of files were found by "
- "DirectoryReference");
- }
-
- for (auto &name : fileNames) {
- (void) createTestFile(name);
- }
-
- dirRef.refresh();
-
- for (auto &name : fileNames) {
-
- bool foundFile;
-
- foundFile = false;
-
- for (auto &fileRef : dirRef) {
- if (fileRef.FullPath().find(name) != std::string::npos) {
- foundFile = true;
- break;
- }
- }
-
- if (!foundFile) {
- RETURN_FALSE("Not all files were found by DirectoryReference");
- }
-
- if (dirRef.size() != fileNames.size() + 2) {
- RETURN_FALSE("Incorrect number of files were found by "
- "DirectoryReference");
- }
-
- }
-
- CodeDweller::DirectoryReference dirRefFilter(testDirName, filter);
-
- bool foundFile = false;
- for (auto &fileRef : dirRefFilter) {
- if (fileRef.FullPath().find(filteredFileName) != std::string::npos) {
- foundFile = true;
- break;
- }
-
- if (!foundFile) {
- RETURN_FALSE("Filtered file was not found by DirectoryReference");
- }
-
- if (dirRefFilter.size() != 3) {
- RETURN_FALSE("Incorrect number of filtered files were found by "
- "DirectoryReference");
- }
-
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("FileReference()");
- return false;
- }
-
- #if 0
- for (auto &name : fileNames) {
- std::remove(name.c_str());
- }
- #endif
- return true;
-
- }
-
- bool testDirectoryReferenceNoDir() {
-
- try {
-
- CodeDweller::DirectoryReference dirRef("Doesntexist");
-
- NO_EXCEPTION_TERM("DirectoryReference with non-existant directory");
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of tests ////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int main()
- {
- std::cout << "CodeDweller::Filesystem unit tests" << std::endl << std::endl;
-
- RUN_TEST(testFileOpsMoveFile);
- RUN_TEST(testFilePathIsAbsolute);
- RUN_TEST(testFilePathJoin);
- RUN_TEST(testFileReferenceFile);
- RUN_TEST(testFileReferenceNoFile);
- RUN_TEST(testFileReferenceDir);
- RUN_TEST(testDirectoryReferenceFiles);
- RUN_TEST(testDirectoryReferenceNoDir);
- SUMMARY;
-
- return 0;
- }
|