Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions dotnet-datetime/StopWatchCSharp/StopWatchCSharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using StopWatchCSharp;
using StopWatchCSharp;
using System.Diagnostics;

var stopWatch = StopWatchMethods.CreateRandomArray(1000);
Expand All @@ -8,4 +8,12 @@
Elapsed Ticks: {stopWatch.ElapsedTicks}
Is Running: {stopWatch.IsRunning}
Frequency: {Stopwatch.Frequency}
Using High Resolution: {Stopwatch.IsHighResolution}");
Using High Resolution: {Stopwatch.IsHighResolution}");

Console.WriteLine($"One second in Stopwatch ticks: {Stopwatch.GetElapsedTime(0, Stopwatch.Frequency)}");

var twoIntervals = StopWatchMethods.MeasureTwoIntervals();
Console.WriteLine($"Two measured intervals: {twoIntervals.ElapsedMilliseconds} ms");

var elapsed = StopWatchMethods.CreateRandomArrayWithTimestamp(1000);
Console.WriteLine($"Elapsed with GetTimestamp: {elapsed.TotalMilliseconds} ms");
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Diagnostics;
using System.Diagnostics;

namespace StopWatchCSharp;

public static class StopWatchMethods
{
{
public static Stopwatch CreateRandomArray(int size)
{
var stopWatch = new Stopwatch();
Expand All @@ -16,4 +17,30 @@ public static Stopwatch CreateRandomArray(int size)

return stopWatch;
}
}

public static Stopwatch MeasureTwoIntervals()
{
var stopWatch = Stopwatch.StartNew();
Thread.Sleep(100);
stopWatch.Stop();

Thread.Sleep(100);

stopWatch.Start();
Thread.Sleep(100);
stopWatch.Stop();

return stopWatch;
}

public static TimeSpan CreateRandomArrayWithTimestamp(int size)
{
var startTimestamp = Stopwatch.GetTimestamp();
var array = new int[size];

for (int i = 0; i < size; i++)
array[i] = Random.Shared.Next();

return Stopwatch.GetElapsedTime(startTimestamp);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,56 @@ public void GivenAStopwatchInstance_WhenStopwatchMethodsInvoked_ThenVerifyReturn
Assert.IsInstanceOfType(stopWatch.ElapsedMilliseconds, typeof(long));
Assert.IsInstanceOfType(stopWatch.ElapsedTicks, typeof(long));
Assert.IsFalse(stopWatch.IsRunning);

stopWatch.Reset();

Assert.AreEqual(stopWatch.Elapsed, TimeSpan.Zero);
Assert.AreEqual(TimeSpan.Zero, stopWatch.Elapsed);
Assert.IsFalse(stopWatch.IsRunning);
stopWatch.Restart();

Assert.AreEqual(stopWatch.Elapsed.CompareTo(TimeSpan.Zero), 1);
Assert.IsTrue(stopWatch.Elapsed > TimeSpan.Zero);
Assert.IsTrue(stopWatch.IsRunning);

stopWatch.Stop();

Assert.IsFalse(stopWatch.IsRunning);
}
}

[TestMethod]
public void GivenAStoppedStopwatch_WhenStartedAgain_ThenElapsedTimeKeepsAccumulating()
{
var stopWatch = Stopwatch.StartNew();
Thread.Sleep(10);
stopWatch.Stop();
var firstInterval = stopWatch.Elapsed;

stopWatch.Start();
Thread.Sleep(10);
stopWatch.Stop();

Assert.IsTrue(stopWatch.Elapsed > firstInterval);
}

[TestMethod]
public void GivenTwoIntervals_WhenMeasured_ThenOnlyTheMeasuredTimeIsCounted()
{
var stopWatch = StopWatchMethods.MeasureTwoIntervals();

Assert.IsFalse(stopWatch.IsRunning);
Assert.IsTrue(stopWatch.Elapsed >= TimeSpan.FromMilliseconds(150));
}

[TestMethod]
public void GivenFrequencyTicks_WhenConvertedWithGetElapsedTime_ThenResultIsOneSecond()
{
Assert.AreEqual(TimeSpan.FromSeconds(1), Stopwatch.GetElapsedTime(0, Stopwatch.Frequency));
}

[TestMethod]
public void GivenATimestamp_WhenGetElapsedTimeCalled_ThenReturnsElapsedTimeSpan()
{
var elapsed = StopWatchMethods.CreateRandomArrayWithTimestamp(10);

Assert.IsTrue(elapsed >= TimeSpan.Zero);
}
}
Loading