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
73 changes: 73 additions & 0 deletions OpenNetMeter.Tests/AxisScaleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using OpenNetMeter.Views.Controls.Charting;

namespace OpenNetMeter.Tests;

public class AxisScaleTests
{
[Theory]
[InlineData(1)]
[InlineData(37)]
[InlineData(100)]
[InlineData(999)]
[InlineData(1000)]
[InlineData(1234.5)]
[InlineData(0.5)]
[InlineData(9_999_999)]
public void Compute_TopIsAlwaysAtLeastMax(double max)
{
var ticks = AxisScale.Compute(max, 2);

Assert.True(ticks.Top >= max);
}

[Theory]
[InlineData(0)]
[InlineData(-5)]
public void Compute_NonPositiveInputDoesNotThrow(double max)
{
var ticks = AxisScale.Compute(max, 2);

Assert.True(ticks.Top > 0);
Assert.True(ticks.Top >= max);
}

[Fact]
public void Compute_StepIsANice1_2_5Multiple()
{
var ticks = AxisScale.Compute(37, 2);

double step = ticks.Values[1] - ticks.Values[0];
double magnitude = Math.Pow(10, Math.Floor(Math.Log10(step)));
double normalized = Math.Round(step / magnitude, 6);

Assert.Contains(normalized, new[] { 1.0, 2.0, 5.0, 10.0 });
}

[Fact]
public void Compute_ValuesAreEvenlySpacedFromZero()
{
var ticks = AxisScale.Compute(1000, 2);

Assert.Equal(3, ticks.Values.Count);
Assert.Equal(0, ticks.Values[0]);
double step = ticks.Values[1] - ticks.Values[0];
Assert.Equal(step, ticks.Values[2] - ticks.Values[1], precision: 9);
Assert.Equal(ticks.Top, ticks.Values[^1], precision: 9);
}

[Fact]
public void Compute_DesiredIntervalsControlsValueCount()
{
var ticks = AxisScale.Compute(100, 4);

Assert.Equal(5, ticks.Values.Count);
}

[Fact]
public void Compute_ZeroOrNegativeIntervalsFallsBackToOne()
{
var ticks = AxisScale.Compute(100, 0);

Assert.Equal(2, ticks.Values.Count);
}
}
103 changes: 103 additions & 0 deletions OpenNetMeter.Tests/SpeedHistoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using OpenNetMeter.Views.Controls.Charting;

namespace OpenNetMeter.Tests;

public class SpeedHistoryTests
{
[Fact]
public void Append_TracksCountAndOrderWithinCapacity()
{
var history = new SpeedHistory();

history.Append(10, 20);
history.Append(30, 40);

Assert.Equal(2, history.Count);
Assert.Equal(new SpeedSample(10, 20), history[0]);
Assert.Equal(new SpeedSample(30, 40), history[1]);
}

[Fact]
public void Append_WrapsAroundOnceCapacityIsExceeded()
{
var history = new SpeedHistory();
int capacity = SpeedHistory.VisibleSamples + 2;

for (int i = 0; i < capacity + 5; i++)
history.Append(i, i);

Assert.Equal(capacity, history.Count);
// Oldest retained sample is the one appended (capacity+5-capacity) = 5 ticks ago.
Assert.Equal(new SpeedSample(5, 5), history[0]);
Assert.Equal(new SpeedSample(capacity + 4, capacity + 4), history[history.Count - 1]);
}

[Fact]
public void Clear_ResetsCountAndRaisesClearedEvent()
{
var history = new SpeedHistory();
history.Append(1, 2);
history.Append(3, 4);

bool clearedRaised = false;
history.Cleared += (_, _) => clearedRaised = true;

history.Clear();

Assert.Equal(0, history.Count);
Assert.True(clearedRaised);
}

[Fact]
public void Append_RaisesSampleAppendedEvent()
{
var history = new SpeedHistory();
int raisedCount = 0;
history.SampleAppended += (_, _) => raisedCount++;

history.Append(1, 2);
history.Append(3, 4);

Assert.Equal(2, raisedCount);
}

[Fact]
public void MaxInWindow_ReturnsMaxAcrossDownloadAndUpload()
{
var history = new SpeedHistory();
history.Append(10, 5);
history.Append(2, 40);
history.Append(7, 3);

Assert.Equal(40, history.MaxInWindow(3));
}

[Fact]
public void MaxInWindow_OnlyConsidersRequestedSampleCount()
{
var history = new SpeedHistory();
history.Append(100, 0);
history.Append(1, 0);
history.Append(2, 0);

Assert.Equal(2, history.MaxInWindow(2));
}

[Fact]
public void MaxInWindow_EmptyHistoryReturnsZero()
{
var history = new SpeedHistory();

Assert.Equal(0, history.MaxInWindow(SpeedHistory.VisibleSamples));
}

[Fact]
public void Indexer_OutOfRangeThrows()
{
var history = new SpeedHistory();
history.Append(1, 1);

Assert.Throws<ArgumentOutOfRangeException>(() => history[1]);
Assert.Throws<ArgumentOutOfRangeException>(() => history[-1]);
}
}
124 changes: 0 additions & 124 deletions OpenNetMeter/Compat/Utilities/Graph.cs

This file was deleted.

1 change: 0 additions & 1 deletion OpenNetMeter/OpenNetMeter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.3.12" />
<PackageReference Include="Avalonia.Desktop" Version="11.3.12" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.12" />
<PackageReference Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.5" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.8" />
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.6" />
<PackageReference Include="System.Diagnostics.EventLog" Version="8.0.0" />
Expand Down
Loading
Loading