Beispiel #1
0
        private void Validate_IsProperSupersetOf(PooledSet <T> set, Span <T> span)
        {
            bool isProperSuperset = true;
            bool setContainsElementsNotInEnumerable = false;
            IEqualityComparer <T> comparer          = set.Comparer;

            foreach (T value in span)
            {
                if (!set.Contains(value))
                {
                    isProperSuperset = false;
                    break;
                }
            }
            foreach (T value in set)
            {
                if (!SpanContains(span, value, comparer))
                {
                    setContainsElementsNotInEnumerable = true;
                    break;
                }
            }
            isProperSuperset = isProperSuperset && setContainsElementsNotInEnumerable;
            Assert.Equal(isProperSuperset, set.IsProperSupersetOf(span));
        }
Beispiel #2
0
 private void Validate_Overlaps(PooledSet <T> set, Span <T> span)
 {
     foreach (T value in span)
     {
         if (set.Contains(value))
         {
             Assert.True(set.Overlaps(span));
             return;
         }
     }
     Assert.False(set.Overlaps(span));
 }
Beispiel #3
0
 private void Validate_IsSupersetOf(PooledSet <T> set, Span <T> span)
 {
     foreach (T value in span)
     {
         if (!set.Contains(value))
         {
             Assert.False(set.IsSupersetOf(span));
             return;
         }
     }
     Assert.True(set.IsSupersetOf(span));
 }
        /// <summary>
        /// Helper function to create an HashSet fulfilling the given specific parameters. The function will
        /// create an HashSet using the Comparer constructor and then add values
        /// to it until it is full. It will begin by adding the desired number of matching,
        /// followed by random (deterministic) elements until the desired count is reached.
        /// </summary>
        protected IEnumerable<T> CreateHashSet(IEnumerable<T> enumerableToMatchTo, int count, int numberOfMatchingElements)
        {
            var set = new PooledSet<T>(GetIEqualityComparer());
            RegisterForDispose(set);
            int seed = 528;
            List<T> match = null;

            // Add Matching elements
            if (enumerableToMatchTo != null)
            {
                match = enumerableToMatchTo.ToList();
                for (int i = 0; i < numberOfMatchingElements; i++)
                    set.Add(match[i]);
            }

            // Add elements to reach the desired count
            while (set.Count < count)
            {
                T toAdd = CreateT(seed++);
                while (set.Contains(toAdd) || (match != null && match.Contains(toAdd, GetIEqualityComparer()))) // Don't want any unexpectedly duplicate values
                    toAdd = CreateT(seed++);
                set.Add(toAdd);
            }

            // Validate that the Enumerable fits the guidelines as expected
            Debug.Assert(set.Count == count);
            if (match != null)
            {
                int actualMatchingCount = 0;
                foreach (T lookingFor in match)
                    actualMatchingCount += set.Contains(lookingFor) ? 1 : 0;
                Assert.Equal(numberOfMatchingElements, actualMatchingCount);
            }

            return set;
        }
        protected string CreateT(PooledSet <string> set, int seed)
        {
            int    stringLength = seed % 10 + 5;
            Random rand         = new Random(seed);

            byte[] bytes = new byte[stringLength];
            rand.NextBytes(bytes);
            string ret = Convert.ToBase64String(bytes);

            while (set.Contains(ret))
            {
                rand.NextBytes(bytes);
                ret = Convert.ToBase64String(bytes);
            }
            return(ret);
        }
Beispiel #6
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));
        }
Beispiel #7
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));
        }
Beispiel #8
0
        private void Validate_IsProperSubsetOf(PooledSet <T> set, Span <T> span)
        {
            bool setContainsValueNotInEnumerable = false;
            bool enumerableContainsValueNotInSet = false;
            IEqualityComparer <T> comparer       = set.Comparer;

            foreach (T value in set) // Every value in Set must be in Enumerable
            {
                if (!SpanContains(span, value, comparer))
                {
                    setContainsValueNotInEnumerable = true;
                    break;
                }
            }
            foreach (T value in span) // Enumerable must contain at least one value not in Set
            {
                if (!set.Contains(value))
                {
                    enumerableContainsValueNotInSet = true;
                    break;
                }
            }
            Assert.Equal(!setContainsValueNotInEnumerable && enumerableContainsValueNotInSet, set.IsProperSubsetOf(span));
        }