Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit cdb5637f authored by Michael KRISPER's avatar Michael KRISPER
Browse files

Added some Code Contract statements for error checking. Corrected some tests

parent 4cebfa50
No related branches found
No related tags found
No related merge requests found
Showing with 65 additions and 21 deletions
......@@ -386,6 +386,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
protected bool Equals(Range other)
{
Contract.Requires(other != null);
return _start == other._start && _end == other._end;
}
......
......@@ -12,6 +12,15 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
[JsonObject(MemberSerialization.Fields)]
public class FuelConsumptionMap : SimulationComponentData
{
[ContractInvariantMethod]
private void Invariant()
{
Contract.Invariant(_entries != null);
Contract.Invariant(_fuelMap != null);
}
private static class Fields
{
/// <summary>
......@@ -50,6 +59,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
#region Equality members
private bool Equals(FuelConsumptionEntry other)
{
Contract.Requires(other != null);
return EngineSpeed.Equals(other.EngineSpeed) && Torque.Equals(other.Torque) &&
FuelConsumption.Equals(other.FuelConsumption);
}
......
......@@ -57,8 +57,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
public double PT1 { get; set; }
#region Equality members
private bool Equals(FullLoadCurveEntry other)
protected bool Equals(FullLoadCurveEntry other)
{
Contract.Requires(other != null);
return EngineSpeed.IsEqual(other.EngineSpeed)
&& TorqueFullLoad.IsEqual(other.TorqueFullLoad)
&& TorqueDrag.IsEqual(other.TorqueDrag)
......
......@@ -174,7 +174,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
writer[ModalResultField.Tq_drag] = _currentState.FullDragTorque;
writer[ModalResultField.Tq_full] = _currentState.DynamicFullLoadTorque;
writer[ModalResultField.Tq_eng] = _currentState.EngineTorque;
writer[ModalResultField.n] = _currentState.EngineSpeed;
writer[ModalResultField.n] = _currentState.EngineSpeed.
SI().Radian.Per.Second.
To().Rounds.Per.Minute.ScalarValue();
try
{
......
......@@ -4,6 +4,7 @@ using Common.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Runtime.Serialization;
using TUGraz.VectoCore.Exceptions;
namespace TUGraz.VectoCore.Utils
......@@ -11,6 +12,13 @@ namespace TUGraz.VectoCore.Utils
[JsonObject(MemberSerialization.Fields)]
public class DelauneyMap
{
[ContractInvariantMethod]
private void Invariant()
{
Contract.Invariant(_points != null);
Contract.Invariant(_triangles != null);
}
private readonly List<Point> _points = new List<Point>();
private List<Triangle> _triangles = new List<Triangle>();
......@@ -78,15 +86,14 @@ namespace TUGraz.VectoCore.Utils
protected bool Equals(DelauneyMap other)
{
return _points.SequenceEqual(other._points)
&& _triangles.SequenceEqual(other._triangles);
return _points.SequenceEqual(other._points) && _triangles.SequenceEqual(other._triangles);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
if (obj.GetType() != GetType()) return false;
return Equals((DelauneyMap)obj);
}
......@@ -129,10 +136,7 @@ namespace TUGraz.VectoCore.Utils
#region Equality members
protected bool Equals(Point other)
{
//const double tolerance = 0.0001m;
//return Math.Abs(X - other.X) < tolerance
// && Math.Abs(X - other.X) < tolerance
// && Math.Abs(X - other.X) < tolerance;
Contract.Requires(other != null);
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z);
}
......@@ -169,6 +173,10 @@ namespace TUGraz.VectoCore.Utils
public Plane(Triangle tr)
{
Contract.Requires(tr != null);
Contract.Requires(tr.P1 != null);
Contract.Requires(tr.P2 != null);
Contract.Requires(tr.P3 != null);
var ab = tr.P2 - tr.P1;
var ac = tr.P3 - tr.P1;
......@@ -203,6 +211,10 @@ namespace TUGraz.VectoCore.Utils
public bool IsInside(double x, double y, bool exact = true)
{
Contract.Requires(P1 != null);
Contract.Requires(P2 != null);
Contract.Requires(P3 != null);
//Barycentric Technique: http://www.blackpawn.com/texts/pointinpoly/default.html
var p = new Point(x, y, 0);
......@@ -229,6 +241,10 @@ namespace TUGraz.VectoCore.Utils
public bool ContainsInCircumcircle(Point p)
{
Contract.Requires(p != null);
Contract.Requires(P1 != null);
Contract.Requires(P2 != null);
Contract.Requires(P3 != null);
var p0 = P1 - p;
var p1 = P2 - p;
var p2 = P3 - p;
......@@ -253,6 +269,7 @@ namespace TUGraz.VectoCore.Utils
public bool SharesVertexWith(Triangle t)
{
Contract.Requires(t != null);
return Contains(t.P1) || Contains(t.P2) || Contains(t.P3);
}
......@@ -265,6 +282,7 @@ namespace TUGraz.VectoCore.Utils
protected bool Equals(Triangle other)
{
Contract.Requires(other != null);
return Equals(P1, other.P1) && Equals(P2, other.P2) && Equals(P3, other.P3);
}
......@@ -317,6 +335,7 @@ namespace TUGraz.VectoCore.Utils
protected bool Equals(Edge other)
{
Contract.Requires(other != null);
return Equals(P1, other.P1) && Equals(P2, other.P2)
|| Equals(P1, other.P2) && Equals(P1, other.P2);
}
......
......@@ -10,6 +10,13 @@ namespace TUGraz.VectoCore.Utils
[DataContract]
public class SI
{
[ContractInvariantMethod]
private void Invariant()
{
Contract.Invariant(_divisor != null);
Contract.Invariant(_divident != null);
}
[DataMember]
private readonly double _value;
......@@ -65,6 +72,8 @@ namespace TUGraz.VectoCore.Utils
bool? reciproc = null, bool? reverse = null, int? exponent = null)
{
Contract.Requires(si != null);
Contract.Requires(si._divisor != null);
Contract.Requires(si._divident != null);
var divisor = si._divisor.ToList();
var divident = si._divident.ToList();
......
......@@ -36,7 +36,7 @@
<CodeContractsArithmeticObligations>True</CodeContractsArithmeticObligations>
<CodeContractsEnumObligations>True</CodeContractsEnumObligations>
<CodeContractsRedundantAssumptions>True</CodeContractsRedundantAssumptions>
<CodeContractsAssertsToContractsCheckBox>True</CodeContractsAssertsToContractsCheckBox>
<CodeContractsAssertsToContractsCheckBox>False</CodeContractsAssertsToContractsCheckBox>
<CodeContractsRedundantTests>True</CodeContractsRedundantTests>
<CodeContractsMissingPublicRequiresAsWarnings>True</CodeContractsMissingPublicRequiresAsWarnings>
<CodeContractsMissingPublicEnsuresAsWarnings>True</CodeContractsMissingPublicEnsuresAsWarnings>
......@@ -47,9 +47,9 @@
<CodeContractsSuggestAssumptions>True</CodeContractsSuggestAssumptions>
<CodeContractsSuggestAssumptionsForCallees>False</CodeContractsSuggestAssumptionsForCallees>
<CodeContractsSuggestRequires>True</CodeContractsSuggestRequires>
<CodeContractsNecessaryEnsures>True</CodeContractsNecessaryEnsures>
<CodeContractsNecessaryEnsures>False</CodeContractsNecessaryEnsures>
<CodeContractsSuggestObjectInvariants>True</CodeContractsSuggestObjectInvariants>
<CodeContractsSuggestReadonly>True</CodeContractsSuggestReadonly>
<CodeContractsSuggestReadonly>False</CodeContractsSuggestReadonly>
<CodeContractsRunInBackground>True</CodeContractsRunInBackground>
<CodeContractsShowSquigglies>True</CodeContractsShowSquigglies>
<CodeContractsUseBaseLine>False</CodeContractsUseBaseLine>
......@@ -66,8 +66,8 @@
<CodeContractsFailBuildOnWarnings>True</CodeContractsFailBuildOnWarnings>
<CodeContractsBeingOptimisticOnExternal>True</CodeContractsBeingOptimisticOnExternal>
<CodeContractsRuntimeCheckingLevel>Full</CodeContractsRuntimeCheckingLevel>
<CodeContractsReferenceAssembly>Build</CodeContractsReferenceAssembly>
<CodeContractsAnalysisWarningLevel>0</CodeContractsAnalysisWarningLevel>
<CodeContractsReferenceAssembly>%28none%29</CodeContractsReferenceAssembly>
<CodeContractsAnalysisWarningLevel>3</CodeContractsAnalysisWarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
......@@ -145,7 +145,9 @@
<Compile Include="Models\SimulationComponent\VectoSimulationComponent.cs" />
<Compile Include="Models\SimulationComponent\Impl\EngineOnlyDrivingCycle.cs" />
<Compile Include="Models\Simulation\Data\IModalDataWriter.cs" />
<Compile Include="Models\Simulation\Data\ModalResult.cs" />
<Compile Include="Models\Simulation\Data\ModalResult.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Models\Simulation\IVectoJob.cs" />
<Compile Include="Models\Simulation\IVectoSimulator.cs" />
<Compile Include="Models\Simulation\Data\ModalDataWriter.cs" />
......
......@@ -63,7 +63,7 @@ namespace TUGraz.VectoCore.Tests.Integration.EngineOnlyCycle
Assert.AreEqual((double) row[field.GetName()] * siFactor[j], dataWriter.GetDouble(field), tolerances[j]);
}
if (row[ModalResultField.FC.GetName()] is double) {
Assert.AreEqual((double)row[ModalResultField.FC.GetName()], dataWriter.GetDouble(ModalResultField.FC).SI().Kilo.Gramm.Per.Second.To().Gramm.Per.Hour, 0.01);
Assert.AreEqual((double)row[ModalResultField.FC.GetName()], dataWriter.GetDouble(ModalResultField.FC), 0.01);
}
else {
Assert.IsTrue(Double.IsNaN(dataWriter.GetDouble(ModalResultField.FC)));
......
......@@ -61,8 +61,8 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation
// todo: assert correct values!
Assert.AreEqual(0.0, outPort.AbsTime.TotalSeconds);
Assert.AreEqual(1.0, outPort.Dt.TotalSeconds);
Assert.AreEqual(80, outPort.Velocity);
Assert.AreEqual(0.03, outPort.Gradient);
Assert.AreEqual(0.0, outPort.Velocity);
Assert.AreEqual(-0.020237973, outPort.Gradient);
Assert.AreEqual(0.5, dataWriter[ModalResultField.time]);
}
......
......@@ -98,7 +98,7 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
absTime += dt;
}
engineSpeed = 869.7512.SI().Newton.Meter;
engineSpeed = 869.7512.SI().Rounds.Per.Minute;
port.Request(absTime, dt, Formulas.PowerToTorque(7984.56.SI().Watt, engineSpeed), engineSpeed);
engine.CommitSimulationStep(dataWriter);
......@@ -107,7 +107,7 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
dataWriter.CommitSimulationStep(absTime, dt);
absTime += dt;
engineSpeed = 644.4445.SI().Newton.Meter;
engineSpeed = 644.4445.SI().Rounds.Per.Minute;
port.Request(absTime, dt, Formulas.PowerToTorque(1351.656.SI().Watt, engineSpeed), engineSpeed);
engine.CommitSimulationStep(dataWriter);
......
<s>,<v>,<grad>,<stop>,<Padd>,<Aux_ALT1>,<Aux_ALT2>,<Aux_ALT3>
<t>,<v>,<grad>,<n>,<Padd>,<Aux_ALT1>,<Aux_ALT2>,<Aux_ALT3>
0,0,-0.020237973,2,6.1,0.25,0.25,0.25
1,64,-0.020237973,0,6.1,0.25,0.25,0.25
2,64,-0.020237973,0,6.1,0.25,0.25,0.25
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment