Browse Source

Added tests

Snow 8 years ago
parent
commit
c9bf3ba1e1
2 changed files with 52 additions and 0 deletions
  1. 9 0
      .travis.yml
  2. 43 0
      tests/test_sample.py

+ 9 - 0
.travis.yml

@@ -0,0 +1,9 @@
+language: python
+python:
+  - "3.4"
+  - "3.5"
+  - "3.6"
+# command to install dependencies
+install: "pip install -r requirements.txt"
+# command to run tests
+script: pytest

+ 43 - 0
tests/test_sample.py

@@ -0,0 +1,43 @@
+# content of test_module.py
+import pytest
+import math
+import matplotlib
+matplotlib.use('Agg')           # supress figures in the following modules
+from hdpg1d.coefficients import coefficients as coeff
+from hdpg1d.adaptation import hdpg1d
+
+
+class TestClass(object):
+    def test_zeroDivision(self, monkeypatch):
+        coeffTest = coeff(*([0] * 7))
+        assert coeffTest.DIFFUSION != 0
+
+    @pytest.mark.parametrize("coeffInput, expected", [
+        ([1e-4, 0, 1, 2, 2, 1, 1], 1e-2),  # diffusion reaction
+        ([0, 1, 0, 2, 2, 1, 1], 0),        # convection
+        # ([1, 1, 0, 2, 2, 1, 1], 1)         # diffusion convection
+    ])
+    def test_solveAdaptive(self, coeffInput, expected):
+        coeffTest = coeff(*coeffInput)
+        hdpgTest = hdpg1d(coeffTest)
+        hdpgTest.adaptive()
+        # get the target function value
+        # and compare to the expected value
+        soln = hdpgTest.trueErrorList[1][-1]
+        assert math.isclose(soln, expected, rel_tol=1e-5, abs_tol=1e-10)
+
+    @pytest.mark.parametrize("coeffInput, expected", [
+        ([1e-4, 0, 1, 2, 2, 1, 1], 1e-2),  # diffusion reaction
+        ([0, 1, 0, 2, 2, 1, 1], 0),        # convection
+        # ([1, 1, 0, 2, 2, 1, 1], 1)         # diffusion convection
+    ])
+    def test_solvePrimal(self, coeffInput, expected):
+        # test the primal solver with a refined mesh and poly order
+        coeffInput[3] = 5
+        coeffInput[4] = 200
+        coeffTest = coeff(*coeffInput)
+        hdpgTest = hdpg1d(coeffTest)
+        hdpgTest.solvePrimal()
+        soln = hdpgTest.primalSoln[hdpgTest.numEle *
+                                   hdpgTest.numBasisFuncs - 1]
+        assert math.isclose(soln, expected, rel_tol=1e-5, abs_tol=1e-10)