tbf/TBFTests/Rig/TestMethods/iPerlCommunication/iPerlHead/FlowDirectionDetectionTest.cs
Michal Buzik bcb33cde78 Store changes on Iperl
Add and expand unit tests for iPerl communication, refactor OptoHead methods, update log4net reference, and increment AssemblyVersion.
2026-03-03 14:34:30 +01:00

159 lines
4.8 KiB
C#

using JetBrains.Annotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TBF.Rig.TestMethods.iPerlCommunication.iPerlHead;
namespace TBFTests.Rig.TestMethods.iPerlCommunication.iPerlHead
{
using System;
using System.Threading;
using JetBrains.Annotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TBF.Rig.TestMethods.iPerlCommunication.iPerlHead;
using Common;
namespace TBFTests.Rig.TestMethods.iPerlCommunication.iPerlHead
{
[TestClass]
[TestSubject(typeof(FlowDirectionDetection))]
public class FlowDirectionDetectionTests
{
private FlowDirectionDetection Create() => new FlowDirectionDetection();
// helper: feed linear data
private void FeedLinearData(FlowDirectionDetection d, double slope, int samples = 64)
{
double volume = 0;
double time = 0;
for (int i = 0; i < samples; i++)
{
volume += slope; // linear growth
time += 0.125; // 8 Hz sampling
d.WriteToFifo(volume, time);
}
}
// --------------------------------------------------------
[TestMethod]
public void Fifo_NotFull_DataInvalid()
{
var d = Create();
d.WriteToFifo(1, 1);
Assert.IsFalse(d.AreFifoDataValid());
}
// --------------------------------------------------------
[TestMethod]
public void Fifo_Full_DataValid()
{
var d = Create();
FeedLinearData(d, 1.0);
Assert.IsTrue(d.AreFifoDataValid());
}
// --------------------------------------------------------
[TestMethod]
public void PositiveFlow_Detected()
{
var d = Create();
FeedLinearData(d, slope: 10); // increasing volume
var result = d.CheckFlowDirection(Counting.Positive, "HEAD");
Assert.AreEqual(OptoHeadState.OptoAndDirOK, result);
}
// --------------------------------------------------------
[TestMethod]
public void NegativeFlow_Detected()
{
var d = Create();
FeedLinearData(d, slope: -10); // decreasing volume
var result = d.CheckFlowDirection(Counting.Negative, "HEAD");
Assert.AreEqual(OptoHeadState.OptoAndDirOK, result);
}
// --------------------------------------------------------
[TestMethod]
public void WrongDirection_Fails()
{
var d = Create();
FeedLinearData(d, slope: -10); // negative flow
var result = d.CheckFlowDirection(Counting.Positive, "HEAD");
Assert.AreEqual(OptoHeadState.DirNok, result);
}
// --------------------------------------------------------
[TestMethod]
public void ArbitraryCounting_AlwaysPassesWhenSignalGood()
{
var d = Create();
FeedLinearData(d, slope: -5);
var result = d.CheckFlowDirection(Counting.Arbitrary, "HEAD");
Assert.AreEqual(OptoHeadState.OptoAndDirOK, result);
}
// --------------------------------------------------------
[TestMethod]
public void NoFlow_ReturnsDirNok()
{
var d = Create();
// flat signal → slope ≈ 0
for (int i = 0; i < 64; i++)
d.WriteToFifo(100, i * 0.125);
var result = d.CheckFlowDirection(Counting.Positive, "HEAD");
Assert.AreEqual(OptoHeadState.DirNok, result);
}
// --------------------------------------------------------
[TestMethod]
public void FifoOverwrite_StillDetectsCorrectly()
{
var d = Create();
// write more than FIFO size → overwrite happens
FeedLinearData(d, slope: 5, samples: 200);
var result = d.CheckFlowDirection(Counting.Positive, "HEAD");
Assert.AreEqual(OptoHeadState.OptoAndDirOK, result);
}
// --------------------------------------------------------
[TestMethod]
public void Timeout_InvalidatesFifo()
{
var d = Create();
FeedLinearData(d, slope: 5);
// simulate dropout > 4.5 sec
Thread.Sleep(5000);
Assert.IsFalse(d.AreFifoDataValid());
}
}
}
}