From 04ecbb3d623aabf38541f0f62428438ff62e7844 Mon Sep 17 00:00:00 2001 From: Emma Smith Zbarsky Date: Tue, 21 Mar 2023 14:43:50 -0400 Subject: [PATCH] Update Startup and add tests --- HelperFunctions/Startup.m | 2 +- tests/functionTests.m | 19 ++++++ tests/model2t.m | 6 ++ tests/model3t.m | 9 +++ tests/model4t.m | 10 ++++ tests/smokeTests.m | 119 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 tests/functionTests.m create mode 100644 tests/model2t.m create mode 100644 tests/model3t.m create mode 100644 tests/model4t.m create mode 100644 tests/smokeTests.m diff --git a/HelperFunctions/Startup.m b/HelperFunctions/Startup.m index 6f16a82..9c56540 100644 --- a/HelperFunctions/Startup.m +++ b/HelperFunctions/Startup.m @@ -2,7 +2,7 @@ % Open the overview file locDir = pwd; if contains(locDir,filesep+"MATLAB Drive") - open("NavigationOverview.mlx") + open("Navigation.mlx") else open("Overview.html") end diff --git a/tests/functionTests.m b/tests/functionTests.m new file mode 100644 index 0000000..62e69a9 --- /dev/null +++ b/tests/functionTests.m @@ -0,0 +1,19 @@ +% Run these tests with runMyTests +% +% Alternately, run these tests with +% results = runtests(tests) +% table(results) +classdef functionTests < matlab.unittest.TestCase + + methods(Test) + function checkStartup(testCase) + Startup + end + + function checkOverview(testCase) + OpenOverview + end + + end % methods + +end % classdef \ No newline at end of file diff --git a/tests/model2t.m b/tests/model2t.m new file mode 100644 index 0000000..8f0ed0e --- /dev/null +++ b/tests/model2t.m @@ -0,0 +1,6 @@ +function dydt = model2t(t,y,L,g) +theta = y(1); +omega = y(2); +dydt = [omega; ... + -g/L*sin(theta)]; +end \ No newline at end of file diff --git a/tests/model3t.m b/tests/model3t.m new file mode 100644 index 0000000..0bf07e4 --- /dev/null +++ b/tests/model3t.m @@ -0,0 +1,9 @@ +function dydt = model3t(t,y,L,g,M,b,c) +theta = y(1); +omega = y(2); +if omega < 0 + c = -c; +end +dydt = [omega; ... + (-g*M*L*sin(theta)-b*omega-c*omega^2)/(M*L^2)]; +end \ No newline at end of file diff --git a/tests/model4t.m b/tests/model4t.m new file mode 100644 index 0000000..c04a850 --- /dev/null +++ b/tests/model4t.m @@ -0,0 +1,10 @@ +function dydt = model4t(t,y,L,g,M,m,b,c) +theta = y(1); +omega = y(2); +if omega < 0 + c = -c; +end +rotInertia = m*L^2/3+M*L^2; +dydt = [omega; ... + (-g*(M*L+m*L/2)*sin(theta)-b*omega-c*omega^2)/rotInertia]; +end \ No newline at end of file diff --git a/tests/smokeTests.m b/tests/smokeTests.m new file mode 100644 index 0000000..552b519 --- /dev/null +++ b/tests/smokeTests.m @@ -0,0 +1,119 @@ +% Run these tests with runMyTests +% All tests so far are on code expected to run without errors +% If/when we end up with a version that _should_ error, +% please add it to this set of examples +classdef smokeTests < matlab.unittest.TestCase + + properties + fc + origProj + openFilesIdx + end + + methods (TestClassSetup) + function setUpPath(testCase) + testCase.origProj = matlab.project.rootProject; + testCase.openFilesIdx = length(matlab.desktop.editor.getAll); + testCase.fc = fullfile(pwd); + rootDirName = extractBefore(testCase.fc,"tests"); + openProject(rootDirName); + end % function setUpPath + end % methods (TestClassSetup) + + methods(Test) + + function dataExists(testCase) + load("hand2.mat","x","y") + clear x y + load("car.mat","xCar","yCar") + clear xCar yCar + load("drawing.mat","x","y") + clear x y + load("lakeData.mat","lakeX","lakeY","scale") + clear lakeY lakeX scale + load("modernLakeData.mat","latScale","longScale","modLakeLat","modLakeLong") + clear latScale longScale modLakeLat modLakeLong + load("myStorm.mat","myStorm") + clear myStorm + end + + function runDerivatives(testCase) + % this function runs all the code in Functions.mlx + % it also logs the final figure in the resulting output + % document while closing the figure window on teardown + import matlab.unittest.diagnostics.FigureDiagnostic; + testCase.log("Running approximatingDerivatives.mlx") + fig = figure; + testCase.addTeardown(@close,fig) + approximatingDerivatives + testCase.log(3,FigureDiagnostic(fig)) + end + + function runInterpolation(testCase) + % this is the simplest possible logged version of a smoke test + % that will run a file called "SharingCode.mlx" + testCase.log("Running interpolation.mlx") + interpolation + end + function runStorms(testCase) + testCase.log("Running track storms...") + trackStorms + end + + function runNumericalIntegration(testCase) + testCase.log("Running Numerical Integration") + numericalIntegration + end + + function runNumODEs(testCase) + testCase.log("Running Numerical ODEs") + diffEqs + end + function runPendulum(testCase) + testCase.log("Running pendulum...") + flag = 0; + try + pendulum + catch ME + if string(ME.identifier) == "MATLAB:unassignedOutputs" + flag = flag+1; + switch flag + case 1 + [t2,theta2] = ode45(@(t,theta) model2t(t,theta,L,g),[t0 tEnd],[theta0 omega0]); + case 2 + [t3,theta3] = ode45(@(t,theta) model3t(t,theta,L,g,M,b,c),[t0 tEnd],[theta0 omega0]); + case 3 + [t4,theta4] = ode45(@(t,theta) model4t(t,theta,L,g,M,m,b,c),[t0 tEnd],[theta0 omega0]); + end + else + rethrow(ME) + end + end + + end + + function runNumPDEs(testCase) + testCase.log("Running Numerical PDEs") + partialDiffEqs + end + end + + methods (TestClassTeardown) + function resetPath(testCase) + + if isempty(testCase.origProj) + close(currentProject) + else + openProject(testCase.origProj.RootFolder) + end + myLastList = matlab.desktop.editor.getAll; + if length(myLastList)>testCase.openFilesIdx + closeNoPrompt(myLastList(testCase.openFilesIdx+1:end)) + end + cd(testCase.fc) + close all force + end + + end % methods (TestClassTeardown) + +end \ No newline at end of file