public void Remove()
        {
            var builder = ImmutableSegmentedHashSet.Create("a").ToBuilder();

            Assert.False(builder.Remove("b"));
            Assert.True(builder.Remove("a"));
        }
Ejemplo n.º 2
0
        public static void TestDebuggerAttributes_Null()
        {
            Type proxyType = DebuggerAttributes.GetProxyType(ImmutableSegmentedHashSet.Create <string>());
            TargetInvocationException tie = Assert.Throws <TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object?)null));

            Assert.IsType <ArgumentNullException>(tie.InnerException);
        }
Ejemplo n.º 3
0
        public void EnumeratorRecyclingMisuse()
        {
            var collection     = ImmutableSegmentedHashSet.Create <int>().Add(5);
            var enumerator     = collection.GetEnumerator();
            var enumeratorCopy = enumerator;

            Assert.True(enumerator.MoveNext());
            Assert.False(enumerator.MoveNext());
            enumerator.Dispose();
            Assert.False(enumerator.MoveNext());
            enumerator.Reset();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumeratorCopy.MoveNext());
            enumeratorCopy.Reset();
            Assert.Equal(0, enumeratorCopy.Current);
            enumerator.Dispose(); // double-disposal should not throw
            enumeratorCopy.Dispose();

            // We expect that acquiring a new enumerator will use the same underlying Stack<T> object,
            // but that it will not throw exceptions for the new enumerator.
            enumerator = collection.GetEnumerator();
            Assert.True(enumerator.MoveNext());
            Assert.False(enumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);
            enumerator.Dispose();
        }
        public void IsProperSupersetOf()
        {
            var builder = ImmutableSegmentedHashSet.CreateRange(Enumerable.Range(1, 3)).ToBuilder();

            Assert.Throws <ArgumentNullException>("other", () => builder.IsProperSupersetOf(null !));
            Assert.False(builder.IsProperSupersetOf(Enumerable.Range(1, 3)));
            Assert.True(builder.IsProperSupersetOf(Enumerable.Range(1, 2)));
        }
        public void Overlaps()
        {
            var builder = ImmutableSegmentedHashSet.CreateRange(Enumerable.Range(1, 3)).ToBuilder();

            Assert.Throws <ArgumentNullException>("other", () => builder.Overlaps(null !));
            Assert.True(builder.Overlaps(Enumerable.Range(3, 2)));
            Assert.False(builder.Overlaps(Enumerable.Range(4, 3)));
        }
        public void Clear()
        {
            var set     = ImmutableSegmentedHashSet.Create(1);
            var builder = set.ToBuilder();

            builder.Clear();
            Assert.Equal(0, builder.Count);
        }
        public void IntersectWith()
        {
            var builder = ImmutableSegmentedHashSet.Create(1, 2, 3).ToBuilder();

            Assert.Throws <ArgumentNullException>("other", () => builder.IntersectWith(null !));
            builder.IntersectWith(new[] { 2, 3, 4 });
            Assert.Equal(new[] { 2, 3 }, builder);
        }
        public void CreateBuilder()
        {
            var builder = ImmutableSegmentedHashSet.CreateBuilder <string>();

            Assert.Same(EqualityComparer <string> .Default, builder.KeyComparer);

            builder = ImmutableSegmentedHashSet.CreateBuilder <string>(StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.OrdinalIgnoreCase, builder.KeyComparer);
        }
        public void SetEquals()
        {
            var builder = ImmutableSegmentedHashSet.Create("a").ToBuilder();

            Assert.Throws <ArgumentNullException>("other", () => builder.SetEquals(null !));
            Assert.False(builder.SetEquals(new[] { "b" }));
            Assert.True(builder.SetEquals(new[] { "a" }));
            Assert.True(builder.SetEquals(builder));
        }
Ejemplo n.º 10
0
        public void RemoveValuesFromCollidedHashCode()
        {
            var set = ImmutableSegmentedHashSet.Create <int>(new BadHasher <int>(), 5, 6);

            Assert.True(IsSame(set, set.Remove(2)));
            var setAfterRemovingFive = set.Remove(5);

            Assert.Equal(1, setAfterRemovingFive.Count);
            Assert.Equal(new[] { 6 }, setAfterRemovingFive);
        }
Ejemplo n.º 11
0
        public void DebuggerAttributesValid()
        {
            DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableSegmentedHashSet.Create <string>());
            ImmutableSegmentedHashSet <int> set  = ImmutableSegmentedHashSet.Create(1, 2, 3);
            DebuggerAttributeInfo           info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(set);
            PropertyInfo itemProperty            = info.Properties.Single(pr => pr.GetCustomAttribute <DebuggerBrowsableAttribute>()?.State == DebuggerBrowsableState.RootHidden);

            int[]? items = itemProperty.GetValue(info.Instance) as int[];
            Assert.Equal(set, items);
        }
Ejemplo n.º 12
0
        public void RemoveValuesFromCollidedHashCode_RefType()
        {
            var set = ImmutableSegmentedHashSet.Create <string>(new BadHasher <string>(), "a", "b");

            Assert.True(IsSame(set, set.Remove("c")));
            var setAfterRemovingA = set.Remove("a");

            Assert.Equal(1, setAfterRemovingA.Count);
            Assert.Equal(new[] { "b" }, setAfterRemovingA);
        }
        public void KeyComparerEmptyCollection()
        {
            var builder = ImmutableSegmentedHashSet.Create <string>().ToBuilder();

            Assert.Same(EqualityComparer <string> .Default, builder.KeyComparer);
            builder.KeyComparer = StringComparer.OrdinalIgnoreCase;
            Assert.Same(StringComparer.OrdinalIgnoreCase, builder.KeyComparer);
            var set = builder.ToImmutable();

            Assert.Same(StringComparer.OrdinalIgnoreCase, set.KeyComparer);
        }
        public void TryGetValue()
        {
            var builder = ImmutableSegmentedHashSet.Create(1, 2, 3).ToBuilder();

            Assert.True(builder.TryGetValue(2, out _));

            builder = ImmutableSegmentedHashSet.Create(CustomEqualityComparer.Instance, 1, 2, 3, 4).ToBuilder();
            var existing = 0;

            Assert.True(builder.TryGetValue(5, out existing));
            Assert.Equal(4, existing);
        }
Ejemplo n.º 15
0
        public void SymmetricExceptWithComparerTests()
        {
            var set             = ImmutableSegmentedHashSet.Create <string>("a").WithComparer(StringComparer.OrdinalIgnoreCase);
            var otherCollection = new[] { "A" };

            var expectedSet = new HashSet <string>(set, set.KeyComparer);

            expectedSet.SymmetricExceptWith(otherCollection);

            var actualSet = set.SymmetricExcept(otherCollection);

            CollectionAssertAreEquivalent(expectedSet.ToList(), actualSet.ToList());
        }
        public void KeyComparerCollisions()
        {
            var builder = ImmutableSegmentedHashSet.Create("a", "A").ToBuilder();

            builder.KeyComparer = StringComparer.OrdinalIgnoreCase;
            Assert.Equal(1, builder.Count);
            Assert.True(builder.Contains("a"));

            var set = builder.ToImmutable();

            Assert.Same(StringComparer.OrdinalIgnoreCase, set.KeyComparer);
            Assert.Equal(1, set.Count);
            Assert.True(set.Contains("a"));
        }
        public void ICollectionOfTMethods()
        {
            ICollection <string> builder = ImmutableSegmentedHashSet.Create("a").ToBuilder();

            builder.Add("b");
            Assert.True(builder.Contains("b"));

            var array = new string[3];

            builder.CopyTo(array, 1);
            Assert.Null(array[0]);
            CollectionAssertAreEquivalent(new[] { null, "a", "b" }, array);

            Assert.False(builder.IsReadOnly);

            CollectionAssertAreEquivalent(new[] { "a", "b" }, builder.ToArray()); // tests enumerator
        }
        public void KeyComparer()
        {
            var builder = ImmutableSegmentedHashSet.Create("a", "B").ToBuilder();

            Assert.Same(EqualityComparer <string> .Default, builder.KeyComparer);
            Assert.True(builder.Contains("a"));
            Assert.False(builder.Contains("A"));

            builder.KeyComparer = StringComparer.OrdinalIgnoreCase;
            Assert.Same(StringComparer.OrdinalIgnoreCase, builder.KeyComparer);
            Assert.Equal(2, builder.Count);
            Assert.True(builder.Contains("a"));
            Assert.True(builder.Contains("A"));

            var set = builder.ToImmutable();

            Assert.Same(StringComparer.OrdinalIgnoreCase, set.KeyComparer);
        }
Ejemplo n.º 19
0
        public void Create()
        {
            var comparer = StringComparer.OrdinalIgnoreCase;

            var set = ImmutableSegmentedHashSet.Create <string?>();

            Assert.Equal(0, set.Count);
            Assert.Same(EqualityComparer <string> .Default, set.KeyComparer);

            set = ImmutableSegmentedHashSet.Create <string?>(comparer);
            Assert.Equal(0, set.Count);
            Assert.Same(comparer, set.KeyComparer);

            set = ImmutableSegmentedHashSet.Create <string?>("a");
            Assert.Equal(1, set.Count);
            Assert.Same(EqualityComparer <string> .Default, set.KeyComparer);

            set = ImmutableSegmentedHashSet.Create <string?>(comparer, "a");
            Assert.Equal(1, set.Count);
            Assert.Same(comparer, set.KeyComparer);

            set = ImmutableSegmentedHashSet.Create <string?>("a", "b");
            Assert.Equal(2, set.Count);
            Assert.Same(EqualityComparer <string> .Default, set.KeyComparer);

            set = ImmutableSegmentedHashSet.Create <string?>(comparer, "a", "b");
            Assert.Equal(2, set.Count);
            Assert.Same(comparer, set.KeyComparer);

            set = ImmutableSegmentedHashSet.CreateRange <string?>((IEnumerable <string>) new[] { "a", "b" });
            Assert.Equal(2, set.Count);
            Assert.Same(EqualityComparer <string> .Default, set.KeyComparer);

            set = ImmutableSegmentedHashSet.CreateRange <string?>(comparer, (IEnumerable <string>) new[] { "a", "b" });
            Assert.Equal(2, set.Count);
            Assert.Same(comparer, set.KeyComparer);

            set = ImmutableSegmentedHashSet.Create((string?)null);
            Assert.Equal(1, set.Count);

            set = ImmutableSegmentedHashSet.CreateRange(new[] { null, "a", null, "b" });
            Assert.Equal(3, set.Count);
        }
        public void ToImmutableHashSet()
        {
            ImmutableSegmentedHashSet <int> .Builder builder = ImmutableSegmentedHashSet.CreateBuilder <int>();
            builder.Add(1);
            builder.Add(2);
            builder.Add(3);

            var set = System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet(builder);

            Assert.True(builder.Contains(1));
            Assert.True(builder.Contains(2));
            Assert.True(builder.Contains(3));

            builder.Remove(3);
            Assert.False(builder.Contains(3));
            Assert.True(set.Contains(3));

            builder.Clear();
            Assert.True(builder.ToImmutableSegmentedHashSet().IsEmpty);
            Assert.False(set.IsEmpty);

            ImmutableSegmentedHashSet <int> .Builder?nullBuilder = null;
            Assert.Throws <ArgumentNullException>("builder", () => nullBuilder !.ToImmutableSegmentedHashSet());
        }
 public void DebuggerAttributesValid()
 {
     DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableSegmentedHashSet.CreateBuilder <int>());
 }
        public void EnumeratorTest()
        {
            var builder = ImmutableSegmentedHashSet.Create(1).ToBuilder();

            ManuallyEnumerateTest(new[] { 1 }, ((IEnumerable <int>)builder).GetEnumerator());
        }