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 // See the LICENSE file in the project root for more information.
4 
5 using System.Collections.Generic;
6 using Xunit;
7 
8 namespace System.Collections.Tests
9 {
10     public abstract partial class HashSet_Generic_Tests<T> : ISet_Generic_Tests<T>
11     {
12         [Theory]
13         [MemberData(nameof(ValidCollectionSizes))]
HashSet_Generic_Constructor_int(int capacity)14         public void HashSet_Generic_Constructor_int(int capacity)
15         {
16             HashSet<T> set = new HashSet<T>(capacity);
17             Assert.Equal(0, set.Count);
18         }
19 
20         [Theory]
21         [MemberData(nameof(ValidCollectionSizes))]
HashSet_Generic_Constructor_int_AddUpToAndBeyondCapacity(int capacity)22         public void HashSet_Generic_Constructor_int_AddUpToAndBeyondCapacity(int capacity)
23         {
24             HashSet<T> set = new HashSet<T>(capacity);
25 
26             AddToCollection(set, capacity);
27             Assert.Equal(capacity, set.Count);
28 
29             AddToCollection(set, capacity + 1);
30             Assert.Equal(capacity + 1, set.Count);
31         }
32 
33         [Fact]
HashSet_Generic_Constructor_int_Negative_ThrowsArgumentOutOfRangeException()34         public void HashSet_Generic_Constructor_int_Negative_ThrowsArgumentOutOfRangeException()
35         {
36             AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => new HashSet<T>(-1));
37             AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => new HashSet<T>(int.MinValue));
38         }
39 
40         [Theory]
41         [MemberData(nameof(ValidCollectionSizes))]
HashSet_Generic_Constructor_int_IEqualityComparer(int capacity)42         public void HashSet_Generic_Constructor_int_IEqualityComparer(int capacity)
43         {
44             IEqualityComparer<T> comparer = GetIEqualityComparer();
45             HashSet<T> set = new HashSet<T>(capacity, comparer);
46             Assert.Equal(0, set.Count);
47             if (comparer == null)
48                 Assert.Equal(EqualityComparer<T>.Default, set.Comparer);
49             else
50                 Assert.Equal(comparer, set.Comparer);
51         }
52 
53         [Theory]
54         [MemberData(nameof(ValidCollectionSizes))]
HashSet_Generic_Constructor_int_IEqualityComparer_AddUpToAndBeyondCapacity(int capacity)55         public void HashSet_Generic_Constructor_int_IEqualityComparer_AddUpToAndBeyondCapacity(int capacity)
56         {
57             IEqualityComparer<T> comparer = GetIEqualityComparer();
58             HashSet<T> set = new HashSet<T>(capacity, comparer);
59 
60             AddToCollection(set, capacity);
61             Assert.Equal(capacity, set.Count);
62 
63             AddToCollection(set, capacity + 1);
64             Assert.Equal(capacity + 1, set.Count);
65         }
66 
67         [Fact]
HashSet_Generic_Constructor_int_IEqualityComparer_Negative_ThrowsArgumentOutOfRangeException()68         public void HashSet_Generic_Constructor_int_IEqualityComparer_Negative_ThrowsArgumentOutOfRangeException()
69         {
70             IEqualityComparer<T> comparer = GetIEqualityComparer();
71             AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => new HashSet<T>(-1, comparer));
72             AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => new HashSet<T>(int.MinValue, comparer));
73         }
74 
75         #region TryGetValue
76 
77         [Fact]
HashSet_Generic_TryGetValue_Contains()78         public void HashSet_Generic_TryGetValue_Contains()
79         {
80             T value = CreateT(1);
81             HashSet<T> set = new HashSet<T> { value };
82             T equalValue = CreateT(1);
83             T actualValue;
84             Assert.True(set.TryGetValue(equalValue, out actualValue));
85             Assert.Equal(value, actualValue);
86             if (!typeof(T).IsValueType)
87             {
88                 Assert.Same(value, actualValue);
89             }
90         }
91 
92         [Fact]
HashSet_Generic_TryGetValue_Contains_OverwriteOutputParam()93         public void HashSet_Generic_TryGetValue_Contains_OverwriteOutputParam()
94         {
95             T value = CreateT(1);
96             HashSet<T> set = new HashSet<T> { value };
97             T equalValue = CreateT(1);
98             T actualValue = CreateT(2);
99             Assert.True(set.TryGetValue(equalValue, out actualValue));
100             Assert.Equal(value, actualValue);
101             if (!typeof(T).IsValueType)
102             {
103                 Assert.Same(value, actualValue);
104             }
105         }
106 
107         [Fact]
HashSet_Generic_TryGetValue_NotContains()108         public void HashSet_Generic_TryGetValue_NotContains()
109         {
110             T value = CreateT(1);
111             HashSet<T> set = new HashSet<T> { value };
112             T equalValue = CreateT(2);
113             T actualValue;
114             Assert.False(set.TryGetValue(equalValue, out actualValue));
115             Assert.Equal(default(T), actualValue);
116         }
117 
118         [Fact]
HashSet_Generic_TryGetValue_NotContains_OverwriteOutputParam()119         public void HashSet_Generic_TryGetValue_NotContains_OverwriteOutputParam()
120         {
121             T value = CreateT(1);
122             HashSet<T> set = new HashSet<T> { value };
123             T equalValue = CreateT(2);
124             T actualValue = equalValue;
125             Assert.False(set.TryGetValue(equalValue, out actualValue));
126             Assert.Equal(default(T), actualValue);
127         }
128 
129         #endregion
130     }
131 }
132