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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//            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 NUnit.Framework;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Linq;
using OpenTap.EngineUnitTestUtils;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class ResultProxyTests 
    {
        
        public class DelegateTestStep : TestStep
        {
            [XmlIgnore]
            public Action<ResultSource> RunAction { get; set; }
 
            public override void Run()
            {
                RunAction(Results);
            }
        }
 
        [Test]
        public void LargeValueTest()
        {
            TestPlan plan = new TestPlan();
            DelegateTestStep step = new DelegateTestStep();
            step.RunAction = (Results) =>
            {
                Results.Publish("LargeValueType", new List<string> { "a", "b", "c", "d" }, 0, 9.91e37, 10, 0); // This is the large  value that X-Apps returns when there was no measurement.
            };
            plan.ChildTestSteps.Add(step);
            TestPlanRunner.RunTestPlan(plan);
        }
 
        [Test]
        public void NaNValueTest()
        {
            TestPlan plan = new TestPlan();
            DelegateTestStep step = new DelegateTestStep();
            step.RunAction = (Results) =>
            {
                Results.Publish("NaNValueType", new List<string> { "a", "b", "c", "d" }, 0, double.NaN, 10, 0);
            };
            plan.ChildTestSteps.Add(step);
            TestPlanRunner.RunTestPlan(plan);
        }
 
        [Display("My object result")]
        internal class MyResult
        {
            [Display("Testing")]
            public int A { get; set; }
 
            [Display("Value [dBm]")]
            public double b { get; set; }
        }
 
        internal class TempResultListener : ResultListener
        {
            public ResultTable LastResult;
 
            public override void OnResultPublished(Guid stepRunID, ResultTable result)
            {
                base.OnResultPublished(stepRunID, result);
                LastResult = result;
            }
        }
 
        [Test]
        public void ObjectResultTest()
        {
            TempResultListener rl = new TempResultListener();
 
            ResultSettings.Current.Add(rl);
 
            TestPlan plan = new TestPlan();
            DelegateTestStep step = new DelegateTestStep();
            step.RunAction = (Results) =>
            {
                Results.Publish(new MyResult { A = 123, b = -123e-1 });
            };
            plan.ChildTestSteps.Add(step);
            TestPlanRunner.RunTestPlan(plan);
 
            Assert.IsNotNull(rl.LastResult);
            Assert.AreEqual("My object result", rl.LastResult.Name);
            Assert.AreEqual(2, rl.LastResult.Columns.Length);
            Assert.AreEqual(1, rl.LastResult.Rows);
            Assert.AreEqual("Testing", rl.LastResult.Columns[0].Name);
            Assert.AreEqual("Value [dBm]", rl.LastResult.Columns[1].Name);
 
            Assert.AreEqual(TypeCode.Int32, rl.LastResult.Columns[0].TypeCode);
            Assert.AreEqual(TypeCode.Double, rl.LastResult.Columns[1].TypeCode);
 
            Assert.AreEqual(123, rl.LastResult.Columns[0].GetValue<Int32>(0));
            Assert.AreEqual(-123e-1, rl.LastResult.Columns[1].GetValue<double>(0));
 
            ResultSettings.Current.Remove(rl);
        }
 
        public class ResultObjectBase
        {
            public double Freq { get; set; }
            public double Power { get; set; }
        }
 
        public class EvmResultObject : ResultObjectBase
        {
            public double Evm { get; set; }
        }
 
 
        [Test]
        public void DerivedObjectResultTest()
        {
            TempResultListener rl = new TempResultListener();
 
            ResultSettings.Current.Add(rl);
 
            TestPlan plan = new TestPlan();
            DelegateTestStep step = new DelegateTestStep();
            ResultObjectBase result = new EvmResultObject() { Freq = 1, Power = 2, Evm = 3 };
            step.RunAction = (Results) =>
            {
                Results.Publish(result);
            };
            plan.ChildTestSteps.Add(step);
            TestPlanRunner.RunTestPlan(plan);
 
            Assert.IsNotNull(rl.LastResult);
            Assert.AreEqual("EvmResultObject", rl.LastResult.Name);
            Assert.AreEqual(3, rl.LastResult.Columns.Length);
            Assert.AreEqual(1, rl.LastResult.Rows);
            Assert.AreEqual("Evm", rl.LastResult.Columns[0].Name);
 
            ResultSettings.Current.Remove(rl);
        }
 
        [Test]
        public void AnonymousObjectResultTest()
        {
            TempResultListener rl = new TempResultListener();
 
            ResultSettings.Current.Add(rl);
 
            TestPlan plan = new TestPlan();
            DelegateTestStep step = new DelegateTestStep();
            step.RunAction = (Results) =>
            {
                Results.Publish("My object result", new { A = 123, b = -123e-1 });
            };
            plan.ChildTestSteps.Add(step);
            TestPlanRunner.RunTestPlan(plan);
 
            Assert.IsNotNull(rl.LastResult);
            Assert.AreEqual("My object result", rl.LastResult.Name);
            Assert.AreEqual(2, rl.LastResult.Columns.Length);
            Assert.AreEqual(1, rl.LastResult.Rows);
            Assert.AreEqual("A", rl.LastResult.Columns[0].Name);
            Assert.AreEqual("b", rl.LastResult.Columns[1].Name);
 
            Assert.AreEqual(TypeCode.Int32, rl.LastResult.Columns[0].TypeCode);
            Assert.AreEqual(TypeCode.Double, rl.LastResult.Columns[1].TypeCode);
 
            Assert.AreEqual(123, rl.LastResult.Columns[0].GetValue<Int32>(0));
            Assert.AreEqual(-123e-1, rl.LastResult.Columns[1].GetValue<double>(0));
 
            ResultSettings.Current.Remove(rl);
        }
 
        private class ResultValidator : ResultListener
        {
            public List<ResultTable> Results { get; } = new List<ResultTable>();
            public List<(ResultTable,TestStepRun)> Results2 { get; } = new List<(ResultTable, TestStepRun)>();
 
 
            Dictionary<Guid, TestStepRun> stepRuns = new Dictionary<Guid, TestStepRun>();
            public override void OnTestStepRunStart(TestStepRun stepRun)
            {
                stepRuns[stepRun.Id] = stepRun;
            }
 
            public override void OnTestStepRunCompleted(TestStepRun stepRun)
            {
                stepRuns.Remove(stepRun.Id);
            }
 
 
            public override void OnResultPublished(Guid stepRunId, ResultTable result)
            {
                Results.Add(result);
                Results2.Add((result,stepRuns[stepRunId]));
            }
        }
        
 
        [Test]
        public void ResultsProxyTest()
        {
            TestPlan tp = new TestPlan();
            tp.ChildTestSteps.Add(new DelegateTestStep
            {
                RunAction = (r) =>
                {
                    var rand = new Random();
 
                    IConvertible[] values = new IConvertible[3];
                    values[0] = rand.NextDouble();
                    values[1] = rand.NextDouble();
                    values[2] = rand.NextDouble();
 
                    r.Publish("Publish1", new List<string> { "a", "b", "c" }, values);
                    r.Publish("Publish2", new List<string> { "a", "b", "c" }, values);
                    r.PublishTable("PublishTable", new List<string> { "a", "b", "c" }, values.Select(v => new IConvertible[1] { v }).ToArray());
 
                    values[0] = rand.NextDouble();
                    values[1] = rand.NextDouble();
                    values[2] = rand.NextDouble();
 
                    values[0] = "abc";
                    values[1] = "rand";
                    values[2] = "rand.NextDouble()";
                    r.Publish("PublishStrings", new List<string> { "a", "b", "c" }, values);
 
                    values[0] = null;
                    values[1] = "rand";
                    values[2] = true;
                    r.Publish("PublishMixed", new List<string> { "a", "b", "c" }, values);
                }
            });
 
            var rl = new ResultValidator();
            tp.Execute(new List<ResultListener> { rl });
 
            Assert.AreEqual(5, rl.Results.Count, "Number of results");
            foreach (var res in rl.Results)
            {
                Assert.IsTrue(res.Name.StartsWith("Publish"), "Name of result");
                Assert.AreEqual(3, res.Columns.Length, "Number of columns in " + res.Name);
 
                Assert.AreEqual(1, res.Columns[0].Data.Length);
                Assert.AreEqual(1, res.Columns[1].Data.Length);
                Assert.AreEqual(1, res.Columns[2].Data.Length);
 
                Assert.AreEqual("a", res.Columns[0].Name);
                Assert.AreEqual("b", res.Columns[1].Name);
                Assert.AreEqual("c", res.Columns[2].Name);
            }
        }
 
        public class TestCls<T>
        {
            public class TestCls2<T2>
            {
                public T X { get; set; }
                public T2 Y { get; set; }
            }
 
            public T X { get; set; }
        }
 
        [Test]
        public void ResultTableName()
        {
            var rl = new ResultValidator();
            TestPlan tp = new TestPlan();
            tp.ChildTestSteps.Add(new DelegateTestStep
            {
                RunAction = (r) =>
                {
                    r.Publish(new TestCls<double>{ X = 1.0 });
                    r.Publish(new TestCls<double>.TestCls2<int> { X = 1.0, Y = 1 });
                }
            });
            var run = tp.Execute(new[] { rl });
            Assert.AreEqual("TestCls`1", rl.Results.First().Name);
            Assert.AreEqual("TestCls2`1", rl.Results.Last().Name);
            bool anyNullGroup = run.Parameters.Any(x => x.Group == null);
            Assert.IsFalse(anyNullGroup);
        }
 
        public class CheckNullGroupStep : TestStep
        {
            public override void Run()
            {
                bool anyNull = StepRun.Parameters.Any(x => x.Group == null);
                if (anyNull)
                    UpgradeVerdict(Verdict.Fail);
                else
                    UpgradeVerdict(Verdict.Pass);
            }
        }
 
        [Test]
        public void NoNullResultGroups()
        {
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(new CheckNullGroupStep());
            var run = plan.Execute();
            Assert.AreEqual(Verdict.Pass, run.Verdict);
            Assert.IsTrue(run.Parameters.All(x => x.Group != null));
        }
 
        public class AdvancedResultsStep : TestStep
        {
            public class ResultCommentAttribute : Attribute, IParameter
            {
                public string Name => "Comment";
 
                public string ObjectType => "string";
 
                public string Group => "";
 
                public IConvertible Value { get; }
                public ResultCommentAttribute(string comment) => Value = comment;
            }
            
            [ResultComment("ResultCommentTest")]
            [Display("XY")]
            public class AdvancedResult
            {
                [Unit("s")]
                [Display("X", "This axis denotes some time spent.")]
                public double X { get; set; }
                [Unit("W")]
                [Display("Y", "This axis denotes some power.")]
                [ResultComment("ResultCommentTest2")]
                public double Y { get; set; }
 
                public static AdvancedResult New(double x, double y)
                {
                    return new AdvancedResult() {X = x, Y = y};
                }
            }
            public override void Run()
            {
                var c1 = new ResultColumn("X", Enumerable.Range(0, 10).ToArray(), new UnitAttribute("s"));
                var c2 = new ResultColumn("Y", Enumerable.Range(0, 10).ToArray(), new UnitAttribute("W"));
                var results = new ResultTable("XY", new []{c1, c2}, new []{new ResultParameter("Comment", "Test Result")});
                
                Results.Publish(results);
                Results.Publish(Enumerable.Range(0, 10).Select( x => AdvancedResult.New(x,x)));
            }
        }
 
        [Test]
        public void ResultTableAndColumnParameters()
        {
            var step = new AdvancedResultsStep();
            var plan = new TestPlan();
            plan.Steps.Add(step);
            var rl = new ResultValidator();
            plan.Execute(new[] {rl});
 
            void validatedResult(ResultTable t, string comment)
            {
                Assert.AreEqual(comment, t.Parameters["Comment"]);
                Assert.AreEqual("XY", t.Name);
                Assert.AreEqual(2, t.Columns.Length);
                Assert.AreEqual(10, t.Rows);
                var x = t.Columns[0];
                var y = t.Columns[1];
 
                Assert.AreEqual("X", x.Name);
                Assert.AreEqual("Y", y.Name);
                Assert.AreEqual(1, x.Data.GetValue(1));
                Assert.AreEqual(9, y.Data.GetValue(9));
                Assert.AreEqual("s", x.Parameters["Unit"]);
                Assert.AreEqual("W",y.Parameters["Unit"]);
                Assert.AreEqual("s", x.Parameters["OpenTap.Unit"]);
                
            }
            
            Assert.AreEqual(2, rl.Results2.Count);
            
            //verify that the same results were generated in two different ways
            validatedResult(rl.Results[0], "Test Result");
            validatedResult(rl.Results[1], "ResultCommentTest");
        }
 
        [Test]
        public void ResultOptimizerTest()
        {
            ResultColumn createColumn<T>(string name)
            {
                return new ResultColumn("x", new T[2]);
            }
 
            {
                var rc1x = createColumn<int>("X");
                var rc1y = createColumn<int>("Y");
                var rt1 = new ResultTable("XY", new[] {rc1x, rc1y});
 
                var rc2x =  createColumn<int>("X");
                var rc2y = createColumn<int>("X");
                var rt2 = new ResultTable("XY", new[] {rc2x, rc2y});
                Assert.IsTrue(ResultTableOptimizer.CanMerge(rt1, rt2));
                var merged = ResultTableOptimizer.MergeTables(new []{rt1, rt2});
                Assert.AreEqual(4, merged.Columns[0].Data.Length);
            }
            {
                var rc1x = createColumn<int>("X");
                var rc1y = createColumn<int>("Y");
                var rt1 = new ResultTable("XY", new[] {rc1x, rc1y});
 
                var rc2x =  createColumn<int>("X");
                var rc2y = createColumn<int>("X");
                rc2y = rc2y.AddParameters(new ResultParameter("Unit", "X"));
                var rt2 = new ResultTable("XY", new[] {rc2x, rc2y});
                Assert.IsTrue(ResultTableOptimizer.CanMerge(rt1, rt2) == false);
            }
        }
        
        // an issue inside ResultTableOptimizer caused it to
        // think that two tables were 'mergable' even though they had different
        // column types. In this test, one is bool and the other is int.
        [Test]
        public void TestResultTableMergeBug()
        {
            var boolColumnTable = new ResultTable("X", new[] {new ResultColumn("A", new bool[1])});
            var boolColumnTable2 = new ResultTable("X", new[] {new ResultColumn("A", new bool[1])});
            var intColumnTable = new ResultTable("X", new[] {new ResultColumn("A", new int[1])});
            var intColumnTable2 = new ResultTable("X", new[] {new ResultColumn("A", new int[1])});
            Assert.IsTrue(ResultTableOptimizer.CanMerge(intColumnTable, intColumnTable2), "These columns should be mergeable.");
            Assert.IsTrue(ResultTableOptimizer.CanMerge(boolColumnTable, boolColumnTable2), "These columns should be mergeable.");
            Assert.IsFalse(ResultTableOptimizer.CanMerge(boolColumnTable, intColumnTable), "It should not be possible to merge an int column and bool column.");
            
            
        }
    }
}