Esempio n. 1
0
        public void None()
        {
            var c = new SequenceComparison <int> (Enumerable.Empty <int>(), Enumerable.Empty <int>());

            Assert.AreEqual(0, c.Stayed.Count());
            Assert.AreEqual(0, c.Added.Count());
            Assert.AreEqual(0, c.Removed.Count());
        }
Esempio n. 2
0
        public void CtorEqualityComparerNull()
        {
            var c = new SequenceComparison <int> (Enumerable.Empty <int>(), Enumerable.Empty <int>());

            Assert.AreEqual(EqualityComparer <int> .Default, c.Comparer);

            c = new SequenceComparison <int> (Enumerable.Empty <int>(), Enumerable.Empty <int>(), null);
            Assert.AreEqual(EqualityComparer <int> .Default, c.Comparer);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SequenceComparer{TElement}"/> class,
        /// using the specified or default sequence comparison rules for sequences of different lengths,
        /// and the specified or default sort-order comparer for the sequence elements.
        /// </summary>
        /// <param name="comparisonType">
        /// Indicates the sequence comparison rules to be used when comparing sequences of different lengths.
        /// If the argument is omitted, <see cref="SequenceComparison.Lexicographical"/> is used.
        /// </param>
        /// <param name="elementComparer">
        /// The sort-order comparison operation to apply to the elements of the sequences.
        /// If the argument is omitted or specified as <see langword="null"/>,
        /// the <see cref="Comparer{T}.Default"/> comparer for type <typeparamref name="TElement"/> is used.
        /// </param>
        protected internal SequenceComparer(
            SequenceComparison comparisonType    = SequenceComparison.Lexicographical,
            IComparer <TElement> elementComparer = null)
        {
            _comparisonType  = comparisonType;
            _elementComparer = elementComparer ?? Comparer <TElement> .Default;

            _lengthDominated =
                comparisonType == SequenceComparison.Shortlex ||
                comparisonType == SequenceComparison.SameLength;
        }
Esempio n. 4
0
 public static bool SequenceEquals <T>(this IEnumerable <T> @this,
                                       IEnumerable <T> another,
                                       SequenceComparison comparison          = SequenceComparison.Sequential,
                                       IEqualityComparer <T>?equalityComparer = null)
 {
     return(comparison switch
     {
         SequenceComparison.Sequential => @this.SequenceEqual(another, equalityComparer),
         SequenceComparison.Unordered => @this.OrderBy(Functions.Identity <T>()).SequenceEqual(another.OrderBy(Functions.Identity <T>()), equalityComparer), // not the fastest way, but still enough
         _ => throw new ArgumentOutOfRangeException(nameof(comparison), comparison, null)
     });
Esempio n. 5
0
        public void JustAdded()
        {
            var c = new SequenceComparison <int> (Enumerable.Empty <int>(), new int[] { 1, 2, 3, 4 });

            Assert.AreEqual(0, c.Stayed.Count());
            Assert.AreEqual(4, c.Added.Count());
            Assert.Contains(1, c.Added.ToList());
            Assert.Contains(2, c.Added.ToList());
            Assert.Contains(3, c.Added.ToList());
            Assert.Contains(4, c.Added.ToList());

            Assert.AreEqual(0, c.Removed.Count());
        }
Esempio n. 6
0
        public void EqualityComparer()
        {
            var c = new SequenceComparison <string> (new string[] { "HI", "hello" }, new string[] { "hi", "blah" },
                                                     CaseInsensitiveStringComparer.Default);

            Assert.AreEqual(1, c.Stayed.Count());
            Assert.Contains("HI", c.Stayed.ToList());

            Assert.AreEqual(1, c.Removed.Count());
            Assert.Contains("hello", c.Removed.ToList());

            Assert.AreEqual(1, c.Added.Count());
            Assert.Contains("blah", c.Added.ToList());
        }
Esempio n. 7
0
        public void AddedRemoved()
        {
            var c = new SequenceComparison <int> (new int[] { 1, 2 }, new int[] { 3, 4 });

            Assert.AreEqual(0, c.Stayed.Count());

            Assert.AreEqual(2, c.Added.Count());
            Assert.Contains(3, c.Added.ToList());
            Assert.Contains(4, c.Added.ToList());

            Assert.AreEqual(2, c.Removed.Count());
            Assert.Contains(1, c.Removed.ToList());
            Assert.Contains(2, c.Removed.ToList());
        }
Esempio n. 8
0
        public void CtorEqualityComparer()
        {
            var c = new SequenceComparison <string> (Enumerable.Empty <string>(), Enumerable.Empty <string>(), CaseInsensitiveStringComparer.Default);

            Assert.AreEqual(CaseInsensitiveStringComparer.Default, c.Comparer);
        }
 /// <summary>
 /// Creates a new instance of the <see cref="SequenceComparer{TElement}"/> class,
 /// using the specified or default sequence comparison rules for sequences of different lengths,
 /// and the specified or default sort-order comparer for the sequence elements.
 /// </summary>
 /// <typeparam name="TElement">The type of the elements of the sequences to compare.</typeparam>
 /// <param name="comparisonType">
 /// Indicates the sequence comparison rules to be used when comparing sequences of different lengths.
 /// If the argument is omitted, <see cref="SequenceComparison.Lexicographical"/> is used.
 /// </param>
 /// <param name="elementComparer">
 /// The sort-order comparison operation to apply to the elements of the sequences.
 /// If the argument is omitted or specified as <see langword="null"/>,
 /// the <see cref="Comparer{T}.Default"/> comparer for type <typeparamref name="TElement"/> is used.
 /// </param>
 /// <remarks>
 /// To get a <see cref="SequenceComparer{TElement}"/> that uses the default comparer
 /// for the sequence elements, access the
 /// <see cref="SequenceComparer{TElement}.Lexicographical"/>,
 /// <see cref="SequenceComparer{TElement}.Shortlex"/>, or
 /// <see cref="SequenceComparer{TElement}.SameLength"/> static property rather than this factory method.
 /// </remarks>
 public static SequenceComparer <TElement> Create <TElement>(
     SequenceComparison comparisonType    = SequenceComparison.Lexicographical,
     IComparer <TElement> elementComparer = null)
 {
     return(new SequenceComparer <TElement>(comparisonType, elementComparer));
 }