Skip to content

Commit d8dd616

Browse files
committed
(Issue #291) Basic MATLAB unittest of create_trsp_solver().
1 parent e72a98f commit d8dd616

2 files changed

Lines changed: 99 additions & 3 deletions

File tree

pounders/m/create_trsp_solver.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
% Please refer to the documentation for the Python version of this
33
% function.
44

5+
arguments
6+
spsolver {mustBeScalarOrEmpty, mustBeNonempty, mustBeInteger}
7+
end
8+
59
% ----- HARDCODED VALUES
610
% Ensure that these match the analogous constants implemented for
7-
% POUNDERS/Python.
11+
% POUNDERS/Python. These same values might be used in the code that
12+
% directly tests this function.
813
%
914
% Both MATLAB and Python implementations should declare the union of all
1015
% solvers available even if they don't support one or more of the solvers.
@@ -53,7 +58,8 @@
5358

5459
% ----- IDENTIFY DESIRED SOLVER
5560
if spsolver == TRSP_SOLVER_SIMPLE
56-
disp("WARNING: The simple TRSP solver should only be used for testing or debugging");
61+
warning("POUNDERS:simpleTrspSolver", ...
62+
"The simple TRSP solver should only be used for testing or debugging");
5763
solver = @bqmin_wrapper;
5864
elseif spsolver == TRSP_SOLVER_MINQ5
5965
check_minq_installation(5);
@@ -62,6 +68,6 @@
6268
check_minq_installation(8);
6369
solver = @minq8_wrapper;
6470
else
65-
error(sprintf("Invalid TRSP solver %d", spsolver));
71+
error('POUNDERS:badValue', sprintf("Invalid TRSP solver %d", spsolver));
6672
end
6773
end
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
% To run this test, you must first install a MINQ clone and add
2+
% /path/to/MINQ/m/minq5
3+
% /path/to/MINQ/m/minq8
4+
% to the MATLAB path.
5+
%
6+
% From MATLAB and the directory containing this file, execute
7+
% >> runtests
8+
%
9+
% If you would like to run this from a different folder that includes these
10+
% tests as a subfolder, then from that folder execute
11+
% >> runtests("IncludeSubfolders", true)
12+
%
13+
% To execute the test suite with coverage enabled and to generate an HTML-format
14+
% coverage report, execute from /path/to/IBCDFO/pounders/m
15+
% >> runtests("IncludeSubfolders", true, "ReportCoverageFor", pwd)
16+
%
17+
18+
classdef TestCreateTrspSolver < matlab.unittest.TestCase
19+
properties (Constant)
20+
WARNING_SIMPLE = 'POUNDERS:simpleTrspSolver'
21+
ERROR_BAD_SOLVER = 'POUNDERS:badValue'
22+
% These should match the values used in create_trsp_solver().
23+
SOLVER_SIMPLE = 1
24+
SOLVER_MINQ5 = 2
25+
SOLVER_MINQ8 = 3
26+
end
27+
28+
properties
29+
oldpath
30+
solvers
31+
emitWarnings
32+
end
33+
34+
methods (TestMethodSetup)
35+
function setUp(testCase)
36+
testCase.solvers = [testCase.SOLVER_SIMPLE, ...
37+
testCase.SOLVER_MINQ5, ...
38+
testCase.SOLVER_MINQ8];
39+
40+
testCase.emitWarnings = struct([]);
41+
testCase.emitWarnings(1).solver = testCase.SOLVER_SIMPLE;
42+
testCase.emitWarnings(1).warnID = testCase.WARNING_SIMPLE;
43+
44+
warning("on");
45+
46+
[here_path, ~, ~] = fileparts(mfilename('fullpath'));
47+
testCase.oldpath = addpath(fullfile(here_path, '..'));
48+
end
49+
end
50+
51+
methods (TestMethodTeardown)
52+
function tearDown(testCase)
53+
path(testCase.oldpath);
54+
end
55+
end
56+
57+
methods (Test)
58+
function testErrors(testCase)
59+
badSolvers = [min(testCase.solvers) - 1, ...
60+
max(testCase.solvers) + 1];
61+
for i = 1:length(badSolvers)
62+
bad = badSolvers(i);
63+
testCase.assertError(@() create_trsp_solver(bad), ...
64+
testCase.ERROR_BAD_SOLVER);
65+
end
66+
end
67+
68+
function testWarnings(testCase)
69+
for i = 1:length(testCase.emitWarnings)
70+
idx = testCase.emitWarnings(i).solver;
71+
warnID = testCase.emitWarnings(i).warnID;
72+
testCase.assertWarning(@() create_trsp_solver(idx), warnID);
73+
end
74+
end
75+
76+
function testSuccessful(testCase)
77+
% Expected emission of specific warnings tested in testWarnings.
78+
% Ignore only those to silence expected warnings without
79+
% inadvertently silencing unintended warnings.
80+
warning("off", testCase.WARNING_SIMPLE);
81+
for i = 1:length(testCase.solvers)
82+
idx = testCase.solvers(i);
83+
solve_trsp = create_trsp_solver(idx);
84+
testCase.assertTrue(isa(solve_trsp, 'function_handle'));
85+
end
86+
warning("on", testCase.WARNING_SIMPLE);
87+
end
88+
end
89+
90+
end

0 commit comments

Comments
 (0)