Skip to content

Commit

Permalink
Merge pull request #133 from bnavigator/fix-warnings
Browse files Browse the repository at this point in the history
Fix warnings capture
  • Loading branch information
roryyorke authored May 30, 2020
2 parents 10d667c + af98e9f commit ac0944b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 53 deletions.
5 changes: 3 additions & 2 deletions slycot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ configure_file(version.py.in version.py @ONLY)

set(PYSOURCE

__init__.py analysis.py examples.py math.py synthesis.py
transform.py ${CMAKE_CURRENT_BINARY_DIR}/version.py)
__init__.py examples.py exceptions.py
analysis.py math.py synthesis.py transform.py
${CMAKE_CURRENT_BINARY_DIR}/version.py)

set(SLYCOT_MODULE "_wrapper")
find_package(PythonExtensions REQUIRED)
Expand Down
37 changes: 34 additions & 3 deletions slycot/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,27 @@
examplefunctions = [fun for (fname, fun) in getmembers(examples)
if isfunction(fun) and "_example" in fname]

# Exempt certain functions to produce warnings with attributes (iwarn, info)
#
# Failed to compute beta(A) within the specified tolerance
examples.ab13fd_example.allow_iwarninfo = [(None, 1)]


def check_warn(recwarn, examplefun):
"""Returns True if a warning occurs that is not exempt"""
for w in recwarn:
try:
if (w.message.iwarn, w.message.info) in examplefun.allow_iwarninfo:
continue
except AttributeError:
pass
return True


@pytest.mark.parametrize('examplefun', examplefunctions)
#ignore numpy ABI change warnings https://github.com/numpy/numpy/pull/432
@pytest.mark.filterwarnings("ignore:numpy.dtype size changed")
@pytest.mark.filterwarnings("ignore:numpy.ufunc size changed")
def test_example(examplefun, capsys, recwarn):
"""
Test the examples.
Expand All @@ -24,6 +43,18 @@ def test_example(examplefun, capsys, recwarn):
"""
examplefun()
captured = capsys.readouterr()
assert len(captured.out) > 0
assert not captured.err
assert not recwarn

# fail for first in order
failconditions = [
((not len(captured.out) > 0), "Example {} did not print any results\n"),
(captured.err, "Example {} wrote to stderr\n"),
(check_warn(recwarn, examplefun), "Example {} produced a warning.\n")]
for failed, msgfmt in failconditions:
if failed:
pytest.fail(msgfmt.format(examplefun.__name__) +
"Captured output:\n{}\n"
"Captured stderr:\n{}\n"
"Captured warnings:\n{}\n"
"".format(captured.out,
captured.err,
[w.message for w in recwarn]))
87 changes: 39 additions & 48 deletions slycot/tests/test_mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,44 @@
# test_mc.py - test suite for polynomial and rational function manipulation
# bnavigator <[email protected]>, Aug 2019

import unittest
import warnings
import pytest
import re

from slycot import mc01td


class test_mc(unittest.TestCase):

def test_mc01td(self):
""" test_mc01td: doc example
data from http://slicot.org/objects/software/shared/doc/MC01TD.html
"""
(dp, stable, nz) = mc01td('C', 4, [2, 0, 1, -1, 1])
self.assertEqual(dp, 4)
self.assertEqual(stable, 0)
self.assertEqual(nz, 2)

def test_mc01td_D(self):
""" test_mc01td_D: test discrete option """
(dp, stable, nz) = mc01td('D', 3, [1, 2, 3, 4])
self.assertEqual(dp, 3)
self.assertEqual(stable, 1)
self.assertEqual(nz, 0)
(dp, stable, nz) = mc01td('D', 3, [4, 3, 2, 1])
self.assertEqual(dp, 3)
self.assertEqual(stable, 0)
self.assertEqual(nz, 3)

def test_mc01td_warnings(self):
""" test_mc01td_warnings: Test warnings """
T = [([0, 0], "\n"
"Entry ``P(x)`` is the zero polynomial."),
([0, 1], "\n"
"The polynomial ``P(x)`` is most probably unstable,\n"
"although it may be stable with one or more zeros\n"
"very close to the imaginary axis.\n"
"The number of unstable zeros (NZ) is not determined."),
([1, 0], "\n"
"The degree of the polynomial ``P(x)`` has been\n"
"reduced to ``(DB - 1)`` because\n"
"``P(DB+1-j) = 0.0`` on entry\n"
"for ``j = 0, 1,..., k-1`` and ``P(DB+1-k) <> 0.0``.")]
for P, m in T:
with warnings.catch_warnings(record=True) as w:
(dp, stable, nz) = mc01td('C', len(P)-1, P)
self.assertEqual(str(w[0].message), m)


if __name__ == "__main__":
unittest.main()
from slycot.exceptions import SlycotResultWarning


def test_mc01td():
""" test_mc01td: doc example
data from http://slicot.org/objects/software/shared/doc/MC01TD.html
"""
(dp, stable, nz) = mc01td('C', 4, [2, 0, 1, -1, 1])
assert dp == 4
assert stable == 0
assert nz == 2

def test_mc01td_D():
""" test_mc01td_D: test discrete option """
(dp, stable, nz) = mc01td('D', 3, [1, 2, 3, 4])
assert dp == 3
assert stable == 1
assert nz == 0
(dp, stable, nz) = mc01td('D', 3, [4, 3, 2, 1])
assert dp == 3
assert stable == 0
assert nz == 3

def test_mc01td_warnings():
""" test_mc01td_warnings: Test warnings """
T = [([0, 0], "Entry ``P(x)`` is the zero polynomial."),
([0, 1], "The polynomial ``P(x)`` is most probably unstable,\n"
"although it may be stable with one or more zeros\n"
"very close to the imaginary axis.\n"
"The number of unstable zeros (NZ) is not determined."),
([1, 0], "The degree of the polynomial ``P(x)`` has been\n"
"reduced to ``(DB - 1)`` because\n"
"``P(DB+1-j) = 0.0`` on entry\n"
"for ``j = 0, 1,..., k-1`` and ``P(DB+1-k) <> 0.0``.")]
for P, m in T:
with pytest.warns(SlycotResultWarning, match=re.escape(m)):
(dp, stable, nz) = mc01td('C', len(P)-1, P)

0 comments on commit ac0944b

Please sign in to comment.