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
//            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 OpenTap.Plugins.BasicSteps;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Tap.Shared;
using OpenTap.Engine.UnitTests.TestTestSteps;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class ReflectionHelperTest
    {
 
        [Test]
        public void EnumerableTest()
        {
            int[] x = new[] { 1, 2, 3, 4 };
            Assert.IsFalse(x.IsLongerThan(4));
            Assert.IsTrue(x.IsLongerThan(3));
            Assert.IsFalse(x.IsLongerThan(400000));
 
            Assert.AreEqual(x.IndexWhen(y => y == 3), 2);
            int sum = 0;
            x.ForEach(y => sum += y);
            Assert.AreEqual(sum, 10);
        }
 
        [Test]
        public void ArrayAppendTest()
        {
            int[] items = {0, 1, 2, 3, 4};
            Sequence.Append(ref items, 5, 6);
            for (int i = 0; i < 7; i++)
                Assert.AreEqual(i, items[i]);
        }
 
        [Test]
        [Platform(Exclude="Unix,Linux,MacOsX")]
        public void TimeoutOperationTest()
        {
            var sem = new System.Threading.Semaphore(0, 1);
            TapThread.Start(() => sem.Release());
            sem.WaitOne();
            {
                bool calledAction = false;
                var operation = TimeoutOperation.Create(TimeSpan.FromMilliseconds(1), () =>
                {
                    calledAction = true;
                    sem.Release();
                });
                sem.WaitOne();
                Assert.IsTrue(calledAction);
            }
            {
                bool calledAction = false;
                var operation = TimeoutOperation.Create(TimeSpan.FromMilliseconds(50), () => calledAction = true);
                operation.Dispose();
                Assert.IsFalse(calledAction);
            }
            {
                bool calledAction = false;
                var operation = TimeoutOperation.Create(TimeSpan.FromMilliseconds(1), () =>
                {
                    calledAction = true;
                });
                operation.Dispose();
                Assert.IsFalse(calledAction);
            }
            {
                bool calledAction = false;
                var operation = TimeoutOperation.Create(TimeSpan.FromMilliseconds(1), () => calledAction = true);
                Assert.IsFalse(calledAction);
                operation.Dispose();
            }
        }
 
        static int getMurMur(int i)
        {
            switch (i)
            {
                case 0: return 1036651960;
                case 1: return -108976867;
                case 2: return -888838031;
                case 3: return 1867787361;
                case 4: return 531708635;
                case 5: return -687432098;
                case 6: return 182881051;
                case 7: return 1461746781;
                case 8: return 619631658;
                case 9: return 2054570891;
                default:
                    throw new InvalidOperationException();
 
            }
        }
 
        [Test]
        public void TestMurmur3()
        {
            var rnd = new Random(100);
            byte[] buffer = new byte[100];
            
 
            for (int i = 0; i < 10; i++){
 
                rnd.NextBytes(buffer);
                var hello = MurMurHash3.Hash(buffer);
                Assert.AreEqual(getMurMur(i), hello);
            }
 
            var test2 = MurMurHash3.Hash("H3wlo World4!!!!");
            Assert.AreEqual(1251584510, test2);
 
        }
 
        public class Things
        {
            public Things[] Sub;
            public int Value;
        }
 
        [Test]
        public void FlattenHeirarchyTest()
        {
            var a = new Things() { Value = 1, Sub = new[] { new Things { Value = 2 }, new Things { Value = 4 } } };
            Assert.AreEqual(7 + 2, Utils.FlattenHeirarchy(new[] { a, a.Sub[0] }, x => x.Sub).Select(x => x.Value).Sum());
            HashSet<Things> set = new HashSet<Things>();
            Utils.FlattenHeirarchyInto(new[] { a, a.Sub[0] }, x => x.Sub, set);
            Assert.AreEqual(7, set.Sum(x => x.Value));
        }
 
        class BaseTest
        {
            public virtual void Test()
            {
 
            }
        }
 
        class BaseTest2 : BaseTest
        {
 
        }
 
        class BaseTest3 : BaseTest
        {
            public override void Test()
            {
                base.Test();
            }
        }
 
 
        [Test]
        public void OverriderTest()
        {
            //
            // this test demonstrates the behavior that can be used to test if a 
            // method is overridden or not.
            //
            // This behavior most work otherwise PrePlanRun/PostPlanRun optimizations wont work.
            //
            MethodInfo baseclaseMethod = typeof(BaseTest).GetMethod("Test");
            MethodInfo inheritedButNotOverridden = typeof(BaseTest2).GetMethod("Test");
            MethodInfo inheritedAndOverridden = typeof(BaseTest3).GetMethod("Test");
            Assert.AreEqual(baseclaseMethod.MethodHandle.Value, inheritedButNotOverridden.MethodHandle.Value);
            Assert.AreNotEqual(baseclaseMethod.MethodHandle.Value, inheritedAndOverridden.MethodHandle.Value);
 
 
            // delayStep does not override PrePlanRun or PostPlanRun
            var delay = new DelayStep();
            Assert.IsFalse(delay.PrePostPlanRunUsed);
 
            // TimingTestStep does..
            var timing = new TimingTestStep();
            Assert.IsTrue(timing.PrePostPlanRunUsed);
        }
 
        [Test]
        public void RemoveIfTest()
        {
            var rnd = new Random();
            var values = Enumerable.Repeat(0, 1000).Select(x => rnd.NextDouble()).ToList();
            Assert.IsTrue(values.IndexWhen(x => x < 0.5) != -1);
            values.RemoveIf(x => x < 0.5); // remove all values < 0.5.
            Assert.IsTrue(values.IndexWhen(x => x < 0.5) == -1);
        }
 
        [Test]
        public void TestCheckOperatingSystem()
        {
            // we can use RuntimeInformation to check that our OperatingSystem implementation works.
            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                Assert.AreEqual(OperatingSystem.Windows, OperatingSystem.Current);
            }
            else if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
            {
                Assert.AreEqual(OperatingSystem.MacOS, OperatingSystem.Current);
            }
            else
            {
                Assert.AreEqual(OperatingSystem.Linux, OperatingSystem.Current);
            }
        }
 
        [Test]
        public void TestDirSearch()
        {
            var opentapdir = Path.GetDirectoryName(typeof(TestPlan).Assembly.Location);
            var opentapfile = Path.GetFileName(typeof(TestPlan).Assembly.Location);
            var files = PathUtils.IterateDirectories(opentapdir, "*.dll", SearchOption.AllDirectories).ToArray();
            var opentapdll = files.FirstOrDefault(x => Path.GetFileName(x) == opentapfile);
            Assert.IsNotNull(opentapdll);
        }
 
        [Test]
        public void MemorizerValidationTest()
        {
            int globalData = 1;
            var mem = new Memorizer<int, string>(i => (i * globalData).ToString())
            {
                Validator = x => x + globalData
            };
            
            Assert.AreEqual("4", mem[4]);
            globalData = 2;
            Assert.AreEqual("8", mem[4]);
            globalData = 3;
            Assert.AreEqual("12", mem[4]);
            globalData = 4;
            Assert.AreEqual("16", mem[4]);
        }
 
        [Flags]
        enum TestFlags
        {
            None = 0,
            Value1 = 1,
            [Display("Value 2")]
            Value2 = 2,
            [Display("Value 4")]
            Value4 = 4
            
        }
 
        const EngineSettings.AbortTestPlanType blankAbortType = 0;
        [TestCase(EngineSettings.AbortTestPlanType.Step_Error | EngineSettings.AbortTestPlanType.Step_Fail, "Break On Fail | Break On Error")]
        [TestCase(EngineSettings.AbortTestPlanType.Step_Error, "Break On Error")]
        [TestCase(EngineSettings.AbortTestPlanType.Step_Fail, "Break On Fail")]
        [TestCase(blankAbortType, "")]
        [TestCase(Verdict.Pass, "Pass")]
        [TestCase(Verdict.NotSet, "Not Set")]
        
        [TestCase(TestFlags.None, "None")]
        [TestCase(TestFlags.None | TestFlags.Value1, "Value1")]
        [TestCase(TestFlags.Value2 | TestFlags.Value1, "Value1 | Value 2")]
        [TestCase(TestFlags.Value2 | TestFlags.Value1 | TestFlags.Value4, "Value1 | Value 2 | Value 4")]
        public void TestEnumToString(Enum testValue, string expectedString)
        {
            var actualString = Utils.EnumToReadableString(testValue);
            Assert.AreEqual(expectedString, actualString);
        }
 
        [Test]
        public void TestInvalidEnumToString()
        {
            TestEnumToString((Verdict) 111, "111");
            // 1 and 2 are included in 111, so actually these flags are set.
            TestEnumToString((EngineSettings.AbortTestPlanType) 111, "Break On Fail | Break On Error | Break On Inconclusive | Break On Pass");
        }
 
 
        [TestCase(1000, "1.00 kB")]
        [TestCase(0, "0 B")]
        [TestCase(110, "110 B")]
        [TestCase(1500, "1.50 kB")]
        [TestCase(15000, "15.00 kB")]
        [TestCase(150000, "150.00 kB")]
        [TestCase(1500000, "1.50 MB")]
        [TestCase(1000000, "1.00 MB")]
        [TestCase(15500000, "15.50 MB")]
        [TestCase(155550000, "155.55 MB")]
        [TestCase(1550000000, "1.55 GB")]
        [TestCase(2000000000, "2.00 GB")]
        [TestCase(20500000000, "20.50 GB")]
        public void TestBytesToReadable(long number, string expected)
        {
            var str = Utils.BytesToReadable(number);
            Assert.AreEqual(expected, str);
        }
        
        [Test]
        public void TrySelectUnwrapTest()
        {
            var x = new int[] { 1, 0, 3, 0 };
            int exceptionsCaught = 0;
            var inv1 = x.TrySelect(x => 1 / x, (e, x) => exceptionsCaught++).ToArray();
            var inv2 = x.TrySelect(x => 1 / x, e => exceptionsCaught++).ToArray();
            Assert.IsTrue(inv1.SequenceEqual(new[] { 1, 0 }));
            Assert.IsTrue(inv1.SequenceEqual(inv2));
            Assert.AreEqual(4, exceptionsCaught);
        }
 
        [Test]
        public void TestTimeFromSeconds()
        {
            Assert.AreEqual(TimeSpan.Zero, Time.FromSeconds(0));
            Assert.AreEqual(TimeSpan.MaxValue, Time.FromSeconds(double.PositiveInfinity));
            Assert.Throws<ArithmeticException>(() => Time.FromSeconds(double.NaN));
            Assert.AreEqual(TimeSpan.MaxValue, Time.FromSeconds(1000000000000000000000000000.0));
            Assert.AreEqual(TimeSpan.MinValue, Time.FromSeconds(-1000000000000000000000000000.0));
        }
 
        [Test]
        public void BatchingTest()
        {
            var inv = Enumerable.Range(0, 1000).Batch(32).OrderByDescending(x => x).ToArray();
            Assert.IsTrue(inv.SequenceEqual(Enumerable.Range(0, 1000).OrderByDescending(x => x)));
        }
 
        [Test]
        public void CompareAndExchangeTest()
        {
            object X = 1;
            object Y = 2;
            Utils.InterlockedSwap(ref X, () => 3);
            Utils.InterlockedSwap(ref Y, () => 4);
            Assert.AreEqual(3, X);
            Assert.AreEqual(4, Y);
 
            X = 0;
            Y = 0;
            int cnt = 5;
            int adds = 10000;
            var tasks = new Task[cnt];
 
            for (int i = 0; i < cnt; i++)
            {
                tasks[i] = TapThread.StartAwaitable(() =>
                {
                    for (int i = 0; i < adds; i++)
                    {
                        Utils.InterlockedSwap(ref X, () => ((int)X + 1));
                        Utils.InterlockedSwap(ref Y, () => ((int)Y + 2));
                    }
                });
            }
            for (int i = 0; i < cnt; i++)
            {
                tasks[i].Wait();
            }
            Assert.AreEqual(X, cnt * adds);
            Assert.AreEqual(Y, cnt * adds * 2);
        }
 
        [Test]
        public void TestRetryUtil()
        {
            int counter = 0;
            var x = Utils.Retry(() =>
            {
                if (counter < 5)
                {
                    counter += 1;
                    throw new IOException();
                }
 
                return counter;
            }, typeof(IOException), sleepBaseMs: 0, maxRetries: 10);
            Assert.AreEqual(x, 5);
        }
        
        [Test]
        public void TestRetryFailure()
        {
            int counter = 0;
            
            Assert.Throws<IOException>(() => Utils.Retry(() =>
            {
                if (counter < 10)
                {
                    counter += 1;
                    throw new IOException();
                }
 
                return counter;
            }, typeof(IOException), sleepBaseMs: 0, maxRetries: 3));
        }
    }
}