コード例 #1
0
        public void Detects_cycle_handles_equality_by_value()
        {
            var one = new SelfSealed {
                Value = 1
            };

            one.Second = new SelfSealed {
                First = one,
                Value = 1
            };

            /*
             *    1-1
             *   / \
             *  N   2-1
             * cycle: / \
             *    1-1 N
             *   / \
             *  N   2-1
             */

            var other = new SelfSealed {
                Second = new SelfSealed {
                    First = new SelfSealed {
                        Second = new SelfSealed {
                            Value = 0
                        },
                        Value = 4
                    },
                    Value = 4
                },
                Value = 3
            };

            /*
             *    3-3
             *   / \
             *  N   4-4
             *     / \
             *    5-4 N
             *   / \
             *  N   6-6
             */

            var expected = SelfSealed.Comparer.Compare(one, other);
            var actual   = ComparerSelfSealed.Compare(one, other);

            using (new AssertionScope()) {
                expected.Should().Be(-1);
                actual.Should().Be(expected);
            }
        }
コード例 #2
0
        public void Detects_cycle_handles_equalty_by_value()
        {
            var one = new SelfSealed {
                Value = 1
            };

            one.Second = new SelfSealed {
                First = one,
                Value = 1
            };

            /*
             *    1-1
             *   / \
             *  N   2-1
             * cycle: / \
             *    1-1 N
             *   / \
             *  N   2-1
             */

            var other = new SelfSealed {
                Second = new SelfSealed {
                    First = new SelfSealed {
                        Second = new SelfSealed {
                            Value = 0
                        },
                        Value = 4
                    },
                    Value = 4
                },
                Value = 3
            };

            /*
             *    3-3
             *   / \
             *  N   4-4
             *     / \
             *    5-4 N
             *   / \
             *  N   6-6
             */

            using (new AssertionScope()) {
                ComparerSelfSealed.GetHashCode(one).Should().Be(-2015758974);
                ComparerSelfSealed.GetHashCode(other).Should().Be(-1064642813);
                ComparerSelfSealed.Equals(one, other).Should().BeFalse();
            }
        }
コード例 #3
0
        public void No_cycle_when_identical_objects_in_collection()
        {
            var one = new SelfSealed {
                Value = 1
            };

            one.Second = new SelfSealed {
                First = one,
                Value = 1
            };
            var two = new SelfSealed {
                Value = 1
            };

            two.Second = new SelfSealed {
                First = two,
                Value = 1
            };
            var x = new[] {
                one,
                one,
                one
            };

            var other = new SelfSealed {
                Second = new SelfSealed {
                    First = new SelfSealed {
                        Value = 4
                    },
                    Value = 4
                },
                Value = 3
            };
            var y = new[] {
                two,
                two,
                other
            };

            var expected = SelfSealed.Comparer.Compare(x, y);
            var comparer = new ComparerBuilder()
                           .For <SelfSealed>(c => c.IgnoreMember(o => o.Id))
                           .GetComparer <SelfSealed[]>();
            var actual = comparer.Compare(x, y);

            using (new AssertionScope()) {
                expected.Should().Be(-1);
                actual.Should().Be(-1);
            }
        }
コード例 #4
0
        public void Detects_cycle_on_second_member()
        {
            var one = new SelfSealed {
                Value = 1
            };

            one.Second = new SelfSealed {
                First = one,
                Value = 2
            };

            /*
             *    1
             *   / \
             *  N   2
             * cycle: / \
             *    1   N
             *   / \
             *  N   2
             */

            var other = new SelfSealed {
                Second = new SelfSealed {
                    First = new SelfSealed {
                        Value = 3
                    },
                    Value = 4
                },
                Value = 5
            };

            /*
             *    3
             *   / \
             *  N   4
             *     / \
             *    5   N
             *   / \
             *  N   N difference here: 2 > N
             */

            var expected = SelfSealed.Comparer.Compare(one, other);
            var actual   = ComparerSelfSealed.Compare(one, other);

            using (new AssertionScope()) {
                expected.Should().Be(1);
                actual.Should().Be(expected);
            }
        }
コード例 #5
0
        public void Cross_reference_should_fail()
        {
            var x = new SelfSealed();
            var y = new SelfSealed {
                First  = x,
                Second = x
            };

            x.First  = y;
            x.Second = y;

            using (new AssertionScope()) {
                ComparerSelfSealed.Equals(x, y).Should().BeTrue();
                ComparerSelfSealed.GetHashCode(x).Should().Be(ComparerSelfSealed.GetHashCode(y));
            }
        }
コード例 #6
0
        public void No_cycle_when_identical_objects_in_collection()
        {
            var one = new SelfSealed {
                Value = 1
            };

            one.Second = new SelfSealed {
                First = one,
                Value = 1
            };
            var two = new SelfSealed {
                Value = 1
            };

            two.Second = new SelfSealed {
                First = two,
                Value = 1
            };
            var x = new[] {
                one,
                one,
                one
            };

            var other = new SelfSealed {
                Second = new SelfSealed {
                    First = new SelfSealed {
                        Value = 4
                    },
                    Value = 4
                },
                Value = 3
            };
            var y = new[] {
                two,
                two,
                other
            };

            var comparer = new ComparerBuilder().For <SelfSealed>().GetEqualityComparer <SelfSealed[]>();

            using (new AssertionScope()) {
                comparer.Equals(x, y).Should().BeFalse();
                comparer.GetHashCode(x).Should().Be(271135911);
                comparer.GetHashCode(y).Should().Be(194708355);
            }
        }
コード例 #7
0
        public void Detects_cycle_on_second_member()
        {
            var one = new SelfSealed {
                Value = 1
            };

            one.Second = new SelfSealed {
                First = one,
                Value = 2
            };

            /*
             *    1
             *   / \
             *  N   2
             * cycle: / \
             *    1   N
             *   / \
             *  N   2
             */

            var other = new SelfSealed {
                Second = new SelfSealed {
                    First = new SelfSealed {
                        Value = 3
                    },
                    Value = 4
                },
                Value = 5
            };

            /*
             *    3
             *   / \
             *  N   4
             *     / \
             *    5   N
             *   / \
             *  N   N difference here: 2 > N
             */

            using (new AssertionScope()) {
                ComparerSelfSealed.GetHashCode(one).Should().Be(-2015759071);
                ComparerSelfSealed.GetHashCode(other).Should().Be(-1670556025);
                ComparerSelfSealed.Equals(one, other).Should().BeFalse();
            }
        }
コード例 #8
0
        public void Object_with_bigger_cycle_is_bigger()
        {
            var one = new SelfSealed();

            one.First = new SelfSealed {
                First = new SelfSealed {
                    First = one
                }
            };
            var other = new SelfSealed {
                First = one
            };

            var expected = SelfSealed.Comparer.Compare(one, other);
            var actual   = ComparerSelfSealed.Compare(one, other);

            actual.Should().Be(expected);
        }
コード例 #9
0
        public void Cross_reference_should_not_fail()
        {
            var other = new SelfSealed();
            var one   = new SelfSealed {
                First  = other,
                Second = other
            };

            other.First  = one;
            other.Second = one;

            var expected = SelfSealed.Comparer.Compare(one, other);
            var actual   = ComparerSelfSealed.Compare(one, other);

            using (new AssertionScope()) {
                expected.Should().Be(0);
                actual.Should().Be(expected);
            }
        }
コード例 #10
0
        public void Object_with_bigger_cycle_is_bigger()
        {
            var one = new SelfSealed();

            one.First = new SelfSealed {
                First = new SelfSealed {
                    First = one
                }
            };
            var other = new SelfSealed {
                First = one
            };

            using (new AssertionScope()) {
                ComparerSelfSealed.GetHashCode(one).Should().Be(1342073958);
                ComparerSelfSealed.GetHashCode(other).Should().Be(-826099324);
                ComparerSelfSealed.Equals(one, other).Should().BeFalse();
            }
        }