• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List

/www/proggenOrg/dedupe/export/trunk/test/unittest/testenvironment.cpp

Go to the documentation of this file.
00001 
00008 #include <boost/test/unit_test.hpp>
00009 
00010 #include "testenvironment.h"
00011 
00012 Dedupe::Test::TestEnvironment::
00013 TestEnvironment( Dedupe::FilePath RootDirectory ) :
00014 RootDir( boost::filesystem::absolute( RootDirectory,
00015                                     boost::filesystem::current_path() ) )
00016 
00017 {
00018   //init srand only one time
00019   std::srand( time( NULL ) );
00020 }
00021 
00022 size_t Dedupe::Test::TestEnvironment::
00023 CreateRandomInt( size_t Min, size_t Max )
00024 {
00025   if( Max < Min ) return 1;
00026   return  Min + rand() % ( Max - Min + 1 );
00027 }
00028 
00029 Dedupe::FilePath  Dedupe::Test::TestEnvironment::CreateRandomPathPart(
00030                                                       size_t MinLength,
00031                                                       size_t MaxLength )
00032 {
00033   std::string Builder = "";
00034 
00035   if( MaxLength < MinLength ) return "ERROR";
00036 
00037   //make a random size between the constants
00038   size_t length =  CreateRandomInt( MinLength, MaxLength );
00039   do
00040   {
00041     /*select a sign from SignSource per random
00042     SignSource.size() -1 is important, otherwise the
00043     termination sign of the string is used, too*/
00044     Builder += Dedupe::Test::SignSource[
00045                 CreateRandomInt( 0, Dedupe::Test::SignSource.size() -1 ) ];
00046   }while( Builder.size() < length );
00047   Dedupe::FilePath returning( Builder );
00048   return returning;
00049 }
00050 
00051 Dedupe::FilePath Dedupe::Test::TestEnvironment::CreateRandomPathstring()
00052 {
00053   Dedupe::FilePath Path = "";
00054 
00055   size_t Pathparts = CreateRandomInt( MinParts, MaxParts );
00056   size_t i = 0;
00057 
00058   do
00059   {
00060     Path /=  CreateRandomPathPart( MinSize, MaxSize );
00061     i++;
00062   }while( i < Pathparts );
00063 
00064   return Path;
00065 }
00066 
00067 Dedupe::FilePath Dedupe::Test::TestEnvironment::
00068 CreateDirectory( Dedupe::FilePath IncomingPath )
00069 {
00070   //RootDir is const so assign to a new Path Object
00071    Dedupe::FilePath Path( RootDir );
00072    //add the relative path to the root path, now Path is a absolute path
00073    Path /= IncomingPath;
00074 
00075   //normalise path for used plattform
00076   Path.string();
00077 
00078   //create a directory or directories from the given path
00079   //if it fails a error message is written to cerr
00080   try
00081   {
00082     if( boost::filesystem::exists( Path ) ) return Path;
00083 
00084     boost::filesystem::create_directories( Path );
00085   }
00086   catch( boost::filesystem::filesystem_error &e )
00087   {
00088     std::cerr << e.what() << std::endl;
00089     return "";
00090   }
00091   if( boost::filesystem::exists( Path ) ) return Path;
00092   else return "";
00093 }
00094 
00095 Dedupe::FilePath Dedupe::Test::TestEnvironment::
00096 CreateFile( Dedupe::FilePath IncomingPath,
00097                     Dedupe::FilePath IncomingFilename,
00098                     unsigned int FileSize)
00099 {
00100    //RootDir is const so assign to a new Path Object
00101    Dedupe::FilePath Path = RootDir;
00102 
00103    try
00104    {
00105     //test if there is diskspace for the file
00106     boost::filesystem::space_info s =
00107     boost::filesystem::space( Path.parent_path() );
00108     if( s.available < FileSize )
00109     {
00110       std::cerr << "Not enought Diskspace" << std::endl;
00111       return "";
00112     }
00113 
00114     if( CreateDirectory( IncomingPath ) == "" )
00115     {
00116       std::cerr << "Could not create directory " << std::endl;
00117       return "";
00118     }
00119 
00120     Path /= IncomingPath;
00121     Path /= IncomingFilename;
00122     boost::filesystem::ofstream File;
00123 
00124     //open the outputstream to write the file on harddisc
00125     File.open( Path, std::ios_base::out );
00126     if( File == NULL )
00127     {
00128       std::cerr << "Could not open file: " << Path << std::endl;
00129       File.close();
00130       return "";
00131     }
00132 
00133     //add chars to the file until whished size is there
00134     for( size_t i = 0; i != FileSize; ++i )
00135     {
00136       File << Dedupe::Test::SignSource[
00137                 CreateRandomInt( 0, Dedupe::Test::SignSource.size() -1 ) ];
00138     };
00139 
00140     //Close File, if the work is done
00141     File.close();
00142   }
00143   catch( boost::filesystem::filesystem_error &e )
00144   {
00145     std::cerr << e.what() << std::endl;
00146     return "";
00147   }
00148   return Path;
00149 }
00150 
00151 Dedupe::FilePath Dedupe::Test::TestEnvironment::CreateRandomFilename()
00152 {
00153   std::string ReturningPath( "" );
00154   ReturningPath = CreateRandomPathPart( MinSize, MaxSize ).string() ;
00155   ReturningPath += ".";
00156   ReturningPath += CreateRandomPathPart( 1, 5 ).string() ;
00157 
00158   return ReturningPath;
00159 }
00160 
00161 void Dedupe::Test::TestEnvironment::DeleteCreated()
00162 {
00163   boost::system::error_code ec;
00164   boost::filesystem::remove_all( RootDir, ec );
00165   if( ec != 0 )
00166   {
00167     std::cerr << ec.message() << std::endl;
00168   }
00169  }
00170 
00171 
00172 BOOST_FIXTURE_TEST_SUITE( Selftest_TestEnvironment,
00173                           Dedupe::Test::TestEnvironmentSelftestFixture )
00174   //Check if a byte is 8 bits on the system
00175   BOOST_AUTO_TEST_CASE( TestIfByteIsEightBitsLong )
00176   {
00177      BOOST_CHECK( sizeof( char ) == 1 );
00178      BOOST_CHECK( CHAR_BIT == 8 );
00179   }
00180 
00181   BOOST_AUTO_TEST_CASE( TestCreateDirectory )
00182   {
00183     //check if the directory is created
00184     Dedupe::FilePath Path = Env.RootDir;
00185     Path /= "Does/I/Exist/for/sure";
00186     Env.CreateDirectory( "Does/I/Exist/for/sure" );
00187     BOOST_CHECK( boost::filesystem::exists( Path ) );
00188   }
00189 
00190   BOOST_AUTO_TEST_CASE( TestCreateFile )
00191   {
00192     //check if the file is created
00193     Dedupe::FilePath Path = Env.RootDir;
00194     Path /= "Does/I/Exist/for/sure/as/file";
00195     Env.CreateFile( "Does/I/Exist/for/sure/as/file" , "Testfile.file", 6883 );
00196     BOOST_CHECK( boost::filesystem::exists( Path ) );
00197     Path /= "Testfile.file";
00198     BOOST_CHECK_EQUAL( boost::filesystem::file_size( Path ), 6883u );
00199   }
00200  BOOST_AUTO_TEST_SUITE_END()

Generated on Mon Mar 11 2013 12:04:52 for Dedupe by  doxygen 1.7.1