chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class ConnectionTest
    {
        public class VirtualPortInstrument : Instrument
        {
            public class VirtualPort : Port
            {   
                public VirtualPort(IResource device, int index) : base(device, "P" + index)
                {
                    Index = index;
                }
                public readonly int Index;
                public override bool Equals(object obj) => obj is VirtualPort vp && vp.Index == Index && vp.Device == Device;
                public override int GetHashCode() =>  (Device?.GetHashCode() ?? 0) + 13 * Index;
            }
            
            public int NPorts { get; set; } = 1;
            
            public IEnumerable<Port> Ports 
            {
                get
                {
                    for (int i = 0; i < NPorts; i++)
                        yield return new VirtualPort(this, i);
                }
            }    
        }
        
        /// <summary>
        /// This test show how virtual ports can be used (ports that represents the same without having reference equality).
        /// </summary>
        [Test]
        public void VirtualPortTest()
        {
            var instr = new VirtualPortInstrument()
            {
                NPorts = 2
            };
 
            var c1 = new RfConnection
            {
                Port1 = instr.Ports.First(),
                Port2 = instr.Ports.Last()
            };
 
            var p1 = instr.Ports.First();
            var p2 = instr.Ports.Last();
            
            Assert.AreNotEqual(p1,p2);
            Assert.AreEqual(c1.GetOtherPort(p1), p2);
            Assert.AreEqual(c1.GetOtherPort(p2), p1);
        }
 
        [Test]
        public void EmptyRfConnectionCableLoss()
        {
            var loss = new RfConnection().GetInterpolatedCableLoss(0.0);
            Assert.AreEqual(0.0, loss);
        }
 
        [Test]
        public void CableLossInterpolationTest()
        {
            // check linear (first order) interpolation:
            RfConnection con = new RfConnection();
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 100, Loss = 2 });
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 200, Loss = 4 });
            double loss = con.GetInterpolatedCableLoss(150);
            Assert.AreEqual(3, loss);
 
            con = new RfConnection();
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 100, Loss = 0 });
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 200, Loss = 4 });
            loss = con.GetInterpolatedCableLoss(175);
            Assert.AreEqual(3, loss);
 
            // check extrapolation (zero order):
            con = new RfConnection();
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 100, Loss = 2 });
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 200, Loss = 4 });
            loss = con.GetInterpolatedCableLoss(300);
            Assert.AreEqual(4, loss);
            loss = con.GetInterpolatedCableLoss(50);
            Assert.AreEqual(2, loss);
 
            // check exact match
            
            con = new RfConnection();
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 100, Loss = 2 });
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 200, Loss = 4 });
            con.CableLoss.Add(new RfConnection.CableLossPoint { Frequency = 300, Loss = 5 });
            loss = con.GetInterpolatedCableLoss(50);
            Assert.AreEqual(2, loss);
            loss = con.GetInterpolatedCableLoss(100);
            Assert.AreEqual(2, loss);
            loss = con.GetInterpolatedCableLoss(200);
            Assert.AreEqual(4, loss);
            loss = con.GetInterpolatedCableLoss(250);
            Assert.AreEqual(4.5, loss);
            loss = con.GetInterpolatedCableLoss(275);
            Assert.AreEqual(4.75, loss);
            loss = con.GetInterpolatedCableLoss(300);
            Assert.AreEqual(5, loss);
            loss = con.GetInterpolatedCableLoss(400);
            Assert.AreEqual(5, loss);
 
            // now generate a large set of cable losses
            var bigCon = new RfConnection();
            for (double x = 100; x < 500; x += 0.001)
            {
                bigCon.CableLoss.Add(new RfConnection.CableLossPoint()
                {
                    Frequency = x,
                    Loss = con.GetInterpolatedCableLoss(x)
                });
            }
            
            // shuffle to verify that we are robust to unsorted data.
            bigCon.CableLoss.Shuffle();
            
            // calculated cable losses should be more or less the same.
            con = bigCon;
            loss = con.GetInterpolatedCableLoss(50);
            Assert.AreEqual(2, loss);
            loss = con.GetInterpolatedCableLoss(100);
            Assert.AreEqual(2, loss);
            loss = con.GetInterpolatedCableLoss(200);
            Assert.IsTrue(Math.Abs(4 -  loss) < 0.0001);
            loss = con.GetInterpolatedCableLoss(250);
            Assert.IsTrue(Math.Abs(4.5 -  loss) < 0.0001);
            loss = con.GetInterpolatedCableLoss(275);
            Assert.IsTrue(Math.Abs(4.75 -  loss) < 0.0001);
            loss = con.GetInterpolatedCableLoss(300);
            Assert.IsTrue(Math.Abs(5 -  loss) < 0.0001);
            loss = con.GetInterpolatedCableLoss(400);
            Assert.IsTrue(Math.Abs(5 -  loss) < 0.0001);
        }
 
        
        public class RevolverSwitchPosition : ViaPoint
        {
            public string Alias { get; set; }
 
            public RevolverSwitchPosition(IInstrument device, string name)
            {
                this.Name = name;
                this.Device = device;
            }
            
        }
        
        public class ViaPointCollection : IReadOnlyList<RevolverSwitchPosition>
        {
            readonly RevolverSwitchPosition[] points;
            public ViaPointCollection(Instrument device, int count)
            {
                points = Enumerable.Range(1, count)
                    .Select(x => new RevolverSwitchPosition(device, $"Switch position {x}"))
                    .ToArray();
            }
 
            public IEnumerator<RevolverSwitchPosition> GetEnumerator() => points.OfType<RevolverSwitchPosition>().GetEnumerator();
 
            IEnumerator IEnumerable.GetEnumerator() => points.GetEnumerator();
 
            public int Count => points.Length;
 
            public RevolverSwitchPosition this[int index] => points[index];
        }
 
        public class TestRevolverSwitchInstrument : Instrument
        {
            public IReadOnlyList<RevolverSwitchPosition> SwitchPositions { get; set; }
            public RevolverSwitchPort A { get; set; } 
 
            public TestRevolverSwitchInstrument()
            {
                SwitchPositions = new ViaPointCollection(this, 6);
                A = new RevolverSwitchPort(this, "A");
            }
        }
        
        public class RevolverSwitchPort : OutputPort
        {
            public string Alias { get; set; }
            public RevolverSwitchPort(IResource device, string name) : base(device, name)
            {
                
            }
        }
    
        [Test]
        public void TestRevolverSwitchInstrumentSerialization()
        {
            var instrument = new TestRevolverSwitchInstrument();
            instrument.SwitchPositions[0].Alias = "test";
            instrument.A.Alias = "test2";
            var serializer = new TapSerializer();
            var xml = serializer.SerializeToString(instrument);
            var instrument2 = (TestRevolverSwitchInstrument) new TapSerializer().DeserializeFromString(xml);
            Assert.AreEqual(instrument.SwitchPositions[0].Alias, instrument2.SwitchPositions[0].Alias);
            Assert.AreEqual(instrument.A.Alias, instrument2.A.Alias);
        }
 
        public class VirtualViaInstrument : Instrument
        {
            public class VirtualVia : ViaPoint
            {
                readonly VirtualViaInstrument instrument;
                public VirtualVia(VirtualViaInstrument instrument, string name)
                {
                    this.instrument = instrument;
                    this.Device = instrument;
                    this.Name = name;
                }
 
                public override bool IsActive
                {
                    get => instrument.activeVia == Name;
                    set
                    {
                        if(value)
                            instrument.activeVia = Name;
                        else
                            if(IsActive)
                                instrument.activeVia = null;
                    }
                }
 
                public void Activate()
                {
                    
                }
            }
            public VirtualVia via1 { get; }
            public VirtualVia via2 { get; }
            string activeVia;
            public VirtualViaInstrument()
            {
                via1 = new VirtualVia(this, "A");
                via2 = new VirtualVia(this, "B");
            }
        }
 
        [Test]
        public void TestVirtualVias()
        {
            using var _ = Session.Create();
            var instr = new VirtualViaInstrument();
            instr.via1.IsActive = true;
            
            var instr2 = new VirtualPortInstrument{NPorts = 2};
            InstrumentSettings.Current.AddRange([instr, instr2]);
 
            var con1 = new RfConnection
            {
                Port1 = instr2.Ports.First(),
                Port2 = instr2.Ports.Last(),
                Via = new List<ViaPoint>(){instr.via1}
            };
            
            var con2 = new RfConnection
            {
                Port1 = instr2.Ports.First(),
                Port2 = instr2.Ports.Last(),
                Via = new List<ViaPoint>(){instr.via2}
            };
 
            ConnectionSettings.Current.Add(con1);
            ConnectionSettings.Current.Add(con2);
            
            Assert.IsFalse(con2.IsActive);
            Assert.IsTrue(con1.IsActive);
 
            instr.via2.IsActive = true;
 
            Assert.IsFalse(con1.IsActive);
            Assert.IsTrue(con2.IsActive);
 
            instr.via2.IsActive = false;
 
            Assert.IsFalse(con1.IsActive);
            Assert.IsFalse(con2.IsActive);
        }
    }
}