kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
More unittests
rodzic
4e020472d8
commit
188cf7b5f9
|
@ -21,16 +21,16 @@ from typing import List, NamedTuple
|
|||
from NanoVNASaver.SITools import Value, Format
|
||||
|
||||
|
||||
def norm_to_impedance(z: complex, ref_impedance: float = 50) -> complex:
|
||||
"""Calculate impedance from normalized z"""
|
||||
return z * ref_impedance
|
||||
|
||||
|
||||
def impedance_to_norm(z: complex, ref_impedance: float = 50) -> complex:
|
||||
"""Calculate normalized z from impedance"""
|
||||
return z / ref_impedance
|
||||
|
||||
|
||||
def norm_to_impedance(z: complex, ref_impedance: float = 50) -> complex:
|
||||
"""Calculate impedance from normalized z"""
|
||||
return z * ref_impedance
|
||||
|
||||
|
||||
def reflection_coefficient(z: complex, ref_impedance: float = 50) -> complex:
|
||||
"""Calculate reflection coefficient for z"""
|
||||
return (z - ref_impedance) / (z + ref_impedance)
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
# NanoVNASaver
|
||||
# A python program to view and export Touchstone data from a NanoVNA
|
||||
# Copyright (C) 2019. Rune B. Broberg
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
import unittest
|
||||
|
||||
# Import targets to be tested
|
||||
from NanoVNASaver.RFTools import \
|
||||
norm_to_impedance, impedance_to_norm, \
|
||||
reflection_coefficient, gamma_to_impedance
|
||||
|
||||
|
||||
class TestRFTools(unittest.TestCase):
|
||||
|
||||
def test_norm_to_impedance(self):
|
||||
self.assertEqual(norm_to_impedance(50, 0), 0)
|
||||
self.assertEqual(norm_to_impedance(1), 50)
|
||||
self.assertEqual(norm_to_impedance(-1), -50)
|
||||
self.assertEqual(norm_to_impedance(1.5), 75)
|
||||
self.assertEqual(norm_to_impedance(1, 75), 75)
|
||||
self.assertEqual(norm_to_impedance(complex(0, 1)), complex(0, 50))
|
||||
self.assertEqual(norm_to_impedance(complex(1, 1)), complex(50, 50))
|
||||
self.assertEqual(norm_to_impedance(complex(0, -1)), complex(0, -50))
|
||||
self.assertAlmostEqual(
|
||||
norm_to_impedance(complex(3.33333, 3.33333), 30),
|
||||
complex(100, 100), 3)
|
||||
|
||||
def test_impedance_to_norm(self):
|
||||
self.assertRaises(ZeroDivisionError, impedance_to_norm, 0, 0)
|
||||
self.assertEqual(impedance_to_norm(0), 0)
|
||||
self.assertEqual(impedance_to_norm(50), 1)
|
||||
self.assertEqual(impedance_to_norm(-50), -1)
|
||||
self.assertEqual(impedance_to_norm(75), 1.5)
|
||||
self.assertEqual(impedance_to_norm(75, 75), 1)
|
||||
self.assertEqual(impedance_to_norm(complex(0, 50)), complex(0, 1))
|
||||
self.assertEqual(impedance_to_norm(complex(50, 50)), complex(1, 1))
|
||||
self.assertEqual(impedance_to_norm(complex(0, -50)), complex(0, -1))
|
||||
|
||||
self.assertAlmostEqual(impedance_to_norm(
|
||||
complex(100, 100), 30), (complex(3.333, 3.333)), 3)
|
||||
|
||||
def test_reflection_coefficient(self):
|
||||
self.assertRaises(ZeroDivisionError, reflection_coefficient, -50)
|
||||
self.assertEqual(reflection_coefficient(50), 0)
|
||||
self.assertEqual(reflection_coefficient(75), 0.2)
|
||||
# TODO: insert more test values here
|
||||
|
||||
def test_gamma_to_impedance(self):
|
||||
self.assertEqual(gamma_to_impedance(0), 50)
|
||||
self.assertAlmostEqual(gamma_to_impedance(0.2), 75)
|
||||
# TODO: insert more test values here
|
|
@ -29,8 +29,8 @@ F_ASSERT_OFFSET_3 = Format(min_offset=9)
|
|||
F_ASSERT_CLAMP = Format(parse_clamp_min=10, parse_clamp_max=9)
|
||||
|
||||
F_DIGITS_3 = Format(max_nr_digits=3)
|
||||
F_DIGITS_4 = Format(max_nr_digits=3)
|
||||
F_DIGITS_31 = Format(max_nr_digits=30)
|
||||
F_DIGITS_4 = Format(max_nr_digits=4)
|
||||
F_DIGITS_31 = Format(max_nr_digits=31)
|
||||
|
||||
F_WITH_SPACE = Format(space_str=" ")
|
||||
F_WITH_UNDERSCORE = Format(space_str="_")
|
||||
|
@ -38,7 +38,7 @@ F_WITH_UNDERSCORE = Format(space_str="_")
|
|||
|
||||
class TestTSIToolsValue(unittest.TestCase):
|
||||
|
||||
def test_format_asssertions(self):
|
||||
def test_format_assertions(self):
|
||||
self.assertRaises(AssertionError, Value, fmt=F_ASSERT_DIGITS_1)
|
||||
self.assertRaises(AssertionError, Value, fmt=F_ASSERT_DIGITS_2)
|
||||
self.assertRaises(AssertionError, Value, fmt=F_ASSERT_OFFSET_1)
|
||||
|
@ -99,3 +99,18 @@ class TestTSIToolsValue(unittest.TestCase):
|
|||
self.assertEqual(str(v.parse("1e-2")), "10m")
|
||||
self.assertEqual(str(v.parse("1e-3")), "1m")
|
||||
|
||||
def test_format_digits_4(self):
|
||||
v = Value(fmt=F_DIGITS_4)
|
||||
self.assertEqual(str(v.parse("1")), "1.000")
|
||||
self.assertEqual(str(v.parse("10")), "10.00")
|
||||
self.assertEqual(str(v.parse("100")), "100.0")
|
||||
self.assertEqual(str(v.parse("1e3")), "1.000k")
|
||||
self.assertEqual(str(v.parse("1e4")), "10.00k")
|
||||
self.assertEqual(str(v.parse("1e5")), "100.0k")
|
||||
self.assertEqual(str(v.parse("1e-1")), "100.0m")
|
||||
self.assertEqual(str(v.parse("1e-2")), "10.00m")
|
||||
self.assertEqual(str(v.parse("1e-3")), "1.000m")
|
||||
|
||||
# TODO: test F_DIGITS_31
|
||||
# F_WITH_SPACE
|
||||
# F_WITH_UNDERSCORE
|
||||
|
|
Ładowanie…
Reference in New Issue