Fix/implement more unit tests that were marked as 'skipped'

This commit is contained in:
Brian Cox 2017-09-24 18:44:47 -07:00
parent 1333f3c15e
commit 9872bef2f2
9 changed files with 156 additions and 297 deletions

View File

@ -46,105 +46,60 @@
#endif
#include <string>
#include <ctime>
///////////////////////////////////////////////////////////////////////////////
// cTaskTimer --
///////////////////////////////////////////////////////////////////////////////
template<class TIME_FN, class TIME_TYPE>
class cTaskTimer
{
public:
cTaskTimer(const TSTRING& name) : mName(name), mTotalTime(0), mStartTime(0), mNumStarts(0) {}
~cTaskTimer();
void Start();
void Stop();
inline ~cTaskTimer()
{
// stop the timer if it is currently running
if(IsRunning())
Stop();
// trace out the time contents...
cDebug d("cTaskTimer");
d.TraceDebug("----- Time to execute %s: %d (started %d times)\n", mName.c_str(), mTotalTime, mNumStarts);
}
inline void Start()
{
ASSERT(! IsRunning());
time_t dummy;
mStartTime = time(&dummy);
mNumStarts++;
}
void Stop()
{
ASSERT(IsRunning());
time_t dummy;
mTotalTime += ( time(&dummy) - mStartTime );
mStartTime = 0;
}
bool IsRunning() { return (mStartTime != 0); }
void Reset() { mNumStarts = mStartTime = mTotalTime = 0 }
int32 GetTotalTime() const;
int32 GetNumTimesStarted() const; // returns the number of times start() was called
const std::string& GetName() const;
void Reset() { mNumStarts = mStartTime = mTotalTime = 0; }
uint32 GetTotalTime() const { return mTotalTime; }
uint32 GetNumTimesStarted() const { return mNumStarts; }
const std::string& GetName() const { return mName; }
private:
TSTRING mName;
int32 mTotalTime;
TIME_TYPE mStartTime;
int32 mNumStarts;
uint32 mTotalTime;
uint32 mStartTime;
uint32 mNumStarts;
};
///////////////////////////////////////////////////////////////////////////////
// cUnixTimeFn -- Unix version, inserts proper function call and overloads
// operator()
///////////////////////////////////////////////////////////////////////////////
#include <ctime>
class cUnixTimeFn
{
public:
typedef uint32 DataType;
uint32 operator()()
{
return time( &dummy_var );
}
private:
time_t dummy_var;
};
///////////////////////////////////////////////////////////////////////////////
// cUnixTaskTimer -- the Unix typedef...
///////////////////////////////////////////////////////////////////////////////
typedef cTaskTimer<cUnixTimeFn, cUnixTimeFn::DataType> cUnixTaskTimer;
typedef cUnixTaskTimer cGenericTaskTimer;
//-----------------------------------------------------------------------------
// inline implementation
//-----------------------------------------------------------------------------
template<class TIME_FN, class TIME_TYPE>
inline void cTaskTimer<TIME_FN, TIME_TYPE>::Start()
{
ASSERT(! IsRunning());
TIME_FN GetTime;
mStartTime = GetTime();
mNumStarts++;
}
template<class TIME_FN, class TIME_TYPE>
inline void cTaskTimer<TIME_FN, TIME_TYPE>::Stop()
{
ASSERT(IsRunning());
TIME_FN GetTime;
mTotalTime += ( GetTime() - mStartTime );
mStartTime = 0;
}
template<class TIME_FN, class TIME_TYPE>
inline int32 cTaskTimer<TIME_FN, TIME_TYPE>::GetTotalTime() const
{
return mTotalTime;
}
template<class TIME_FN, class TIME_TYPE>
inline const std::string& cTaskTimer<TIME_FN, TIME_TYPE>::GetName() const
{
return mName
}
template<class TIME_FN, class TIME_TYPE>
inline cTaskTimer<TIME_FN, TIME_TYPE>::~cTaskTimer()
{
// stop the timer if it is currently running
if(IsRunning())
Stop();
// trace out the time contents...
cDebug d("cTaskTimer");
d.TraceDebug("----- Time to execute %s: %d (started %d times)\n", mName.c_str(), mTotalTime, mNumStarts);
}
#endif

View File

@ -751,7 +751,7 @@ int cTWModeDbInit::Execute(cErrorQueue* pQueue)
cTWCmdLineUtil::ParsePolicyFile(genreSpecList, mpData->mPolFile, mpData->mSiteKeyFile, pQueue);
#ifdef TW_PROFILE
cWin32TaskTimer timer("cTripwire::GenerateDatabase");
cTaskTimer timer("cTripwire::GenerateDatabase");
timer.Start();
#endif
@ -1375,7 +1375,7 @@ int cTWModeIC::Execute(cErrorQueue* pQueue)
iUserNotify::GetInstance()->Notify(1, TSS_GetString( cTripwire, tripwire::STR_INTEGRITY_CHECK).c_str());
#ifdef TW_PROFILE
cWin32TaskTimer timer("cTripwire::IntegrityCheck");
cTaskTimer timer("cTripwire::IntegrityCheck");
timer.Start();
#endif
cIntegrityCheck ic( (cGenre::Genre)dbIter.GetGenre(), specList, dbIter.GetDb(), report, pQueue );

View File

@ -345,7 +345,7 @@ void cTWUtil::WriteDatabase(const TCHAR* filename, cFCODatabaseFile& db, bool bE
//fileHeader.SetVersion(1);
#ifdef TW_PROFILE
cWin32TaskTimer timer(_T("Write Database"));
cTaskTimer timer(_T("Write Database"));
timer.Start();
#endif
@ -370,7 +370,7 @@ void cTWUtil::ReadDatabase(const TCHAR* filename, cFCODatabaseFile& db, const cE
cDisplayEncoder::EncodeInline( filename ).c_str() );
#ifdef TW_PROFILE
cWin32TaskTimer timer("cTWUtil::ReadDatabase");
cTaskTimer timer("cTWUtil::ReadDatabase");
timer.Start();
#endif

View File

@ -43,7 +43,7 @@
#include "twtest/test.h"
void PrintChars( const TSTRING& str )
void CheckChars( const TSTRING& str, int length_expected = 1)
{
TSTRING::const_iterator cur = str.begin();
TSTRING::const_iterator end = str.end();
@ -51,19 +51,9 @@ void PrintChars( const TSTRING& str )
while( cCharUtil::PopNextChar( cur, end, first, last ) )
{
TCOUT << _T("char length: ") << (int)(last - first) << std::endl;
TCOUT << _T("char: <");
for( TSTRING::const_iterator at = first; at != last; at++ )
{
if( at != first )
TCOUT << _T(",");
TCOUT << (int)*at;
}
TCOUT << _T(">") << std::endl;
int length = (int)(last - first);
TEST(length == length_expected);
}
TCOUT << _T("----------------------------") << std::endl;
}
///////////////////////////////////////////////////////////////////////////
@ -71,8 +61,9 @@ void PrintChars( const TSTRING& str )
///////////////////////////////////////////////////////////////////////////
void TestCharUtilBasic()
{
PrintChars( _T("foo") );
PrintChars( _T("fo\x23 54") );
CheckChars( "foo" );
CheckChars( "fo\x23 54" );
CheckChars( "\U0001F408", 4 ); //Cat emoji. Assumes UTF-8
}
void RegisterSuite_CharUtil()

View File

@ -36,84 +36,89 @@
#include "core/debug.h"
#include "twtest/test.h"
#include "fco/fco.h"
#include "fco/twfactory.h"
#include "core/errorbucketimpl.h"
/*
static void PrintDb( cHierDatabase::iterator iter, cDebug d, bool bFirst = true )
namespace
{
if( ! bFirst )
{
iter.Descend();
}
d.TraceDebug( "-- Processing directory %s\n", iter.GetCwd().c_str() );
int num_processed = 0;
for( iter.SeekBegin(); ! iter.Done(); iter.Next() )
{
d.TraceDebug( "Processing entry %s\n", iter.GetName().c_str() );
if( iter.CanDescend() )
{
d.TraceDebug( ">>Descending...\n" );
PrintDb(iter, d, false);
}
}
d.TraceDebug( "-- Done Processing directory %s\n", iter.GetCwd().c_str() );
}
*/
static void PrintIter( cFSDataSourceIter iter, cDebug& d )
void util_ProcessDir( iFCODataSourceIter* pIter )
{
int count = 0;
if( ! iter.CanDescend() )
{
d.TraceError( "Iterator cannot descend; returning!\n");
TEST(!"Unexpected !CanDescend at beginning of test");
TEST( ! pIter->Done() );
TEST( pIter->CanDescend() );
if( ! pIter->CanDescend() )
return;
}
iter.Descend();
iter.TraceContents();
for( iter.SeekBegin(); ! iter.Done(); iter.Next() )
pIter->Descend();
for( pIter->SeekBegin(); ! pIter->Done(); pIter->Next() )
{
count++;
iFCO* pFCO = iter.CreateFCO();
iFCO* pFCO = pIter->CreateFCO();
if( pFCO )
{
pFCO->TraceContents();
num_processed++;
pFCO->Release();
if( pIter->CanDescend() )
{
TW_UNIQUE_PTR<iFCODataSourceIter> pCopy( pIter->CreateCopy() );
util_ProcessDir( pCopy.get() );
}
}
else
{
d.TraceError( "*** Create of FCO failed!\n");
fail("CreateFCO() failure");
fail("CreateFCO failure");
}
if( iter.CanDescend() )
}
}
void util_ProcessDir( const cFCOName& name )
{
//Create a cFSDataSourceIter the same way we do in DB gen / IC
TW_UNIQUE_PTR<iFCODataSourceIter> pDSIter(iTWFactory::GetInstance()->CreateDataSourceIter());
cErrorQueue errorQueue;
pDSIter->SetErrorBucket(&errorQueue);
pDSIter->SeekToFCO ( name, false ); // false means don't generate my peers...
if( ! pDSIter->Done() )
{
iFCO* pFCO = pDSIter->CreateFCO();
if( pFCO )
{
d.TraceDebug( ">>Descending...\n" );
PrintIter(iter, d);
num_processed++;
pFCO->Release();
if( pDSIter->CanDescend() )
{
TW_UNIQUE_PTR<iFCODataSourceIter> pCopy( pDSIter->CreateCopy() );
util_ProcessDir( pCopy.get() );
}
}
else
{
fail("CreateFCO failure");
}
}
TEST(count > 0);
TEST( 0 == errorQueue.GetNumErrors());
TEST( 0 < num_processed );
}
} // namespace
//////////////////////////////////////
void TestFSDataSourceIter()
{
skip("Fix this test");
cFSDataSourceIter iter;
cDebug d("TestFSDataSourceIter");
cFCOName base(TwTestDir());
// go to my temp directory and iterate over everything!
iter.SeekToFCO( cFCOName(TwTestDir()) );
//
// print out everything below the iterator
//
PrintIter( iter, d );
util_ProcessDir(base);
}
void RegisterSuite_FSDataSourceIter()

View File

@ -32,13 +32,34 @@
// fsobject_t -- the file system object test driver
#include "fs/stdfs.h"
#include "fs/fsobject.h"
#include "fs/fspropcalc.h"
#include "test.h"
#include <fstream>
void TestFSObject()
{
cDebug d("TestFSObject");
d.TraceError("Implement this!\n");
skip("TestFSObject not implemented");
cFCOName path( TwTestPath("fsobject") );
std::ofstream fstr(path.AsString().c_str());
if(fstr.bad())
{
d.TraceError("Unable to create test file %s!\n", path.AsString().c_str());
TEST(false);
}
fstr.close();
cFSObject obj(path);
cFSPropCalc propCalc;
cFCOPropVector v(obj.GetPropSet()->GetValidVector().GetSize());
for( int x=0; x < cFSPropSet::PROP_NUMITEMS; x++)
v.AddItem(x);
propCalc.SetPropVector(v);
obj.AcceptVisitor(&propCalc);
TEST(obj.GetPropSet()->GetNumProps() == cFSPropSet::PROP_NUMITEMS);
}
void RegisterSuite_FSObject()

View File

@ -158,9 +158,13 @@ void TestRefCountObj()
}
}
//These fields only exist in debug builds, so we can't use TEST() here.
ASSERT(cRefCountObj::objectCounter == 0);
ASSERT(cRefCountObj::referenceCounter == 0);
#ifdef DEBUG
//These fields only exist in debug builds
TEST(cRefCountObj::objectCounter == 0);
TEST(cRefCountObj::referenceCounter == 0);
#else
TEST("This test can only make useful assertions in debug builds");
#endif
db.TraceAlways("Done...\n");

View File

@ -32,12 +32,29 @@
// tasktimer_t -- test driver for cTaskTimer
#include "core/stdcore.h"
#include "test.h"
#include "core/tasktimer.h"
#include <unistd.h>
void TestTaskTimer()
{
cDebug d("TestTaskTimer");
d.TraceError("Implement this!\n");
skip("TestTaskTimer unimplemented");
cTaskTimer timer("unit testing");
TEST(!timer.IsRunning());
TEST(0 == timer.GetTotalTime());
TEST(0 == timer.GetNumTimesStarted());
for( int counter=0; counter<5; counter++)
{
timer.Start();
TEST(timer.IsRunning());
sleep(1);
timer.Stop();
TEST(!timer.IsRunning());
}
TEST(!timer.IsRunning());
TEST(5 >= timer.GetTotalTime());
TEST(5 == timer.GetNumTimesStarted());
}
void RegisterSuite_TaskTimer()

View File

@ -55,38 +55,6 @@ void TestHex();
TEST( false ); \
} catch( error& ) {}
/* We don't do atoi stuff in cTWLocale anymore
void TestAtoi()
{
//
// setup
//
int32 n;
TSTRING str = _T("123456");
//
// Try formatting with our default locale
//
TWLocale::InitGlobalLocale();
n = cTWLocale::FormatNumberClassic( str );
TEST( n == 123456 );
//
// Try formatting with "" locale
//
std::locale::global( std::locale("") );
n = cTWLocale::FormatNumberClassic( str );
TEST( n == 123456 );
//
// Try formatting with "C" locale
//
std::locale::global( std::locale("") );
n = cTWLocale::FormatNumberClassic( str );
TEST( n == 123456 );
}
*/
void TestItoa()
{
try
@ -129,110 +97,8 @@ void TestItoa()
}
}
/* We don't do atoi stuff in cTWLocale anymore, so no roundtrip
void TestRoundtrip()
{
//
// init
//
cTWLocale::InitGlobalLocale();
//
// atoitoa
//
TSTRING strIn = _T("123456");
TSTRING strOut;
strOut = cTWLocale::FormatNumber( cTWLocale::FormatNumberClassic( strIn ), strOut );
// don't know if string will be the same due to possible changes in formatting from locale
// ASSERT( strOut == strIn ); <---- can't do this ^^^
TEST( 123456 == cTWLocale::FormatNumberClassic( strOut ) );
//
// itoatoi
//
int32 nIn = 654321;
int32 nOut;
nOut = cTWLocale::FormatNumberClassic( cTWLocale::FormatNumber( nIn, strIn ) );
TEST( nOut == nIn );
}
*/
void TestFlags()
{
skip("Modernize & re-enable this");
#if 0
//
// init
//
cTWLocale::InitGlobalLocale();
//
// hex
//
TSTRING str = _T("FF");
int n = cTWLocale::FormatNumber( str, std::ios_base::hex );
TEST( n == 0xFF );
//
// bad number for dec
//
ASSERT_THAT_IT_THROWS( cTWLocale::FormatNumberAsHex( str ), eError );
//
// oct
//
TSTRING strOct = _T("0712");
n = cTWLocale::FormatNumber( strOct, std::ios_base::oct );
TEST( n == 0712 );
//
// oct again
//
strOct = _T("00712");
n = cTWLocale::FormatNumber( strOct, std::ios_base::oct );
TEST( n == 0712 );
//
// oct again again
//
strOct = _T("712");
n = cTWLocale::FormatNumber( strOct, std::ios_base::oct );
TEST( n == 0712 );
//
// try bad oct
//
ASSERT_THAT_IT_THROWS( cTWLocale::FormatNumber( _T("99"), std::ios_base::oct ), eError );
#endif
}
/*
void doTestHex(const uint32 value, const std::string& expected, const std::string& expected2 = "")
{
TSTRING str = cTWLocale::FormatNumberAsHex( value );
TCERR << "STR = " << str << " | Expected = " << expected << " | Expected2 = " << expected2 << std::endl;
TEST( str == expected || (!expected2.empty() && str == expected2) );
}
void TestHex()
{
TSTRING str;
doTestHex( 0x1234, _T("1234") );
doTestHex( 16, _T("10") );
doTestHex( 0x12344321, _T("12344321") );
doTestHex( 0xFFFFFFFF, _T("FFFFFFFF"), _T("ffffffff"));
}
*/
void RegisterSuite_TWLocale()
{
// RegisterTest("TWLocale", "Hex", TestHex);
// RegisterTest("TWLocale", "Atoi", TestAtoi);
RegisterTest("TWLocale", "Itoa", TestItoa);
RegisterTest("TWLocale", "Flags", TestFlags);
// RegisterTest("TWLocale", "Roundtrip", TestRoundtrip);
}