Newer
Older
Steve Williams
committed
#ifndef ADDSAMPLELOGTEST_H_
#define ADDSAMPLELOGTEST_H_
#include <cxxtest/TestSuite.h>
#include <string>
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
Steve Williams
committed
#include "MantidAlgorithms/AddSampleLog.h"
Janik Zikovsky
committed
#include "MantidKernel/TimeSeriesProperty.h"
Steve Williams
committed
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Algorithms;
class AddSampleLogTest : public CxxTest::TestSuite {
Steve Williams
committed
public:
void test_Workspace2D() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
Janik Zikovsky
committed
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0);
Gigg, Martyn Anthony
committed
}
void test_EventWorkspace() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::CreateEventWorkspace(10, 10);
Janik Zikovsky
committed
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0);
Gigg, Martyn Anthony
committed
}
void test_CanOverwrite() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
Janik Zikovsky
committed
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0);
ExecuteAlgorithm(ws, "My Name", "String", "My New Value", 0.0);
}
void test_Number() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
Janik Zikovsky
committed
ExecuteAlgorithm(ws, "My Name", "Number", "1.234", 1.234);
ExecuteAlgorithm(ws, "My Name", "Number", "2.456", 2.456);
ExecuteAlgorithm(ws, "My Name", "Number", "-987654321", -987654321);
ExecuteAlgorithm(ws, "My Name", "Number", "963", 963);
Janik Zikovsky
committed
}
void test_BadNumber() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
Janik Zikovsky
committed
ExecuteAlgorithm(ws, "My Name", "Number", "OneTwoThreeFour", 0.0, true);
}
void test_BadNumberSeries() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
Janik Zikovsky
committed
ExecuteAlgorithm(ws, "My Name", "Number Series", "FiveSixSeven", 0.0, true);
}
void test_NumberSeries() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
ws->mutableRun().setStartAndEndTime(DateAndTime("2013-12-18T13:40:00"),
DateAndTime("2013-12-18T13:42:00"));
Janik Zikovsky
committed
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234);
ExecuteAlgorithm(ws, "My Name", "Number Series", "2.456", 2.456);
ExecuteAlgorithm(ws, "My Name", "Number Series", "-1", -1);
ExecuteAlgorithm(ws, "Another Name", "Number Series", "0", 0);
ExecuteAlgorithm(ws, "Another Name", "Number Series", "123456789",
123456789);
Janik Zikovsky
committed
}
void test_Units() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
ws->mutableRun().setStartAndEndTime(DateAndTime("2013-12-18T13:40:00"),
DateAndTime("2013-12-18T13:42:00"));
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234, false,
"myUnit");
ExecuteAlgorithm(ws, "My New Name", "Number", "963", 963, false,
"differentUnit");
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0, false,
"stringUnit");
void test_number_type() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
ws->mutableRun().setStartAndEndTime(DateAndTime("2013-12-18T13:40:00"),
DateAndTime("2013-12-18T13:42:00"));
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234, false,
"myUnit", "Double");
ExecuteAlgorithm(ws, "My New Name", "Number", "963", 963, false,
"differentUnit", "Int");
// Can force '963' to be interpreted as a double
ExecuteAlgorithm(ws, "My New Name", "Number", "963", 963.0, false,
"differentUnit", "Double");
// Should throw error as NumberType defined for a String
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0, true,
"stringUnit", "Double", true);
// Should throw error trying to interpret '1.234' as Int
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234, true,
"myUnit", "Int", true);
}
void
ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string LogName,
std::string LogType, std::string LogText, T expectedValue,
bool fails = false, std::string LogUnit = "",
std::string NumberType = "AutoDetect", bool throws = false) {
// add the workspace to the ADS
AnalysisDataService::Instance().addOrReplace("AddSampleLogTest_Temporary",
testWS);
Gigg, Martyn Anthony
committed
AddSampleLog alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
if (throws)
alg.setRethrows(true);
Steve Williams
committed
alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary");
Janik Zikovsky
committed
alg.setPropertyValue("LogName", LogName);
alg.setPropertyValue("LogText", LogText);
alg.setPropertyValue("LogUnit", LogUnit);
Janik Zikovsky
committed
alg.setPropertyValue("LogType", LogType);
alg.setPropertyValue("NumberType", NumberType);
if (throws) {
TS_ASSERT_THROWS_ANYTHING(alg.execute())
return;
}
Steve Williams
committed
TS_ASSERT_THROWS_NOTHING(alg.execute())
if (fails) {
TS_ASSERT(!alg.isExecuted())
Janik Zikovsky
committed
return;
} else {
TS_ASSERT(alg.isExecuted())
Janik Zikovsky
committed
}
Steve Williams
committed
// check output
MatrixWorkspace_sptr output =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
alg.getProperty("Workspace"));
const Run &wSpaceRun = output->run();
Property *prop = NULL;
Janik Zikovsky
committed
TS_ASSERT_THROWS_NOTHING(prop = wSpaceRun.getLogData(LogName);)
Janik Zikovsky
committed
if (LogType == "String") {
TS_ASSERT_EQUALS(prop->value(), LogText);
} else if (LogType == "Number") {
auto testProp = dynamic_cast<PropertyWithValue<T> *>(prop);
Janik Zikovsky
committed
TS_ASSERT(testProp);
TS_ASSERT_DELTA((*testProp)(), expectedValue, 1e-5);
} else if (LogType == "Number Series") {
auto testProp = dynamic_cast<TimeSeriesProperty<T> *>(prop);
Janik Zikovsky
committed
TS_ASSERT(testProp);
TS_ASSERT_EQUALS(testProp->firstTime(),
DateAndTime("2013-12-18T13:40:00"));
Janik Zikovsky
committed
TS_ASSERT_DELTA(testProp->firstValue(), expectedValue, 1e-5);
}
Gigg, Martyn Anthony
committed
}
Steve Williams
committed
};
#endif /*ADDSAMPLELOGTEST_H_*/