diff --git a/VectoCore/VectoCore/Utils/PathHelper.cs b/VectoCore/VectoCore/Utils/PathHelper.cs new file mode 100644 index 0000000000000000000000000000000000000000..9df975ec0cafe255e13718378a3b9257f1b7788b --- /dev/null +++ b/VectoCore/VectoCore/Utils/PathHelper.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Windows.Forms.VisualStyles; +using Castle.Core.Internal; + +namespace TUGraz.VectoCore.Utils +{ + public static class PathHelper + { + public static string GetRelativePath(string relativeTo, string path) + { + + + var relativeFullPath = Path.GetFullPath(relativeTo); + var relativeToFileName = Path.GetFileName(relativeFullPath); + relativeFullPath = relativeFullPath.RemoveLastOccurence(relativeToFileName); + + var pathFullPath = Path.GetFullPath(path); + var pathFileName = Path.GetFileName(pathFullPath); + pathFullPath = pathFullPath.RemoveLastOccurence(pathFileName); + + var rootEqual = Path.GetPathRoot(relativeFullPath) == Path.GetPathRoot(path); + if (!rootEqual) + { + throw new ArgumentException("Paths must be on the same drive"); + } + + + var pathArray = pathFullPath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Where(s => !s.IsNullOrEmpty()); + var relativeToArray = relativeFullPath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Where(s => !s.IsNullOrEmpty()); + var result = GetRelativePath(relativeToArray, pathArray); + + + return Path.Combine(result, pathFileName); + } + + private static string GetRelativePath(IEnumerable<string> relativeTo, IEnumerable<string> path) + { + var relList = relativeTo.ToList(); + var pathList = path.ToList(); + var resultList = new LinkedList<string>(); + for (int i = 0; i < Math.Max(relList.Count, pathList.Count); i++) { + var relRemaining = i < relList.Count; + var pathRemaining = i < pathList.Count; + + if ((relRemaining && pathRemaining) && relList[i] == pathList[i]) { + //ignore CommonNode + continue; + } + + if (relRemaining) { + resultList.AddFirst(".."); + } + + if (pathRemaining) { + resultList.AddLast(pathList[i]); + } + } + return Path.Combine(resultList.ToArray()); + } + + private static string RemoveLastOccurence(this string original, string remove) + { + + var lastIndex = original.LastIndexOf(remove, StringComparison.InvariantCulture); + + + return lastIndex != -1 ? original.Remove(lastIndex, remove.Length) : original; + } + } +} \ No newline at end of file diff --git a/VectoCore/VectoCore/VectoCore.csproj b/VectoCore/VectoCore/VectoCore.csproj index 7b843a9d3759c0fc6d7b16a2d9b21b81eb99defe..714303fae7ccb5016e5243589dbce0d277c7febf 100644 --- a/VectoCore/VectoCore/VectoCore.csproj +++ b/VectoCore/VectoCore/VectoCore.csproj @@ -570,6 +570,7 @@ <Compile Include="Models\SimulationComponent\Impl\DrivingCycleEnumerator.cs" /> <Compile Include="Models\SimulationComponent\Impl\EngineFanAuxiliary.cs" /> <Compile Include="Models\SimulationComponent\Impl\GearRating.cs" /> + <Compile Include="Utils\PathHelper.cs" /> <Compile Include="Utils\SchmittTrigger.cs" /> <Compile Include="Models\SimulationComponent\Impl\SimplePowertrainContainer.cs" /> <Compile Include="Models\SimulationComponent\Impl\TorqueConverterWrapper.cs" /> diff --git a/VectoCore/VectoCoreTest/Integration/Multistage/MultistageMultipleRunsTest.cs b/VectoCore/VectoCoreTest/Integration/Multistage/MultistageMultipleRunsTest.cs new file mode 100644 index 0000000000000000000000000000000000000000..948fcd75b8def04d1fcc5dd8276123ff030b358e --- /dev/null +++ b/VectoCore/VectoCoreTest/Integration/Multistage/MultistageMultipleRunsTest.cs @@ -0,0 +1,55 @@ +using NUnit.Framework; +using TUGraz.VectoCommon.InputData; +using TUGraz.VectoCommon.Models; +using TUGraz.VectoCore.InputData.FileIO.JSON; +using TUGraz.VectoCore.InputData.FileIO.XML.Declaration.DataProvider; +using TUGraz.VectoCore.Models.Simulation.Impl; +using TUGraz.VectoCore.OutputData; +using TUGraz.VectoCore.OutputData.FileIO; + +namespace TUGraz.VectoCore.Tests.Integration.Multistage +{ + [TestFixture] + public class MultistageMultipleRunsTest + { + //public const + [SetUp] + public void SetUp() + { + + } + + [TestCase] + public void ExemptedPrimaryAndCompletedTest() + { + + } + + [TestCase] + public void ExemptedPrimaryAndInterimTest() + { + + } + + [TestCase] + public void PrimaryAndCompletedTest() + { + + + + } + + [TestCase] + public void PrimaryAndInterimTest() + { + + + + } + + + + + + } +} \ No newline at end of file diff --git a/VectoCore/VectoCoreTest/Utils/PathHelperTest.cs b/VectoCore/VectoCoreTest/Utils/PathHelperTest.cs new file mode 100644 index 0000000000000000000000000000000000000000..e659b0389cdc9502de3e0f719c8a47e961f50840 --- /dev/null +++ b/VectoCore/VectoCoreTest/Utils/PathHelperTest.cs @@ -0,0 +1,80 @@ + +using System; +using NUnit.Framework; +using TUGraz.VectoCommon.InputData; +using TUGraz.VectoCore.Utils; + +namespace TUGraz.VectoCore.Tests.Utils +{ + [TestFixture] + public class PathHelperTest + { + [Test] + public void RelativePathTest1() + { + var path = + "C:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\VectoCoreTest\\TestData\\Integration\\Buses\\PrimaryAndStageInput\\"; + var relativeTo = + "C:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\VectoCoreTest\\TestData\\Integration\\Buses\\"; + + + + var result = PathHelper.GetRelativePath(relativeTo, path); + + Assert.AreEqual("PrimaryAndStageInput", result ); + + } + + [Test] + public void RelativePathTest2() + { + var path = + "C:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\VectoCoreTest\\TestData\\Integration\\Buses\\PrimaryAndStageInput\\file1.file"; + var relativeTo = + "C:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\VectoCoreTest\\TestData\\Integration\\Buses\\file3.file"; + + + + var result = PathHelper.GetRelativePath(relativeTo, path); + + Assert.AreEqual("PrimaryAndStageInput\\file1.file", result); + + } + [Test] + public void RelativePathTest3() + { + var path = + "C:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\file.file"; + var relativeTo = + "C:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\VectoCoreTest\\TestData\\Integration\\Buses\\file3.file"; + + + + var result = PathHelper.GetRelativePath(relativeTo, path); + + Assert.AreEqual("PrimaryAndStageInput\\file1.file", result); + + } + + + [Test] + public void RelativePathDifferentDrives() + { + var path = + "D:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\VectoCoreTest\\TestData\\Integration\\Buses\\PrimaryAndStageInput\\"; + var relativeTo = + "C:\\Users\\Harry\\source\\repos\\vecto-dev\\VectoCore\\VectoCoreTest\\TestData\\Integration\\Buses\\"; + + + Assert.Throws<ArgumentException>( + () => { + PathHelper.GetRelativePath(path, relativeTo); + }); + } + + + + + + } +} \ No newline at end of file diff --git a/VectoCore/VectoCoreTest/VectoCoreTest.csproj b/VectoCore/VectoCoreTest/VectoCoreTest.csproj index fb8d358a377e5388ef659c15f3b2d1def434b5a6..c434ff65384a2121e5b949d8c6937f64bc522c55 100644 --- a/VectoCore/VectoCoreTest/VectoCoreTest.csproj +++ b/VectoCore/VectoCoreTest/VectoCoreTest.csproj @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <Import Project="..\..\packages\NUnit3TestAdapter.3.11.2\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\..\packages\NUnit3TestAdapter.3.11.2\build\net35\NUnit3TestAdapter.props')" /> <Import Project="..\..\packages\NUnit.3.11.0\build\NUnit.props" Condition="Exists('..\..\packages\NUnit.3.11.0\build\NUnit.props')" /> + <Import Project="..\..\packages\NUnit3TestAdapter.3.11.2\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\..\packages\NUnit3TestAdapter.3.11.2\build\net35\NUnit3TestAdapter.props')" /> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> @@ -61,7 +61,6 @@ </Reference> <Reference Include="nunit.framework, Version=3.11.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL"> <HintPath>..\..\packages\NUnit.3.11.0\lib\net45\nunit.framework.dll</HintPath> - <Private>True</Private> </Reference> <Reference Include="System" /> <Reference Include="System.ComponentModel.DataAnnotations" /> @@ -116,6 +115,7 @@ <Compile Include="Integration\Declaration\VocationalVehicleTest.cs" /> <Compile Include="Integration\Declaration\TestMaxMassInMUCycle.cs" /> <Compile Include="Integration\DualFuel\DualFuelTests.cs" /> + <Compile Include="Integration\Multistage\MultistageMultipleRunsTest.cs" /> <Compile Include="Integration\RoadSweepers\RoadSweeperTests.cs" /> <Compile Include="Integration\Hybrid\ParallelHybridTest.cs" /> <Compile Include="Integration\Multistage\MultistageVehicleTest.cs" /> @@ -215,6 +215,7 @@ <Compile Include="Utils\MockDriver.cs" /> <Compile Include="Utils\MockVehicle.cs" /> <Compile Include="Utils\MockDeclarationVehicleInputData.cs" /> + <Compile Include="Utils\PathHelperTest.cs" /> <Compile Include="Utils\ResultFileHelper.cs" /> <Compile Include="Utils\MockPorts.cs" /> <Compile Include="Models\Simulation\SimulationTests.cs" /> @@ -6084,8 +6085,8 @@ <PropertyGroup> <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> </PropertyGroup> - <Error Condition="!Exists('..\..\packages\NUnit.3.11.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\NUnit.3.11.0\build\NUnit.props'))" /> <Error Condition="!Exists('..\..\packages\NUnit3TestAdapter.3.11.2\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\NUnit3TestAdapter.3.11.2\build\net35\NUnit3TestAdapter.props'))" /> + <Error Condition="!Exists('..\..\packages\NUnit.3.11.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\NUnit.3.11.0\build\NUnit.props'))" /> </Target> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets.