CSVReaderTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4. using CSVReader;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. using System.Threading;
  8. using System;
  9. namespace Tests
  10. {
  11. public class CSVReaderTest
  12. {
  13. private ICSVReader ICSVReader { get; set; }
  14. [SetUp]
  15. public void Setup()
  16. {
  17. string filepath = "SampleOutput.bin";
  18. ICSVReader = new CSVReader.UnitCSVReader(filepath);
  19. }
  20. [Test]
  21. public void TestPeekAndReadForward()
  22. {
  23. InputObject inpObj;
  24. //Read first entry of file
  25. Assert.AreEqual(0.0, ICSVReader.Peek(true), "Peek at 1. Elem of File");
  26. inpObj = ICSVReader.ReadNextEntry(true);
  27. Assert.IsNotNull(inpObj, "Test if Item is null");
  28. Assert.AreEqual(0.0, inpObj.Time, "Test timestamp of first InputObject");
  29. //jump to last entry and try to read next
  30. Assert.AreEqual(99.75, ICSVReader.JumpToEntry(99.75, true), "Jump in File");
  31. Assert.AreEqual(99.75, ICSVReader.Peek(true), "Peek at (last+1) Elem of File");
  32. ICSVReader.ReadNextEntry(true);//read through the 99.75 time slot
  33. ICSVReader.ReadNextEntry(true);
  34. ICSVReader.ReadNextEntry(true);
  35. ICSVReader.ReadNextEntry(true);
  36. inpObj = ICSVReader.ReadNextEntry(true);
  37. Assert.IsNull(inpObj, "(last+1) Elem should be null");
  38. }
  39. [Test]
  40. public void TestPeekAndReadBackward()
  41. {
  42. InputObject inpObj;
  43. //try to read -1 entry
  44. Assert.AreEqual(0, ICSVReader.Peek(false), "Test backward peek of first element");
  45. inpObj = ICSVReader.ReadNextEntry(false);
  46. Assert.IsNull(inpObj, ("Found another Entry after the last with " + true));
  47. //jump in file and read backwards
  48. Assert.AreEqual(10, ICSVReader.JumpToEntry(10, false), "Jump in File");
  49. Assert.AreEqual(10, ICSVReader.Peek(false), "Peek at backward Elem");
  50. inpObj = ICSVReader.ReadNextEntry(false);
  51. Assert.IsNotNull(inpObj, "Test if entry is not null");
  52. Assert.AreEqual(10, inpObj.Time, "Test timestamp of InputObject");
  53. }
  54. [Test]
  55. public void TestOutOfBounds()
  56. {
  57. Assert.IsTrue(ICSVReader.IsInBounds(0));
  58. Assert.IsTrue(ICSVReader.IsInBounds(5));
  59. Assert.IsTrue(ICSVReader.IsInBounds(99.75));
  60. Assert.IsFalse(ICSVReader.IsInBounds(-1));
  61. Assert.IsFalse(ICSVReader.IsInBounds(100));
  62. Assert.IsFalse(ICSVReader.IsInBounds(double.NaN));
  63. Assert.IsFalse(ICSVReader.IsInBounds(double.MaxValue));
  64. Assert.IsFalse(ICSVReader.IsInBounds(double.MinValue));
  65. }
  66. [Test]
  67. public void TestWrongFilename()
  68. {
  69. InputObject inpObj;
  70. ICSVReader iAmWrong = new UnitCSVReader("WerBinIch.bin");
  71. inpObj = iAmWrong.ReadNextEntry(true);
  72. Assert.IsNull(inpObj, "Hat Werte trotz falschem Dateinamen");
  73. inpObj = iAmWrong.ReadNextEntry(false);
  74. Assert.IsNull(inpObj, "Hat Werte trotz falschem Dateinamen");
  75. Assert.AreEqual(-1, iAmWrong.JumpToEntry(1, false));
  76. Assert.AreEqual(false, iAmWrong.IsInBounds(1));
  77. Assert.AreEqual(-1, iAmWrong.Peek(true));
  78. Assert.AreEqual(-1, iAmWrong.Peek(false));
  79. bool except = false;
  80. string msg = string.Empty;
  81. try
  82. {
  83. iAmWrong.TearDown();
  84. }
  85. catch (Exception e)
  86. {
  87. except = true;
  88. msg = e.Message;
  89. }
  90. Assert.IsFalse(except, "Exception was thrown on Teardown: " + msg);
  91. }
  92. public void TestFirstAndLastTimestamp()
  93. {
  94. Assert.AreEqual(0, ICSVReader.GetFirstTimestamp());
  95. Assert.AreEqual(99.75, ICSVReader.GetLastTimestamp());
  96. }
  97. [TearDown]
  98. public void TearDown()
  99. {
  100. ICSVReader.TearDown();
  101. }
  102. }
  103. }