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 )
{
if ( ! (*iter).id
|| ! (*iter).object
|| currentTime < (*iter).timeout ) // no timer expired
if ( ! iter->id
|| ! iter->object
|| currentTime < iter->timeout ) // no timer expired
break;
(*iter).timeout += (*iter).interval;
iter->timeout += iter->interval;
if ( (*iter).timeout < currentTime )
(*iter).timeout = currentTime + (*iter).interval;
if ( iter->timeout < currentTime )
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++;
FTimerEvent t_ev(fc::Timer_Event, (*iter).id);
sendEvent((*iter).object, &t_ev);
FTimerEvent t_ev(fc::Timer_Event, iter->id);
sendEvent(iter->object, &t_ev);
++iter;
}

View File

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

View File

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

View File

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

View File

@ -3402,7 +3402,7 @@ void FTerm::init_termcaps()
while ( iter != terminals.end() )
{
// 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
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;
switch ( (*iter).code )
switch ( iter->code )
{
case '\n':
area->cursor_y++;

View File

@ -45,6 +45,11 @@ class FObject_protected : public FObject
{
return FObject::event(ev);
}
FObject::TimerList* getTimerList() const
{
return timer_list;
}
};
#pragma pack(pop)
@ -69,6 +74,9 @@ class FObjectTest : public CPPUNIT_NS::TestFixture
void removeParentTest();
void addTest();
void delTest();
void iteratorTest();
void timeTest();
void timerTest();
private:
// Adds code needed to register the test suite
@ -81,6 +89,9 @@ class FObjectTest : public CPPUNIT_NS::TestFixture
CPPUNIT_TEST (removeParentTest);
CPPUNIT_TEST (addTest);
CPPUNIT_TEST (delTest);
CPPUNIT_TEST (iteratorTest);
CPPUNIT_TEST (timeTest);
CPPUNIT_TEST (timerTest);
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
@ -120,6 +131,13 @@ void FObjectTest::NoArgumentTest()
FEvent* ev = new FEvent(fc::None_Event);
CPPUNIT_ASSERT ( ! t.event(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;
}
//----------------------------------------------------------------------
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
CPPUNIT_TEST_SUITE_REGISTRATION (FObjectTest);