|
|
@@ -0,0 +1,245 @@ |
|
|
|
#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());
|
|
|
|
|
|
|
|
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 diffTimestamp = fileRef.ModTimestamp() - 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;
|
|
|
|
}
|