Skip to content
Snippets Groups Projects
AlgorithmManagerTest.h 4.2 KiB
Newer Older
#ifndef AlgorithmManagerTest_H_
#define AlgorithmManagerTest_H_
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/AlgorithmManager.h"
Nick Draper's avatar
Nick Draper committed
using namespace Mantid::API;
Matt Clarke's avatar
Matt Clarke committed
{
Nick Draper's avatar
Nick Draper committed
public:

  AlgTest() : Algorithm() {}
  virtual ~AlgTest() {}
Nick Draper's avatar
Nick Draper committed
  void init() { }
Dickon Champion's avatar
Dickon Champion committed
  void exec() { }
Dickon Champion's avatar
Dickon Champion committed
  virtual const std::string name() const {return "AlgTest";}
  virtual const int version() const {return(1);}
Dickon Champion's avatar
Dickon Champion committed

};

class AlgTestFail : public Algorithm
{
public:

  AlgTestFail() : Algorithm() {}
  virtual ~AlgTestFail() {}
  void init() { }
  void exec() { }
Dickon Champion's avatar
Dickon Champion committed
  virtual const std::string name() const {return "AlgTest";}
  virtual const int version() const {return(1);}
Dickon Champion's avatar
Dickon Champion committed

};

class AlgTestPass : public Algorithm
{
public:

  AlgTestPass() : Algorithm() {}
  virtual ~AlgTestPass() {}
  void init() { }
  void exec() { }
Dickon Champion's avatar
Dickon Champion committed
  virtual const std::string name() const {return "AlgTest";}
  virtual const int version() const {return(2);}
Dickon Champion's avatar
Dickon Champion committed

Matt Clarke's avatar
Matt Clarke committed
};
class AlgTestSecond : public Algorithm
Matt Clarke's avatar
Matt Clarke committed
{
Nick Draper's avatar
Nick Draper committed
public:

  AlgTestSecond() : Algorithm() {}
  virtual ~AlgTestSecond() {}
Nick Draper's avatar
Nick Draper committed
  void init() { }
  void exec() { }
Dickon Champion's avatar
Dickon Champion committed
  virtual const std::string name() const {return "AlgTestSecond";}
  virtual const int version() const {return(1);}
Dickon Champion's avatar
Dickon Champion committed

Matt Clarke's avatar
Matt Clarke committed
};

class AlgorithmManagerTest : public CxxTest::TestSuite
{
public:

Nick Draper's avatar
Nick Draper committed
  AlgorithmManagerTest() 
  {
  }

Dickon Champion's avatar
Dickon Champion committed
  void testVersionFail()
  {
Dickon Champion's avatar
Dickon Champion committed
//    AlgorithmFactory::Instance().subscribe<AlgTest>("AlgTest");
	TS_ASSERT_THROWS(AlgorithmFactory::Instance().subscribe<AlgTestFail>(),std::runtime_error);
Dickon Champion's avatar
Dickon Champion committed
  }

 void testVersionPass()
  {
Dickon Champion's avatar
Dickon Champion committed
	TS_ASSERT_THROWS_NOTHING(AlgorithmFactory::Instance().subscribe<AlgTestPass>());
Dickon Champion's avatar
Dickon Champion committed
  }

Nick Draper's avatar
Nick Draper committed
  void testInstance()
  {
    // Not really much to test
    //AlgorithmManager *tester = AlgorithmManager::Instance();
    //TS_ASSERT_EQUALS( manager, tester);
    TS_ASSERT_THROWS_NOTHING( AlgorithmManager::Instance().create("AlgTest") )
Dickon Champion's avatar
Dickon Champion committed
    TS_ASSERT_THROWS(AlgorithmManager::Instance().create("AlgTest",3), std::runtime_error )
    TS_ASSERT_THROWS(AlgorithmManager::Instance().create("aaaaaa"), std::runtime_error )
Nick Draper's avatar
Nick Draper committed
  }
  
  void testGetNames()
  {
	AlgorithmManager::Instance().clear();
	TS_ASSERT_THROWS_NOTHING( AlgorithmManager::Instance().create("AlgTest") );
	TS_ASSERT_THROWS_NOTHING(AlgorithmManager::Instance().create("AlgTestSecond") );
	std::vector<std::string> names = AlgorithmManager::Instance().getNames();
	TS_ASSERT_EQUALS(names.size(), 2)
  }
Nick Draper's avatar
Nick Draper committed

  void testClear()
  {
Dickon Champion's avatar
Dickon Champion committed
//    AlgorithmFactory::Instance().subscribe<AlgTest>();
//    AlgorithmFactory::Instance().subscribe<AlgTestSecond>();
    TS_ASSERT_THROWS_NOTHING( AlgorithmManager::Instance().create("AlgTest") );
    TS_ASSERT_THROWS_NOTHING(AlgorithmManager::Instance().create("AlgTestSecond") );
    TS_ASSERT_EQUALS(AlgorithmManager::Instance().size(),2);
    AlgorithmManager::Instance().clear();
    TS_ASSERT_EQUALS(AlgorithmManager::Instance().size(),0);
Nick Draper's avatar
Nick Draper committed
  }

  void testReturnType()
  {
Dickon Champion's avatar
Dickon Champion committed
//    AlgorithmFactory::Instance().subscribe<AlgTest>();
//    AlgorithmFactory::Instance().subscribe<AlgTestSecond>();
Nick Draper's avatar
Nick Draper committed
    Algorithm_sptr alg;
Dickon Champion's avatar
Dickon Champion committed
    TS_ASSERT_THROWS_NOTHING( alg = AlgorithmManager::Instance().create("AlgTest",1) );
    TS_ASSERT_DIFFERS(dynamic_cast<AlgTest*>(alg.get()),static_cast<AlgTest*>(0));
Dickon Champion's avatar
Dickon Champion committed
    TS_ASSERT_THROWS_NOTHING( alg = AlgorithmManager::Instance().create("AlgTestSecond",1) );
    TS_ASSERT_DIFFERS(dynamic_cast<AlgTestSecond*>(alg.get()),static_cast<AlgTestSecond*>(0));
Nick Draper's avatar
Nick Draper committed
    TS_ASSERT_DIFFERS(dynamic_cast<Algorithm*>(alg.get()),static_cast<Algorithm*>(0));
    TS_ASSERT_EQUALS(AlgorithmManager::Instance().size(),2);   // To check that crea is called on local objects
Nick Draper's avatar
Nick Draper committed
  }

  void testManagedType()
  {
Nick Draper's avatar
Nick Draper committed
    Algorithm_sptr Aptr, Bptr;
    Aptr=AlgorithmManager::Instance().create("AlgTest");
    Bptr=AlgorithmManager::Instance().createUnmanaged("AlgTest");
Nick Draper's avatar
Nick Draper committed
    TS_ASSERT_DIFFERS(Aptr,Bptr);
    TS_ASSERT_EQUALS(AlgorithmManager::Instance().size(),1);
Nick Draper's avatar
Nick Draper committed
    TS_ASSERT_DIFFERS(Aptr.get(),static_cast<Algorithm*>(0));
    TS_ASSERT_DIFFERS(Bptr.get(),static_cast<Algorithm*>(0));
  }