Пример #1
0
            public void DetectsNotEqualMultipleIfOrderedIncorrectly()
            {
                //Arrange
                var first = new List <ComparableObject>()
                {
                    new ComparableObject()
                    {
                        FirstName = "Test1", LastName = "User1"
                    },
                    new ComparableObject()
                    {
                        FirstName = "Test", LastName = "User"
                    }
                };
                var second = new List <ComparableObject>()
                {
                    new ComparableObject()
                    {
                        FirstName = "Test", LastName = "User"
                    },
                    new ComparableObject()
                    {
                        FirstName = "Test1", LastName = "User1"
                    }
                };

                //Act
                var result = EnumerableComparer.SequenceEqual(first, second, t => t.FirstName, t => t.LastName);

                //Assert
                Assert.False(result);
            }
Пример #2
0
 private static void AssertInequalities(string[] subject)
 {
     var reference = new[] { "foo", "bar", "baz" };
     IComparer<IEnumerable<string>> comparer = new EnumerableComparer<string>();
     Assert.AreEqual(-1, comparer.Compare(reference, subject));
     Assert.AreEqual(1, comparer.Compare(subject, reference));
 }
Пример #3
0
        private static int CountSegmentPermutations(IEnumerable <int> input)
        {
            var dataToProcess = new List <IEnumerable <int> > {
                input
            };
            var permutations = new List <IEnumerable <int> >();

            while (dataToProcess.Any())
            {
                var data = dataToProcess[0];
                dataToProcess.RemoveAt(0);
                permutations.Add(data);

                if (data.Count() <= 2)
                {
                    continue;
                }

                for (int i = 1; i < data.Count() - 1; i++)
                {
                    var el1 = data.ElementAt(i - 1);
                    var el3 = data.ElementAt(i + 1);

                    if (el3 - el1 <= 3)
                    {
                        dataToProcess.Add(data.Take(i).Concat(data.Skip(i + 1)));
                    }
                }
            }

            var comparer = new EnumerableComparer <int>();

            return(permutations.Distinct(comparer).Count());
        }
Пример #4
0
            public void DetectsEqualMultipleInFirst()
            {
                //Arrange
                var first = new List <ComparableObject>()
                {
                    new ComparableObject()
                    {
                        FirstName = "Test", LastName = "User"
                    },
                    new ComparableObject()
                    {
                        FirstName = "Test", LastName = "User"
                    }
                };
                var second = new List <ComparableObject>()
                {
                    new ComparableObject()
                    {
                        FirstName = "Test", LastName = "User"
                    }
                };

                //Act
                var result = EnumerableComparer.SequenceEqual(first, second, (first, second) => first.FirstName == second.FirstName && first.LastName == second.LastName);

                //Assert
                Assert.False(result);
            }
Пример #5
0
            public void CorrectlyDetectsChangesForUpdates()
            {
                //Arrange
                var oldList = new List <SampleTestClass>()
                {
                    new SampleTestClass()
                    {
                        Id = 1, Content = "old1"
                    }
                };

                var newList = new List <SampleTestClass>()
                {
                    new SampleTestClass()
                    {
                        Id = 1, Content = "new2"
                    }
                };

                //Act
                var result = EnumerableComparer.CompareEnumerables(oldList, newList, (item) => item.Id);

                //Assert
                Assert.Empty(result.Added);
                Assert.Empty(result.Removed);
                Assert.Single(result.Updated);

                var updatedItem = result.Updated.Single();

                Assert.Equal("old1", updatedItem.Old.Content);
                Assert.Equal("new2", updatedItem.New.Content);
            }
Пример #6
0
 public int GetHashCode(IEnumerable <IEnumerable <int> > partition)
 {
     return(partition.OrderBy(part => part.Min()).Aggregate(17,
                                                            (current, part) =>
                                                            current * 23 +
                                                            EnumerableComparer <int> .GetEnumerableHashCode(part)));
 }
Пример #7
0
            public void CorrectlyDetectsChangesForMultipleThings()
            {
                //Arrange
                var oldList = new List <SampleTestClass>()
                {
                    new SampleTestClass()
                    {
                        Id = 1, Content = "old1"
                    },
                    new SampleTestClass()
                    {
                        Id = 2, Content = "old2"
                    },
                    new SampleTestClass()
                    {
                        Id = 3, Content = "old3"
                    }
                };

                var newList = new List <SampleTestClass>()
                {
                    new SampleTestClass()
                    {
                        Id = 2, Content = "new2"
                    },
                    new SampleTestClass()
                    {
                        Id = 3, Content = "new3"
                    },
                    new SampleTestClass()
                    {
                        Id = 4, Content = "new4"
                    },
                    new SampleTestClass()
                    {
                        Id = 5, Content = "new5"
                    }
                };

                //Act
                var result = EnumerableComparer.CompareEnumerables(oldList, newList, (item) => item.Id);

                //Assert
                Assert.Equal(2, result.Added.Count);
                Assert.Equal("new4", result.Added[0].Content);
                Assert.Equal("new5", result.Added[1].Content);

                Assert.Single(result.Removed);
                Assert.Equal("old1", result.Removed[0].Content);

                Assert.Equal(2, result.Updated.Count);
                var updated1 = result.Updated[0];
                var updated2 = result.Updated[1];

                Assert.Equal("old2", updated1.Old.Content);
                Assert.Equal("new2", updated1.New.Content);
                Assert.Equal("old3", updated2.Old.Content);
                Assert.Equal("new3", updated2.New.Content);
            }
Пример #8
0
        public int GetHashCode(SizeOfPrototype obj)
        {
            var hash = EnumerableComparer.EmptyHash;

            hash = EnumerableComparer.FoldIntoHashCode(hash, obj.MeasuredType.GetHashCode());
            hash = EnumerableComparer.FoldIntoHashCode(hash, obj.ResultType.GetHashCode());
            return(hash);
        }
Пример #9
0
        public int GetHashCode(LoadPrototype obj)
        {
            var hash = EnumerableComparer.EmptyHash;

            hash = EnumerableComparer.FoldIntoHashCode(hash, obj.ResultType.GetHashCode());
            hash = EnumerableComparer.FoldIntoHashCode(hash, obj.IsVolatile);
            hash = EnumerableComparer.FoldIntoHashCode(hash, obj.Alignment);
            return(hash);
        }
Пример #10
0
        public int GetHashCode(ClrModifierType obj)
        {
            var hashCode = EnumerableComparer.EmptyHash;

            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.ElementType);
            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.ModifierType);
            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.IsRequired);
            return(hashCode);
        }
Пример #11
0
 public IndividualDiffBuilder(
     ContainerDiffBuilder containerDiffBuilder,
     IObjectComparer comparer,
     EnumerableComparer enumerableComparer)
 {
     _containerDiffBuilder = containerDiffBuilder;
     _comparer             = comparer;
     _enumerableComparer   = enumerableComparer;
 }
Пример #12
0
        /// <inheritdoc/>
        public override int GetHashCode()
        {
            var hashCode = EnumerableComparer.HashEnumerable(Arguments);

            hashCode = EnumerableComparer.FoldIntoHashCode(
                hashCode,
                Target.GetHashCode());
            return(hashCode);
        }
Пример #13
0
        public int GetHashCode(NewDelegatePrototype obj)
        {
            int hashCode = EnumerableComparer.EmptyHash;

            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.ResultType);
            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.Callee);
            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.HasThisArgument);
            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.Lookup);
            return(hashCode);
        }
Пример #14
0
        public void EnumerableEqualityComparerReturnsTrueIfFirstAndSecondAreBothNull()
        {
            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> first  = null;
            IEnumerable <int> second = null;

            bool result = comparer.Equals(first, second);

            Assert.IsTrue(result);
        }
Пример #15
0
        public void EnumerableEqualityComparerGetHashCodeReturnsDefaultHashCodeIfSequenceIsEmpty()
        {
            const int expectedHashCode = 23;

            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> sequence = new List <int>();

            int result = comparer.GetHashCode(sequence);

            Assert.AreEqual(expectedHashCode, result);
        }
Пример #16
0
        /// <summary>
        /// Computes the hash code for a particular switch flow.
        /// </summary>
        /// <param name="obj">The switch flow to compute a hash code for.</param>
        /// <returns>A hash code.</returns>
        public int GetHashCode(SwitchFlow obj)
        {
            int hashCode = EnumerableComparer.HashUnorderedSet(obj.Cases);

            hashCode = EnumerableComparer.FoldIntoHashCode(
                hashCode,
                obj.DefaultBranch.GetHashCode());
            hashCode = EnumerableComparer.FoldIntoHashCode(
                hashCode,
                obj.SwitchValue.GetHashCode());
            return(hashCode);
        }
Пример #17
0
        /// <inheritdoc/>
        public override int GetHashCode()
        {
            int hashCode = EnumerableComparer.EmptyHash;
            int argCount = Arguments.Count;

            for (int i = 0; i < argCount; i++)
            {
                hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, Arguments[i]);
            }
            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, Prototype);
            return(hashCode);
        }
Пример #18
0
        /// <inheritdoc/>
        public override int GetHashCode()
        {
            var hashCode = EnumerableComparer.EmptyHash;

            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, Name);
            foreach (var kvPair in Annotations.OrderBy(pair => pair.Key))
            {
                hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, kvPair.Key);
                hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, kvPair.Value);
            }
            return(hashCode);
        }
Пример #19
0
        public void EnumerableEqualityComparerGetHashCodeReturnsZeroIfSequenceIsNull()
        {
            const int expectedHashCode = 0;

            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> sequence = null;

            int result = comparer.GetHashCode(sequence);

            Assert.AreEqual(expectedHashCode, result);
        }
Пример #20
0
        public void EnumerableEqualityComparerReturnsTrueIfSequencesAreSameReference()
        {
            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> first = new List <int> {
                1, 2, 3, 4, 5
            };
            IEnumerable <int> second = first;

            bool result = comparer.Equals(first, second);

            Assert.IsTrue(result);
        }
Пример #21
0
        public void EnumerableEqualityComparerReturnsFalseIfSecondListIsNull()
        {
            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> first = new List <int> {
                1, 2, 3, 4, 5
            };
            IEnumerable <int> second = null;

            bool result = comparer.Equals(first, second);

            Assert.IsFalse(result);
        }
Пример #22
0
        public void EnumerableEqualityComparerReturnsFalseIfFirstIsGreaterLengthThanSecond()
        {
            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> first = new List <int> {
                1, 2, 3, 4, 5
            };
            IEnumerable <int> second = new List <int>();

            bool result = comparer.Equals(first, second);

            Assert.IsFalse(result);
        }
Пример #23
0
        public int GetHashCode(IndirectCallPrototype obj)
        {
            int hashCode = EnumerableComparer.EmptyHash;

            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.ResultType);
            var paramTypes     = obj.ParameterTypes;
            var paramTypeCount = paramTypes.Count;

            for (int i = 0; i < paramTypeCount; i++)
            {
                hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, paramTypes[i]);
            }
            return(hashCode);
        }
        public int Compare(T x, T y)
        {
            var enumerableComparer = new EnumerableComparer();

            if (enumerableComparer.CanCompare(x, y))
            {
                return(enumerableComparer.Compare((IEnumerable)x, (IEnumerable)y));
            }

            var nullComparer = new NullComparer();

            if (nullComparer.CanCompare(x, y))
            {
                return(nullComparer.Compare(x, y));
            }

            // Same type?
            if (x.GetType() != y.GetType())
            {
                return(-1);
            }

            // Implements IComparable<T>?
            IComparable <T> comparable1 = x as IComparable <T>;

            if (comparable1 != null)
            {
                return(comparable1.CompareTo(y));
            }

            // Implements IComparable?
            IComparable comparable2 = x as IComparable;

            if (comparable2 != null)
            {
                return(comparable2.CompareTo(y));
            }

            // Implements IEquatable<T>?
            IEquatable <T> equatable = x as IEquatable <T>;

            if (equatable != null)
            {
                return(equatable.Equals(y) ? 0 : -1);
            }

            // Last case, rely on Object.Equals
            return(object.Equals(x, y) ? 0 : -1);
        }
Пример #25
0
        public void EnumerableEqualityComparerReturnsFalseIfFirstAndSecondSequenceElementsAreNotTheSame()
        {
            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> first = new List <int> {
                1, 2, 3, 4, 5
            };
            IEnumerable <int> second = new List <int> {
                6, 7, 8, 9, 10
            };

            bool result = comparer.Equals(first, second);

            Assert.IsFalse(result);
        }
Пример #26
0
        public void EnumerableEqualityComparerReturnsFalseIfFirstAndSecondSequenceElementsAreTheSameButDifferentOrder()
        {
            var comparer = new EnumerableComparer <int>();

            IEnumerable <int> first = new List <int> {
                1, 2, 3, 4, 5
            };
            IEnumerable <int> second = new List <int> {
                5, 4, 3, 2, 1
            };

            bool result = comparer.Equals(first, second);

            Assert.IsFalse(result);
        }
Пример #27
0
        /// <summary>
        /// Tries to update the property that the selector uses.
        /// </summary>
        /// <param name="newDocument">
        /// The new version of the document. Only non-null items are updated in the current document.
        /// </param>
        /// <param name="delta">
        /// The delta document. Has all changes that differ from the document.
        /// </param>
        /// <param name="selector">
        /// Returns the <see cref="IEnumerable{T}"/> to update.
        /// </param>
        /// <typeparam name="T">
        /// The type of the items in the selector.
        /// </typeparam>
        /// <returns>
        /// True if the document was updated, false otherwise.
        /// </returns>
        private bool TryUpdate <T>(SerializableDocumentDescriptor newDocument, SerializableDocumentDescriptor delta, [NotNull] Expression <Func <SerializableDocumentDescriptor, IReadOnlyList <T> > > selector)
        {
            var propertyInfo  = (PropertyInfo)((MemberExpression)selector.Body).Member;
            var getValue      = selector.Compile();
            var newValue      = getValue(newDocument);
            var existingValue = getValue(this.Descriptor);

            if (newValue == null || existingValue != null && EnumerableComparer.Equals(newValue, existingValue))
            {
                return(false);
            }

            propertyInfo.SetValue(delta, newValue);
            propertyInfo.SetValue(this.Descriptor, newValue);

            return(true);
        }
Пример #28
0
        public void EnumerableEqualityComparerGetHashCodeReturnsSameHashCodeIfSequenceContainsNullElements()
        {
            var comparer = new EnumerableComparer <int?>();

            IEnumerable <int?> sequence = new List <int?> {
                1, 2, null, 4, 5
            };

            //get the HashCode 2000 times to confirm it is consistent for lifetime of the enumerable
            HashSet <int> hashes = new HashSet <int>();

            for (int i = 0; i < 2000; i++)
            {
                hashes.Add(comparer.GetHashCode(sequence));
            }

            Assert.AreEqual(1, hashes.Count);
        }
Пример #29
0
        public int GetHashCode(InstructionPattern obj)
        {
            int hashCode = EnumerableComparer.EmptyHash;

            hashCode = EnumerableComparer.FoldIntoHashCode(
                hashCode,
                obj.PrototypeKind.GetHashCode());

            var numbering = new Dictionary <Symbol, int>();

            foreach (var arg in obj.PrototypeArgs)
            {
                hashCode = EnumerableComparer.FoldIntoHashCode(
                    hashCode,
                    HashArg(arg, numbering));
            }

            return(hashCode);
        }
Пример #30
0
        /// <inheritdoc/>
        public override int GetHashCode()
        {
            int hashCode = EnumerableComparer.EmptyHash;

            hashCode = EnumerableComparer.FoldIntoHashCode(
                hashCode,
                Name.GetHashCode());

            hashCode = EnumerableComparer.FoldIntoHashCode(
                hashCode,
                PropertyType.GetHashCode());

            foreach (var type in IndexerParameterTypes)
            {
                hashCode = EnumerableComparer.FoldIntoHashCode(
                    hashCode,
                    type.GetHashCode());
            }
            return(hashCode);
        }
Пример #31
0
        /// <summary>
        /// Computes a hash code for an instruction.
        /// </summary>
        /// <param name="obj">The instruction to hash.</param>
        /// <returns>A hash code.</returns>
        public int GetHashCode(Instruction obj)
        {
            // Compute a hash code for the instruction based on its
            // prototype and the value numbers of its arguments.
            // TODO: this implementation of GetHashCode will always produce
            // a collision for non-copyable instructions (e.g., calls).
            // Is there something we can do about this?

            int hashCode = EnumerableComparer.EmptyHash;
            int argCount = obj.Arguments.Count;

            for (int i = 0; i < argCount; i++)
            {
                hashCode = EnumerableComparer.FoldIntoHashCode(
                    hashCode,
                    numbering.GetNumber(obj.Arguments[i]));
            }
            hashCode = EnumerableComparer.FoldIntoHashCode(hashCode, obj.Prototype);
            return(hashCode);
        }
Пример #32
0
 private void Return_false(EnumerableComparer obj)
 {
     _result.ShouldBeFalse();
 }
Пример #33
0
 private static void AssertCompareEqual(string[] x)
 {
     var y = x.Clone() as string[];
     IComparer<IEnumerable<string>> comparer = new EnumerableComparer<string>();
     Assert.AreEqual(0, comparer.Compare(x, y));
 }
Пример #34
0
 private void Return_true(EnumerableComparer obj)
 {
     _result.ShouldBeTrue();
 }
Пример #35
0
            private void Same_distinct_items_that_occur_a_different_number_of_times(EnumerableComparer obj)
            {
                _listA = new List<int>
                    {
                        4,
                        5,
                        5,
                        6
                    };

                _listB = new ArrayList
                    {
                        4,
                        5,
                        6,
                        6
                    };
            }
Пример #36
0
            private void Same_number_and_kind_of_items_in_different_order(EnumerableComparer obj)
            {
                _listA = new List<int>
                    {
                        4,
                        5,
                        6
                    };

                _listB = new ArrayList
                    {
                        6,
                        5,
                        4
                    };
            }
Пример #37
0
            private void Same_total_items_but_different_distinct_items(EnumerableComparer obj)
            {
                _listA = new List<int>
                    {
                        4,
                        5,
                        6
                    };

                _listB = new ArrayList
                    {
                        5,
                        6,
                        7
                    };
            }
Пример #38
0
 private void Asked_if_two_inputs_have_the_same_contents(EnumerableComparer obj)
 {
     _result = obj.HaveSameContents(_listA, _listB);
 }
Пример #39
0
            private void Different_total_items(EnumerableComparer obj)
            {
                _listA = new List<int>
                    {
                        4,
                        5,
                        6
                    };

                _listB = new ArrayList
                    {
                        5,
                        6
                    };
            }