From 255c2e7b7bf721296d4d4f22cb106ab3cf197e0a Mon Sep 17 00:00:00 2001 From: Thomas Albers Raviola Date: Tue, 28 May 2024 11:23:02 +0200 Subject: Add unit test with pytest --- .gitignore | 1 + README.md | 9 +++++++++ solvers.py | 2 +- test/__init__.py | 0 test/test_solvers.py | 32 +++++++++++++++++++++++++++++++ test_solvers.py | 54 ---------------------------------------------------- 6 files changed, 43 insertions(+), 55 deletions(-) create mode 100644 test/__init__.py create mode 100644 test/test_solvers.py delete mode 100644 test_solvers.py diff --git a/.gitignore b/.gitignore index 3883e04..a2eeca3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ .mypy_cache +.pytest_cache diff --git a/README.md b/README.md index 22bc85d..28ed767 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ # linsolver Toy implementation of gaussian elimination with partial pivoting in python + +# Testing + +In order to test the code, run the following command in the project main's +folder + +``` +$ python -m pytest +``` diff --git a/solvers.py b/solvers.py index b544f1b..040c6f4 100644 --- a/solvers.py +++ b/solvers.py @@ -114,4 +114,4 @@ def gaussian_eliminate( y = forward_substitution(ll, pp @ bb) # U @ x = y x = back_substitution(uu, y) - return x + return x[:, 0] diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_solvers.py b/test/test_solvers.py new file mode 100644 index 0000000..e5b73d8 --- /dev/null +++ b/test/test_solvers.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""Contains routines to test the solvers module""" + +import numpy as np +from numpy.typing import NDArray +import solvers + +def test_elimination_3() -> None: + """Tests elimination with 3 variables.""" + aa = np.array([[2.0, 4.0, 4.0], [5.0, 4.0, 2.0], [1.0, 2.0, -1.0]], dtype=np.float_) + bb = np.array([1.0, 4.0, 2.0], dtype=np.float_) + xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5], dtype=np.float_) + xx_gauss = solvers.gaussian_eliminate(aa, bb) + assert np.allclose(xx_expected, xx_gauss) + + +def test_pivot_3() -> None: + """Tests a solution with 3 variables, where pivot is necessary.""" + aa = np.array([[2.0, 4.0, 4.0], [1.0, 2.0, -1.0], [5.0, 4.0, 2.0]]) + bb = np.array([1.0, 2.0, 4.0]) + xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5]) + xx_gauss = solvers.gaussian_eliminate(aa, bb) + assert np.allclose(xx_expected, xx_gauss) + + +def test_lindep_3() -> None: + """Tests a linearly dependent system wit three variables.""" + aa = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) + bb = np.array([1.0, 2.0, 3.0]) + xx_expected = None + xx_gauss = solvers.gaussian_eliminate(aa, bb) + assert xx_expected == xx_gauss diff --git a/test_solvers.py b/test_solvers.py deleted file mode 100644 index ad37c8c..0000000 --- a/test_solvers.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python3 -"""Contains routines to test the solvers module""" - -import numpy as np -from numpy.typing import NDArray -import solvers - - -def main() -> None: - """Main testing function.""" - - print("\nTest elimination") - test_elimination_3() - print("\nTest pivot") - test_pivot_3() - print("\nTest linear dependency") - test_lindep_3() - - -def test_elimination_3() -> None: - """Tests elimination with 3 variables.""" - aa = np.array([[2.0, 4.0, 4.0], [5.0, 4.0, 2.0], [1.0, 2.0, -1.0]], dtype=np.float_) - bb = np.array([1.0, 4.0, 2.0], dtype=np.float_) - xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5], dtype=np.float_) - xx_gauss = solvers.gaussian_eliminate(aa, bb) - _check_result(xx_expected, xx_gauss) - - -def test_pivot_3() -> None: - """Tests a solution with 3 variables, where pivot is necessary.""" - aa = np.array([[2.0, 4.0, 4.0], [1.0, 2.0, -1.0], [5.0, 4.0, 2.0]]) - bb = np.array([1.0, 2.0, 4.0]) - xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5]) - xx_gauss = solvers.gaussian_eliminate(aa, bb) - _check_result(xx_expected, xx_gauss) - - -def test_lindep_3() -> None: - """Tests a linearly dependent system wit three variables.""" - aa = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) - bb = np.array([1.0, 2.0, 3.0]) - xx_expected = None - xx_gauss = solvers.gaussian_eliminate(aa, bb) - _check_result(xx_expected, xx_gauss) - - -def _check_result(expected: NDArray[np.float_] | None, obtained: NDArray[np.float_] | None) -> None: - """Checks results by printing expected and obtained one.""" - print("Expected:", expected) - print("Obtained:", obtained) - - -if __name__ == "__main__": - main() -- cgit v1.2.3