Unit tests update

This commit is contained in:
Markus Gans 2018-03-16 01:05:45 +01:00
parent 1d21c0ded9
commit d22ff10a28
7 changed files with 167 additions and 34 deletions

View File

@ -1517,21 +1517,21 @@ int FApplication::processTimerEvent()
while ( iter != last ) while ( iter != last )
{ {
if ( ! (*iter).id if ( ! iter->id
|| ! (*iter).object || ! iter->object
|| currentTime < (*iter).timeout ) // no timer expired || currentTime < iter->timeout ) // no timer expired
break; break;
(*iter).timeout += (*iter).interval; iter->timeout += iter->interval;
if ( (*iter).timeout < currentTime ) if ( iter->timeout < currentTime )
(*iter).timeout = currentTime + (*iter).interval; iter->timeout = currentTime + iter->interval;
if ( (*iter).interval.tv_usec > 0 || (*iter).interval.tv_sec > 0 ) if ( iter->interval.tv_usec > 0 || iter->interval.tv_sec > 0 )
activated++; activated++;
FTimerEvent t_ev(fc::Timer_Event, (*iter).id); FTimerEvent t_ev(fc::Timer_Event, iter->id);
sendEvent((*iter).object, &t_ev); sendEvent(iter->object, &t_ev);
++iter; ++iter;
} }

View File

@ -533,7 +533,7 @@ void FFileDialog::clear()
while ( iter != last ) while ( iter != last )
{ {
std::free ((*iter).name); std::free (iter->name);
++iter; ++iter;
} }
@ -553,7 +553,7 @@ int FFileDialog::numOfDirs()
while ( iter != last ) while ( iter != last )
{ {
if ( (*iter).directory && std::strcmp((*iter).name, ".") != 0 ) if ( iter->directory && std::strcmp(iter->name, ".") != 0 )
n++; n++;
++iter; ++iter;
@ -730,10 +730,10 @@ void FFileDialog::dirEntriesToList()
while ( iter != last ) while ( iter != last )
{ {
if ( (*iter).directory ) if ( iter->directory )
filebrowser->insert(FString((*iter).name), fc::SquareBrackets); filebrowser->insert(FString(iter->name), fc::SquareBrackets);
else else
filebrowser->insert(FString((*iter).name)); filebrowser->insert(FString(iter->name));
++iter; ++iter;
} }
@ -780,7 +780,7 @@ int FFileDialog::changeDir (const FString& dirname)
while ( iter != last ) while ( iter != last )
{ {
if ( std::strcmp((*iter).name, baseName) == 0 ) if ( std::strcmp(iter->name, baseName) == 0 )
{ {
filebrowser->setCurrentItem(i); filebrowser->setCurrentItem(i);
filename->setText(FString(baseName) + '/'); filename->setText(FString(baseName) + '/');
@ -871,9 +871,9 @@ void FFileDialog::cb_processActivate (FWidget*, data_ptr)
while ( iter != last ) while ( iter != last )
{ {
if ( (*iter).name && input && ! input.isNull() if ( iter->name && input && ! input.isNull()
&& std::strcmp((*iter).name, input) == 0 && std::strcmp(iter->name, input) == 0
&& (*iter).directory ) && iter->directory )
{ {
found = true; found = true;
changeDir(input); changeDir(input);

View File

@ -594,8 +594,8 @@ FObject::FObjectIterator FListView::insert ( FListViewItem* item
while ( header_iter != header.end() ) while ( header_iter != header.end() )
{ {
int width = (*header_iter).width; int width = header_iter->width;
bool fixed_width = (*header_iter).fixed_width; bool fixed_width = header_iter->fixed_width;
if ( ! fixed_width ) if ( ! fixed_width )
{ {
@ -607,10 +607,10 @@ FObject::FObjectIterator FListView::insert ( FListViewItem* item
len = 0; len = 0;
if ( len > width ) if ( len > width )
(*header_iter).width = len; header_iter->width = len;
} }
line_width += (*header_iter).width + padding_space; // width + trailing space line_width += header_iter->width + padding_space; // width + trailing space
column_idx++; column_idx++;
++header_iter; ++header_iter;
} }
@ -1285,7 +1285,7 @@ void FListView::drawColumnLabels()
while ( iter != header.end() ) while ( iter != header.end() )
{ {
const FString& text = (*iter).name; const FString& text = iter->name;
if ( text.isNull() || text.isEmpty() ) if ( text.isNull() || text.isEmpty() )
{ {
@ -1511,8 +1511,8 @@ void FListView::drawColumnText (headerItems::const_iterator& iter)
// Print lable text // Print lable text
static const int leading_space = 1; static const int leading_space = 1;
static const int trailing_space = 1; static const int trailing_space = 1;
const FString& text = (*iter).name; const FString& text = iter->name;
int width = (*iter).width; int width = iter->width;
FString txt = " " + text; FString txt = " " + text;
uInt txt_length = txt.getLength(); uInt txt_length = txt.getLength();
int column_width = leading_space + width; int column_width = leading_space + width;
@ -1547,7 +1547,7 @@ void FListView::drawColumnEllipsis ( headerItems::const_iterator& iter
{ {
// Print lable ellipsis // Print lable ellipsis
static const int ellipsis_length = 2; static const int ellipsis_length = 2;
int width = (*iter).width; int width = iter->width;
headerline << ' '; headerline << ' ';
headerline << text.left(uInt(width - ellipsis_length)); headerline << text.left(uInt(width - ellipsis_length));

View File

@ -87,7 +87,7 @@ FObject::~FObject() // destructor
while ( iter != last ) while ( iter != last )
{ {
delete (*iter); delete *iter;
++iter; ++iter;
} }
} }
@ -146,6 +146,8 @@ void FObject::removeParent()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FObject::addChild (FObject* obj) void FObject::addChild (FObject* obj)
{ {
// Adds an object obj to the children list
if ( ! obj ) if ( ! obj )
return; return;
@ -160,6 +162,8 @@ void FObject::addChild (FObject* obj)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FObject::delChild (FObject* obj) void FObject::delChild (FObject* obj)
{ {
// Deletes the child object obj from children list
if ( ! obj ) if ( ! obj )
return; return;
@ -174,6 +178,8 @@ void FObject::delChild (FObject* obj)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FObject::getCurrentTime (timeval* time) void FObject::getCurrentTime (timeval* time)
{ {
// Get the current time as timeval struct
gettimeofday(time, 0); gettimeofday(time, 0);
// NTP fix // NTP fix
@ -201,6 +207,8 @@ void FObject::getCurrentTime (timeval* time)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FObject::isTimeout (timeval* time, register long timeout) bool FObject::isTimeout (timeval* time, register long timeout)
{ {
// Checks whether the specified time span (timeout in µs) has elapse
register long diff_usec; register long diff_usec;
struct timeval now; struct timeval now;
struct timeval diff; struct timeval diff;
@ -222,6 +230,9 @@ bool FObject::isTimeout (timeval* time, register long timeout)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int FObject::addTimer (int interval) int FObject::addTimer (int interval)
{ {
// Create a timer and returns the timer identifier number
// (interval in ms)
FObject::TimerList::iterator iter, last; FObject::TimerList::iterator iter, last;
timeval time_interval; timeval time_interval;
timeval currentTime; timeval currentTime;
@ -250,7 +261,7 @@ int FObject::addTimer (int interval)
while ( iter != last ) while ( iter != last )
{ {
if ( (*iter).id == id ) if ( iter->id == id )
{ {
iter = timer_list->begin(); iter = timer_list->begin();
id++; id++;
@ -274,7 +285,7 @@ int FObject::addTimer (int interval)
iter = timer_list->begin(); iter = timer_list->begin();
last = timer_list->end(); last = timer_list->end();
while ( iter != last && (*iter).timeout < t.timeout ) while ( iter != last && iter->timeout < t.timeout )
++iter; ++iter;
timer_list->insert (iter, t); timer_list->insert (iter, t);
@ -286,16 +297,18 @@ int FObject::addTimer (int interval)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FObject::delTimer (int id) bool FObject::delTimer (int id)
{ {
// Deletes a timer by using the timer identifier number
FObject::TimerList::iterator iter, last; FObject::TimerList::iterator iter, last;
if ( id <= 0 || id > int(timer_list->size()) ) if ( id <= 0 )
return false; return false;
timer_modify_lock = true; timer_modify_lock = true;
iter = timer_list->begin(); iter = timer_list->begin();
last = timer_list->end(); last = timer_list->end();
while ( iter != last && (*iter).id != id ) while ( iter != last && iter->id != id )
++iter; ++iter;
if ( iter != last ) if ( iter != last )
@ -312,6 +325,8 @@ bool FObject::delTimer (int id)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FObject::delOwnTimer() bool FObject::delOwnTimer()
{ {
// Deletes all timers of this object
FObject::TimerList::iterator iter; FObject::TimerList::iterator iter;
if ( ! timer_list ) if ( ! timer_list )
@ -325,7 +340,7 @@ bool FObject::delOwnTimer()
while ( iter != timer_list->end() ) while ( iter != timer_list->end() )
{ {
if ( (*iter).object == this ) if ( iter->object == this )
iter = timer_list->erase(iter); iter = timer_list->erase(iter);
else else
++iter; ++iter;
@ -338,6 +353,8 @@ bool FObject::delOwnTimer()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FObject::delAllTimer() bool FObject::delAllTimer()
{ {
// Deletes all timers of all objects
if ( ! timer_list ) if ( ! timer_list )
return false; return false;
@ -355,6 +372,8 @@ bool FObject::delAllTimer()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
bool FObject::event (FEvent* ev) bool FObject::event (FEvent* ev)
{ {
// Receives events on this object
if ( ev->type() == fc::Timer_Event ) if ( ev->type() == fc::Timer_Event )
{ {
onTimer ( const_cast<FTimerEvent*>(static_cast<const FTimerEvent*>(ev)) ); onTimer ( const_cast<FTimerEvent*>(static_cast<const FTimerEvent*>(ev)) );

View File

@ -3402,7 +3402,7 @@ void FTerm::init_termcaps()
while ( iter != terminals.end() ) while ( iter != terminals.end() )
{ {
// Copy c-string + terminating null-character ('\0') // Copy c-string + terminating null-character ('\0')
std::strncpy (termtype, (*iter).c_str(), (*iter).length() + 1); std::strncpy (termtype, iter->c_str(), iter->length() + 1);
// Open the termcap file + load entry for termtype // Open the termcap file + load entry for termtype
status = tgetent(term_buffer, termtype); status = tgetent(term_buffer, termtype);

View File

@ -521,7 +521,7 @@ int FVTerm::print (term_area* area, const std::vector<char_data>& term_string)
{ {
bool printable_character = false; bool printable_character = false;
switch ( (*iter).code ) switch ( iter->code )
{ {
case '\n': case '\n':
area->cursor_y++; area->cursor_y++;

View File

@ -45,6 +45,11 @@ class FObject_protected : public FObject
{ {
return FObject::event(ev); return FObject::event(ev);
} }
FObject::TimerList* getTimerList() const
{
return timer_list;
}
}; };
#pragma pack(pop) #pragma pack(pop)
@ -69,6 +74,9 @@ class FObjectTest : public CPPUNIT_NS::TestFixture
void removeParentTest(); void removeParentTest();
void addTest(); void addTest();
void delTest(); void delTest();
void iteratorTest();
void timeTest();
void timerTest();
private: private:
// Adds code needed to register the test suite // Adds code needed to register the test suite
@ -81,6 +89,9 @@ class FObjectTest : public CPPUNIT_NS::TestFixture
CPPUNIT_TEST (removeParentTest); CPPUNIT_TEST (removeParentTest);
CPPUNIT_TEST (addTest); CPPUNIT_TEST (addTest);
CPPUNIT_TEST (delTest); CPPUNIT_TEST (delTest);
CPPUNIT_TEST (iteratorTest);
CPPUNIT_TEST (timeTest);
CPPUNIT_TEST (timerTest);
// End of test suite definition // End of test suite definition
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
@ -120,6 +131,13 @@ void FObjectTest::NoArgumentTest()
FEvent* ev = new FEvent(fc::None_Event); FEvent* ev = new FEvent(fc::None_Event);
CPPUNIT_ASSERT ( ! t.event(ev) ); CPPUNIT_ASSERT ( ! t.event(ev) );
delete ev; delete ev;
ev = new FEvent(fc::Timer_Event);
CPPUNIT_ASSERT ( t.event(ev) );
delete ev;
CPPUNIT_ASSERT ( ! fc::emptyFString::get().isNull() );
CPPUNIT_ASSERT ( fc::emptyFString::get().isEmpty() );
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -259,7 +277,103 @@ void FObjectTest::delTest()
delete obj; delete obj;
} }
//----------------------------------------------------------------------
void FObjectTest::iteratorTest()
{/*
* obj -> child1
* -> child2
* -> child3
*/
FObject* obj = new FObject();
FObject* child1 = new FObject(obj);
FObject* child2 = new FObject(obj);
FObject* child3 = new FObject(obj);
CPPUNIT_ASSERT ( child1->getParent() == obj );
CPPUNIT_ASSERT ( child2->getParent() == obj );
CPPUNIT_ASSERT ( child3->getParent() == obj );
FObject::constFObjectIterator c_iter, c_last;
c_iter = obj->begin();
c_last = obj->end();
int i = 0;
while ( c_iter != c_last )
{
i++;
++c_iter;
}
CPPUNIT_ASSERT ( obj->numOfChildren() == i );
CPPUNIT_ASSERT ( i == 3 );
FObject::FObjectIterator iter, last;
iter = obj->begin();
last = obj->end();
i = 0;
while ( iter != last )
{
i++;
++iter;
}
CPPUNIT_ASSERT ( obj->numOfChildren() == i );
CPPUNIT_ASSERT ( i == 3 );
delete obj;
}
//----------------------------------------------------------------------
void FObjectTest::timeTest()
{
struct timeval time1;
long timeout = 750000; // 750 ms
FObject::getCurrentTime(&time1);
CPPUNIT_ASSERT ( ! FObject::isTimeout (&time1, timeout) );
sleep(1);
CPPUNIT_ASSERT ( FObject::isTimeout (&time1, timeout) );
}
//----------------------------------------------------------------------
void FObjectTest::timerTest()
{
FObject_protected t1;
FObject_protected t2;
int id1, id2;
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
id1 = t1.addTimer(300);
CPPUNIT_ASSERT ( ! t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
id2 = t1.addTimer(900);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 2 );
t1.delTimer (id1);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
t1.delTimer (id2);
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
t1.addTimer(250);
t1.addTimer(500);
t2.addTimer(750);
t2.addTimer(1000);
CPPUNIT_ASSERT_EQUAL ( t1.getTimerList(), t2.getTimerList() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 4 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 4 );
t1.delOwnTimer();
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 2 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 2 );
t1.addTimer(250);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 3 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 3 );
t2.delAllTimer();
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t2.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 0 );
}
// Put the test suite in the registry // Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION (FObjectTest); CPPUNIT_TEST_SUITE_REGISTRATION (FObjectTest);