Newer
Older
#ifndef ALGORITHMPROXYTEST_H_
#define ALGORITHMPROXYTEST_H_
#include <cxxtest/TestSuite.h>
Peterson, Peter
committed
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AlgorithmProxy.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/AlgorithmObserver.h"
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
class ToyAlgorithmProxy : public Algorithm
{
public:
ToyAlgorithmProxy() : Algorithm() {}
virtual ~ToyAlgorithmProxy() {}
Peterson, Peter
committed
const std::string name() const { return "ToyAlgorithmProxy";} ///< Algorithm's name for identification
Janik Zikovsky
committed
int version() const { return 1;} ///< Algorithm's version for identification
Peterson, Peter
committed
const std::string category() const { return "Cat";} ///< Algorithm's category for identification
void init()
{
declareProperty("prop1","value");
declareProperty("prop2",1);
declareProperty("out",8,Direction::Output);
std::string p1 = getProperty("prop1");
int p2 = getProperty("prop2");
Poco::Thread::current()->sleep(200);
progress(0.333,"Running");
interruption_point();
Algorithm* alg = dynamic_cast<Algorithm*>( this );
TS_ASSERT_EQUALS( p1, "stuff" );
TS_ASSERT_EQUALS( p2, 17 );
}
};
DECLARE_ALGORITHM(ToyAlgorithmProxy)
class TestProxyObserver: public AlgorithmObserver
{
public:
bool start,progress,finish;
TestProxyObserver():AlgorithmObserver(),start(),progress(),finish(){}
TestProxyObserver(IAlgorithm_const_sptr alg):AlgorithmObserver(alg),start(),progress(),finish(){}
void startHandle(const IAlgorithm* alg)
{
start = true;
}
void progressHandle(const IAlgorithm* alg,double p,const std::string& msg)
{
progress = true;
TS_ASSERT_EQUALS( p, 0.333 );
TS_ASSERT_EQUALS( msg, "Running" );
}
void finishHandle(const IAlgorithm* alg)
{
finish = true;
}
};
Peterson, Peter
committed
class AlgorithmProxyTest : public CxxTest::TestSuite
{
public:
void testCreateProxy()
{
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("ToyAlgorithmProxy");
TS_ASSERT( dynamic_cast<AlgorithmProxy*>(alg.get()) );
TS_ASSERT_EQUALS( alg->name() , "ToyAlgorithmProxy" );
TS_ASSERT_EQUALS( alg->version() , 1 );
TS_ASSERT_EQUALS( alg->category() , "Cat" );
TS_ASSERT( alg->isInitialized() );
Peterson, Peter
committed
alg->setChild(true);
TS_ASSERT( !alg->isChild() );
TS_ASSERT( alg->existsProperty("prop1") );
TS_ASSERT( alg->existsProperty("prop2") );
TS_ASSERT( !alg->isRunning() );
TS_ASSERT( !alg->isRunningAsync() );
Peterson, Peter
committed
alg->setProperty("prop1","stuff");
alg->setProperty("prop2",17);
TS_ASSERT_THROWS_NOTHING( alg->execute() );
TS_ASSERT( alg->isExecuted() );
Peterson, Peter
committed
int out = alg->getProperty("out");
Peterson, Peter
committed
}
void testRunning()
{
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("ToyAlgorithmProxy");
TS_ASSERT( dynamic_cast<AlgorithmProxy*>(alg.get()) );
Peterson, Peter
committed
alg->setProperty("prop1","stuff");
alg->setProperty("prop2",17);
Poco::ActiveResult<bool> res = alg->executeAsync();
res.tryWait(100);
TS_ASSERT( alg->isRunning() );
TS_ASSERT( alg->isRunningAsync() );
Peterson, Peter
committed
res.wait();
TS_ASSERT( res.data() );
TS_ASSERT( alg->isExecuted() );
Peterson, Peter
committed
}
void testCancel()
{
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("ToyAlgorithmProxy");
TS_ASSERT( dynamic_cast<AlgorithmProxy*>(alg.get()) );
Peterson, Peter
committed
alg->setProperty("prop1","stuff");
alg->setProperty("prop2",17);
Poco::ActiveResult<bool> res = alg->executeAsync();
res.tryWait(100);
alg->cancel();
res.wait();
TS_ASSERT( !alg->isExecuted() );
Peterson, Peter
committed
int out = alg->getProperty("out");
Peterson, Peter
committed
}
void testAddObserver()
{
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("ToyAlgorithmProxy");
TS_ASSERT( dynamic_cast<AlgorithmProxy*>(alg.get()) );
Peterson, Peter
committed
alg->setProperty("prop1","stuff");
alg->setProperty("prop2",17);
TestProxyObserver obs(alg);
Poco::ActiveResult<bool> res = alg->executeAsync();
res.wait();
TS_ASSERT( obs.start );
TS_ASSERT( obs.progress );
TS_ASSERT( obs.finish );
Peterson, Peter
committed
}
};
#endif /*ALGORITHMPROXYTEST_H_*/