123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System.Collections;
- using System.Collections.Generic;
- using NUnit.Framework;
- using CSVReader;
- using UnityEngine;
- using UnityEngine.TestTools;
- using System.Threading;
- using System;
- namespace Tests
- {
- public class CSVReaderTest
- {
- private ICSVReader ICSVReader { get; set; }
- [SetUp]
- public void Setup()
- {
- string filepath = "SampleOutput.bin";
- ICSVReader = new CSVReader.UnitCSVReader(filepath);
- }
- [Test]
- public void TestPeekAndReadForward()
- {
- InputObject inpObj;
- //Read first entry of file
- Assert.AreEqual(0.0, ICSVReader.Peek(true), "Peek at 1. Elem of File");
- inpObj = ICSVReader.ReadNextEntry(true);
- Assert.IsNotNull(inpObj, "Test if Item is null");
- Assert.AreEqual(0.0, inpObj.Time, "Test timestamp of first InputObject");
- //jump to last entry and try to read next
- Assert.AreEqual(99.75, ICSVReader.JumpToEntry(99.75, true), "Jump in File");
- Assert.AreEqual(99.75, ICSVReader.Peek(true), "Peek at (last+1) Elem of File");
- ICSVReader.ReadNextEntry(true);//read through the 99.75 time slot
- ICSVReader.ReadNextEntry(true);
- ICSVReader.ReadNextEntry(true);
- ICSVReader.ReadNextEntry(true);
- inpObj = ICSVReader.ReadNextEntry(true);
- Assert.IsNull(inpObj, "(last+1) Elem should be null");
- }
- [Test]
- public void TestPeekAndReadBackward()
- {
- InputObject inpObj;
- //try to read -1 entry
- Assert.AreEqual(0, ICSVReader.Peek(false), "Test backward peek of first element");
- inpObj = ICSVReader.ReadNextEntry(false);
- Assert.IsNull(inpObj, ("Found another Entry after the last with " + true));
- //jump in file and read backwards
- Assert.AreEqual(10, ICSVReader.JumpToEntry(10, false), "Jump in File");
- Assert.AreEqual(10, ICSVReader.Peek(false), "Peek at backward Elem");
- inpObj = ICSVReader.ReadNextEntry(false);
- Assert.IsNotNull(inpObj, "Test if entry is not null");
- Assert.AreEqual(10, inpObj.Time, "Test timestamp of InputObject");
- }
- [Test]
- public void TestOutOfBounds()
- {
- Assert.IsTrue(ICSVReader.IsInBounds(0));
- Assert.IsTrue(ICSVReader.IsInBounds(5));
- Assert.IsTrue(ICSVReader.IsInBounds(99.75));
- Assert.IsFalse(ICSVReader.IsInBounds(-1));
- Assert.IsFalse(ICSVReader.IsInBounds(100));
- Assert.IsFalse(ICSVReader.IsInBounds(double.NaN));
- Assert.IsFalse(ICSVReader.IsInBounds(double.MaxValue));
- Assert.IsFalse(ICSVReader.IsInBounds(double.MinValue));
- }
- [Test]
- public void TestWrongFilename()
- {
- InputObject inpObj;
- ICSVReader iAmWrong = new UnitCSVReader("WerBinIch.bin");
- inpObj = iAmWrong.ReadNextEntry(true);
- Assert.IsNull(inpObj, "Hat Werte trotz falschem Dateinamen");
- inpObj = iAmWrong.ReadNextEntry(false);
- Assert.IsNull(inpObj, "Hat Werte trotz falschem Dateinamen");
- Assert.AreEqual(-1, iAmWrong.JumpToEntry(1, false));
- Assert.AreEqual(false, iAmWrong.IsInBounds(1));
- Assert.AreEqual(-1, iAmWrong.Peek(true));
- Assert.AreEqual(-1, iAmWrong.Peek(false));
- bool except = false;
- string msg = string.Empty;
- try
- {
- iAmWrong.TearDown();
- }
- catch (Exception e)
- {
- except = true;
- msg = e.Message;
- }
- Assert.IsFalse(except, "Exception was thrown on Teardown: " + msg);
- }
- public void TestFirstAndLastTimestamp()
- {
- Assert.AreEqual(0, ICSVReader.GetFirstTimestamp());
- Assert.AreEqual(99.75, ICSVReader.GetLastTimestamp());
- }
- [TearDown]
- public void TearDown()
- {
- ICSVReader.TearDown();
- }
- }
- }
|