private void Validate_IntersectWith(ISet <T> set, IEnumerable <T> enumerable)
 {
     if (set.Count == 0 || Enumerable.Count(enumerable) == 0)
     {
         set.IntersectWith(enumerable);
         Assert.Equal(0, set.Count);
     }
     else if (set == enumerable)
     {
         PooledSet <T> beforeOperation = new PooledSet <T>(set, GetIEqualityComparer());
         RegisterForDispose(beforeOperation);
         set.IntersectWith(enumerable);
         Assert.True(beforeOperation.SetEquals(set));
     }
     else
     {
         IEqualityComparer <T> comparer = GetIEqualityComparer();
         PooledSet <T>         expected = new PooledSet <T>(comparer);
         RegisterForDispose(expected);
         foreach (T value in set)
         {
             if (enumerable.Contains(value, comparer))
             {
                 expected.Add(value);
             }
         }
         set.IntersectWith(enumerable);
         Assert.Equal(expected.Count, set.Count);
         Assert.True(expected.SetEquals(set));
     }
 }
Esempio n. 2
0
        public void HashSet_Generic_Constructor_IEnumerable(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
        {
            IEnumerable <T> enumerable = CreateEnumerable(enumerableType, null, enumerableLength, 0, numberOfDuplicateElements);
            PooledSet <T>   set        = new PooledSet <T>(enumerable);

            Assert.True(set.SetEquals(enumerable));
        }
Esempio n. 3
0
        public void IEnumerable_Generic_Serialize_Deserialize(int count)
        {
            if (!SupportsSerialization)
            {
                return;
            }

            IEnumerable <T> enumerable = GenericIEnumerableFactory(count);
            var             formatter  = new BinaryFormatter();

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, enumerable);
                stream.Position = 0L;

                var copy = (IEnumerable <T>)formatter.Deserialize(stream);

                Assert.NotSame(enumerable, copy);
                Assert.Equal(enumerable.Count(), copy.Count());

                // We can't compare contents, because Dictionary/Set might not
                // return items in the same order. Use a PooledSet to compare content
                // without regard to order:
                using (var set = new PooledSet <T>(enumerable))
                {
                    Assert.True(set.SetEquals(copy), "Deserialized content differs!");
                }

                if (copy is IDisposable disposable)
                {
                    disposable.Dispose();
                }
            }
        }
Esempio n. 4
0
        public void SetComparer_SetEqualsTests()
        {
            List <T> objects = new List <T>()
            {
                CreateT(1), CreateT(2), CreateT(3), CreateT(4), CreateT(5), CreateT(6)
            };

            var set = new PooledSet <PooledSet <T> >()
            {
                RegisterForDispose(new PooledSet <T> {
                    objects[0], objects[1], objects[2]
                }),
                RegisterForDispose(new PooledSet <T> {
                    objects[3], objects[4], objects[5]
                })
            };

            RegisterForDispose(set);

            var noComparerSet = new PooledSet <PooledSet <T> >()
            {
                RegisterForDispose(new PooledSet <T> {
                    objects[0], objects[1], objects[2]
                }),
                RegisterForDispose(new PooledSet <T> {
                    objects[3], objects[4], objects[5]
                })
            };

            RegisterForDispose(noComparerSet);

            var comparerSet1 = new PooledSet <PooledSet <T> >(PooledSet <T> .CreateSetComparer())
            {
                RegisterForDispose(new PooledSet <T> {
                    objects[0], objects[1], objects[2]
                }),
                RegisterForDispose(new PooledSet <T> {
                    objects[3], objects[4], objects[5]
                })
            };

            RegisterForDispose(comparerSet1);

            var comparerSet2 = new PooledSet <PooledSet <T> >(PooledSet <T> .CreateSetComparer())
            {
                RegisterForDispose(new PooledSet <T> {
                    objects[3], objects[4], objects[5]
                }),
                RegisterForDispose(new PooledSet <T> {
                    objects[0], objects[1], objects[2]
                })
            };

            RegisterForDispose(comparerSet2);

            Assert.False(noComparerSet.SetEquals(set));
            Assert.True(comparerSet1.SetEquals(set));
            Assert.True(comparerSet2.SetEquals(set));
        }
Esempio n. 5
0
        public void HashSet_Generic_Constructor_IEnumerable_IEqualityComparer(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
        {
            IEnumerable <T> enumerable = CreateEnumerable(enumerableType, null, enumerableLength, 0, 0);
            PooledSet <T>   set        = new PooledSet <T>(enumerable, GetIEqualityComparer());

            RegisterForDispose(set);
            Assert.True(set.SetEquals(enumerable));
        }
Esempio n. 6
0
        public void HashSet_Generic_Constructor_IEnumerable_WithManyDuplicates(int count)
        {
            IEnumerable <T> items = CreateEnumerable(EnumerableType.List, null, count, 0, 0);
            PooledSet <T>   hashSetFromDuplicates   = new PooledSet <T>(Enumerable.Range(0, 40).SelectMany(i => items).ToArray());
            PooledSet <T>   hashSetFromNoDuplicates = new PooledSet <T>(items);

            RegisterForDispose(hashSetFromDuplicates, hashSetFromNoDuplicates);
            Assert.True(hashSetFromNoDuplicates.SetEquals(hashSetFromDuplicates));
        }
Esempio n. 7
0
        public void HashSet_Generic_TrimExcess_Repeatedly(int setLength)
        {
            PooledSet <T> set      = (PooledSet <T>)GenericISetFactory(setLength);
            List <T>      expected = set.ToList();

            set.TrimExcess();
            set.TrimExcess();
            set.TrimExcess();
            Assert.True(set.SetEquals(expected));
        }
Esempio n. 8
0
        private void Validate_SetEquals(PooledSet <T> set, Span <T> span)
        {
            IEqualityComparer <T> comparer = set.Comparer;

            foreach (T value in set)
            {
                if (!SpanContains(span, value, comparer))
                {
                    Assert.False(set.SetEquals(span));
                    return;
                }
            }
            foreach (T value in span)
            {
                if (!set.Contains(value, comparer))
                {
                    Assert.False(set.SetEquals(span));
                    return;
                }
            }
            Assert.True(set.SetEquals(span));
        }
Esempio n. 9
0
        public void HashSet_Generic_Constructor_HashSet_SparselyFilled(int count)
        {
            PooledSet <T> source         = (PooledSet <T>)CreateEnumerable(EnumerableType.HashSet, null, count, 0, 0);
            List <T>      sourceElements = source.ToList();

            foreach (int i in NonSquares(count))
            {
                source.Remove(sourceElements[i]);// Unevenly spaced survivors increases chance of catching any spacing-related bugs.
            }
            PooledSet <T> set = new PooledSet <T>(source, GetIEqualityComparer());

            RegisterForDispose(set);
            Assert.True(set.SetEquals(source));
        }
Esempio n. 10
0
        public void HashSet_Generic_TrimExcess_AfterRemovingOneElement(int setLength)
        {
            if (setLength > 0)
            {
                PooledSet <T> set             = (PooledSet <T>)GenericISetFactory(setLength);
                List <T>      expected        = set.ToList();
                T             elementToRemove = set.ElementAt(0);

                set.TrimExcess();
                Assert.True(set.Remove(elementToRemove));
                expected.Remove(elementToRemove);
                set.TrimExcess();

                Assert.True(set.SetEquals(expected));
            }
        }
        private void Validate_UnionWith(ISet <T> set, IEnumerable <T> enumerable)
        {
            IEqualityComparer <T> comparer = GetIEqualityComparer();
            PooledSet <T>         expected = new PooledSet <T>(set, comparer);

            RegisterForDispose(expected);
            foreach (T element in enumerable)
            {
                if (!set.Contains(element, comparer))
                {
                    expected.Add(element);
                }
            }
            set.UnionWith(enumerable);
            Assert.Equal(expected.Count, set.Count);
            Assert.True(expected.SetEquals(set));
        }
Esempio n. 12
0
        private void Validate_UnionWith(PooledSet <T> set, Span <T> span)
        {
            IEqualityComparer <T> comparer = set.Comparer;
            PooledSet <T>         expected = new PooledSet <T>(set, comparer);

            RegisterForDispose(expected);
            foreach (T element in span)
            {
                if (!set.Contains(element))
                {
                    expected.Add(element);
                }
            }
            set.UnionWith(span);
            Assert.Equal(expected.Count, set.Count);
            Assert.True(expected.SetEquals(set));
        }
 private void Validate_ExceptWith(ISet <T> set, IEnumerable <T> enumerable)
 {
     if (set.Count == 0 || enumerable == set)
     {
         set.ExceptWith(enumerable);
         Assert.Equal(0, set.Count);
     }
     else
     {
         PooledSet <T> expected = new PooledSet <T>(set, GetIEqualityComparer());
         RegisterForDispose(expected);
         foreach (T element in enumerable)
         {
             expected.Remove(element);
         }
         set.ExceptWith(enumerable);
         Assert.Equal(expected.Count, set.Count);
         Assert.True(expected.SetEquals(set));
     }
 }
Esempio n. 14
0
 private void Validate_ExceptWith(PooledSet <T> set, Span <T> span)
 {
     if (set.Count == 0)
     {
         set.ExceptWith(span);
         Assert.Equal(0, set.Count);
     }
     else
     {
         PooledSet <T> expected = new PooledSet <T>(set, set.Comparer);
         RegisterForDispose(expected);
         foreach (T element in span)
         {
             expected.Remove(element);
         }
         set.ExceptWith(span);
         Assert.Equal(expected.Count, set.Count);
         Assert.True(expected.SetEquals(set));
     }
 }
        public void ISet_Generic_SymmetricExceptWith_AfterRemovingElements(EnumerableType enumerableType, int setLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
        {
            ISet <T> set   = GenericISetFactory(setLength);
            T        value = CreateT(532);

            if (!set.Contains(value))
            {
                set.Add(value);
            }
            set.Remove(value);
            IEnumerable <T> enumerable = CreateEnumerable(enumerableType, set, enumerableLength, numberOfMatchingElements, numberOfDuplicateElements);

            Debug.Assert(enumerable != null);

            IEqualityComparer <T> comparer = GetIEqualityComparer();
            PooledSet <T>         expected = new PooledSet <T>(comparer);

            RegisterForDispose(expected);
            foreach (T element in enumerable)
            {
                if (!set.Contains(element, comparer))
                {
                    expected.Add(element);
                }
            }
            foreach (T element in set)
            {
                if (!enumerable.Contains(element, comparer))
                {
                    expected.Add(element);
                }
            }
            set.SymmetricExceptWith(enumerable);
            Assert.Equal(expected.Count, set.Count);
            Assert.True(expected.SetEquals(set));
        }
Esempio n. 16
0
 private void Validate_IntersectWith(PooledSet <T> set, Span <T> span)
 {
     if (set.Count == 0 || span.Length == 0)
     {
         set.IntersectWith(span);
         Assert.Equal(0, set.Count);
     }
     else
     {
         IEqualityComparer <T> comparer = set.Comparer;
         PooledSet <T>         expected = new PooledSet <T>(comparer);
         RegisterForDispose(expected);
         foreach (T value in set)
         {
             if (SpanContains(span, value, comparer))
             {
                 expected.Add(value);
             }
         }
         set.IntersectWith(span);
         Assert.Equal(expected.Count, set.Count);
         Assert.True(expected.SetEquals(set));
     }
 }