|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace System.Collections.ObjectModel.Tests |
| 8 | +{ |
| 9 | + public class ReadOnlySetTests |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void Ctor_NullSet_ThrowsArgumentNullException() |
| 13 | + { |
| 14 | + AssertExtensions.Throws<ArgumentNullException>("set", () => new ReadOnlySet<int>(null)); |
| 15 | + } |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void Ctor_SetProperty_Roundtrips() |
| 19 | + { |
| 20 | + var set = new HashSet<int>(); |
| 21 | + Assert.Same(set, new DerivedReadOnlySet<int>(set).Set); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void Empty_EmptyAndIdempotent() |
| 26 | + { |
| 27 | + Assert.Same(ReadOnlySet<int>.Empty, ReadOnlySet<int>.Empty); |
| 28 | + Assert.Empty(ReadOnlySet<int>.Empty); |
| 29 | + Assert.Same(ReadOnlySet<int>.Empty.GetEnumerator(), ReadOnlySet<int>.Empty.GetEnumerator()); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void MembersDelegateToWrappedSet() |
| 34 | + { |
| 35 | + var set = new ReadOnlySet<int>(new HashSet<int>() { 1, 2, 3 }); |
| 36 | + |
| 37 | + Assert.True(set.Contains(2)); |
| 38 | + Assert.False(set.Contains(4)); |
| 39 | + |
| 40 | + Assert.Equal(3, set.Count); |
| 41 | + |
| 42 | + Assert.True(set.IsProperSubsetOf([1, 2, 3, 4])); |
| 43 | + Assert.False(set.IsProperSubsetOf([1, 2, 5])); |
| 44 | + |
| 45 | + Assert.True(set.IsProperSupersetOf([1, 2])); |
| 46 | + Assert.False(set.IsProperSupersetOf([1, 4])); |
| 47 | + |
| 48 | + Assert.True(set.IsSubsetOf([1, 2, 3, 4])); |
| 49 | + Assert.False(set.IsSubsetOf([1, 2, 5])); |
| 50 | + |
| 51 | + Assert.True(set.IsSupersetOf([1, 2])); |
| 52 | + Assert.False(set.IsSupersetOf([1, 4])); |
| 53 | + |
| 54 | + Assert.True(set.Overlaps([-1, 0, 1])); |
| 55 | + Assert.False(set.Overlaps([-1, 0])); |
| 56 | + |
| 57 | + Assert.True(set.SetEquals([1, 2, 3])); |
| 58 | + Assert.False(set.SetEquals([1, 2, 4])); |
| 59 | + |
| 60 | + int[] result = new int[3]; |
| 61 | + ((ICollection<int>)set).CopyTo(result, 0); |
| 62 | + Assert.Equal(result, new int[] { 1, 2, 3 }); |
| 63 | + |
| 64 | + Array.Clear(result); |
| 65 | + ((ICollection)set).CopyTo(result, 0); |
| 66 | + Assert.Equal(result, new int[] { 1, 2, 3 }); |
| 67 | + |
| 68 | + Assert.NotNull(set.GetEnumerator()); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void ChangesToUnderlyingSetReflected() |
| 73 | + { |
| 74 | + var set = new HashSet<int> { 1, 2, 3 }; |
| 75 | + var readOnlySet = new ReadOnlySet<int>(set); |
| 76 | + |
| 77 | + set.Add(4); |
| 78 | + Assert.Equal(4, readOnlySet.Count); |
| 79 | + Assert.True(readOnlySet.Contains(4)); |
| 80 | + |
| 81 | + set.Remove(2); |
| 82 | + Assert.Equal(3, readOnlySet.Count); |
| 83 | + Assert.False(readOnlySet.Contains(2)); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void IsReadOnly_True() |
| 88 | + { |
| 89 | + var set = new ReadOnlySet<int>(new HashSet<int> { 1, 2, 3 }); |
| 90 | + Assert.True(((ICollection<int>)set).IsReadOnly); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void MutationThrows_CollectionUnmodified() |
| 95 | + { |
| 96 | + var set = new HashSet<int> { 1, 2, 3 }; |
| 97 | + var readOnlySet = new ReadOnlySet<int>(set); |
| 98 | + |
| 99 | + Assert.Throws<NotSupportedException>(() => ((ICollection<int>)readOnlySet).Add(4)); |
| 100 | + Assert.Throws<NotSupportedException>(() => ((ICollection<int>)readOnlySet).Remove(1)); |
| 101 | + Assert.Throws<NotSupportedException>(() => ((ICollection<int>)readOnlySet).Clear()); |
| 102 | + |
| 103 | + Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).Add(4)); |
| 104 | + Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).ExceptWith([1, 2, 3])); |
| 105 | + Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).IntersectWith([1, 2, 3])); |
| 106 | + Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).SymmetricExceptWith([1, 2, 3])); |
| 107 | + Assert.Throws<NotSupportedException>(() => ((ISet<int>)readOnlySet).UnionWith([1, 2, 3])); |
| 108 | + |
| 109 | + Assert.Equal(3, set.Count); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void ICollection_Synchronization() |
| 114 | + { |
| 115 | + var set = new ReadOnlySet<int>(new HashSet<int> { 1, 2, 3 }); |
| 116 | + |
| 117 | + Assert.False(((ICollection)set).IsSynchronized); |
| 118 | + Assert.Same(set, ((ICollection)set).SyncRoot); |
| 119 | + } |
| 120 | + |
| 121 | + private class DerivedReadOnlySet<T> : ReadOnlySet<T> |
| 122 | + { |
| 123 | + public DerivedReadOnlySet(HashSet<T> set) : base(set) { } |
| 124 | + |
| 125 | + public new ISet<T> Set => base.Set; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments