finalcut/src/test/fobject-test.cpp

383 lines
12 KiB
C++
Raw Normal View History

2018-03-14 00:53:28 +01:00
/***********************************************************************
* fobject-test.cpp - FPoint unit tests *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2018 Markus Gans *
* *
* The Final Cut is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* The Final Cut is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <final/final.h>
2018-03-14 23:56:44 +01:00
//----------------------------------------------------------------------
// class FObject_protected
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class FObject_protected : public FObject
{
public:
bool event (FEvent* ev)
{
return FObject::event(ev);
}
2018-03-16 01:05:45 +01:00
FObject::TimerList* getTimerList() const
{
return timer_list;
}
2018-03-14 23:56:44 +01:00
};
#pragma pack(pop)
2018-03-14 00:53:28 +01:00
//----------------------------------------------------------------------
// class FObjectTest
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class FObjectTest : public CPPUNIT_NS::TestFixture
{
public:
FObjectTest()
{ }
protected:
void classNameTest();
void NoArgumentTest();
2018-03-14 23:56:44 +01:00
void childObjectTest();
void removeParentTest();
void addTest();
void delTest();
2018-03-16 01:05:45 +01:00
void iteratorTest();
void timeTest();
void timerTest();
2018-03-14 00:53:28 +01:00
private:
// Adds code needed to register the test suite
CPPUNIT_TEST_SUITE (FObjectTest);
// Add a methods to the test suite
CPPUNIT_TEST (classNameTest);
CPPUNIT_TEST (NoArgumentTest);
2018-03-14 23:56:44 +01:00
CPPUNIT_TEST (childObjectTest);
CPPUNIT_TEST (removeParentTest);
CPPUNIT_TEST (addTest);
CPPUNIT_TEST (delTest);
2018-03-16 01:05:45 +01:00
CPPUNIT_TEST (iteratorTest);
CPPUNIT_TEST (timeTest);
CPPUNIT_TEST (timerTest);
2018-03-14 00:53:28 +01:00
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
};
#pragma pack(pop)
//----------------------------------------------------------------------
void FObjectTest::classNameTest()
{
FObject o;
const char* const classname = o.getClassName();
CPPUNIT_ASSERT ( std::strcmp(classname, "FObject") == 0 );
}
//----------------------------------------------------------------------
void FObjectTest::NoArgumentTest()
{
FObject o1;
CPPUNIT_ASSERT ( ! o1.hasParent() );
CPPUNIT_ASSERT ( o1.getParent() == 0 );
CPPUNIT_ASSERT ( ! o1.hasChildren() );
CPPUNIT_ASSERT ( o1.getChild(0) == 0 );
CPPUNIT_ASSERT ( o1.getChild(1) == 0 );
CPPUNIT_ASSERT ( o1.numOfChildren() == 0 );
const FObject::FObjectList& children_list = o1.getChildren();
CPPUNIT_ASSERT ( children_list.begin() == o1.begin() );
CPPUNIT_ASSERT ( children_list.begin() == o1.end() );
CPPUNIT_ASSERT ( children_list.end() == o1.begin() );
CPPUNIT_ASSERT ( children_list.end() == o1.end() );
CPPUNIT_ASSERT ( ! o1.isChild(&o1) );
CPPUNIT_ASSERT ( ! o1.isDirectChild(&o1) );
CPPUNIT_ASSERT ( ! o1.isWidget() );
CPPUNIT_ASSERT ( o1.isInstanceOf("FObject") );
CPPUNIT_ASSERT ( ! o1.isTimerInUpdating() );
2018-03-14 23:56:44 +01:00
FObject_protected t;
2018-03-14 00:53:28 +01:00
FEvent* ev = new FEvent(fc::None_Event);
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( ! t.event(ev) );
2018-03-14 00:53:28 +01:00
delete ev;
2018-03-16 01:05:45 +01:00
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() );
2018-03-14 00:53:28 +01:00
}
//----------------------------------------------------------------------
2018-03-14 23:56:44 +01:00
void FObjectTest::childObjectTest()
{/*
* obj -> c1 -> c5 -> c6
* -> c2
* -> c3
* -> c4
*/
FObject obj;
FObject* c1 = new FObject(&obj);
FObject* c2 = new FObject(&obj);
FObject* c3 = new FObject(&obj);
FObject* c4 = new FObject(&obj);
FObject* c5 = new FObject(c1);
FObject* c6 = new FObject(c5);
CPPUNIT_ASSERT ( obj.hasChildren() );
CPPUNIT_ASSERT ( obj.getChild(0) == 0 );
CPPUNIT_ASSERT ( obj.getChild(1) != 0 );
CPPUNIT_ASSERT ( obj.numOfChildren() == 4 );
CPPUNIT_ASSERT ( obj.isChild(c1) );
CPPUNIT_ASSERT ( obj.isChild(c2) );
CPPUNIT_ASSERT ( obj.isChild(c3) );
CPPUNIT_ASSERT ( obj.isChild(c4) );
CPPUNIT_ASSERT ( obj.isChild(c5) );
CPPUNIT_ASSERT ( obj.isChild(c6) );
2018-03-14 00:53:28 +01:00
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( obj.isDirectChild(c1) );
CPPUNIT_ASSERT ( obj.isDirectChild(c2) );
CPPUNIT_ASSERT ( obj.isDirectChild(c3) );
CPPUNIT_ASSERT ( obj.isDirectChild(c4) );
CPPUNIT_ASSERT ( ! obj.isDirectChild(c5) );
CPPUNIT_ASSERT ( c1->isDirectChild(c5) );
CPPUNIT_ASSERT ( ! obj.isDirectChild(c6) );
CPPUNIT_ASSERT ( ! c1->isDirectChild(c6) );
CPPUNIT_ASSERT ( c5->isDirectChild(c6) );
CPPUNIT_ASSERT ( c1->hasParent() );
CPPUNIT_ASSERT ( c1->getParent() == &obj );
CPPUNIT_ASSERT ( c1->hasChildren() );
CPPUNIT_ASSERT ( ! c2->hasChildren() );
CPPUNIT_ASSERT ( c1->getChild(0) == 0 );
CPPUNIT_ASSERT ( c1->getChild(1) != 0 );
CPPUNIT_ASSERT ( c2->getChild(1) == 0 );
CPPUNIT_ASSERT ( c1->numOfChildren() == 1 );
CPPUNIT_ASSERT ( c2->numOfChildren() == 0 );
const FObject::FObjectList& children_list2 = c1->getChildren();
CPPUNIT_ASSERT ( children_list2.begin() == c1->begin() );
CPPUNIT_ASSERT ( children_list2.begin() != c1->end() );
CPPUNIT_ASSERT ( children_list2.end() != c1->begin() );
CPPUNIT_ASSERT ( children_list2.end() == c1->end() );
CPPUNIT_ASSERT ( ! c1->isDirectChild(c1) );
CPPUNIT_ASSERT ( ! c1->isWidget() );
CPPUNIT_ASSERT ( c1->isInstanceOf("FObject") );
CPPUNIT_ASSERT ( ! c1->isTimerInUpdating() );
2018-03-14 00:53:28 +01:00
}
//----------------------------------------------------------------------
2018-03-14 23:56:44 +01:00
void FObjectTest::removeParentTest()
{/*
* obj -> child
*/
FObject* obj = new FObject();
FObject* child = new FObject(obj);
CPPUNIT_ASSERT ( obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 1 );
CPPUNIT_ASSERT ( obj->isChild(child) );
CPPUNIT_ASSERT ( child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() == obj );
child->removeParent();
CPPUNIT_ASSERT ( ! obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 0 );
CPPUNIT_ASSERT ( ! obj->isChild(child) );
CPPUNIT_ASSERT ( ! child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() != obj );
delete child;
delete obj; // also deletes the child object
}
//----------------------------------------------------------------------
void FObjectTest::addTest()
{/*
* obj -> child
*/
FObject* obj = new FObject();
FObject* child = new FObject();
CPPUNIT_ASSERT ( ! obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 0 );
CPPUNIT_ASSERT ( ! obj->isChild(child) );
CPPUNIT_ASSERT ( ! child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() != obj );
obj->addChild(child);
CPPUNIT_ASSERT ( obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 1 );
CPPUNIT_ASSERT ( obj->isChild(child) );
CPPUNIT_ASSERT ( child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() == obj );
delete obj; // also deletes the child object
}
//----------------------------------------------------------------------
void FObjectTest::delTest()
{/*
* obj -> child
*/
FObject* obj = new FObject();
FObject* child = new FObject(obj);
CPPUNIT_ASSERT ( obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 1 );
CPPUNIT_ASSERT ( obj->isChild(child) );
CPPUNIT_ASSERT ( child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() == obj );
obj->delChild(child);
CPPUNIT_ASSERT ( ! obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 0 );
CPPUNIT_ASSERT ( ! obj->isChild(child) );
CPPUNIT_ASSERT ( ! child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() != obj );
2018-03-14 00:53:28 +01:00
2018-03-14 23:56:44 +01:00
delete child;
delete obj;
2018-03-14 00:53:28 +01:00
}
2018-03-16 01:05:45 +01:00
//----------------------------------------------------------------------
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) );
}
2018-03-14 00:53:28 +01:00
2018-03-16 01:05:45 +01:00
//----------------------------------------------------------------------
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 );
}
2018-03-14 00:53:28 +01:00
// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION (FObjectTest);
// The general unit test main part
#include <main-test.inc>