1 //
2 // Copyright (c) 2010-2022 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using NUnit.Framework;
10 using Antmicro.Renode.Time;
11 using System.Collections.Generic;
12 
13 namespace Antmicro.Renode.UnitTests
14 {
15     [TestFixture]
16     public class TimeTests
17     {
18         [Test]
ShouldTickWithOneHandler()19         public void ShouldTickWithOneHandler()
20         {
21             var clocksource = new BaseClockSource();
22             var counter = 0;
23 
24             clocksource.AddClockEntry(new ClockEntry(2, 1, () => counter++, null, String.Empty) { Value = 0 });
25             clocksource.Advance(TimeInterval.FromSeconds(1));
26             Assert.AreEqual(0, counter);
27             clocksource.Advance(TimeInterval.FromSeconds(1));
28             Assert.AreEqual(1, counter);
29             clocksource.Advance(TimeInterval.FromSeconds(1));
30             Assert.AreEqual(1, counter);
31             clocksource.Advance(TimeInterval.FromSeconds(2));
32             Assert.AreEqual(2, counter);
33         }
34 
35         [Test]
ShouldTickWithTwoHandlers()36         public void ShouldTickWithTwoHandlers()
37         {
38             var clocksource = new BaseClockSource();
39             var counterA = 0;
40             var counterB = 0;
41 
42             clocksource.AddClockEntry(new ClockEntry(2, 1, () => counterA++, null, String.Empty) { Value = 0 });
43             clocksource.AddClockEntry(new ClockEntry(5, 1, () => counterB++, null, String.Empty) { Value = 0 });
44             clocksource.Advance(TimeInterval.FromSeconds(2));
45             Assert.AreEqual(1, counterA);
46             Assert.AreEqual(0, counterB);
47             clocksource.Advance(TimeInterval.FromSeconds(2));
48             Assert.AreEqual(2, counterA);
49             Assert.AreEqual(0, counterB);
50             clocksource.Advance(TimeInterval.FromSeconds(1));
51             Assert.AreEqual(2, counterA);
52             Assert.AreEqual(1, counterB);
53         }
54 
55         [Test]
ShouldHaveHandlersInSync()56         public void ShouldHaveHandlersInSync()
57         {
58             // we test here whether handler executed by the slower clock entry
59             // always "sees" value of the faster one as ten times its own value
60             var clockSource = new BaseClockSource();
61 
62             Action firstHandler = () =>
63             {
64             };
65 
66             var values = new List<ulong>();
67 
68             clockSource.AddClockEntry(new ClockEntry(10000, 10, firstHandler, null, String.Empty));
69             clockSource.AddClockEntry(new ClockEntry(10, 1, () => values.Add(clockSource.GetClockEntry(firstHandler).Value), null, String.Empty));
70 
71             clockSource.Advance(TimeInterval.FromSeconds(9), true);
72             clockSource.Advance(TimeInterval.FromSeconds(8), true);
73             clockSource.Advance(TimeInterval.FromSeconds(20), true);
74 
75             CollectionAssert.AreEqual(new [] { 100, 200, 300 }, values);
76         }
77 
78         [Test]
ShouldObserveShorterPeriodClockAfterAdd()79         public void ShouldObserveShorterPeriodClockAfterAdd()
80         {
81             var clockSource = new BaseClockSource();
82             var counterA = 0;
83             var counterB = 0;
84             Action handlerA = () => counterA++;
85             Action handlerB = () => counterB++;
86             ClockEntry entryA = new ClockEntry(1000, 1, handlerA, null, String.Empty) { Value = 0 };
87             ClockEntry entryB = new ClockEntry(100, 1, handlerB, null, String.Empty) { Value = 0 };
88 
89             clockSource.AddClockEntry(entryA);
90             clockSource.Advance(TimeInterval.FromSeconds(500));
91             clockSource.AddClockEntry(entryB);
92             entryA = clockSource.GetClockEntry(handlerA);
93             entryB = clockSource.GetClockEntry(handlerB);
94             Assert.AreEqual(entryA.Value, 500);
95             Assert.AreEqual(entryA.Period, 1000);
96             Assert.AreEqual(entryB.Value, 0);
97             Assert.AreEqual(entryB.Period, 100);
98 
99             clockSource.Advance(TimeInterval.FromSeconds(50));
100             entryA = clockSource.GetClockEntry(handlerA);
101             entryB = clockSource.GetClockEntry(handlerB);
102             Assert.AreEqual(counterA, 0);
103             Assert.AreEqual(counterB, 0);
104             Assert.AreEqual(entryA.Value, 550);
105             Assert.AreEqual(entryA.Period, 1000);
106             Assert.AreEqual(entryB.Value, 50);
107             Assert.AreEqual(entryB.Period, 100);
108 
109             clockSource.Advance(TimeInterval.FromSeconds(50));
110             entryA = clockSource.GetClockEntry(handlerA);
111             entryB = clockSource.GetClockEntry(handlerB);
112             Assert.AreEqual(counterA, 0);
113             Assert.AreEqual(counterB, 1);
114             Assert.AreEqual(entryA.Value, 600);
115             Assert.AreEqual(entryA.Period, 1000);
116             Assert.AreEqual(entryB.Value, 0);
117             Assert.AreEqual(entryB.Period, 100);
118         }
119     }
120 
121     [TestFixture]
122     public class TimeIntervalTests
123     {
124         [Test]
ShouldReturnCorrectResultFromSubstraction()125         public void ShouldReturnCorrectResultFromSubstraction()
126         {
127             TimeInterval lower = TimeInterval.FromMicroseconds(1242);
128             TimeInterval higher = TimeInterval.FromMicroseconds(1243);
129 
130             Assert.AreEqual(TimeInterval.FromMicroseconds(1), higher - lower);
131         }
132 
133         [Test]
ShouldNotAllowOverflowOnSubstraction()134         public void ShouldNotAllowOverflowOnSubstraction()
135         {
136             TimeInterval lower = TimeInterval.FromMicroseconds(1242);
137             TimeInterval higher = TimeInterval.FromMicroseconds(1243);
138 
139             Assert.Throws<OverflowException>(() => {var dummy = lower - higher; });
140         }
141 
142         [Test]
ShouldNotAllowOverflowOnAddition()143         public void ShouldNotAllowOverflowOnAddition()
144         {
145             TimeInterval value = TimeInterval.FromMicroseconds(1);
146 
147             Assert.Throws<OverflowException>(() => {var dummy = TimeInterval.Maximal + value; });
148         }
149     }
150 }
151 
152