From 808795a53e46e96051519b99275dbee202fabf35 Mon Sep 17 00:00:00 2001
From: Vickie Lynch <vlynch@ornl.gov>
Date: Thu, 15 Aug 2013 19:41:15 -0400
Subject: [PATCH] Refs #7449 add test for separating background

---
 .../Framework/Algorithms/CMakeLists.txt       |   1 +
 .../src/SeparateBackgroundFromSignal.cpp      |   1 +
 .../test/SeparateBackgroundFromSignalTest.h   | 113 ++++++++++++++++++
 3 files changed, 115 insertions(+)
 create mode 100644 Code/Mantid/Framework/Algorithms/test/SeparateBackgroundFromSignalTest.h

diff --git a/Code/Mantid/Framework/Algorithms/CMakeLists.txt b/Code/Mantid/Framework/Algorithms/CMakeLists.txt
index 613d822314e..89b40416faa 100644
--- a/Code/Mantid/Framework/Algorithms/CMakeLists.txt
+++ b/Code/Mantid/Framework/Algorithms/CMakeLists.txt
@@ -622,6 +622,7 @@ set ( TEST_FILES
 	SassenaFFTTest.h
 	ScaleTest.h
 	ScaleXTest.h
+	SeparateBackgroundFromSignalTest.h
 	ShiftLogTimeTest.h
 	SignalOverErrorTest.h
 	SmoothDataTest.h
diff --git a/Code/Mantid/Framework/Algorithms/src/SeparateBackgroundFromSignal.cpp b/Code/Mantid/Framework/Algorithms/src/SeparateBackgroundFromSignal.cpp
index c5a25f83fb8..c39e697b60f 100644
--- a/Code/Mantid/Framework/Algorithms/src/SeparateBackgroundFromSignal.cpp
+++ b/Code/Mantid/Framework/Algorithms/src/SeparateBackgroundFromSignal.cpp
@@ -179,6 +179,7 @@ namespace Algorithms
 			  cont_stop.push_back(l-1);
 		  }
 	  }
+	  if(cont_start.size() > cont_stop.size()) cont_stop.push_back(n-1);
 	  vector<size_t> cont_len;
 	  for (size_t l = 0; l < cont_start.size(); ++l)
 	  {
diff --git a/Code/Mantid/Framework/Algorithms/test/SeparateBackgroundFromSignalTest.h b/Code/Mantid/Framework/Algorithms/test/SeparateBackgroundFromSignalTest.h
new file mode 100644
index 00000000000..23b493cf120
--- /dev/null
+++ b/Code/Mantid/Framework/Algorithms/test/SeparateBackgroundFromSignalTest.h
@@ -0,0 +1,113 @@
+#ifndef MANTID_ALGORITHMS_SeparateBackgroundFromSignal_H_
+#define MANTID_ALGORITHMS_SeparateBackgroundFromSignalTEST_H_
+
+#include <cxxtest/TestSuite.h>
+
+#include "MantidAlgorithms/SeparateBackgroundFromSignal.h"
+#include "MantidAPI/WorkspaceFactory.h"
+#include "MantidAPI/MatrixWorkspace.h"
+#include "MantidDataObjects/Workspace2D.h"
+
+#include <cmath>
+
+using namespace Mantid;
+using namespace Mantid::API;
+using namespace Mantid::Kernel;
+using namespace Mantid::DataObjects;
+
+using namespace std;
+
+using Mantid::Algorithms::SeparateBackgroundFromSignal;
+
+class SeparateBackgroundFromSignalTest : public CxxTest::TestSuite
+{
+public:
+  // This pair of boilerplate methods prevent the suite being created statically
+  // This means the constructor isn't called when running other tests
+  static SeparateBackgroundFromSignalTest *createSuite() { return new SeparateBackgroundFromSignalTest(); }
+  static void destroySuite( SeparateBackgroundFromSignalTest *suite ) { delete suite; }
+
+
+  void test_Calculation()
+  {
+
+    // 1. Generate input workspace
+    MatrixWorkspace_sptr inWS = generateTestWorkspace();
+
+    // 2. Create
+    Algorithms::SeparateBackgroundFromSignal alg;
+
+    alg.initialize();
+    TS_ASSERT(alg.isInitialized());
+
+    alg.setProperty("InputWorkspace", inWS);
+    alg.setProperty("OutputWorkspace", "Signal");
+    alg.setProperty("WorkspaceIndex", 0);
+
+    alg.execute();
+    TS_ASSERT(alg.isExecuted());
+
+    Workspace2D_sptr outWS = boost::dynamic_pointer_cast<Workspace2D>
+        (AnalysisDataService::Instance().retrieve("Signal"));
+
+    const MantidVec& Signal = outWS->readY(0);
+    TS_ASSERT_DELTA(Signal[2], 0.0000, 0.0001);
+    TS_ASSERT_DELTA(Signal[10], 28.000, 0.0001);
+
+    const MantidVec& vecX = outWS->readX(0);
+    TS_ASSERT_DELTA(vecX[0], 0.0, 0.000001);
+    TS_ASSERT_DELTA(vecX[5], 5.0, 0.000001);
+    TS_ASSERT_DELTA(vecX[10], 10.0, 0.000001);
+
+    return;
+  }
+
+
+  /** Generate a workspace for test
+    */
+  MatrixWorkspace_sptr generateTestWorkspace()
+  {
+    vector<double> data;
+    data.push_back(1);
+    data.push_back(2);
+    data.push_back(1);
+    data.push_back(1);
+    data.push_back(9);
+    data.push_back(11);
+    data.push_back(13);
+    data.push_back(20);
+    data.push_back(24);
+    data.push_back(32);
+    data.push_back(28);
+    data.push_back(48);
+    data.push_back(42);
+    data.push_back(77);
+    data.push_back(67);
+    data.push_back(33);
+    data.push_back(27);
+    data.push_back(20);
+    data.push_back(9);
+    data.push_back(2);
+
+    MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>
+        (WorkspaceFactory::Instance().create("Workspace2D", 1, data.size(), data.size()));
+
+    MantidVec& vecX = ws->dataX(0);
+    MantidVec& vecY = ws->dataY(0);
+    MantidVec& vecE = ws->dataE(0);
+
+    for (size_t i = 0; i < data.size(); ++i)
+    {
+      vecX[i] = static_cast<double>(i);
+      vecY[i] = data[i];
+      vecE[i] = sqrt(data[i]);
+    }
+
+    return ws;
+  }
+
+
+};
+
+
+#endif /* MANTID_ALGORITHMS_SeparateBackgroundFromSignalTEST_H_ */
-- 
GitLab