using System.Collections; using System.Collections.Generic; using NUnit.Framework; using Processor; using CSVReader; using UnityEngine; using UnityEngine.TestTools; using System.Threading; using System.Diagnostics; using System; namespace Tests { public class ProzessorTest { private IProcessor IProzessor { get; set; } private const string Filepath = "SampleOutput.bin"; private const int SampleRate = 4;//Hz private const int NrOfObjects = 4;//How many object are updated per step? [SetUp] public void Setup() { IProzessor = new Processor.Processor(Filepath); } [Test] public void TestTimeOrder() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); Thread.Sleep(1000 / SampleRate);//wait for data InputObject[] retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length); for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order { if (i != 0) Assert.IsTrue(retList[i].Time >= retList[i - 1].Time); } } [Test] public void TestTimeOrderReverse() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, false)); Assert.IsTrue(IProzessor.JumpToTimestamp(0 + 1, out _)); Thread.Sleep(1000 / SampleRate);//wait for data InputObject[] retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length); for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order { if (i != 0) Assert.IsTrue(retList[i].Time <= retList[i - 1].Time); } } [Test] public void TestReadTiming() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); Stopwatch watch = new Stopwatch(); watch.Start(); Thread.Sleep(1000 / SampleRate);//wait for data Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count); //read and expect NrOfObjects values double time1 = watch.ElapsedMilliseconds; Thread.Sleep(1000 / SampleRate);//wait for data Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count);//read and expect NrOfObjects values double time2 = watch.ElapsedMilliseconds; Thread.Sleep(1000 / SampleRate);//wait for data Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count);//read and expect NrOfObjects values double time3 = watch.ElapsedMilliseconds; watch.Stop(); Assert.AreEqual(time1, time2 - time1, 15);//Times for same operation should not differ too much Assert.AreEqual(time2 - time1, time3 - time2, 15);//Times for same operation should not differ too much Assert.Less(time1, 2 * (1000 / SampleRate)); //check if time is smaller than samplerate Assert.Less(time2 - time1, 2 * (1000 / SampleRate)); //check if time is smaller than samplerate Assert.Less(time3 - time2, 2 * (1000 / SampleRate)); //check if time is smaller than samplerate } [Test] public void TestTimeJump() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, false)); Assert.IsTrue(IProzessor.JumpToTimestamp(0 + 1, out double x)); Assert.AreEqual(1, x); Thread.Sleep(1000 / SampleRate);//wait for Data Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().ToArray().Length); IProzessor.JumpToTimestamp(1.25, out x);//jump forward 0.25 sec Thread.Sleep(1000 / SampleRate);//wait for Data Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().ToArray().Length); IProzessor.ReverseTime();// and reverse Time IProzessor.JumpToTimestamp(2.5, out x);//jump forward 1.25 sec Thread.Sleep(1000 / SampleRate);//wait for Data Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().ToArray().Length); } [Test] public void TestOutOfBoundsReading() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); Assert.IsTrue(IProzessor.JumpToTimestamp(0, out _)); Assert.IsFalse(IProzessor.JumpToTimestamp(-1, out double x)); Assert.IsFalse(IProzessor.JumpToTimestamp(100, out x)); Assert.IsFalse(IProzessor.JumpToTimestamp(double.MaxValue, out x)); Assert.IsFalse(IProzessor.JumpToTimestamp(double.MinValue, out x)); Assert.IsFalse(IProzessor.JumpToTimestamp(double.NaN, out x)); } [Test] public void TestStopStreaming() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); IProzessor.StopStreaming(); Assert.Throws(delegate { IProzessor.ReadNextValues(); }); } [Test] public void TestStopAndRestart() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); IProzessor.StopStreaming(); IProzessor = new Processor.Processor(Filepath); Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); Thread.Sleep(1000 / SampleRate);//wait for data InputObject[] retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length); } [Test] public void TestReadSameDatatBackAndForth() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); Thread.Sleep(1000 / SampleRate);//wait for data InputObject[] retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length); IProzessor.ReverseTime(); Thread.Sleep(1000 / SampleRate);//wait for data InputObject[] retList2 = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length); for (int i = 0; i < NrOfObjects; i++) Assert.IsTrue(retList[NrOfObjects - 1 - i].Equals(retList2[i])); } [Test] public void TestOldestAndNewestTimestamp() { Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true)); Assert.AreEqual(0, IProzessor.GetOldestTimeStamp()); Assert.AreEqual(99.75, IProzessor.GetNewestTimeStamp()); Thread.Sleep(1000 / SampleRate);//wait for data IProzessor.ReadNextValues(); Thread.Sleep(1000 / SampleRate);//wait for data //These values hould never change on a static file Assert.AreEqual(0, IProzessor.GetOldestTimeStamp()); Assert.AreEqual(99.75, IProzessor.GetNewestTimeStamp()); } [TearDown] public void TearDown() { IProzessor.StopStreaming(); } } }