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 System.Linq;
7 using Xunit;
8 
9 namespace System.Collections.Tests
10 {
11     public class SortedSet_TreeSubset_Int_Tests : SortedSet_TreeSubset_Tests<int>
12     {
13         protected override int Min => int.MinValue;
14         protected override int Max => int.MaxValue;
15 
16         protected override bool DefaultValueAllowed => true;
17 
CreateT(int seed)18         protected override int CreateT(int seed)
19         {
20             Random rand = new Random(seed);
21             return rand.Next();
22         }
23     }
24 
25     public class SortedSet_TreeSubset_String_Tests : SortedSet_TreeSubset_Tests<string>
26     {
27         protected override string Min => 0.ToString().PadLeft(10);
28         protected override string Max => int.MaxValue.ToString().PadLeft(10);
29 
CreateT(int seed)30         protected override string CreateT(int seed)
31         {
32             return seed.ToString().PadLeft(10);
33         }
34 
ICollection_Generic_Remove_DefaultValueContainedInCollection(int count)35         public override void ICollection_Generic_Remove_DefaultValueContainedInCollection(int count)
36         {
37             if (!IsReadOnly && !AddRemoveClear_ThrowsNotSupported && DefaultValueAllowed && !Enumerable.Contains(InvalidValues, default(string)))
38             {
39                 int seed = count * 21;
40                 ICollection<string> collection = GenericICollectionFactory(count);
41                 AssertExtensions.Throws<ArgumentOutOfRangeException>("item", () => collection.Remove(default(string)));
42             }
43         }
44 
ICollection_Generic_Contains_DefaultValueOnCollectionContainingDefaultValue(int count)45         public override void ICollection_Generic_Contains_DefaultValueOnCollectionContainingDefaultValue(int count)
46         {
47             if (DefaultValueAllowed && !IsReadOnly && !AddRemoveClear_ThrowsNotSupported)
48             {
49                 ICollection<string> collection = GenericICollectionFactory(count);
50                 AssertExtensions.Throws<ArgumentOutOfRangeException>("item", () => collection.Add(default(string)));
51             }
52         }
53     }
54 
55     public abstract class SortedSet_TreeSubset_Tests<T> : SortedSet_Generic_Tests<T>
56     {
57         protected abstract T Min { get; }
58         protected abstract T Max { get; }
59         private SortedSet<T> OriginalSet { get; set; }
60 
GenericISetFactory()61         protected override ISet<T> GenericISetFactory()
62         {
63             OriginalSet = new SortedSet<T>();
64             return OriginalSet.GetViewBetween(Min, Max);
65         }
66 
ICollection_Generic_Add_DefaultValue(int count)67         public override void ICollection_Generic_Add_DefaultValue(int count)
68         {
69             // Adding an item to a TreeSubset does nothing - it updates the parent.
70             if (DefaultValueAllowed && !IsReadOnly && !AddRemoveClear_ThrowsNotSupported)
71             {
72                 ICollection<T> collection = GenericICollectionFactory(count);
73                 collection.Add(default(T));
74                 Assert.Equal(count, collection.Count);
75                 Assert.Equal(count + 1, OriginalSet.Count);
76             }
77         }
78     }
79 }
80