Index: Core/Common/src/Core.Common.Base/Data/RoundedDouble.cs =================================================================== diff -u -r0df7cded06f5afbac08b97e025242ba55c90ec57 -r4cb56b1d97a0dd45602a673cbc1a2a7356feae01 --- Core/Common/src/Core.Common.Base/Data/RoundedDouble.cs (.../RoundedDouble.cs) (revision 0df7cded06f5afbac08b97e025242ba55c90ec57) +++ Core/Common/src/Core.Common.Base/Data/RoundedDouble.cs (.../RoundedDouble.cs) (revision 4cb56b1d97a0dd45602a673cbc1a2a7356feae01) @@ -32,7 +32,8 @@ /// number of places. /// [TypeConverter(typeof(RoundedDoubleConverter))] - public struct RoundedDouble : IEquatable, IEquatable, IFormattable + public struct RoundedDouble : IEquatable, IEquatable, IFormattable, IComparable, + IComparable, IComparable { /// /// The maximum number of decimal places supported by this class. @@ -175,6 +176,36 @@ return ToString(null, null); } + public int CompareTo(object obj) + { + if (obj == null) + { + return 1; + } + + if (obj is RoundedDouble) + { + return CompareTo((RoundedDouble) obj); + } + + if (obj is double) + { + return CompareTo((double) obj); + } + + throw new ArgumentException("Arg must be double or RoundedDouble"); + } + + public int CompareTo(double other) + { + return value.CompareTo(other); + } + + public int CompareTo(RoundedDouble other) + { + return value.CompareTo(other.value); + } + public bool Equals(double other) { return Value.Equals(other); Index: Core/Common/test/Core.Common.Base.Test/Data/RoundedDoubleTest.cs =================================================================== diff -u -r41a37c93cb0b3e36ff7023c9f42b4e6225598b55 -r4cb56b1d97a0dd45602a673cbc1a2a7356feae01 --- Core/Common/test/Core.Common.Base.Test/Data/RoundedDoubleTest.cs (.../RoundedDoubleTest.cs) (revision 41a37c93cb0b3e36ff7023c9f42b4e6225598b55) +++ Core/Common/test/Core.Common.Base.Test/Data/RoundedDoubleTest.cs (.../RoundedDoubleTest.cs) (revision 4cb56b1d97a0dd45602a673cbc1a2a7356feae01) @@ -46,6 +46,9 @@ Assert.IsInstanceOf>(roundedDouble); Assert.IsInstanceOf>(roundedDouble); Assert.IsInstanceOf(roundedDouble); + Assert.IsInstanceOf(roundedDouble); + Assert.IsInstanceOf>(roundedDouble); + Assert.IsInstanceOf>(roundedDouble); Assert.AreEqual(numberOfDecimalPlaces, roundedDouble.NumberOfDecimalPlaces); Assert.AreEqual(0.0, roundedDouble.Value); @@ -767,5 +770,147 @@ Assert.AreEqual(result1.NumberOfDecimalPlaces, result2.NumberOfDecimalPlaces); Assert.AreEqual(result1.Value, result2.Value); } + + [Test] + public void CompareTo_Null_ReturnsExpectedResult() + { + // Setup + var roundedDouble = new RoundedDouble(1, 10); + + // Call + var result = roundedDouble.CompareTo(null); + + // Assert + Assert.AreEqual(1, result); + } + + [Test] + public void CompareTo_Object_ThrowsArgumentException() + { + // Setup + var roundedDouble = new RoundedDouble(1, 10); + + // Call + TestDelegate call = () => roundedDouble.CompareTo(new object()); + + // Assert + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, "Arg must be double or RoundedDouble"); + } + + [Test] + public void CompareTo_Itself_ReturnsZero() + { + // Setup + var roundedDouble = new RoundedDouble(1, 10); + + // Call + int result = roundedDouble.CompareTo(roundedDouble); + + // Assert + Assert.AreEqual(0, result); + } + + [Test] + [TestCase(10, 10, 0)] + [TestCase(10.000005, 10, 0)] + [TestCase(10, -10, 1)] + [TestCase(10.05, 10, 1)] + [TestCase(-10, 10, -1)] + [TestCase(10, 10.05, -1)] + [TestCase(10, 10.000005, -1)] + [TestCase(10, double.NaN, 1)] + [TestCase(double.NaN, 10, -1)] + [TestCase(double.NaN, double.NaN, 0)] + public void CompareTo_RoundedDoubleToDouble_ReturnsExpectedResult(double roundedDoubleValue, double value, + int expectedRoundedDoubleIndex) + { + // Setup + var roundedDouble = new RoundedDouble(1, roundedDoubleValue); + + // Call + int roundedDoubleResult = roundedDouble.CompareTo(value); + int doubleResult = value.CompareTo(roundedDouble); + + // Assert + Assert.AreEqual(expectedRoundedDoubleIndex, roundedDoubleResult); + Assert.AreEqual(-1*expectedRoundedDoubleIndex, doubleResult); + } + + [Test] + [TestCase(10, 10, 0)] + [TestCase(10.000005, 10, 0)] + [TestCase(10, 10.000005, 0)] + [TestCase(10.005, 10, 0)] + [TestCase(10, 10.005, 0)] + [TestCase(10, -10, 1)] + [TestCase(10.05, 10, 1)] + [TestCase(-10, 10, -1)] + [TestCase(10, 10.05, -1)] + [TestCase(10, double.NaN, 1)] + [TestCase(double.NaN, 10, -1)] + [TestCase(double.NaN, double.NaN, 0)] + public void CompareTo_RoundedDoubleToRoundedDouble_ReturnsExpectedResult(double roundedDoubleValue, double roundedDoubleValue2, + int expectedRoundedDoubleIndex) + { + // Setup + var roundedDouble1 = new RoundedDouble(1, roundedDoubleValue); + var roundedDouble2 = new RoundedDouble(1, roundedDoubleValue2); + + // Call + int roundedDouble1Result = roundedDouble1.CompareTo(roundedDouble2); + int roundedDouble2Result = roundedDouble2.CompareTo(roundedDouble1); + + // Assert + Assert.AreEqual(expectedRoundedDoubleIndex, roundedDouble1Result); + Assert.AreEqual(-1*expectedRoundedDoubleIndex, roundedDouble2Result); + } + + [Test] + [TestCase(10, 10, 10, 0)] + [TestCase(10.005, 10, 10, 0)] + [TestCase(10, 10.005, 10, 0)] + [TestCase(10, 10, 10.005, 0)] + [TestCase(10, 11, 12, -1)] + [TestCase(12, 11, 10, 1)] + public void CompareTo_TransitiveRoundedDouble_ReturnsExpectedResult(double roundedDoubleValue1, double roundedDoubleValue2, + double roundedDoubleValue3, int expectedValue) + { + // Setup + var roundedDouble1 = new RoundedDouble(1, roundedDoubleValue1); + var roundedDouble2 = new RoundedDouble(1, roundedDoubleValue2); + var roundedDouble3 = new RoundedDouble(1, roundedDoubleValue3); + + // Call + int roundedDoubleResult12 = roundedDouble1.CompareTo(roundedDouble2); + int roundedDoubleResult23 = roundedDouble2.CompareTo(roundedDouble3); + int roundedDoubleResult13 = roundedDouble1.CompareTo(roundedDouble3); + + // Assert + Assert.AreEqual(expectedValue, roundedDoubleResult12); + Assert.AreEqual(expectedValue, roundedDoubleResult23); + Assert.AreEqual(expectedValue, roundedDoubleResult13); + } + + [Test] + [TestCase(10, 10, 10, 0)] + [TestCase(10.005, 10, 10, 0)] + [TestCase(10, 11, 12, -1)] + [TestCase(12, 11, 10, 1)] + public void CompareTo_TransitiveDouble_ReturnsExpectedResult(double roundedDoubleValue, double value2, + double value3, int expectedValue) + { + // Setup + var roundedDouble1 = new RoundedDouble(1, roundedDoubleValue); + + // Call + int roundedDoubleResult12 = roundedDouble1.CompareTo(value2); + int roundedDoubleResult23 = value2.CompareTo(value3); + int roundedDoubleResult13 = roundedDouble1.CompareTo(value3); + + // Assert + Assert.AreEqual(expectedValue, roundedDoubleResult12); + Assert.AreEqual(expectedValue, roundedDoubleResult23); + Assert.AreEqual(expectedValue, roundedDoubleResult13); + } } } \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Forms/UITypeEditors/HydraulicBoundaryLocationEditor.cs =================================================================== diff -u -rcc2b4067500d40bf504c7c4b3b076061cc390af4 -r4cb56b1d97a0dd45602a673cbc1a2a7356feae01 --- Ringtoets/Common/src/Ringtoets.Common.Forms/UITypeEditors/HydraulicBoundaryLocationEditor.cs (.../HydraulicBoundaryLocationEditor.cs) (revision cc2b4067500d40bf504c7c4b3b076061cc390af4) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/UITypeEditors/HydraulicBoundaryLocationEditor.cs (.../HydraulicBoundaryLocationEditor.cs) (revision 4cb56b1d97a0dd45602a673cbc1a2a7356feae01) @@ -49,8 +49,10 @@ protected override SelectableHydraulicBoundaryLocation GetCurrentOption(ITypeDescriptorContext context) { - return new SelectableHydraulicBoundaryLocation(GetPropertiesObject(context).SelectedHydraulicBoundaryLocation, - GetPropertiesObject(context).GetReferenceLocation()); + return GetPropertiesObject(context) != null + ? new SelectableHydraulicBoundaryLocation(GetPropertiesObject(context).SelectedHydraulicBoundaryLocation, + GetPropertiesObject(context).GetReferenceLocation()) + : null; } protected override HydraulicBoundaryLocation ConvertToDomainType(object selectedItem) Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/UITypeEditors/HydraulicBoundaryLocationEditorTest.cs =================================================================== diff -u -rcc2b4067500d40bf504c7c4b3b076061cc390af4 -r4cb56b1d97a0dd45602a673cbc1a2a7356feae01 --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/UITypeEditors/HydraulicBoundaryLocationEditorTest.cs (.../HydraulicBoundaryLocationEditorTest.cs) (revision cc2b4067500d40bf504c7c4b3b076061cc390af4) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/UITypeEditors/HydraulicBoundaryLocationEditorTest.cs (.../HydraulicBoundaryLocationEditorTest.cs) (revision 4cb56b1d97a0dd45602a673cbc1a2a7356feae01) @@ -179,7 +179,7 @@ IEnumerable expectedList = locations.Select(location => new SelectableHydraulicBoundaryLocation(location, properties.ReferencePoint)) - .OrderBy(hbl => hbl.Distance.Value) + .OrderBy(hbl => hbl.Distance) .ThenBy(hbl => hbl.HydraulicBoundaryLocation.Name); CollectionAssert.AreEqual(expectedList, availableHydraulicBoundaryLocations); mockRepository.VerifyAll(); @@ -226,7 +226,7 @@ IEnumerable expectedList = locations.Select(hbl => new SelectableHydraulicBoundaryLocation(hbl, properties.ReferencePoint)) - .OrderBy(hbl => hbl.Distance.Value) + .OrderBy(hbl => hbl.Distance) .ThenBy(hbl => hbl.HydraulicBoundaryLocation.Name); CollectionAssert.AreEqual(expectedList, availableHydraulicBoundaryLocations); mockRepository.VerifyAll();