public void compares_different_blocks()
        {
            var o1 = UnifiedBlock.Create(
                new UnifiedExpression[] {
                UnifiedBinaryExpression.Create(
                    UnifiedVariableIdentifier.Create("n"),
                    UnifiedBinaryOperator.Create(
                        "=",
                        UnifiedBinaryOperatorKind.Assign),
                    UnifiedInt32Literal.Create(1)),
                UnifiedReturn.Create(
                    UnifiedInt32Literal.Create(2)),
            });
            var o2 = UnifiedBlock.Create(
                new UnifiedExpression[] {
                UnifiedBinaryExpression.Create(
                    UnifiedVariableIdentifier.Create("n"),
                    UnifiedBinaryOperator.Create(
                        "=",
                        UnifiedBinaryOperatorKind.Assign),
                    UnifiedInt32Literal.Create(2)),
                UnifiedReturn.Create(
                    UnifiedInt32Literal.Create(2)),
            });

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
Exemple #2
0
        public void compares_equal_ints()
        {
            var o1 = 0;
            var o2 = 0;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
Exemple #3
0
        public void compares_null_null()
        {
            object o1 = null;
            object o2 = null;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
Exemple #4
0
        public void compares_string_null()
        {
            var    o1 = "abc";
            object o2 = null;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
Exemple #5
0
        public void compares_same_strings()
        {
            var o1 = "abc";
            var o2 = o1;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
        /// <summary>
        /// 获取指定的 <see cref="DictionaryEntry"/> 中的键值的哈希代码。
        /// </summary>
        /// <param name="obj">要为其获取哈希代码的 <see cref="IEnumerable"/> 对象。</param>
        /// <param name="computed">已经计算过哈希代码的对象。</param>
        /// <returns><paramref name="obj"/> 中的键值的哈希代码。</returns>
        protected override int GetHashCodeCore(DictionaryEntry obj, ISet <object> computed)
        {
            var keyComparer   = StructuralEqualityComparer.OfType(obj.Key?.GetType());
            var valueComparer = StructuralEqualityComparer.OfType(obj.Value?.GetType());

            return(this.CombineHashCode(
                       keyComparer.GetHashCode(obj, computed),
                       valueComparer.GetHashCode(obj, computed)));
        }
Exemple #7
0
        public void compares_different_ints()
        {
            var o1 = 1;
            var o2 = -1;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
Exemple #8
0
        public void compares_different_strings()
        {
            var o1 = "abc";
            var o2 = "abcd";

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
Exemple #9
0
        public void compares_different_types()
        {
            var o1 = "abc";
            var o2 = new object();

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
Exemple #10
0
        public void compares_null_string()
        {
            object o1 = null;
            var    o2 = "abc";

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
        public void Compare_Int32_parity(int value1, int value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<int>
            {
                { x => x % 2, (x, y) => x == y }
            };

            bool result = comparer.Equals(value1, value2);
            Assert.AreEqual(expected, result);
        }
        public void GetEmailBooking_WithMissingCostCentre_Should_DefaultFieldTo_UNKNOWN(string email, decimal total)
        {
            var sut      = CreateSut();
            var actual   = sut.GetEmailBooking(email);
            var expected = new EmailBooking("UNKNOWN", total, "", "", "", "");

            var structuralEqualityComparer = new StructuralEqualityComparer();

            Assert.That(actual, Is.EqualTo(expected).Using(structuralEqualityComparer));
        }
Exemple #13
0
        public void GetDetailBooking(string email, string costCentre, string paymentMethod, string vendor, string description, string date, decimal total, decimal gst, decimal totalExcludingGst)
        {
            var sut      = CreateSut();
            var actual   = sut.GetDetailBooking(email);
            var expected = new DetailBooking(new EmailBooking(costCentre, total, paymentMethod, vendor, description, date), gst, totalExcludingGst);

            var structuralEqualityComparer = new StructuralEqualityComparer();

            Assert.That(actual, Is.EqualTo(expected).Using(structuralEqualityComparer));
        }
        public void Compare_Int32_parity(int value1, int value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer <int>
            {
                { x => x % 2, (x, y) => x == y }
            };

            bool result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
        public void Compare_null_enumerations(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<Foo>
            {
                { x => x.Children, new StructuralEqualityComparer<ChildFoo> { x => x.Tag } }
            };

            var result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
        public void compares_same_expressions_containing_null()
        {
            var o2 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"), null,
                UnifiedInt32Literal.Create(1));
            var o1 = o2;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
Exemple #17
0
        public void compares_same_stringLists()
        {
            var o1 = new List <string> {
                "1", "2"
            };
            var o2 = o1;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
Exemple #18
0
        public void compares_same_intLists()
        {
            var o1 = new List <int> {
                1, 2
            };
            var o2 = o1;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
        public void Compare_String_length_and_first_characters(string value1, string value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer <string>
            {
                { x => x.Length, (x, y) => x == y },
                { x => x.Substring(0, 3), (x, y) => String.Compare(x, y, true) == 0 }
            };

            bool result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
        public void Strict_nested_comparer_on_child_enumeration(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer <Foo>
            {
                { x => x.Number },
                { x => x.Values, StructuralEqualityComparer <int> .Default }
            };

            bool result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
        public void Nested_comparer_on_child_enumeration_ignoring_order(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer <Foo>
            {
                { x => x.Number },
                { x => x.Values, StructuralEqualityComparer <int> .Default, StructuralEqualityComparerOptions.IgnoreEnumerableOrder }
            };

            bool result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
        /// <summary>
        /// 获取指定的 <see cref="IEnumerable"/> 中的元素的哈希代码。
        /// </summary>
        /// <param name="obj">要获取哈希代码的 <see cref="IEnumerable"/> 对象。</param>
        /// <param name="computed">已经计算过哈希代码的对象。</param>
        /// <returns><paramref name="obj"/> 中的元素的哈希代码。</returns>
        protected override int GetHashCodeCore(T obj, ISet <object> computed)
        {
            var hashCode = obj.GetType().GetHashCode();

            foreach (var item in obj)
            {
                var comparer     = StructuralEqualityComparer.OfType(item?.GetType());
                var nextHashCode = comparer.GetHashCode(item !, computed);
                hashCode = this.CombineHashCode(hashCode, nextHashCode);
            }
            return(hashCode);
        }
        public void compares_same_expressions()
        {
            var o2 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"),
                UnifiedBinaryOperator.Create(
                    "-", UnifiedBinaryOperatorKind.Subtract),
                UnifiedInt32Literal.Create(1));
            var o1 = o2;

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
        public void Compare_null_enumerations_ignoring_order(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer <Foo>
            {
                { x => x.Children, new StructuralEqualityComparer <ChildFoo> {
                      x => x.Tag
                  }, StructuralEqualityComparerOptions.IgnoreEnumerableOrder }
            };

            var result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
Exemple #25
0
        public void compares_different_stringLists()
        {
            var o1 = new List <string> {
                "1", "2"
            };
            var o2 = new List <string> {
                "1", "3"
            };

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
Exemple #26
0
        public void compares_different_intLists()
        {
            var o1 = new List <int> {
                1, 2
            };
            var o2 = new List <int> {
                1, 3
            };

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
        /// <summary>
        /// 获取指定的数组中的元素的哈希代码。
        /// </summary>
        /// <param name="obj">要获取哈希代码的数组。</param>
        /// <param name="computed">已经计算过哈希代码的对象。</param>
        /// <returns><paramref name="obj"/> 中的元素的哈希代码。</returns>
        protected override int GetHashCodeCore(TItem[] obj, ISet <object> computed)
        {
            var length   = obj.Length;
            var hashCode = obj.GetType().GetHashCode();

            for (int index = 0; index < length; index++)
            {
                var comparer     = StructuralEqualityComparer.OfType(obj[index]?.GetType());
                var nextHashCode = comparer.GetHashCode(obj[index] !);
                hashCode = this.CombineHashCode(hashCode, nextHashCode);
            }
            return(hashCode);
        }
        public void Compare_null_enumerations(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer <Foo>
            {
                { x => x.Children, new StructuralEqualityComparer <ChildFoo> {
                      x => x.Tag
                  } }
            };

            var result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
Exemple #29
0
        /// <summary>
        /// 获取指定的数组中的元素的哈希代码。
        /// </summary>
        /// <param name="obj">要获取哈希代码的数组。</param>
        /// <param name="computed">已经计算过哈希代码的对象。</param>
        /// <returns><paramref name="obj"/> 中的元素的哈希代码。</returns>
        protected override int GetHashCodeCore([DisallowNull] T obj, ISet <object> computed)
        {
            var array = (Array)(object)obj;

            var hashCode = array.GetType().GetHashCode();

            for (int index = 0; index < array.Length; index++)
            {
                var item     = array.GetValue(array.OffsetToIndices(index));
                var comparer = StructuralEqualityComparer.OfType(item?.GetType());
                hashCode = this.CombineHashCode(
                    hashCode, comparer.GetHashCode(item !, computed));
            }
            return(hashCode);
        }
        public void compares_different_expressions_containing_null()
        {
            var o1 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"), null,
                UnifiedInt32Literal.Create(1));
            var o2 = UnifiedBinaryExpression.Create(
                UnifiedVariableIdentifier.Create("n"),
                UnifiedBinaryOperator.Create(
                    null, UnifiedBinaryOperatorKind.Add),
                UnifiedInt32Literal.Create(1));

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
        public void Complex_comparers_with_nested_enumerations(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer <Foo>
            {
                { x => x.Number % 2 },
                { x => x.Text, (x, y) => String.Compare(x, y, true) == 0 },
                { x => x.Values, StructuralEqualityComparer <int> .Default, StructuralEqualityComparerOptions.IgnoreEnumerableOrder },
                { x => x.Children, new StructuralEqualityComparer <ChildFoo> {
                      { x => x.Tag }
                  }, StructuralEqualityComparerOptions.IgnoreEnumerableOrder }
            };

            bool result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
        public void Compare_null_references()
        {
            var comparer = new StructuralEqualityComparer <Foo>
            {
                { x => x.Number },
            };

            var foo = new Foo()
            {
                Number = 123, Text = "Hello", Duration = TimeSpan.FromMinutes(600)
            };

            Assert.IsFalse(comparer.Equals(foo, null));
            Assert.IsFalse(comparer.Equals(null, foo));
            Assert.IsTrue(comparer.Equals(null, null));
        }
        public void Compare_null_references()
        {
            var comparer = new StructuralEqualityComparer<Foo>
            {
                { x => x.Number },
            };

            var foo = new Foo() { Number = 123, Text = "Hello", Duration = TimeSpan.FromMinutes(600) };

            Assert.IsFalse(comparer.Equals(foo, null));
            Assert.IsFalse(comparer.Equals(null, foo));
            Assert.IsTrue(comparer.Equals(null, null));
        }
        public void Compare_null_enumerations_ignoring_order(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<Foo>
            {
                { x => x.Children, new StructuralEqualityComparer<ChildFoo> { x => x.Tag }, StructuralEqualityComparerOptions.IgnoreEnumerableOrder }
            };

            var result = comparer.Equals(value1, value2);

            Assert.AreEqual(expected, result);
        }
        public void Strict_nested_comparer_on_child_enumeration(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<Foo>
            {
                { x => x.Number },
                { x => x.Values,  StructuralEqualityComparer<int>.Default }
            };

            bool result = comparer.Equals(value1, value2);
            Assert.AreEqual(expected, result);
        }
        public void Nested_comparer_on_child_enumeration_ignoring_order(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<Foo>
            {
                { x => x.Number },
                { x => x.Values,  StructuralEqualityComparer<int>.Default, StructuralEqualityComparerOptions.IgnoreEnumerableOrder }
            };

            bool result = comparer.Equals(value1, value2);
            Assert.AreEqual(expected, result);
        }
        public void Nested_comparers_on_complex_type(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<Foo>
            {
                { x => x.Number },

                { (x, y) => String.Compare(x.Text, y.Text, true) == 0 },

                { x => x.Duration,
                    new StructuralEqualityComparer<TimeSpan>
                    {
                        { x => x.TotalHours, (x, y) => Math.Abs(x - y) <= 1 }
                    }
                }
            };

            bool result = comparer.Equals(value1, value2);
            Assert.AreEqual(expected, result);
        }
        public void Complex_comparers_with_nested_enumerations(Foo value1, Foo value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<Foo>
            {
                { x => x.Number % 2 },
                { x => x.Text, (x, y) => String.Compare(x, y, true) == 0 },
                { x => x.Values, StructuralEqualityComparer<int>.Default, StructuralEqualityComparerOptions.IgnoreEnumerableOrder },
                { x => x.Children, new StructuralEqualityComparer<ChildFoo> { { x => x.Tag } }, StructuralEqualityComparerOptions.IgnoreEnumerableOrder }
            };

            bool result = comparer.Equals(value1, value2);
            Assert.AreEqual(expected, result);
        }
        public void Compare_String_length_and_first_characters(string value1, string value2, bool expected)
        {
            var comparer = new StructuralEqualityComparer<string>
            {
                { x => x.Length, (x, y) => x == y },
                { x => x.Substring(0, 3), (x, y) => String.Compare(x, y, true) == 0 }
            };

            bool result = comparer.Equals(value1, value2);
            Assert.AreEqual(expected, result);
        }