Skip to content

Commit e72a98f

Browse files
committed
(Issue #291) Basic unittest of create_trsp_solver().
Passing locally when called directly. Called by nocoverage tox task.
1 parent c9907ac commit e72a98f

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

pounders/py/create_trsp_solver.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create_trsp_solver(spsolver):
3030
3131
.. code:: python
3232
33-
Xsp, mdec, flag = solve_trsp(H, G, Low, Upp)
33+
Xsp, mdec, found_solution = solve_trsp(H, G, Low, Upp)
3434
3535
where
3636
@@ -43,9 +43,8 @@ def create_trsp_solver(spsolver):
4343
* ``Xsp`` is the subproblem solution,
4444
* ``mdec`` is the value of the subproblem objective function at
4545
the solution, and
46-
* ``flag`` communicates the termination condition of the solver with a
47-
negative value indicating failure and all other values indicating
48-
success.
46+
* ``found_solution`` is True if a solution was found that should be
47+
acceptable for POUNDERS's purposes; False, otherwise.
4948
"""
5049
if spsolver == TRSP_SOLVER_SIMPLE:
5150
# Since this solver is for testing/debugging only, we do not mention it
@@ -56,7 +55,7 @@ def create_trsp_solver(spsolver):
5655
def __bqmin_wrapper(H, G, Low, Upp):
5756
# Assume that solver error checks its arguments thoroughly.
5857
Xsp, mdec = bqmin(H, G, Low, Upp)
59-
return Xsp, mdec, 0
58+
return Xsp, mdec, True
6059

6160
return __bqmin_wrapper
6261

@@ -81,10 +80,11 @@ def __minq5_wrapper(H, G, Low, Upp):
8180
# better approximation to the solution. But, it might be useful for
8281
# developers/power users to be able to identify when the budget
8382
# limit is reached. Once we have improved logging, print debug
84-
# messages at high verbosity levels if minq_err == 99? Better to
85-
# let POUNDERS log?
83+
# messages at high verbosity levels if minq_err == 99? Since we are
84+
# returing a boolean, all logging would have to be done by this
85+
# wrapper layer.
8686
# assert minq_err != 99
87-
return Xsp, mdec, minq_err
87+
return Xsp, mdec, (minq_err >= 0)
8888

8989
return __minq5_wrapper
9090

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Unit test of create_trsp_solver()
3+
"""
4+
5+
import warnings
6+
import unittest
7+
8+
import ibcdfo
9+
10+
11+
class TestCreateTrspSolver(unittest.TestCase):
12+
def setUp(self):
13+
self.__solvers = {ibcdfo.pounders.constants.TRSP_SOLVER_SIMPLE, ibcdfo.pounders.TRSP_SOLVER_MINQ5}
14+
self.__emit_warnings = {(ibcdfo.pounders.constants.TRSP_SOLVER_SIMPLE, ("testing", "debugging"))}
15+
16+
def testErrors(self):
17+
for bad in [0, ibcdfo.pounders.TRSP_SOLVER_MINQ8]:
18+
with self.assertRaises(ValueError):
19+
ibcdfo.pounders.create_trsp_solver(bad)
20+
21+
def testWarnings(self):
22+
for idx, words in self.__emit_warnings:
23+
with warnings.catch_warnings(record=True) as w:
24+
warnings.simplefilter("always")
25+
ibcdfo.pounders.create_trsp_solver(idx)
26+
self.assertEqual(len(w), 1)
27+
for each in words:
28+
self.assertTrue(each in str(w[0].message))
29+
30+
def testSuccessful(self):
31+
for idx in self.__solvers:
32+
# Expected emission of warnings tested in testWarnings. Ignore only those.
33+
warnings.simplefilter("default")
34+
for warn_idx, _ in self.__emit_warnings:
35+
if warn_idx == idx:
36+
warnings.simplefilter("ignore")
37+
break
38+
39+
solve_trsp = ibcdfo.pounders.create_trsp_solver(idx)
40+
self.assertTrue(callable(solve_trsp))

0 commit comments

Comments
 (0)