ProzessorTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4. using Processor;
  5. using CSVReader;
  6. using UnityEngine;
  7. using UnityEngine.TestTools;
  8. using System.Threading;
  9. using System.Diagnostics;
  10. using System;
  11. namespace Tests
  12. {
  13. public class ProzessorTest
  14. {
  15. private IProcessor IProzessor { get; set; }
  16. private const string Filepath = "SampleOutput.bin";
  17. private const int SampleRate = 4;//Hz
  18. private const int NrOfObjects = 4;//How many object are updated per step?
  19. [SetUp]
  20. public void Setup()
  21. {
  22. IProzessor = new Processor.Processor(Filepath);
  23. }
  24. [Test]
  25. public void TestTimeOrder()
  26. {
  27. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  28. Thread.Sleep(1000 / SampleRate);//wait for data
  29. InputObject[] retList = IProzessor.ReadNextValues().ToArray();
  30. Assert.AreEqual(NrOfObjects, retList.Length);
  31. for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order
  32. {
  33. if (i != 0)
  34. Assert.IsTrue(retList[i].Time >= retList[i - 1].Time);
  35. }
  36. }
  37. [Test]
  38. public void TestTimeOrderReverse()
  39. {
  40. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, false));
  41. Assert.IsTrue(IProzessor.JumpToTimestamp(0 + 1, out _));
  42. Thread.Sleep(1000 / SampleRate);//wait for data
  43. InputObject[] retList = IProzessor.ReadNextValues().ToArray();
  44. Assert.AreEqual(NrOfObjects, retList.Length);
  45. for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order
  46. {
  47. if (i != 0)
  48. Assert.IsTrue(retList[i].Time <= retList[i - 1].Time);
  49. }
  50. }
  51. [Test]
  52. public void TestReadTiming()
  53. {
  54. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  55. Stopwatch watch = new Stopwatch();
  56. watch.Start();
  57. Thread.Sleep(1000 / SampleRate);//wait for data
  58. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count); //read and expect NrOfObjects values
  59. double time1 = watch.ElapsedMilliseconds;
  60. Thread.Sleep(1000 / SampleRate);//wait for data
  61. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count);//read and expect NrOfObjects values
  62. double time2 = watch.ElapsedMilliseconds;
  63. Thread.Sleep(1000 / SampleRate);//wait for data
  64. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count);//read and expect NrOfObjects values
  65. double time3 = watch.ElapsedMilliseconds;
  66. watch.Stop();
  67. Assert.AreEqual(time1, time2 - time1, 15);//Times for same operation should not differ too much
  68. Assert.AreEqual(time2 - time1, time3 - time2, 15);//Times for same operation should not differ too much
  69. Assert.Less(time1, 2 * (1000 / SampleRate)); //check if time is smaller than samplerate
  70. Assert.Less(time2 - time1, 2 * (1000 / SampleRate)); //check if time is smaller than samplerate
  71. Assert.Less(time3 - time2, 2 * (1000 / SampleRate)); //check if time is smaller than samplerate
  72. }
  73. [Test]
  74. public void TestTimeJump()
  75. {
  76. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, false));
  77. Assert.IsTrue(IProzessor.JumpToTimestamp(0 + 1, out double x));
  78. Assert.AreEqual(1, x);
  79. Thread.Sleep(1000 / SampleRate);//wait for Data
  80. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().ToArray().Length);
  81. IProzessor.JumpToTimestamp(1.25, out x);//jump forward 0.25 sec
  82. Thread.Sleep(1000 / SampleRate);//wait for Data
  83. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().ToArray().Length);
  84. IProzessor.ReverseTime();// and reverse Time
  85. IProzessor.JumpToTimestamp(2.5, out x);//jump forward 1.25 sec
  86. Thread.Sleep(1000 / SampleRate);//wait for Data
  87. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().ToArray().Length);
  88. }
  89. [Test]
  90. public void TestOutOfBoundsReading()
  91. {
  92. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  93. Assert.IsTrue(IProzessor.JumpToTimestamp(0, out _));
  94. Assert.IsFalse(IProzessor.JumpToTimestamp(-1, out double x));
  95. Assert.IsFalse(IProzessor.JumpToTimestamp(100, out x));
  96. Assert.IsFalse(IProzessor.JumpToTimestamp(double.MaxValue, out x));
  97. Assert.IsFalse(IProzessor.JumpToTimestamp(double.MinValue, out x));
  98. Assert.IsFalse(IProzessor.JumpToTimestamp(double.NaN, out x));
  99. }
  100. [Test]
  101. public void TestStopStreaming()
  102. {
  103. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  104. IProzessor.StopStreaming();
  105. Assert.Throws<NullReferenceException>(delegate { IProzessor.ReadNextValues(); });
  106. }
  107. [Test]
  108. public void TestStopAndRestart()
  109. {
  110. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  111. IProzessor.StopStreaming();
  112. IProzessor = new Processor.Processor(Filepath);
  113. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  114. Thread.Sleep(1000 / SampleRate);//wait for data
  115. InputObject[] retList = IProzessor.ReadNextValues().ToArray();
  116. Assert.AreEqual(NrOfObjects, retList.Length);
  117. }
  118. [Test]
  119. public void TestReadSameDatatBackAndForth()
  120. {
  121. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  122. Thread.Sleep(1000 / SampleRate);//wait for data
  123. InputObject[] retList = IProzessor.ReadNextValues().ToArray();
  124. Assert.AreEqual(NrOfObjects, retList.Length);
  125. IProzessor.ReverseTime();
  126. Thread.Sleep(1000 / SampleRate);//wait for data
  127. InputObject[] retList2 = IProzessor.ReadNextValues().ToArray();
  128. Assert.AreEqual(NrOfObjects, retList.Length);
  129. for (int i = 0; i < NrOfObjects; i++)
  130. Assert.IsTrue(retList[NrOfObjects - 1 - i].Equals(retList2[i]));
  131. }
  132. [Test]
  133. public void TestOldestAndNewestTimestamp()
  134. {
  135. Assert.IsTrue(IProzessor.StartStreaming(SampleRate, true));
  136. Assert.AreEqual(0, IProzessor.GetOldestTimeStamp());
  137. Assert.AreEqual(99.75, IProzessor.GetNewestTimeStamp());
  138. Thread.Sleep(1000 / SampleRate);//wait for data
  139. IProzessor.ReadNextValues();
  140. Thread.Sleep(1000 / SampleRate);//wait for data
  141. //These values hould never change on a static file
  142. Assert.AreEqual(0, IProzessor.GetOldestTimeStamp());
  143. Assert.AreEqual(99.75, IProzessor.GetNewestTimeStamp());
  144. }
  145. [TearDown]
  146. public void TearDown()
  147. {
  148. IProzessor.StopStreaming();
  149. }
  150. }
  151. }