From 4c60835d3d4f182d7aeda0a6af4ea0936f4539ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Sierro?= Date: Thu, 21 Oct 2021 09:12:53 +0200 Subject: [PATCH] starting to write tests --- src/scgenerator/legacy.py | 11 +++-- src/scgenerator/math.py | 1 + testing/test_cache.py | 0 testing/test_env.py | 0 testing/test_evaluator.py | 0 testing/test_legacy.py | 0 testing/test_logger.py | 0 testing/test_math.py | 100 ++++++++++++++++++++++++++++++++++++++ testing/test_operators.py | 0 testing/test_parameter.py | 0 testing/test_pbar.py | 0 testing/test_plotting.py | 0 testing/test_resonance.py | 1 - testing/test_spectra.py | 0 testing/test_utils.py | 0 15 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 testing/test_cache.py create mode 100644 testing/test_env.py create mode 100644 testing/test_evaluator.py create mode 100644 testing/test_legacy.py create mode 100644 testing/test_logger.py create mode 100644 testing/test_math.py create mode 100644 testing/test_operators.py create mode 100644 testing/test_parameter.py create mode 100644 testing/test_pbar.py create mode 100644 testing/test_plotting.py create mode 100644 testing/test_spectra.py create mode 100644 testing/test_utils.py diff --git a/src/scgenerator/legacy.py b/src/scgenerator/legacy.py index ac02a7a..c812edd 100644 --- a/src/scgenerator/legacy.py +++ b/src/scgenerator/legacy.py @@ -1,6 +1,6 @@ import os import sys -from collections import MutableMapping +from collections.abc import MutableMapping from pathlib import Path from typing import Any, Set @@ -93,13 +93,18 @@ def translate_parameters(d: dict[str, Any]) -> dict[str, Any]: Parameters ---------- d : dict[str, Any] - [description] + any parameter dictionary WITHOUT "variable" part Returns ------- dict[str, Any] - [description] + translated parameters """ + if {"variable", "Fiber"} & d.keys(): + raise ValueError( + "The dict to translate should be a single parameter set " + "(no 'variable' nor 'Fiber' entry)" + ) old_names = dict( interp_degree="interpolation_degree", beta="beta2_coefficients", diff --git a/src/scgenerator/math.py b/src/scgenerator/math.py index 0037f2a..68c0968 100644 --- a/src/scgenerator/math.py +++ b/src/scgenerator/math.py @@ -65,6 +65,7 @@ def _power_fact_single(x, n): def _power_fact_array(x, n): + """returns x^2/n!""" result = np.ones(len(x), dtype=np.float64) for k in range(n): result = result * x / (n - k) diff --git a/testing/test_cache.py b/testing/test_cache.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_env.py b/testing/test_env.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_evaluator.py b/testing/test_evaluator.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_legacy.py b/testing/test_legacy.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_logger.py b/testing/test_logger.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_math.py b/testing/test_math.py new file mode 100644 index 0000000..a1f8929 --- /dev/null +++ b/testing/test_math.py @@ -0,0 +1,100 @@ +import numpy as np +import pytest +import scgenerator.math as m +from math import factorial + + +def test__power_fact_array(): + x = np.random.rand(5) + for i in range(5): + assert m._power_fact_array(x, i) == pytest.approx(x ** i / factorial(i)) + + +def test__power_fact_single(): + pass + + +def test_abs2(): + x = np.random.rand(5) + assert m.abs2(5) == 25 + assert m.abs2(2 - 2j) == 8 + assert all(m.abs2(x) == abs(x) ** 2) + + +def test_all_zeros(): + x = np.geomspace(0.1, 1, 100) + y = np.sin(1 / x) + target = [1 / (3 * np.pi), 1 / (2 * np.pi), 1 / np.pi] + assert m.all_zeros(x, y) == pytest.approx(target, abs=1e-4) + + x = np.array([0, 1]) + y = np.array([-1, 1]) + assert all(m.all_zeros(x, y) == np.array([0.5])) + + x = np.array([0, 1]) + y = np.array([1, 1]) + assert len(m.all_zeros(x, y)) == 0 + + +def test_argclosest(): + pass + + +def test_build_sim_grid(): + pass + + +def test_indft(): + pass + + +def test_indft_matrix(): + pass + + +def test_jn_zeros(): + pass + + +def test_length(): + pass + + +def test_ndft(): + pass + + +def test_ndft_matrix(): + pass + + +def test_np_cache(): + pass + + +def test_power_fact(): + pass + + +def test_sigmoid(): + pass + + +def test_span(): + pass + + +def test_tspace(): + pass + + +def test_u_nm(): + pass + + +def test_update_frequency_domain(): + pass + + +def test_wspace(): + pass diff --git a/testing/test_operators.py b/testing/test_operators.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_parameter.py b/testing/test_parameter.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_pbar.py b/testing/test_pbar.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_plotting.py b/testing/test_plotting.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_resonance.py b/testing/test_resonance.py index 133a193..065b541 100644 --- a/testing/test_resonance.py +++ b/testing/test_resonance.py @@ -1,7 +1,6 @@ import numpy as np import scgenerator as sc import matplotlib.pyplot as plt -from pathlib import Path def main(): diff --git a/testing/test_spectra.py b/testing/test_spectra.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/test_utils.py b/testing/test_utils.py new file mode 100644 index 0000000..e69de29