#include <testenvironment.h>
Public Member Functions | |
TestEnvironment (Dedupe::FilePath RootDirectory) | |
size_t | CreateRandomInt (size_t Min, size_t Max) |
Dedupe::FilePath | CreateRandomPathPart (size_t MinLength, size_t MaxLength) |
Dedupe::FilePath | CreateRandomPathstring () |
Dedupe::FilePath | CreateRandomFilename () |
Dedupe::FilePath | CreateDirectory (Dedupe::FilePath IncomingPath) |
Dedupe::FilePath | CreateFile (Dedupe::FilePath IncomingPath, Dedupe::FilePath IncomingFilename, unsigned int FileSize) |
void | DeleteCreated () |
Data Fields | |
const Dedupe::FilePath | RootDir |
TestEnvironment can create files and directories randomly. It is used for unittests
Definition at line 53 of file testenvironment.h.
Dedupe::Test::TestEnvironment::TestEnvironment | ( | Dedupe::FilePath | RootDirectory | ) |
Give a path object with the name of the root order Make sure the Paths contains only one deep, cause DeleteCreated() only deletes the last part of a given directorypath
Definition at line 13 of file testenvironment.cpp.
: RootDir( boost::filesystem::absolute( RootDirectory, boost::filesystem::current_path() ) ) { //init srand only one time std::srand( time( NULL ) ); }
Dedupe::FilePath Dedupe::Test::TestEnvironment::CreateDirectory | ( | Dedupe::FilePath | IncomingPath | ) |
Creates a directory or a directory tree from the given path.
Definition at line 68 of file testenvironment.cpp.
References RootDir.
Referenced by CreateFile().
{ //RootDir is const so assign to a new Path Object Dedupe::FilePath Path( RootDir ); //add the relative path to the root path, now Path is a absolute path Path /= IncomingPath; //normalise path for used plattform Path.string(); //create a directory or directories from the given path //if it fails a error message is written to cerr try { if( boost::filesystem::exists( Path ) ) return Path; boost::filesystem::create_directories( Path ); } catch( boost::filesystem::filesystem_error &e ) { std::cerr << e.what() << std::endl; return ""; } if( boost::filesystem::exists( Path ) ) return Path; else return ""; }
Dedupe::FilePath Dedupe::Test::TestEnvironment::CreateFile | ( | Dedupe::FilePath | IncomingPath, | |
Dedupe::FilePath | IncomingFilename, | |||
unsigned int | FileSize | |||
) |
Creates a file at the given path.If the path don't exits, it will be created. If a char is 1 byte on your prefered system then the FileSize should be exact.
Definition at line 96 of file testenvironment.cpp.
References CreateDirectory(), CreateRandomInt(), RootDir, and Dedupe::Test::SignSource.
{ //RootDir is const so assign to a new Path Object Dedupe::FilePath Path = RootDir; try { //test if there is diskspace for the file boost::filesystem::space_info s = boost::filesystem::space( Path.parent_path() ); if( s.available < FileSize ) { std::cerr << "Not enought Diskspace" << std::endl; return ""; } if( CreateDirectory( IncomingPath ) == "" ) { std::cerr << "Could not create directory " << std::endl; return ""; } Path /= IncomingPath; Path /= IncomingFilename; boost::filesystem::ofstream File; //open the outputstream to write the file on harddisc File.open( Path, std::ios_base::out ); if( File == NULL ) { std::cerr << "Could not open file: " << Path << std::endl; File.close(); return ""; } //add chars to the file until whished size is there for( size_t i = 0; i != FileSize; ++i ) { File << Dedupe::Test::SignSource[ CreateRandomInt( 0, Dedupe::Test::SignSource.size() -1 ) ]; }; //Close File, if the work is done File.close(); } catch( boost::filesystem::filesystem_error &e ) { std::cerr << e.what() << std::endl; return ""; } return Path; }
Dedupe::FilePath Dedupe::Test::TestEnvironment::CreateRandomFilename | ( | ) |
Creates a random Filename with extension. The size of the filename is defined with MinSize and MaxSize. The length of the extension is between 1 and 5 letters
Definition at line 151 of file testenvironment.cpp.
References CreateRandomPathPart(), Dedupe::Test::MaxSize, and Dedupe::Test::MinSize.
{ std::string ReturningPath( "" ); ReturningPath = CreateRandomPathPart( MinSize, MaxSize ).string() ; ReturningPath += "."; ReturningPath += CreateRandomPathPart( 1, 5 ).string() ; return ReturningPath; }
size_t Dedupe::Test::TestEnvironment::CreateRandomInt | ( | size_t | Min, | |
size_t | Max | |||
) |
Creates random int between the two given values. If Min is greater than Max the function returns 1;
Definition at line 23 of file testenvironment.cpp.
Referenced by CreateFile(), CreateRandomPathPart(), and CreateRandomPathstring().
{ if( Max < Min ) return 1; return Min + rand() % ( Max - Min + 1 ); }
Dedupe::FilePath Dedupe::Test::TestEnvironment::CreateRandomPathPart | ( | size_t | MinLength, | |
size_t | MaxLength | |||
) |
Creates a random string with a size between the given values. If MinLength ist greater than MaxLength, the function returns "ERROR"
Definition at line 29 of file testenvironment.cpp.
References CreateRandomInt(), and Dedupe::Test::SignSource.
Referenced by CreateRandomFilename(), and CreateRandomPathstring().
{ std::string Builder = ""; if( MaxLength < MinLength ) return "ERROR"; //make a random size between the constants size_t length = CreateRandomInt( MinLength, MaxLength ); do { /*select a sign from SignSource per random SignSource.size() -1 is important, otherwise the termination sign of the string is used, too*/ Builder += Dedupe::Test::SignSource[ CreateRandomInt( 0, Dedupe::Test::SignSource.size() -1 ) ]; }while( Builder.size() < length ); Dedupe::FilePath returning( Builder ); return returning; }
Dedupe::FilePath Dedupe::Test::TestEnvironment::CreateRandomPathstring | ( | ) |
Creates a random Path, while using the defined constants upside this headerfile
Definition at line 51 of file testenvironment.cpp.
References CreateRandomInt(), CreateRandomPathPart(), Dedupe::Test::MaxParts, Dedupe::Test::MaxSize, Dedupe::Test::MinParts, and Dedupe::Test::MinSize.
{ Dedupe::FilePath Path = ""; size_t Pathparts = CreateRandomInt( MinParts, MaxParts ); size_t i = 0; do { Path /= CreateRandomPathPart( MinSize, MaxSize ); i++; }while( i < Pathparts ); return Path; }
void Dedupe::Test::TestEnvironment::DeleteCreated | ( | ) |
Deletes all created files, while deleting the Root order from constructor
Definition at line 161 of file testenvironment.cpp.
References RootDir.
{ boost::system::error_code ec; boost::filesystem::remove_all( RootDir, ec ); if( ec != 0 ) { std::cerr << ec.message() << std::endl; } }
const Dedupe::FilePath Dedupe::Test::TestEnvironment::RootDir |
Holds the absolute path to the order from the constructor
Definition at line 119 of file testenvironment.h.
Referenced by CreateDirectory(), CreateFile(), and DeleteCreated().