예제 #1
0
        /// <summary>
        /// Tests that the current Tolerance is linear with a
        /// numeric value, throwing an exception if it is not.
        /// </summary>
        private void CheckLinearAndNumeric()
        {
            if (mode != ToleranceMode.Linear)
            {
                throw new InvalidOperationException(mode == ToleranceMode.None
                    ? ModeMustFollowTolerance
                    : MultipleToleranceModes);
            }

            if (!Numerics.IsNumericType(amount))
            {
                throw new InvalidOperationException(NumericToleranceRequired);
            }
        }
예제 #2
0
        /// <summary>
        /// Compares two objects
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(object x, object y)
        {
            if (x == null)
            {
                return(y == null ? 0 : -1);
            }
            else if (y == null)
            {
                return(+1);
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.Compare(x, y));
            }

            if (x is IComparable)
            {
                return(((IComparable)x).CompareTo(y));
            }

            if (y is IComparable)
            {
                return(-((IComparable)y).CompareTo(x));
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            MethodInfo method = xType.GetMethod("CompareTo", new Type[] { yType });

            if (method != null)
            {
                return((int)method.Invoke(x, new object[] { y }));
            }

            method = yType.GetMethod("CompareTo", new Type[] { xType });
            if (method != null)
            {
                return(-(int)method.Invoke(y, new object[] { x }));
            }

            throw new ArgumentException("Neither value implements IComparable or IComparable<T>");
        }
        /// <summary>
        /// Compares two objects for equality.
        /// </summary>
        public bool ObjectsEqual(object x, object y)
        {
            this.failurePoints = new ArrayList();

            if (x == null && y == null)
            {
                return(true);
            }

            if (x == null || y == null)
            {
                return(false);
            }

            if (object.ReferenceEquals(x, y))
            {
                return(true);
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            if (xType.IsArray && yType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)x, (Array)y));
            }

            if (x is IDictionary && y is IDictionary)
            {
                return(DictionariesEqual((IDictionary)x, (IDictionary)y));
            }

            if (x is ICollection && y is ICollection)
            {
                return(CollectionsEqual((ICollection)x, (ICollection)y));
            }

            if (x is IEnumerable && y is IEnumerable && !(x is string && y is string))
            {
                return(EnumerablesEqual((IEnumerable)x, (IEnumerable)y));
            }

            if (externalComparer != null)
            {
                return(externalComparer.ObjectsEqual(x, y));
            }

            if (x is string && y is string)
            {
                return(StringsEqual((string)x, (string)y));
            }

            if (x is Stream && y is Stream)
            {
                return(StreamsEqual((Stream)x, (Stream)y));
            }

            if (x is DirectoryInfo && y is DirectoryInfo)
            {
                return(DirectoriesEqual((DirectoryInfo)x, (DirectoryInfo)y));
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.AreEqual(x, y, ref tolerance));
            }

            if (tolerance != null && tolerance.Value is TimeSpan)
            {
                TimeSpan amount = (TimeSpan)tolerance.Value;

                if (x is DateTime && y is DateTime)
                {
                    return(((DateTime)x - (DateTime)y).Duration() <= amount);
                }

                if (x is TimeSpan && y is TimeSpan)
                {
                    return(((TimeSpan)x - (TimeSpan)y).Duration() <= amount);
                }
            }

            return(x.Equals(y));
        }