コード例 #1
0
        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);
        }
コード例 #2
0
        public void compares_equal_ints()
        {
            var o1 = 0;
            var o2 = 0;

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

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

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

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.True);
        }
コード例 #6
0
        /// <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)));
        }
コード例 #7
0
        public void compares_different_ints()
        {
            var o1 = 1;
            var o2 = -1;

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

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

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

            Assert.That(
                StructuralEqualityComparer.StructuralEquals(o1, o2),
                Is.False);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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));
        }
コード例 #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));
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        /// <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);
        }
コード例 #23
0
        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);
        }
コード例 #24
0
        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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #27
0
        /// <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);
        }
コード例 #28
0
        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);
        }
コード例 #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);
        }
コード例 #30
0
        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);
        }
コード例 #31
0
        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);
        }
コード例 #32
0
        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));
        }
コード例 #33
0
        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));
        }
コード例 #34
0
        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);
        }
コード例 #35
0
        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);
        }
コード例 #36
0
        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);
        }
コード例 #37
0
        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);
        }
コード例 #38
0
        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);
        }
コード例 #39
0
        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);
        }