Index: Ringtoets/MacroStabilityInwards/src/Ringtoets.MacroStabilityInwards.Primitives/SoilLayerProperties.cs =================================================================== diff -u -r9caa7daa14d0070e2a3f68c4b9e66109318bd9ab -rf2234f62cc9b8e06b06c39acc6e0754d85366ac7 --- Ringtoets/MacroStabilityInwards/src/Ringtoets.MacroStabilityInwards.Primitives/SoilLayerProperties.cs (.../SoilLayerProperties.cs) (revision 9caa7daa14d0070e2a3f68c4b9e66109318bd9ab) +++ Ringtoets/MacroStabilityInwards/src/Ringtoets.MacroStabilityInwards.Primitives/SoilLayerProperties.cs (.../SoilLayerProperties.cs) (revision f2234f62cc9b8e06b06c39acc6e0754d85366ac7) @@ -67,5 +67,73 @@ public double PopMean { get; set; } = double.NaN; public double PopDeviation { get; set; } = double.NaN; public double PopShift { get; set; } = double.NaN; + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + return Equals((SoilLayerProperties) obj); + } + + public override int GetHashCode() + { + unchecked + { + int hashCode = StringComparer.InvariantCulture.GetHashCode(materialName); + hashCode = (hashCode * 397) ^ IsAquifer.GetHashCode(); + hashCode = (hashCode * 397) ^ Color.GetHashCode(); + hashCode = (hashCode * 397) ^ UsePop.GetHashCode(); + hashCode = (hashCode * 397) ^ (int) ShearStrengthModel; + hashCode = (hashCode * 397) ^ AbovePhreaticLevelMean.GetHashCode(); + hashCode = (hashCode * 397) ^ AbovePhreaticLevelDeviation.GetHashCode(); + hashCode = (hashCode * 397) ^ BelowPhreaticLevelMean.GetHashCode(); + hashCode = (hashCode * 397) ^ BelowPhreaticLevelDeviation.GetHashCode(); + hashCode = (hashCode * 397) ^ CohesionMean.GetHashCode(); + hashCode = (hashCode * 397) ^ CohesionDeviation.GetHashCode(); + hashCode = (hashCode * 397) ^ CohesionShift.GetHashCode(); + hashCode = (hashCode * 397) ^ FrictionAngleMean.GetHashCode(); + hashCode = (hashCode * 397) ^ FrictionAngleDeviation.GetHashCode(); + hashCode = (hashCode * 397) ^ FrictionAngleShift.GetHashCode(); + hashCode = (hashCode * 397) ^ ShearStrengthRatioMean.GetHashCode(); + hashCode = (hashCode * 397) ^ ShearStrengthRatioDeviation.GetHashCode(); + hashCode = (hashCode * 397) ^ ShearStrengthRatioShift.GetHashCode(); + hashCode = (hashCode * 397) ^ StrengthIncreaseExponentMean.GetHashCode(); + hashCode = (hashCode * 397) ^ StrengthIncreaseExponentDeviation.GetHashCode(); + hashCode = (hashCode * 397) ^ StrengthIncreaseExponentShift.GetHashCode(); + hashCode = (hashCode * 397) ^ PopMean.GetHashCode(); + hashCode = (hashCode * 397) ^ PopDeviation.GetHashCode(); + hashCode = (hashCode * 397) ^ PopShift.GetHashCode(); + return hashCode; + } + } + + private bool Equals(SoilLayerProperties other) + { + return string.Equals(materialName, other.materialName, StringComparison.InvariantCulture) + && IsAquifer == other.IsAquifer + && Color.Equals(other.Color) + && UsePop == other.UsePop + && ShearStrengthModel == other.ShearStrengthModel + && AbovePhreaticLevelMean.Equals(other.AbovePhreaticLevelMean) + && AbovePhreaticLevelDeviation.Equals(other.AbovePhreaticLevelDeviation) + && BelowPhreaticLevelMean.Equals(other.BelowPhreaticLevelMean) + && BelowPhreaticLevelDeviation.Equals(other.BelowPhreaticLevelDeviation) + && CohesionMean.Equals(other.CohesionMean) + && CohesionDeviation.Equals(other.CohesionDeviation) + && CohesionShift.Equals(other.CohesionShift) + && FrictionAngleMean.Equals(other.FrictionAngleMean) + && FrictionAngleDeviation.Equals(other.FrictionAngleDeviation) + && FrictionAngleShift.Equals(other.FrictionAngleShift) + && ShearStrengthRatioMean.Equals(other.ShearStrengthRatioMean) + && ShearStrengthRatioDeviation.Equals(other.ShearStrengthRatioDeviation) + && ShearStrengthRatioShift.Equals(other.ShearStrengthRatioShift) + && StrengthIncreaseExponentMean.Equals(other.StrengthIncreaseExponentMean) + && StrengthIncreaseExponentDeviation.Equals(other.StrengthIncreaseExponentDeviation) + && StrengthIncreaseExponentShift.Equals(other.StrengthIncreaseExponentShift) + && PopMean.Equals(other.PopMean) + && PopDeviation.Equals(other.PopDeviation) + && PopShift.Equals(other.PopShift); + } } } \ No newline at end of file Index: Ringtoets/MacroStabilityInwards/test/Ringtoets.MacroStabilityInwards.Primitives.Test/MacroStabilityInwardsSoilLayerTest.cs =================================================================== diff -u -r9caa7daa14d0070e2a3f68c4b9e66109318bd9ab -rf2234f62cc9b8e06b06c39acc6e0754d85366ac7 --- Ringtoets/MacroStabilityInwards/test/Ringtoets.MacroStabilityInwards.Primitives.Test/MacroStabilityInwardsSoilLayerTest.cs (.../MacroStabilityInwardsSoilLayerTest.cs) (revision 9caa7daa14d0070e2a3f68c4b9e66109318bd9ab) +++ Ringtoets/MacroStabilityInwards/test/Ringtoets.MacroStabilityInwards.Primitives.Test/MacroStabilityInwardsSoilLayerTest.cs (.../MacroStabilityInwardsSoilLayerTest.cs) (revision f2234f62cc9b8e06b06c39acc6e0754d85366ac7) @@ -165,9 +165,7 @@ var random = new Random(randomSeed); return new MacroStabilityInwardsSoilLayer(random.NextDouble()) { - MaterialName = string.Join("", Enumerable.Repeat('x', random.Next(0, 40))), - Color = Color.FromKnownColor(random.NextEnumValue()), - IsAquifer = random.NextBoolean() + Color = Color.FromKnownColor(random.NextEnumValue()) }; } } Index: Ringtoets/MacroStabilityInwards/test/Ringtoets.MacroStabilityInwards.Primitives.Test/SoilLayerPropertiesTest.cs =================================================================== diff -u -r9caa7daa14d0070e2a3f68c4b9e66109318bd9ab -rf2234f62cc9b8e06b06c39acc6e0754d85366ac7 --- Ringtoets/MacroStabilityInwards/test/Ringtoets.MacroStabilityInwards.Primitives.Test/SoilLayerPropertiesTest.cs (.../SoilLayerPropertiesTest.cs) (revision 9caa7daa14d0070e2a3f68c4b9e66109318bd9ab) +++ Ringtoets/MacroStabilityInwards/test/Ringtoets.MacroStabilityInwards.Primitives.Test/SoilLayerPropertiesTest.cs (.../SoilLayerPropertiesTest.cs) (revision f2234f62cc9b8e06b06c39acc6e0754d85366ac7) @@ -20,7 +20,10 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.Drawing; +using System.Linq; +using Core.Common.TestUtil; using NUnit.Framework; namespace Ringtoets.MacroStabilityInwards.Primitives.Test @@ -99,5 +102,137 @@ // Assert Assert.AreEqual(materialName, layer.MaterialName); } + + [Test] + public void Equals_Null_ReturnsFalse() + { + // Setup + SoilLayerProperties layer = CreateRandomProperties(21); + + // Call + bool areEqual = layer.Equals(null); + + // Assert + Assert.IsFalse(areEqual); + } + + private static IEnumerable ChangeSingleProperties() + { + yield return new TestCaseData(new Action(lp => lp.ShearStrengthModel = (ShearStrengthModel) 9)); + yield return new TestCaseData(new Action(lp => lp.MaterialName = "interesting")); + yield return new TestCaseData(new Action(lp => lp.IsAquifer = !lp.IsAquifer)); + yield return new TestCaseData(new Action(lp => lp.UsePop = !lp.IsAquifer)); + yield return new TestCaseData(new Action(lp => lp.Color = lp.Color == Color.Aqua ? Color.Bisque : Color.Aqua)); + yield return new TestCaseData(new Action(lp => lp.AbovePhreaticLevelMean = 1.0 - lp.AbovePhreaticLevelMean)); + yield return new TestCaseData(new Action(lp => lp.AbovePhreaticLevelDeviation = 1.0 - lp.AbovePhreaticLevelDeviation)); + yield return new TestCaseData(new Action(lp => lp.BelowPhreaticLevelMean = 1.0 - lp.BelowPhreaticLevelMean)); + yield return new TestCaseData(new Action(lp => lp.BelowPhreaticLevelDeviation = 1.0 - lp.BelowPhreaticLevelDeviation)); + yield return new TestCaseData(new Action(lp => lp.CohesionMean = 1.0 - lp.CohesionMean)); + yield return new TestCaseData(new Action(lp => lp.CohesionDeviation = 1.0 - lp.CohesionDeviation)); + yield return new TestCaseData(new Action(lp => lp.CohesionShift = 1.0 - lp.CohesionShift)); + yield return new TestCaseData(new Action(lp => lp.FrictionAngleMean = 1.0 - lp.FrictionAngleMean)); + yield return new TestCaseData(new Action(lp => lp.FrictionAngleDeviation = 1.0 - lp.FrictionAngleDeviation)); + yield return new TestCaseData(new Action(lp => lp.FrictionAngleShift = 1.0 - lp.FrictionAngleShift)); + yield return new TestCaseData(new Action(lp => lp.ShearStrengthRatioMean = 1.0 - lp.ShearStrengthRatioMean)); + yield return new TestCaseData(new Action(lp => lp.ShearStrengthRatioDeviation = 1.0 - lp.ShearStrengthRatioDeviation)); + yield return new TestCaseData(new Action(lp => lp.ShearStrengthRatioShift = 1.0 - lp.ShearStrengthRatioShift)); + yield return new TestCaseData(new Action(lp => lp.StrengthIncreaseExponentMean = 1.0 - lp.StrengthIncreaseExponentMean)); + yield return new TestCaseData(new Action(lp => lp.StrengthIncreaseExponentDeviation = 1.0 - lp.StrengthIncreaseExponentDeviation)); + yield return new TestCaseData(new Action(lp => lp.StrengthIncreaseExponentShift = 1.0 - lp.StrengthIncreaseExponentShift)); + yield return new TestCaseData(new Action(lp => lp.PopMean = 1.0 - lp.PopMean)); + yield return new TestCaseData(new Action(lp => lp.PopDeviation = 1.0 - lp.PopDeviation)); + yield return new TestCaseData(new Action(lp => lp.PopShift = 1.0 - lp.PopShift)); + } + + [Test] + [TestCaseSource(nameof(ChangeSingleProperties))] + public void Equals_ChangeSingleProperty_ReturnsFalse(Action changeProperty) + { + // Setup + SoilLayerProperties layer = CreateRandomProperties(21); + SoilLayerProperties layerToChange = CreateRandomProperties(21); + + changeProperty(layerToChange); + + // Call + bool areEqualOne = layer.Equals(layerToChange); + bool areEqualTwo = layerToChange.Equals(layer); + + // Assert + Assert.IsFalse(areEqualOne); + Assert.IsFalse(areEqualTwo); + } + + [Test] + [TestCaseSource(nameof(PropertiesCombinations))] + public void Equals_DifferentScenarios_ReturnsExpectedResult(SoilLayerProperties layer, SoilLayerProperties otherLayer, bool expectedEqual) + { + // Call + bool areEqualOne = layer.Equals(otherLayer); + bool areEqualTwo = otherLayer.Equals(layer); + + // Assert + Assert.AreEqual(expectedEqual, areEqualOne); + Assert.AreEqual(expectedEqual, areEqualTwo); + } + + private static TestCaseData[] PropertiesCombinations() + { + SoilLayerProperties propertiesA = CreateRandomProperties(21); + SoilLayerProperties propertiesB = CreateRandomProperties(21); + SoilLayerProperties propertiesC = CreateRandomProperties(73); + + return new[] + { + new TestCaseData(propertiesA, propertiesA, true) + { + TestName = "Equals_LayerALayerA_True" + }, + new TestCaseData(propertiesA, propertiesB, true) + { + TestName = "Equals_LayerALayerB_True" + }, + new TestCaseData(propertiesB, propertiesC, false) + { + TestName = "Equals_LayerBLayerC_False" + }, + new TestCaseData(propertiesC, propertiesC, true) + { + TestName = "Equals_LayerCLayerC_True" + } + }; + } + + private static SoilLayerProperties CreateRandomProperties(int randomSeed) + { + var random = new Random(randomSeed); + return new SoilLayerProperties + { + MaterialName = string.Join("", Enumerable.Repeat('x', random.Next(0, 40))), + Color = Color.FromKnownColor(random.NextEnumValue()), + IsAquifer = random.NextBoolean(), + UsePop = random.NextBoolean(), + ShearStrengthModel = random.NextEnumValue(), + AbovePhreaticLevelMean = random.NextDouble(), + AbovePhreaticLevelDeviation = random.NextDouble(), + BelowPhreaticLevelMean = random.NextDouble(), + BelowPhreaticLevelDeviation = random.NextDouble(), + CohesionMean = random.NextDouble(), + CohesionDeviation = random.NextDouble(), + CohesionShift = random.NextDouble(), + FrictionAngleMean = random.NextDouble(), + FrictionAngleDeviation = random.NextDouble(), + FrictionAngleShift = random.NextDouble(), + ShearStrengthRatioMean = random.NextDouble(), + ShearStrengthRatioDeviation = random.NextDouble(), + ShearStrengthRatioShift = random.NextDouble(), + StrengthIncreaseExponentMean = random.NextDouble(), + StrengthIncreaseExponentDeviation = random.NextDouble(), + StrengthIncreaseExponentShift = random.NextDouble(), + PopMean = random.NextDouble(), + PopDeviation = random.NextDouble(), + PopShift = random.NextDouble() + }; + } } } \ No newline at end of file