From 7b6092c291b10a11c14ea46b7ad5172f55b71c39 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 13 Jun 2019 17:35:03 -0400 Subject: [PATCH] fix: Fixing issues with Remap methods, added unit tests --- .../Attributes/RangeAttributeTest.cs | 4 +++ ICD.Common.Utils/Attributes/RangeAttribute.cs | 31 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ICD.Common.Utils.Tests/Attributes/RangeAttributeTest.cs b/ICD.Common.Utils.Tests/Attributes/RangeAttributeTest.cs index c53793f..b702d58 100644 --- a/ICD.Common.Utils.Tests/Attributes/RangeAttributeTest.cs +++ b/ICD.Common.Utils.Tests/Attributes/RangeAttributeTest.cs @@ -67,11 +67,15 @@ namespace ICD.Common.Utils.Tests.Attributes Assert.AreEqual(expected, RangeAttribute.Remap(value, type)); } + [TestCase(0, 100, ushort.MaxValue, typeof(ushort), ushort.MaxValue)] + [TestCase(0, 100, ushort.MaxValue, typeof(short), short.MaxValue)] public void ClampMinMaxThenRemapTest(object min, object max, object value, Type type, object expected) { Assert.AreEqual(expected, new RangeAttribute(min, max).ClampMinMaxThenRemap(value, type)); } + [TestCase(0, 100, ushort.MaxValue, 100)] + [TestCase(0, 100, ushort.MinValue, 0)] public void RemapMinMaxTest(object min, object max, object value, object expected) { Assert.AreEqual(expected, new RangeAttribute(min, max).RemapMinMax(value)); diff --git a/ICD.Common.Utils/Attributes/RangeAttribute.cs b/ICD.Common.Utils/Attributes/RangeAttribute.cs index ac304f5..db53ce9 100644 --- a/ICD.Common.Utils/Attributes/RangeAttribute.cs +++ b/ICD.Common.Utils/Attributes/RangeAttribute.cs @@ -413,7 +413,7 @@ namespace ICD.Common.Utils.Attributes double doubleValue = Convert.ToDouble(value); double clamped = MathUtils.Clamp(doubleValue, min, max); - object remapped = Remap(clamped, type); + object remapped = RemapMinMax(clamped, type); return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture); } @@ -441,7 +441,34 @@ namespace ICD.Common.Utils.Attributes double remapped = MathUtils.MapRange(sourceMin, sourceMax, targetMin, targetMax, doubleValue); - return Convert.ChangeType(remapped, Min.GetType(), CultureInfo.InvariantCulture); + return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture); + } + + private object RemapMinMax(object value, Type type) + { + if (value == null) + throw new ArgumentNullException("value"); + + if (type == null) + throw new ArgumentNullException("type"); + + if (!type.IsNumeric()) + throw new ArgumentException("Target type is not numeric"); + + if (!value.GetType().IsNumeric()) + throw new ArgumentException("Source value is not numeric"); + + double sourceMin = Convert.ToDouble(Min); + double sourceMax = Convert.ToDouble(Max); + + double targetMin = GetMinAsDouble(type); + double targetMax = GetMaxAsDouble(type); + + double doubleValue = Convert.ToDouble(value); + + double remapped = MathUtils.MapRange(sourceMin, sourceMax, targetMin, targetMax, doubleValue); + + return Convert.ChangeType(remapped, type, CultureInfo.InvariantCulture); } #endregion