|
| 1 | +######################################################################################## |
| 2 | +## |
| 3 | +## TESTS FOR |
| 4 | +## 'amplifier.py' |
| 5 | +## |
| 6 | +######################################################################################## |
| 7 | + |
| 8 | +# IMPORTS ============================================================================== |
| 9 | + |
| 10 | +import unittest |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +from pathsim_rf import RFAmplifier |
| 14 | + |
| 15 | + |
| 16 | +# TESTS ================================================================================ |
| 17 | + |
| 18 | +class TestRFAmplifier(unittest.TestCase): |
| 19 | + """Test the RFAmplifier block.""" |
| 20 | + |
| 21 | + def test_init_default(self): |
| 22 | + """Test default initialization.""" |
| 23 | + amp = RFAmplifier() |
| 24 | + self.assertEqual(amp.gain, 10.0) |
| 25 | + self.assertIsNone(amp.saturation) |
| 26 | + |
| 27 | + def test_init_custom(self): |
| 28 | + """Test custom initialization.""" |
| 29 | + amp = RFAmplifier(gain=20.0, saturation=5.0) |
| 30 | + self.assertEqual(amp.gain, 20.0) |
| 31 | + self.assertEqual(amp.saturation, 5.0) |
| 32 | + |
| 33 | + def test_init_validation(self): |
| 34 | + """Test input validation.""" |
| 35 | + with self.assertRaises(ValueError): |
| 36 | + RFAmplifier(gain=0) |
| 37 | + with self.assertRaises(ValueError): |
| 38 | + RFAmplifier(gain=-1) |
| 39 | + with self.assertRaises(ValueError): |
| 40 | + RFAmplifier(saturation=0) |
| 41 | + with self.assertRaises(ValueError): |
| 42 | + RFAmplifier(saturation=-1) |
| 43 | + |
| 44 | + def test_port_labels(self): |
| 45 | + """Test port label definitions.""" |
| 46 | + self.assertEqual(RFAmplifier.input_port_labels["rf_in"], 0) |
| 47 | + self.assertEqual(RFAmplifier.output_port_labels["rf_out"], 0) |
| 48 | + |
| 49 | + def test_linear_gain(self): |
| 50 | + """Linear mode: output = gain * input.""" |
| 51 | + amp = RFAmplifier(gain=5.0) |
| 52 | + amp.inputs[0] = 2.0 |
| 53 | + amp.update(None) |
| 54 | + self.assertAlmostEqual(amp.outputs[0], 10.0) |
| 55 | + |
| 56 | + def test_linear_negative_input(self): |
| 57 | + """Linear mode works with negative inputs.""" |
| 58 | + amp = RFAmplifier(gain=3.0) |
| 59 | + amp.inputs[0] = -4.0 |
| 60 | + amp.update(None) |
| 61 | + self.assertAlmostEqual(amp.outputs[0], -12.0) |
| 62 | + |
| 63 | + def test_linear_zero_input(self): |
| 64 | + """Zero input produces zero output.""" |
| 65 | + amp = RFAmplifier(gain=10.0) |
| 66 | + amp.inputs[0] = 0.0 |
| 67 | + amp.update(None) |
| 68 | + self.assertAlmostEqual(amp.outputs[0], 0.0) |
| 69 | + |
| 70 | + def test_saturation_small_signal(self): |
| 71 | + """With saturation, small signals are approximately linear.""" |
| 72 | + amp = RFAmplifier(gain=10.0, saturation=100.0) |
| 73 | + amp.inputs[0] = 0.01 # small signal: gain*input/sat = 0.001 |
| 74 | + amp.update(None) |
| 75 | + # tanh(x) ≈ x for small x, so output ≈ gain * input |
| 76 | + self.assertAlmostEqual(amp.outputs[0], 10.0 * 0.01, places=3) |
| 77 | + |
| 78 | + def test_saturation_large_signal(self): |
| 79 | + """With saturation, large signals are clipped to saturation level.""" |
| 80 | + amp = RFAmplifier(gain=100.0, saturation=5.0) |
| 81 | + amp.inputs[0] = 1000.0 # heavily driven |
| 82 | + amp.update(None) |
| 83 | + # tanh(large) ≈ 1, so output ≈ saturation |
| 84 | + self.assertAlmostEqual(amp.outputs[0], 5.0, places=3) |
| 85 | + |
| 86 | + def test_saturation_symmetry(self): |
| 87 | + """Saturation is symmetric for positive and negative inputs.""" |
| 88 | + amp = RFAmplifier(gain=100.0, saturation=5.0) |
| 89 | + |
| 90 | + amp.inputs[0] = 1000.0 |
| 91 | + amp.update(None) |
| 92 | + pos = amp.outputs[0] |
| 93 | + |
| 94 | + amp.inputs[0] = -1000.0 |
| 95 | + amp.update(None) |
| 96 | + neg = amp.outputs[0] |
| 97 | + |
| 98 | + self.assertAlmostEqual(pos, -neg) |
| 99 | + |
| 100 | + def test_saturation_zero(self): |
| 101 | + """Zero input gives zero output even with saturation.""" |
| 102 | + amp = RFAmplifier(gain=10.0, saturation=5.0) |
| 103 | + amp.inputs[0] = 0.0 |
| 104 | + amp.update(None) |
| 105 | + self.assertAlmostEqual(amp.outputs[0], 0.0) |
| 106 | + |
| 107 | + |
| 108 | +# RUN TESTS LOCALLY ==================================================================== |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + unittest.main(verbosity=2) |
0 commit comments