소스 검색

Tested FileReference.


git-svn-id: https://svn.microneil.com/svn/CodeDweller-Tests/trunk@31 b3372362-9eaa-4a85-aa2b-6faa1ab7c995
master
adeniz 9 년 전
부모
커밋
3d27b4faba
2개의 변경된 파일254개의 추가작업 그리고 0개의 파일을 삭제
  1. 9
    0
      TestFilesystem/buildAndRun
  2. 245
    0
      TestFilesystem/testFilesystem.cpp

+ 9
- 0
TestFilesystem/buildAndRun 파일 보기

@@ -0,0 +1,9 @@
CFLAGS='-I.. -std=c++11 -g -O0'
g++ $CFLAGS testFilesystem.cpp ../CodeDweller/filesystem.cpp -o testFilesystem
if [ $? -ne 0 ]
then
exit -1
fi

mkdir -p testDir
./testFilesystem

+ 245
- 0
TestFilesystem/testFilesystem.cpp 파일 보기

@@ -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;
}

Loading…
취소
저장