public void TryCompareTo() { int result; Assert.AreEqual(true, ComparableUtils.TryCompareTo(new SourceComparable(1), new TargetComparable(2), out result)); Assert.AreEqual(-1, result); }
public void TryCompareTo( [Values(1, 0, 5, 0, 0, -1, 1)] IComparable sourceValue, [Values(3, 1, 5, 0, -1, 0, "2")] IComparable targetValue, [Values(true, true, true, true, true, true, false)] bool canCompare, [Values(-1, -1, 0, 0, 1, -1, 0)] int expectedResult) { int result; Assert.AreEqual(canCompare, ComparableUtils.TryCompareTo(sourceValue, targetValue, out result)); Assert.AreEqual(expectedResult, result); }
/// <summary> /// Determines whether the specified value is valid. /// </summary> /// <param name="value">The value.</param> /// <returns><c>true</c> if the specified value is valid otherwise <c>false</c></returns> public override bool IsValid(object value) { if (value == null) { return(true); } IComparable comparableValue = value as IComparable; if (comparableValue == null) { return(false); } int compareResult; if (ComparableUtils.TryCompareTo(m_ValueToCompare, comparableValue, out compareResult)) { return(compareResult < 0); } return(false); }
/// <summary> /// Determines whether the specified value is valid. /// </summary> /// <param name="value">The value.</param> /// <returns><c>true</c> if the specified value is valid otherwise <c>false</c></returns> public override bool IsValid(object value) { if (value == null) { return(true); } IComparable comparableValue = value as IComparable; if (comparableValue == null) { return(false); } int compareWithFromValueResult, compareWithToValueResult; if (ComparableUtils.TryCompareTo(comparableValue, FromValue, out compareWithFromValueResult) && ComparableUtils.TryCompareTo(comparableValue, ToValue, out compareWithToValueResult)) { return(compareWithFromValueResult >= 0 && compareWithToValueResult <= 0); } return(false); }
/// <summary> /// Determines whether [are equal] [the specified values]. /// </summary> /// <param name="sourceValue">The source value.</param> /// <param name="destinationValue">The destination value.</param> /// <param name="comparer">The comparer.</param> /// <returns><c>true</c> if the specified values are equal otherwise <c>false</c></returns> private static bool AreEqual(object sourceValue, object destinationValue, IEqualityComparer comparer = null) { if (comparer != null) { return(comparer.Equals(sourceValue, destinationValue)); } IComparable sourceComparable = sourceValue as IComparable; if (sourceComparable != null) { IComparable destinationComparable = destinationValue as IComparable; if (destinationComparable != null) { int compareResult; if (ComparableUtils.TryCompareTo(sourceComparable, destinationComparable, out compareResult)) { return(compareResult == 0); } } } return(sourceValue == destinationValue); }
public void TryCompareToMustThrowArgumentNullExceptionWhenTargetValueIsNull() { int result; ComparableUtils.TryCompareTo(1, null, out result); }
public void TryCompareToMustThrowArgumentNullExceptionWhenSourceValueIsNull() { int result; ComparableUtils.TryCompareTo(null, 1, out result); }