To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit ef271c97 authored by Jonas Fankhauser's avatar Jonas Fankhauser
Browse files

Merge branch 'patch/assertAlmostEqual' into 'developer'

Patch/assert almost equal

See merge request !55
parents c9603556 f0829bbf
Pipeline #88626 failed with stages
in 1 minute and 53 seconds
......@@ -38,18 +38,56 @@ class BaseTest(unittest.TestCase):
self.randomly_change_parameters()
self.assign_parameters()
@staticmethod
def is_the_same(val1: float, val2: float,
threshold: float = 1e-12,
print_results: bool = False) -> bool:
def assertAlmostEqual(self,
val1: Union[float, complex],
val2: Union[float, complex],
threshold: float = 1e-12,
print_results: bool = False):
""" Asserting the relative difference of two values when num. rounding
errors can occur
If the values are complex, then the real and imaginary part will be
tested separately. Also, if one of the test fails, automatically, a
nice formatted output is generated as error message.
:param val1: first value to be compared
:type val1: Union[float, complex]
:param val2: second value to be compared
:type val2: Union[float, complex]
:param threshold: threshold of relative difference; default is 1e-12
:type threshold: float
:param print_results: if True the two values are printed to the console
for easier debugging
:type print_results: bool
"""
if print_results:
print(f'\nval1: {val1:+.8e}')
print(f'val2: {val2:+.8e}')
msg = f'\nval1 : {val1:+.8e}\n'
msg += f'val2 : {val2:+.8e}\n'
if not val1 == 0 and not val2 == 0:
msg += f'rel diff.: {abs(val1-val2)/val1:+.8e}'
print(msg)
self._assertAlmostEqual(val1.real, val2.real, threshold)
if isinstance(val1, complex):
self._assertAlmostEqual(val1.imag, val2.imag, threshold)
def _assertAlmostEqual(self, val1: float, val2: float, threshold: float):
if val1 == 0 or val2 == 0:
return val1 == val2
msg = '\nOne of the values is Zero; the other not\n'
msg += f'val1 : {val1:+.8e}\n'
msg += f'val2 : {val2:+.8e}\n'
self.assertEqual(first=val1, second=val2, msg=msg)
else:
diff = abs(val1 - val2)
return diff / val1 < threshold
test = diff / val1 < threshold
msg = '\nThe rel. diff. is bigger than the threshold\n'
msg += f'val1 : {val1:+.8e}\n'
msg += f'val2 : {val2:+.8e}\n'
msg += f'rel diff.: {diff/val1:+.8e}\n'
msg += f'threshold: {threshold:+.8e}'
self.assertTrue(expr=test, msg=msg)
class BaseTestComposite(BaseTest):
......
import unittest
from tests.basetest_composite import BaseTest
from tests.basetest import BaseTest
from gorkov import pi, sin
from gorkov import Gorkov1962
......@@ -96,41 +96,35 @@ class TestGorkovARF(BaseTest):
return out
def test_f2(self) -> None:
self.assertTrue(self.is_the_same(
self.compute_f2(), self.cls.f2))
self.assertAlmostEqual(self.compute_f2(), self.cls.f2)
def test_Phi(self) -> None:
self.assertTrue(self.is_the_same(self.compute_Phi(),
self.cls.Phi))
self.assertAlmostEqual(self.compute_Phi(), self.cls.Phi)
def test_f1(self) -> None:
self.assertTrue(self.is_the_same(self.compute_f1(),
self.cls.f1))
self.assertAlmostEqual(self.compute_f1(), self.cls.f1)
def test_traveling_arf(self) -> None:
self.cls.wave_type = 'traveling'
self.assertTrue(self.is_the_same(
self.compute_traveling_arf(),
self.cls.acoustic_radiation_force()))
self.assertAlmostEqual(self.compute_traveling_arf(),
self.cls.acoustic_radiation_force())
for _ in self.range_runs:
self.cls.wave_type = 'travelling'
self.change_and_assign()
self.assertTrue(self.is_the_same(
self.assertAlmostEqual(
self.compute_traveling_arf(),
self.cls.acoustic_radiation_force()))
self.cls.acoustic_radiation_force())
def test_standing_arf(self) -> None:
self.cls.wave_type = 'standing'
self.assertTrue(self.is_the_same(
self.compute_standing_arf(),
self.cls.acoustic_radiation_force()))
self.assertAlmostEqual(self.compute_standing_arf(),
self.cls.acoustic_radiation_force())
for _ in self.range_runs:
self.change_and_assign()
self.assertTrue(self.is_the_same(
self.compute_standing_arf(),
self.cls.acoustic_radiation_force()))
self.assertAlmostEqual(self.compute_standing_arf(),
self.cls.acoustic_radiation_force())
if __name__ == '__main__':
......
import unittest
from tests.basetest_composite import BaseTest
from tests.basetest import BaseTest
from gorkov import pi, sin
from gorkov import King1934
......@@ -77,24 +77,20 @@ class TestKingARF(BaseTest):
return out
def test_f2(self) -> None:
self.assertTrue(self.is_the_same(
self.compute_f2(), self.cls.f2))
self.assertAlmostEqual(self.compute_f2(), self.cls.f2)
def test_Phi(self) -> None:
self.assertTrue(self.is_the_same(self.compute_Phi(),
self.cls.Phi))
self.assertAlmostEqual(self.compute_Phi(), self.cls.Phi)
def test_arf(self) -> None:
self.assertTrue(self.is_the_same(
self.compute_arf(),
self.cls.acoustic_radiation_force()))
self.assertAlmostEqual(
self.compute_arf(), self.cls.acoustic_radiation_force())
n = 0
while (n < 10):
self.change_and_assign()
self.assertTrue(self.is_the_same(
self.compute_arf(),
self.cls.acoustic_radiation_force()))
self.assertAlmostEqual(
self.compute_arf(), self.cls.acoustic_radiation_force())
n += 1
self.cls.small_particle_limit = False
......
......@@ -2,7 +2,7 @@ import random
import unittest
from gorkov import pi, exp, sqrt
from gorkov import BackgroundField, ViscousFluid
from tests.basetest_composite import BaseTestComposite
from tests.basetest import BaseTestComposite
class TestViscousBackgroundField(BaseTestComposite):
......
import unittest
from basetest import BaseTest
from gorkov.core.basecomposite import (
BaseFrequencyComposite,
BaseSphereFrequencyComposite
......@@ -11,7 +13,7 @@ from gorkov.core.frequency import Frequency
from gorkov import pi
class TestBaseFrequencyComposite(unittest.TestCase):
class TestBaseFrequencyComposite(BaseTest):
def test_types_for_frequency(self):
cls = BaseFrequencyComposite(10.1)
......@@ -28,7 +30,7 @@ class TestBaseFrequencyComposite(unittest.TestCase):
self.assertRaises(TypeError, BaseFrequencyComposite, 's')
class TestBaseSphereFrequencyComposite(unittest.TestCase):
class TestBaseSphereFrequencyComposite(BaseTest):
def test_types_for_sphere(self):
cls = BaseSphereFrequencyComposite(10.1, 10)
......
......@@ -2,7 +2,7 @@ import unittest
from gorkov import InviscidFluid, ViscousFluid, ViscoelasticFluid
from gorkov import sqrt, pi
from tests.basetest_composite import BaseTestComposite
from tests.basetest import BaseTestComposite
import numpy as np
......@@ -59,55 +59,47 @@ class TestInviscidFluid(BaseTestComposite):
# -------------------------------------------------------------------------
def test_omega(self) -> None:
self.assertTrue(self.is_the_same(self.cls.omega, self.compute_omega()))
self.assertTrue(self.is_the_same(self.composite_cls.omega,
self.compute_omega()))
self.assertAlmostEqual(self.cls.omega, self.compute_omega())
self.assertAlmostEqual(
self.composite_cls.omega, self.compute_omega())
# change frequency and test if modification are migrated
for _ in self.range_runs:
self.change_and_assign()
self.assertTrue(self.is_the_same(self.cls.omega,
self.compute_omega()))
self.assertTrue(self.is_the_same(self.composite_cls.omega,
self.compute_omega()))
self.assertAlmostEqual(self.cls.omega, self.compute_omega())
self.assertAlmostEqual(
self.composite_cls.omega, self.compute_omega())
def test_k_f(self) -> None:
self.assertTrue(self.is_the_same(self.cls.k_f, self.compute_k_f()))
self.assertTrue(self.is_the_same(self.composite_cls.k_f,
self.compute_k_f()))
self.assertAlmostEqual(self.cls.k_f, self.compute_k_f())
self.assertAlmostEqual(self.composite_cls.k_f, self.compute_k_f())
# change frequency and test if modification are migrated
for _ in self.range_runs:
self.change_and_assign()
self.assertTrue(self.is_the_same(self.cls.k_f, self.compute_k_f()))
self.assertTrue(self.is_the_same(self.composite_cls.k_f,
self.compute_k_f()))
self.assertAlmostEqual(self.cls.k_f, self.compute_k_f())
self.assertAlmostEqual(
self.composite_cls.k_f, self.compute_k_f())
def test_lambda_f(self) -> None:
self.assertTrue(
self.is_the_same(self.compute_lambda_f(), self.cls.lambda_f))
self.assertTrue(
self.is_the_same(self.compute_lambda_f(),
self.composite_cls.lambda_f))
self.assertAlmostEqual(self.compute_lambda_f(), self.cls.lambda_f)
self.assertAlmostEqual(
self.compute_lambda_f(), self.composite_cls.lambda_f)
for _ in self.range_runs:
self.change_and_assign()
self.assertTrue(
self.is_the_same(self.compute_lambda_f(), self.cls.lambda_f))
self.assertTrue(
self.is_the_same(self.compute_lambda_f(),
self.composite_cls.lambda_f))
self.assertAlmostEqual(
self.compute_lambda_f(), self.cls.lambda_f)
self.assertAlmostEqual(
self.compute_lambda_f(), self.composite_cls.lambda_f)
def test_kappa_f(self) -> None:
self.assertTrue(
self.is_the_same(self.compute_kappa_f(), self.cls.kappa_f))
self.assertTrue(
self.is_the_same(self.compute_kappa_f(),
self.composite_cls.kappa_f))
self.assertAlmostEqual(self.compute_kappa_f(), self.cls.kappa_f)
self.assertAlmostEqual(
self.compute_kappa_f(), self.composite_cls.kappa_f)
for _ in self.range_runs:
self.change_and_assign()
self.assertTrue(
self.is_the_same(self.compute_kappa_f(), self.cls.kappa_f))
self.assertTrue(
self.is_the_same(self.compute_kappa_f(),
self.composite_cls.kappa_f))
self.assertAlmostEqual(
self.compute_kappa_f(), self.cls.kappa_f)
self.assertAlmostEqual(
self.compute_kappa_f(), self.composite_cls.kappa_f)
class TestViscousFluid(TestInviscidFluid):
......
......@@ -15,8 +15,10 @@ from scipy.special import jv, yv, hankel1, lpmv
import numpy as np
from sympy.physics.quantum.cg import CG
from basetest import BaseTest
class TestFunctions(unittest.TestCase):
class TestFunctions(BaseTest):
def test_full_range(self):
res = full_range(1)
......@@ -188,17 +190,15 @@ class TestFunctions(unittest.TestCase):
def func(x):
return sin(x)**2
self.assertAlmostEqual(integrate(func, 0, 2 * pi), pi, places=10)
self.assertAlmostEqual(integrate(func, 0, 2 * pi, 1e-12), pi,
places=10)
self.assertAlmostEqual(integrate(func, 0, 2 * pi), pi)
self.assertAlmostEqual(integrate(func, 0, 2 * pi, 1e-12), pi)
def func(x):
return -2 * x + 1
self.assertAlmostEqual(integrate(func, 0, 2), -2, places=12)
self.assertAlmostEqual(integrate(func, -2, 1 / 2), 6.25, places=12)
self.assertAlmostEqual(integrate(func, -2, 1 / 2, 1e-12),
6.25, places=12)
self.assertAlmostEqual(integrate(func, 0, 2), -2)
self.assertAlmostEqual(integrate(func, -2, 1 / 2), 6.25)
self.assertAlmostEqual(integrate(func, -2, 1 / 2, 1e-12), 6.25)
def test_clebsch_gordan(self):
def random_value():
......@@ -217,7 +217,8 @@ class TestFunctions(unittest.TestCase):
result = cg(j1, m1, j2, m2, j, m)
self.assertAlmostEqual(result, CG(j1, m1, j2, m2, j, m).doit())
self.assertAlmostEqual(result,
float(CG(j1, m1, j2, m2, j, m).doit()))
if result != 0:
nbr_nonzero += 1
......@@ -254,7 +255,7 @@ class TestFunctions(unittest.TestCase):
self.assertAlmostEqual(
leg.first_cos_poly(theta, coefficients),
tmp(theta, coefficients), places=12)
tmp(theta, coefficients))
# first_legendre_cos_monomial
n = np.random.randint(0, 100)
......
import unittest
from tests.basetest_composite import BaseTestComposite
from tests.basetest import BaseTestComposite
from gorkov import RigidSolid, ElasticSolid
from gorkov.core.functions import pi, sqrt
class TestRigidSolid(BaseTestComposite):
def setUp(self) -> None:
......@@ -172,30 +173,24 @@ class TestElasticSolid(BaseTestComposite):
self.assertEqual(self.composite_cls.c_l, self.compute_c_l())
def test_kappa_s(self) -> None:
self.assertTrue(self.is_the_same(self.compute_kappa_s(),
self.cls.kappa_s))
self.assertTrue(self.is_the_same(self.compute_kappa_s(),
self.composite_cls.kappa_s))
self.assertAlmostEqual(self.compute_kappa_s(), self.cls.kappa_s)
self.assertAlmostEqual(self.compute_kappa_s(),
self.composite_cls.kappa_s)
# change frequency and test if modification are migrated
for _ in self.range_runs:
self.change_and_assign()
self.assertTrue(self.is_the_same(self.compute_kappa_s(),
self.cls.kappa_s))
self.assertTrue(self.is_the_same(self.compute_kappa_s(),
self.composite_cls.kappa_s))
self.assertAlmostEqual(self.compute_kappa_s(), self.cls.kappa_s)
self.assertAlmostEqual(
self.compute_kappa_s(), self.composite_cls.kappa_s)
def test_B_s(self) -> None:
self.assertTrue(self.is_the_same(self.compute_B_s(),
self.cls.B_s))
self.assertTrue(self.is_the_same(self.compute_B_s(),
self.composite_cls.B_s))
self.assertAlmostEqual(self.compute_B_s(), self.cls.B_s)
self.assertAlmostEqual(self.compute_B_s(), self.composite_cls.B_s)
# change frequency and test if modification are migrated
for _ in self.range_runs:
self.change_and_assign()
self.assertTrue(self.is_the_same(self.compute_B_s(),
self.cls.B_s))
self.assertTrue(self.is_the_same(self.compute_B_s(),
self.composite_cls.B_s))
self.assertAlmostEqual(self.compute_B_s(), self.cls.B_s)
self.assertAlmostEqual(self.compute_B_s(), self.composite_cls.B_s)
if __name__ == '__main__':
......
import unittest
from basetest import BaseTest
from gorkov import (
PassiveVariable,
ActiveVariable
)
class TestVariables(unittest.TestCase):
class TestVariables(BaseTest):
def test_hierachy(self) -> None:
P1 = PassiveVariable(-3.0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment