Skip to content

Commit

Permalink
Add 3 Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Despiix committed Nov 6, 2024
1 parent ec560e4 commit 190679a
Showing 1 changed file with 50 additions and 44 deletions.
94 changes: 50 additions & 44 deletions Framework/Algorithms/test/CreateMonteCarloWorkspaceTest.h
Original file line number Diff line number Diff line change
@@ -1,74 +1,80 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2024 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include <cxxtest/TestSuite.h>
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAlgorithms/CreateSampleWorkspace.h"
#include "MantidAlgorithms/CreateMonteCarloWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
#include "MantidHistogramData/Histogram.h"
#include <cxxtest/TestSuite.h>

#include <random>

using namespace Mantid::Algorithms;
using namespace Mantid::API;
using namespace Mantid::HistogramData;

using Mantid::Algorithms::CreateMonteCarloWorkspace;

class CreateMonteCarloWorkspaceTest : 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 CreateMonteCarloWorkspaceTest *createSuite() { return new CreateMonteCarloWorkspaceTest(); }
static void destroySuite(CreateMonteCarloWorkspaceTest *suite) { delete suite; }

void test_Init() {
CreateMonteCarloWorkspace alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
}

void test_exec() {
// Name of the output workspace.
std::string outWSName("MonteCarloTest_WS");
void test_computeNumberOfIterations() {
CreateMonteCarloWorkspace alg;
Mantid::HistogramData::HistogramY yData = {1.0, 2.0, 3.0, 4.0};
int iterations = alg.computeNumberOfIterations(yData);
TS_ASSERT_EQUALS(iterations, 10); // Verify if the sum of yData is rounded correctly
}

// Create input workspace for the algorithm
MatrixWorkspace_sptr inputWS = WorkspaceCreationHelper::create2DWorkspace(1, 10);
void test_computeNormalizedCDF() {
CreateMonteCarloWorkspace alg;
Mantid::HistogramData::HistogramY yData = {1.0, 2.0, 3.0, 4.0};
std::vector<double> cdf = alg.computeNormalizedCDF(yData);
TS_ASSERT_EQUALS(cdf.size(), yData.size());
TS_ASSERT_DELTA(cdf.back(), 1.0, 1e-6); // Check if the last element is normalized to 1.0
}

void test_fillHistogramWithRandomData() {
CreateMonteCarloWorkspace alg;
// Don't put output in ADS by default
alg.setChild(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())

const int num_iterations = 1000;
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InstrumentWorkspace", inputWS));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Iterations", num_iterations));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Seed", 32));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", outWSName));

TS_ASSERT_THROWS_NOTHING(alg.execute(););
TS_ASSERT(alg.isExecuted());
std::vector<double> cdf = {0.1, 0.3, 0.6, 1.0};
std::mt19937 gen(32);

// Retrieve the output workspace
Workspace_sptr outputWS = alg.getProperty("OutputWorkspace");
TS_ASSERT(outputWS);
Mantid::HistogramData::HistogramY outputY = alg.fillHistogramWithRandomData(cdf, 100, gen);

// Cast to the correct type (not sure if needed)
auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(outputWS);
TS_ASSERT(matrixWS);
auto sumCounts = std::accumulate(outputY.begin(), outputY.end(), 0.0);
TS_ASSERT_EQUALS(sumCounts, 100); // Ensure that the total number of counts is correct
}

std::cout << "Number of histograms: " << matrixWS->getNumberHistograms() << std::endl;
std::cout << "Size of Y data (expected " << num_iterations << "): " << matrixWS->y(0).size() << std::endl;
void test_exec() {
CreateMonteCarloWorkspace alg;
alg.initialize();

TS_ASSERT_EQUALS(matrixWS->y(0).size(), num_iterations); // Check if Y data has the expected number of entries
auto inputWS = WorkspaceCreationHelper::create2DWorkspace(1, 10);
auto &yData = inputWS->mutableY(0);
std::fill(yData.begin(), yData.end(), 5.0);

TS_FAIL("TODO: Remove this line once the test works as expected.");
}
alg.setProperty("InputWorkspace", inputWS);
alg.setProperty("Seed", 32);
alg.setPropertyValue("OutputWorkspace", "MonteCarloTest_WS");

TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());

using Mantid::API::AnalysisDataService; // To Retrieve the output workspace
MatrixWorkspace_sptr outputWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("MonteCarloTest_WS");
TS_ASSERT(outputWS);

// Verify the output data
const auto &outputY = outputWS->y(0);
auto sumOutput = std::accumulate(outputY.begin(), outputY.end(), 0.0);
TS_ASSERT_EQUALS(sumOutput, alg.computeNumberOfIterations(yData)); // Compare number of iterations

// Clean up: Remove the workspace from ADS after test
AnalysisDataService::Instance().remove("MonteCarloTest_WS");
}
};

0 comments on commit 190679a

Please sign in to comment.