From 190679ad3900da03f39ceac80c8c491764040f53 Mon Sep 17 00:00:00 2001 From: Despiix Date: Wed, 6 Nov 2024 15:40:27 +0000 Subject: [PATCH] Add 3 Unit tests --- .../test/CreateMonteCarloWorkspaceTest.h | 94 ++++++++++--------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/Framework/Algorithms/test/CreateMonteCarloWorkspaceTest.h b/Framework/Algorithms/test/CreateMonteCarloWorkspaceTest.h index 0b1b7d1340d3..bf2732f516e4 100644 --- a/Framework/Algorithms/test/CreateMonteCarloWorkspaceTest.h +++ b/Framework/Algorithms/test/CreateMonteCarloWorkspaceTest.h @@ -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 #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 +#include 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 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 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(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("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"); + } };