From cc7623065c46a935822e46608f8d995e3141986b Mon Sep 17 00:00:00 2001 From: gabrigghia <gabriele.ghia@skywarder.eu> Date: Wed, 13 Nov 2024 10:54:29 +0100 Subject: [PATCH] [unit-test][UnitTestClasses] emergency push --- unitTests/unitTestClasses/TestSimulator.m | 115 +++++++++------------- unitTests/unitTestClasses/mainUnitTest.m | 2 +- 2 files changed, 45 insertions(+), 72 deletions(-) diff --git a/unitTests/unitTestClasses/TestSimulator.m b/unitTests/unitTestClasses/TestSimulator.m index 52f8c93b..8e94a72c 100644 --- a/unitTests/unitTestClasses/TestSimulator.m +++ b/unitTests/unitTestClasses/TestSimulator.m @@ -1,33 +1,43 @@ classdef TestSimulator < UnitTest properties - simulatorSettings struct % [-] + simulatorSettings struct % [-] refSimulator end properties (TestParameter) - verifiable = {'multipleAB', 'ballistic', 'engineCut', 'solid','HRE'} + verifiable = {'multipleAB', 'ballistic', 'engineCut', 'solid', 'HRE'}; end - methods - function saveTest(testCase, verifiable, mission) - + methods (Static) + function runSelectedTests(selectedParams) + if nargin < 1 || isempty(selectedParams) + selectedParams = {'multipleAB', 'ballistic', 'engineCut', 'solid', 'HRE'}; + fprintf('\t Running tests with default parameters\n'); + else + fprintf('\t Running tests with selected parameters: %s\n', strjoin(selectedParams, ', ')); + end + params = matlab.unittest.parameters.Parameter.fromData("Data",selectedParams); + suite = matlab.unittest.TestSuite.fromClass(?TestSimulator, 'ExternalParameters', params); + results = run(suite); + disp(results); + end + end + methods + function saveTest(testCase, verifiable, mission) Time = tic; rocket = Rocket(mission); environment = Environment(mission, rocket.motor); wind = WindCustom(mission); - - - - % simulator - [ascent, descent, simulatorSettings] = mainSimulator(rocket, wind, environment); - stateA = ascent(end, :); % state at end of ascent - stateF = descent(end, :); % final state, at touchdown + % Run simulator + [ascent, descent, simulatorSettings] = mainSimulator(rocket, wind, environment); + stateA = ascent(end, :); % State at end of ascent + stateF = descent(end, :); % Final state, at touchdown - % saving final state + % Save final state fileName = sprintf("referenceState_%s.mat", verifiable); folderPath = fullfile('data', 'testSimulator', verifiable); filePath = fullfile(folderPath, fileName); @@ -35,84 +45,47 @@ classdef TestSimulator < UnitTest if ~exist(folderPath, "dir") mkdir(folderPath) end - save(filePath,'stateA','stateF', 'simulatorSettings',... - 'mission', 'rocket',... - 'environment', 'wind'); - - Time = toc(Time); - fprintf('\t %s test created in: %2.2f seconds\n',verifiable, Time) - - end - - - function values = setVerifiable(testCase, externalValues) - - % Function to allow user to change, if wanted, the parameters with - % which the test is run, so as to allow test running for certain - % conditions only - - if isempty(externalValues) - values = testCase.verifiable; - fprintf('\t testing with general params\n'); - else - values = externalValues; - end + save(filePath, 'stateA', 'stateF', 'simulatorSettings', 'mission', 'rocket', 'environment', 'wind'); + Time = toc(Time); + fprintf('\t %s test created in: %2.2f seconds\n', verifiable, Time); end function createTestSimulator(testCase, verifiable) - - verifiable = testCase.setVerifiable(verifiable); - currentPath = fileparts(mfilename('fullpath')); addpath(currentPath); - + fileName = sprintf("referenceState_%s.mat", verifiable); filePath = fullfile('data', 'testSimulator', verifiable, fileName); - - + % Load the reference file - testCase.rocket = load(filePath, 'rocket').rocket; - testCase.environment = load(filePath, 'environment').environment; - testCase.wind = load(filePath, 'wind').wind; - - % fieldName = ['referenceState_' verifiable]; - testCase.refSimulator = load(filePath, 'stateA', 'stateF'); - testCase.simulatorSettings = load(filePath, 'simulatorSettings').simulatorSettings; - - % Construct the file path dynamically based on the test parameter - + loadedData = load(filePath); + testCase.rocket = loadedData.rocket; + testCase.environment = loadedData.environment; + testCase.wind = loadedData.wind; + testCase.refSimulator = struct('stateA', loadedData.stateA, 'stateF', loadedData.stateF); + testCase.simulatorSettings = loadedData.simulatorSettings; end - end - - - - - - - methods (Test) - - function mainSimulatorTest(testCase, verifiable) - - verifiable = testCase.setVerifiable(verifiable); + if nargin < 2 || isempty(verifiable) + error('The "verifiable" parameter must be provided when not using TestParameter.'); + end + testCase.createTestSimulator(verifiable); mainSimPath = fullfile('..', 'simulator'); addpath(mainSimPath); - + + % Run main simulator [postp.stateA, postp.stateF, ~] = mainSimulator(testCase.rocket, testCase.wind, testCase.environment); - postp.stateA = postp.stateA(end, :); % state at end of ascent + postp.stateA = postp.stateA(end, :); postp.stateF = postp.stateF(end, :); - testCase.verifyEqual(postp, testCase.refSimulator, 'AbsTol', testCase.absToll, ... - 'RelTol', testCase.relToll) + % Verify results + testCase.verifyEqual(postp, testCase.refSimulator, 'AbsTol', testCase.absToll, 'RelTol', testCase.relToll); end - end - - -end \ No newline at end of file +end diff --git a/unitTests/unitTestClasses/mainUnitTest.m b/unitTests/unitTestClasses/mainUnitTest.m index 654ca926..3be54ada 100644 --- a/unitTests/unitTestClasses/mainUnitTest.m +++ b/unitTests/unitTestClasses/mainUnitTest.m @@ -37,7 +37,7 @@ end if opt.testSimulator test = TestSimulator(); - sensResults = run(test); + test.runSelectedTests({}); end -- GitLab