Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS will be completely phased out by mid-2025. To see alternatives please check here

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

added equality operators on all needed objects for equality test assertion

parent 1100f62b
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using TUGraz.VectoCore.Exceptions;
using TUGraz.VectoCore.Models.SimulationComponent.Data.Engine;
......@@ -58,6 +59,31 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
[JsonProperty(Required = Required.Always)]
public double FileVersion;
protected bool Equals(DataHeader other)
{
return string.Equals(CreatedBy, other.CreatedBy) && Date.Equals(other.Date) && string.Equals(AppVersion, other.AppVersion) && FileVersion.Equals(other.FileVersion);
}
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;
return Equals((DataHeader) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (CreatedBy != null ? CreatedBy.GetHashCode() : 0);
hashCode = (hashCode*397) ^ Date.GetHashCode();
hashCode = (hashCode*397) ^ (AppVersion != null ? AppVersion.GetHashCode() : 0);
hashCode = (hashCode*397) ^ FileVersion.GetHashCode();
return hashCode;
}
}
}
[JsonProperty(Required = Required.Always)]
......@@ -87,6 +113,27 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
[JsonProperty(Required = Required.Always)]
public string Gears;
protected bool Equals(DataFullLoadCurve other)
{
return string.Equals(Path, other.Path) && string.Equals(Gears, other.Gears);
}
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;
return Equals((DataFullLoadCurve) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((Path != null ? Path.GetHashCode() : 0)*397) ^ (Gears != null ? Gears.GetHashCode() : 0);
}
}
}
[JsonProperty(Required = Required.Always)]
......@@ -103,16 +150,80 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
[JsonProperty("WHTC-Motorway")]
public double WHTCMotorway;
protected bool Equals(DataBody other)
{
return SavedInDeclarationMode.Equals(other.SavedInDeclarationMode)
&& string.Equals(ModelName, other.ModelName)
&& Displacement.Equals(other.Displacement)
&& IdleSpeed.Equals(other.IdleSpeed)
&& Inertia.Equals(other.Inertia)
&& FullLoadCurves.SequenceEqual(other.FullLoadCurves)
&& string.Equals(FuelMap, other.FuelMap)
&& WHTCUrban.Equals(other.WHTCUrban)
&& WHTCRural.Equals(other.WHTCRural)
&& WHTCMotorway.Equals(other.WHTCMotorway);
}
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;
return Equals((DataBody)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = SavedInDeclarationMode.GetHashCode();
hashCode = (hashCode * 397) ^ (ModelName != null ? ModelName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Displacement.GetHashCode();
hashCode = (hashCode * 397) ^ IdleSpeed.GetHashCode();
hashCode = (hashCode * 397) ^ Inertia.GetHashCode();
hashCode = (hashCode * 397) ^ (FullLoadCurves != null ? FullLoadCurves.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (FuelMap != null ? FuelMap.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ WHTCUrban.GetHashCode();
hashCode = (hashCode * 397) ^ WHTCRural.GetHashCode();
hashCode = (hashCode * 397) ^ WHTCMotorway.GetHashCode();
return hashCode;
}
}
}
[JsonProperty(Required = Required.Always)]
public DataBody Body;
protected bool Equals(Data other)
{
return Equals(Header, other.Header) && Equals(Body, other.Body);
}
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;
return Equals((Data) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((Header != null ? Header.GetHashCode() : 0)*397) ^ (Body != null ? Body.GetHashCode() : 0);
}
}
}
private Data _data;
protected bool Equals(CombustionEngineData other)
{
return Equals(_data, other._data)
&& Equals(_fullLoadCurves, other._fullLoadCurves)
return Equals(_data, other._data)
&& _fullLoadCurves.Keys.SequenceEqual(other._fullLoadCurves.Keys)
&& _fullLoadCurves.Values.SequenceEqual(other._fullLoadCurves.Values)
&& Equals(ConsumptionMap, other.ConsumptionMap);
}
......@@ -121,22 +232,20 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((CombustionEngineData) obj);
return Equals((CombustionEngineData)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (_data != null ? _data.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (_fullLoadCurves != null ? _fullLoadCurves.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (ConsumptionMap != null ? ConsumptionMap.GetHashCode() : 0);
var hashCode = _data.GetHashCode();
hashCode = (hashCode * 397) ^ (_fullLoadCurves != null ? _fullLoadCurves.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (ConsumptionMap != null ? ConsumptionMap.GetHashCode() : 0);
return hashCode;
}
}
private Data _data;
public bool SavedInDeclarationMode
{
get { return _data.Body.SavedInDeclarationMode; }
......@@ -234,7 +343,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
public FullLoadCurve GetFullLoadCurve(uint gear)
{
// TODO: @@@quam refactor
// TODO: @@@quam refactor
foreach (var gearRange in _fullLoadCurves.Keys)
{
var low = uint.Parse(gearRange.Split('-').First().Trim());
......
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TUGraz.VectoCore.Exceptions;
using TUGraz.VectoCore.Utils;
......@@ -27,7 +28,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
protected bool Equals(FuelConsumptionMap other)
{
return Equals(_entries, other._entries) && Equals(_fuelMap, other._fuelMap);
return _entries.SequenceEqual(other._entries)
&& Equals(_fuelMap, other._fuelMap);
}
public override bool Equals(object obj)
......@@ -46,15 +48,15 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
}
}
private class FuelConsumptionEntry
private struct FuelConsumptionEntry
{
public double EngineSpeed { get; set; }
public double Torque { get; set; }
public double FuelConsumption { get; set; }
}
private IList<FuelConsumptionEntry> _entries = new List<FuelConsumptionEntry>();
private DelauneyMap _fuelMap = new DelauneyMap();
private readonly IList<FuelConsumptionEntry> _entries = new List<FuelConsumptionEntry>();
private readonly DelauneyMap _fuelMap = new DelauneyMap();
private FuelConsumptionMap() { }
......
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TUGraz.VectoCore.Utils;
namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
......@@ -27,7 +28,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
protected bool Equals(FullLoadCurve other)
{
return Equals(entries, other.entries);
return _entries.SequenceEqual(other._entries);
}
public override bool Equals(object obj)
......@@ -40,7 +41,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
public override int GetHashCode()
{
return (entries != null ? entries.GetHashCode() : 0);
return (_entries != null ? _entries.GetHashCode() : 0);
}
private class FullLoadCurveEntry
......@@ -49,15 +50,43 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
public double TorqueFullLoad { get; set; }
public double TorqueDrag { get; set; }
public double PT1 { get; set; }
protected bool Equals(FullLoadCurveEntry other)
{
return EngineSpeed.Equals(other.EngineSpeed)
&& TorqueFullLoad.Equals(other.TorqueFullLoad)
&& TorqueDrag.Equals(other.TorqueDrag)
&& PT1.Equals(other.PT1);
}
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;
return Equals((FullLoadCurveEntry) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = EngineSpeed.GetHashCode();
hashCode = (hashCode*397) ^ TorqueFullLoad.GetHashCode();
hashCode = (hashCode*397) ^ TorqueDrag.GetHashCode();
hashCode = (hashCode*397) ^ PT1.GetHashCode();
return hashCode;
}
}
}
private List<FullLoadCurveEntry> entries;
private List<FullLoadCurveEntry> _entries;
public static FullLoadCurve ReadFromFile(string fileName)
{
var fullLoadCurve = new FullLoadCurve();
var data = VectoCSVFile.Read(fileName);
fullLoadCurve.entries = new List<FullLoadCurveEntry>();
fullLoadCurve._entries = new List<FullLoadCurveEntry>();
//todo: catch exceptions if value format is wrong.
foreach (DataRow row in data.Rows)
......@@ -67,7 +96,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
entry.TorqueFullLoad = row.GetDouble(Fields.TorqueFullLoad);
entry.TorqueDrag = row.GetDouble(Fields.TorqueDrag);
entry.PT1 = row.GetDouble(Fields.PT1);
fullLoadCurve.entries.Add(entry);
fullLoadCurve._entries.Add(entry);
}
return fullLoadCurve;
}
......@@ -75,8 +104,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
public double FullLoadStationaryTorque(double rpm)
{
var idx = FindIndexForRpm(rpm);
return VectoMath.Interpolate(entries[idx - 1].EngineSpeed, entries[idx].EngineSpeed,
entries[idx - 1].TorqueFullLoad, entries[idx].TorqueFullLoad, rpm);
return VectoMath.Interpolate(_entries[idx - 1].EngineSpeed, _entries[idx].EngineSpeed,
_entries[idx - 1].TorqueFullLoad, _entries[idx].TorqueFullLoad, rpm);
}
public double FullLoadStationaryPower(double rpm)
......@@ -87,8 +116,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
public double DragLoadStationaryTorque(double rpm)
{
var idx = FindIndexForRpm(rpm);
return VectoMath.Interpolate(entries[idx - 1].EngineSpeed, entries[idx].EngineSpeed,
entries[idx - 1].TorqueDrag, entries[idx].TorqueDrag, rpm);
return VectoMath.Interpolate(_entries[idx - 1].EngineSpeed, _entries[idx].EngineSpeed,
_entries[idx - 1].TorqueDrag, _entries[idx].TorqueDrag, rpm);
}
public double DragLoadStationaryPower(double rpm)
......@@ -99,22 +128,22 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
public double PT1(double rpm)
{
var idx = FindIndexForRpm(rpm);
return VectoMath.Interpolate(entries[idx - 1].EngineSpeed, entries[idx].EngineSpeed,
entries[idx - 1].PT1, entries[idx].PT1, rpm);
return VectoMath.Interpolate(_entries[idx - 1].EngineSpeed, _entries[idx].EngineSpeed,
_entries[idx - 1].PT1, _entries[idx].PT1, rpm);
}
protected int FindIndexForRpm(double rpm)
{
int idx;
if (rpm < entries[0].EngineSpeed) {
if (rpm < _entries[0].EngineSpeed) {
Log.ErrorFormat("requested rpm below minimum rpm in FLD curve - extrapolating. n: {0}, rpm_min: {1}", rpm,
entries[0].EngineSpeed);
_entries[0].EngineSpeed);
idx = 1;
} else {
idx = entries.FindIndex(x => x.EngineSpeed > rpm);
idx = _entries.FindIndex(x => x.EngineSpeed > rpm);
}
if (idx <= 0) {
idx = rpm > entries[0].EngineSpeed ? entries.Count - 1 : 1;
idx = rpm > _entries[0].EngineSpeed ? _entries.Count - 1 : 1;
}
return idx;
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using TUGraz.VectoCore.Exceptions;
using TUGraz.VectoCore.Models.Connector.Ports;
using TUGraz.VectoCore.Models.Simulation.Data;
......@@ -37,7 +38,11 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
protected bool Equals(EngineState other)
{
return OperationMode == other.OperationMode && EnginePower.Equals(other.EnginePower) && EngineSpeed.Equals(other.EngineSpeed) && EnginePowerLoss.Equals(other.EnginePowerLoss) && AbsTime.Equals(other.AbsTime);
return OperationMode == other.OperationMode
&& EnginePower.Equals(other.EnginePower)
&& EngineSpeed.Equals(other.EngineSpeed)
&& EnginePowerLoss.Equals(other.EnginePowerLoss)
&& AbsTime.Equals(other.AbsTime);
}
public override bool Equals(object obj)
......@@ -87,7 +92,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
return Equals(_data, other._data)
&& Equals(_previousState, other._previousState)
&& Equals(_currentState, other._currentState)
&& Equals(_enginePowerCorrections, other._enginePowerCorrections);
&& _enginePowerCorrections.SequenceEqual(other._enginePowerCorrections);
}
public override bool Equals(object obj)
......
......@@ -33,7 +33,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent
{
const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var props = type.GetFields(flags).Select(f => CreateProperty(f, memberSerialization)).ToList();
//props.ForEach(p => { p.Writable = true; p.Readable = true; });
props.ForEach(p => { p.Writable = true; p.Readable = true; });
return props;
}
}
......@@ -48,7 +48,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent
{
try
{
return JsonConvert.DeserializeObject<T>(data);
var settings = new JsonSerializerSettings { ContractResolver = new MyContractResolver() };
return JsonConvert.DeserializeObject<T>(data, settings);
}
catch (Exception e)
{
......
......@@ -17,7 +17,8 @@ namespace TUGraz.VectoCore.Utils
protected bool Equals(DelauneyMap other)
{
return Equals(_points, other._points) && Equals(_triangles, other._triangles);
return _points.SequenceEqual(other._points)
&& _triangles.SequenceEqual(other._triangles);
}
public override bool Equals(object obj)
......
......@@ -2,7 +2,6 @@
using System.IO;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using TUGraz.VectoCore.Models.Simulation.Data;
using TUGraz.VectoCore.Models.SimulationComponent;
using TUGraz.VectoCore.Models.SimulationComponent.Data;
......
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