diff --git a/src/libraries/System.Collections.Immutable/src/Properties/InternalsVisibleTo.cs b/src/libraries/System.Collections.Immutable/src/Properties/InternalsVisibleTo.cs deleted file mode 100644 index a72ff9ffdcb321..00000000000000 --- a/src/libraries/System.Collections.Immutable/src/Properties/InternalsVisibleTo.cs +++ /dev/null @@ -1,6 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("System.Collections.Immutable.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")] diff --git a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj index 47c5898a66c102..d2eac5d7d4577a 100644 --- a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj +++ b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj @@ -11,7 +11,6 @@ The System.Collections.Immutable library is built-in as part of the shared frame - @@ -68,18 +67,13 @@ The System.Collections.Immutable library is built-in as part of the shared frame - - - - - @@ -87,7 +81,6 @@ The System.Collections.Immutable library is built-in as part of the shared frame - diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Generic/IHashKeyCollection.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Generic/IHashKeyCollection.cs deleted file mode 100644 index 30ce4bc7ddd6b6..00000000000000 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Generic/IHashKeyCollection.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Collections.Generic -{ - /// - /// Defined on a generic collection that hashes its contents using an . - /// - /// The type of element hashed in the collection. - internal interface IHashKeyCollection - { - /// - /// Gets the comparer used to obtain hash codes for the keys and check equality. - /// - IEqualityComparer KeyComparer { get; } - } -} diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Generic/ISortKeyCollection.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Generic/ISortKeyCollection.cs deleted file mode 100644 index cb5c4452cba447..00000000000000 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Generic/ISortKeyCollection.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; - -namespace System.Collections.Generic -{ - /// - /// Defined on a generic collection that sorts its contents using an . - /// - /// The type of element sorted in the collection. - internal interface ISortKeyCollection - { - /// - /// Gets the comparer used to sort keys. - /// - IComparer KeyComparer { get; } - } -} diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IBinaryTree.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IBinaryTree.cs deleted file mode 100644 index 7f4dc492d76ad2..00000000000000 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IBinaryTree.cs +++ /dev/null @@ -1,58 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Collections.Immutable -{ - /// - /// An interface for binary tree nodes that allow our common enumerator to walk the graph. - /// - internal interface IBinaryTree - { - /// - /// Gets the depth of the tree below this node. - /// - int Height { get; } - - /// - /// Gets a value indicating whether this node is empty. - /// - bool IsEmpty { get; } - - /// - /// Gets the number of non-empty nodes at this node and below. - /// - /// Thrown if the implementation does not store this value at the node. - int Count { get; } - /// - /// Gets the left branch of this node. - /// - IBinaryTree? Left { get; } - - /// - /// Gets the right branch of this node. - /// - IBinaryTree? Right { get; } - } - - /// - /// An interface for binary tree nodes that allow our common enumerator to walk the graph. - /// - /// The type of value for each node. - internal interface IBinaryTree : IBinaryTree - { - /// - /// Gets the value represented by the current node. - /// - T Value { get; } - - /// - /// Gets the left branch of this node. - /// - new IBinaryTree? Left { get; } - - /// - /// Gets the right branch of this node. - /// - new IBinaryTree? Right { get; } - } -} diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs deleted file mode 100644 index cb19d186960902..00000000000000 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs +++ /dev/null @@ -1,332 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; - -namespace System.Collections.Immutable -{ - /// - /// An interface that describes the methods that the and types have in common. - /// - /// The type of element in the collection. - internal interface IImmutableListQueries : IReadOnlyList - { - /// - /// Converts the elements in the current to - /// another type, and returns a list containing the converted elements. - /// - /// - /// A delegate that converts each element from - /// one type to another type. - /// - /// - /// The type of the elements of the target array. - /// - /// - /// A of the target type containing the converted - /// elements from the current . - /// - ImmutableList ConvertAll(Func converter); - - /// - /// Performs the specified action on each element of the list. - /// - /// The delegate to perform on each element of the list. - void ForEach(Action action); - - /// - /// Creates a shallow copy of a range of elements in the source . - /// - /// - /// The zero-based index at which the range - /// starts. - /// - /// - /// The number of elements in the range. - /// - /// - /// A shallow copy of a range of elements in the source . - /// - ImmutableList GetRange(int index, int count); - - /// - /// Copies the entire to a compatible one-dimensional - /// array, starting at the beginning of the target array. - /// - /// - /// The one-dimensional that is the destination of the elements - /// copied from . The must have - /// zero-based indexing. - /// - void CopyTo(T[] array); - - /// - /// Copies the entire to a compatible one-dimensional - /// array, starting at the specified index of the target array. - /// - /// - /// The one-dimensional that is the destination of the elements - /// copied from . The must have - /// zero-based indexing. - /// - /// - /// The zero-based index in at which copying begins. - /// - void CopyTo(T[] array, int arrayIndex); - - /// - /// Copies a range of elements from the to - /// a compatible one-dimensional array, starting at the specified index of the - /// target array. - /// - /// - /// The zero-based index in the source at - /// which copying begins. - /// - /// - /// The one-dimensional that is the destination of the elements - /// copied from . The must have - /// zero-based indexing. - /// - /// The zero-based index in at which copying begins. - /// The number of elements to copy. - void CopyTo(int index, T[] array, int arrayIndex, int count); - - /// - /// Determines whether the contains elements - /// that match the conditions defined by the specified predicate. - /// - /// - /// The delegate that defines the conditions of the elements - /// to search for. - /// - /// - /// true if the contains one or more elements - /// that match the conditions defined by the specified predicate; otherwise, - /// false. - /// - bool Exists(Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the first occurrence within the entire . - /// - /// - /// The delegate that defines the conditions of the element - /// to search for. - /// - /// - /// The first element that matches the conditions defined by the specified predicate, - /// if found; otherwise, the default value for type . - /// - T? Find(Predicate match); - - /// - /// Retrieves all the elements that match the conditions defined by the specified - /// predicate. - /// - /// - /// The delegate that defines the conditions of the elements - /// to search for. - /// - /// - /// A containing all the elements that match - /// the conditions defined by the specified predicate, if found; otherwise, an - /// empty . - /// - ImmutableList FindAll(Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the zero-based index of the first occurrence within - /// the entire . - /// - /// - /// The delegate that defines the conditions of the element - /// to search for. - /// - /// - /// The zero-based index of the first occurrence of an element that matches the - /// conditions defined by , if found; otherwise, -1. - /// - int FindIndex(Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the zero-based index of the first occurrence within - /// the range of elements in the that extends - /// from the specified index to the last element. - /// - /// The zero-based starting index of the search. - /// The delegate that defines the conditions of the element to search for. - /// - /// The zero-based index of the first occurrence of an element that matches the - /// conditions defined by , if found; otherwise, -1. - /// - int FindIndex(int startIndex, Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the zero-based index of the first occurrence within - /// the range of elements in the that starts - /// at the specified index and contains the specified number of elements. - /// - /// The zero-based starting index of the search. - /// The number of elements in the section to search. - /// The delegate that defines the conditions of the element to search for. - /// - /// The zero-based index of the first occurrence of an element that matches the - /// conditions defined by , if found; otherwise, -1. - /// - int FindIndex(int startIndex, int count, Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the last occurrence within the entire . - /// - /// - /// The delegate that defines the conditions of the element - /// to search for. - /// - /// - /// The last element that matches the conditions defined by the specified predicate, - /// if found; otherwise, the default value for type . - /// - T? FindLast(Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the zero-based index of the last occurrence within - /// the entire . - /// - /// - /// The delegate that defines the conditions of the element - /// to search for. - /// - /// - /// The zero-based index of the last occurrence of an element that matches the - /// conditions defined by , if found; otherwise, -1. - /// - int FindLastIndex(Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the zero-based index of the last occurrence within - /// the range of elements in the that extends - /// from the first element to the specified index. - /// - /// The zero-based starting index of the backward search. - /// The delegate that defines the conditions of the element - /// to search for. - /// - /// The zero-based index of the last occurrence of an element that matches the - /// conditions defined by , if found; otherwise, -1. - /// - int FindLastIndex(int startIndex, Predicate match); - - /// - /// Searches for an element that matches the conditions defined by the specified - /// predicate, and returns the zero-based index of the last occurrence within - /// the range of elements in the that contains - /// the specified number of elements and ends at the specified index. - /// - /// The zero-based starting index of the backward search. - /// The number of elements in the section to search. - /// - /// The delegate that defines the conditions of the element - /// to search for. - /// - /// - /// The zero-based index of the last occurrence of an element that matches the - /// conditions defined by , if found; otherwise, -1. - /// - int FindLastIndex(int startIndex, int count, Predicate match); - - /// - /// Determines whether every element in the - /// matches the conditions defined by the specified predicate. - /// - /// - /// The delegate that defines the conditions to check against - /// the elements. - /// - /// - /// true if every element in the matches the - /// conditions defined by the specified predicate; otherwise, false. If the list - /// has no elements, the return value is true. - /// - bool TrueForAll(Predicate match); - - /// - /// Searches the entire sorted for an element - /// using the default comparer and returns the zero-based index of the element. - /// - /// The object to locate. The value can be null for reference types. - /// - /// The zero-based index of in the sorted , - /// if is found; otherwise, a negative number that is the bitwise complement - /// of the index of the next element that is larger than or, if there is - /// no larger element, the bitwise complement of . - /// - /// - /// The default comparer cannot - /// find an implementation of the generic interface or - /// the interface for type . - /// - int BinarySearch(T item); - - /// - /// Searches the entire sorted for an element - /// using the specified comparer and returns the zero-based index of the element. - /// - /// The object to locate. The value can be null for reference types. - /// - /// The implementation to use when comparing - /// elements.-or-null to use the default comparer . - /// - /// - /// The zero-based index of in the sorted , - /// if is found; otherwise, a negative number that is the bitwise complement - /// of the index of the next element that is larger than or, if there is - /// no larger element, the bitwise complement of . - /// - /// - /// is null, and the default comparer - /// cannot find an implementation of the generic interface - /// or the interface for type . - /// - int BinarySearch(T item, IComparer? comparer); - - /// - /// Searches a range of elements in the sorted - /// for an element using the specified comparer and returns the zero-based index - /// of the element. - /// - /// The zero-based starting index of the range to search. - /// The length of the range to search. - /// The object to locate. The value can be null for reference types. - /// - /// The implementation to use when comparing - /// elements, or null to use the default comparer . - /// - /// - /// The zero-based index of in the sorted , - /// if is found; otherwise, a negative number that is the bitwise complement - /// of the index of the next element that is larger than or, if there is - /// no larger element, the bitwise complement of . - /// - /// - /// is less than 0.-or- is less than 0. - /// - /// - /// and do not denote a valid range in the . - /// - /// - /// is null, and the default comparer - /// cannot find an implementation of the generic interface - /// or the interface for type . - /// - int BinarySearch(int index, int count, T item, IComparer? comparer); - } -} diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IOrderedCollection.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IOrderedCollection.cs deleted file mode 100644 index c4f33bae782fce..00000000000000 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IOrderedCollection.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; - -namespace System.Collections.Immutable -{ - /// - /// Describes an ordered collection of elements. - /// - /// The type of element in the collection. - internal interface IOrderedCollection : IEnumerable - { - /// - /// Gets the number of elements in the collection. - /// - int Count { get; } - - /// - /// Gets the element in the collection at a given index. - /// - T this[int index] { get; } - } -} diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs index ee164957d36aa2..5e2f31a961b4fb 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs @@ -15,7 +15,7 @@ namespace System.Collections.Immutable /// The type of the value. [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableDictionaryDebuggerProxy<,>))] - public sealed partial class ImmutableDictionary : IImmutableDictionary, IImmutableDictionaryInternal, IHashKeyCollection, IDictionary, IDictionary where TKey : notnull + public sealed partial class ImmutableDictionary : IImmutableDictionary, IImmutableDictionaryInternal, IDictionary, IDictionary where TKey : notnull { /// /// An empty immutable dictionary with default equality comparers. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs index 1522e0a8f05415..b3258e35eb921c 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs @@ -54,7 +54,7 @@ public ImmutableDictionaryDebuggerProxy(IReadOnlyDictionary dictio /// This class should only be used with immutable enumerables, since it /// caches the enumerable into an array for display in the debugger. /// - internal class ImmutableEnumerableDebuggerProxy + internal sealed class ImmutableEnumerableDebuggerProxy { /// /// The enumerable to show to the debugger. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs index 8d941eb0bcec12..637bc0e022c1d8 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs @@ -41,11 +41,11 @@ internal static bool IsValueType() /// The type of elements in the collection. /// The collection. /// An ordered collection. May not be thread-safe. Never null. - internal static IOrderedCollection AsOrderedCollection(this IEnumerable sequence) + internal static IReadOnlyList AsReadOnlyList(this IEnumerable sequence) { Requires.NotNull(sequence, nameof(sequence)); - if (sequence is IOrderedCollection orderedCollection) + if (sequence is IReadOnlyList orderedCollection) { return orderedCollection; } @@ -105,7 +105,7 @@ internal static DisposableEnumeratorAdapter GetEnumerableDisposa /// Wraps a as an ordered collection. /// /// The type of element in the collection. - private sealed class ListOfTWrapper : IOrderedCollection + private sealed class ListOfTWrapper : IReadOnlyList { /// /// The list being exposed. @@ -165,7 +165,7 @@ IEnumerator IEnumerable.GetEnumerator() /// Wraps any as an ordered, indexable list. /// /// The type of element in the collection. - private sealed class FallbackWrapper : IOrderedCollection + private sealed class FallbackWrapper : IReadOnlyList { /// /// The original sequence. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByRefEqualityComparer.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByRefEqualityComparer.cs index 05cd5ddc786087..b9b4f0200d4693 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByRefEqualityComparer.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByRefEqualityComparer.cs @@ -8,7 +8,7 @@ namespace System.Collections.Immutable /// /// Contains the inner class. /// - public sealed partial class ImmutableHashSet : IImmutableSet, IHashKeyCollection, IReadOnlyCollection, ICollection, ISet, ICollection, IStrongEnumerable.Enumerator> + public sealed partial class ImmutableHashSet : IImmutableSet, IReadOnlyCollection, ICollection, ISet, ICollection, IStrongEnumerable.Enumerator> { /// /// Compares equality between two instances diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByValueEqualityComparer.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByValueEqualityComparer.cs index 3cac674f7065cf..1cc97a6f466238 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByValueEqualityComparer.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucketByValueEqualityComparer.cs @@ -8,7 +8,7 @@ namespace System.Collections.Immutable /// /// Contains the inner class. /// - public sealed partial class ImmutableHashSet : IImmutableSet, IHashKeyCollection, IReadOnlyCollection, ICollection, ISet, ICollection, IStrongEnumerable.Enumerator> + public sealed partial class ImmutableHashSet : IImmutableSet, IReadOnlyCollection, ICollection, ISet, ICollection, IStrongEnumerable.Enumerator> { /// /// Compares equality between two instances diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs index 2ff4b68d19af36..c6b6f3140e4cc2 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs @@ -16,9 +16,9 @@ namespace System.Collections.Immutable [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableEnumerableDebuggerProxy<>))] #if NET - public sealed partial class ImmutableHashSet : IImmutableSet, IHashKeyCollection, IReadOnlyCollection, ICollection, ISet, IReadOnlySet, ICollection, IStrongEnumerable.Enumerator> + public sealed partial class ImmutableHashSet : IImmutableSet, IReadOnlyCollection, ICollection, ISet, IReadOnlySet, ICollection, IStrongEnumerable.Enumerator> #else - public sealed partial class ImmutableHashSet : IImmutableSet, IHashKeyCollection, IReadOnlyCollection, ICollection, ISet, ICollection, IStrongEnumerable.Enumerator> + public sealed partial class ImmutableHashSet : IImmutableSet, IReadOnlyCollection, ICollection, ISet, ICollection, IStrongEnumerable.Enumerator> #endif { /// @@ -102,8 +102,6 @@ public bool IsEmpty get { return this.Count == 0; } } - #region IHashKeyCollection Properties - /// /// See the interface. /// @@ -112,8 +110,6 @@ public IEqualityComparer KeyComparer get { return _equalityComparer; } } - #endregion - #region IImmutableSet Properties /// @@ -152,14 +148,6 @@ bool ICollection.IsSynchronized #endregion - /// - /// Gets the root node (for testing purposes). - /// - internal IBinaryTree Root - { - get { return _root; } - } - /// /// Gets a data structure that captures the current state of this map, as an input into a query or mutating function. /// diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs index 5f43b156cd6c27..fc75f69a594d6c 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs @@ -28,7 +28,7 @@ public sealed partial class ImmutableList /// [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableListBuilderDebuggerProxy<>))] - public sealed class Builder : IList, IList, IOrderedCollection, IImmutableListQueries, IReadOnlyList + public sealed class Builder : IList, IList, IReadOnlyList { /// /// The binary tree used to store the contents of the list. Contents are typically not entirely frozen. @@ -137,17 +137,6 @@ public T this[int index] } } - /// - /// Gets the element in the collection at a given index. - /// - T IOrderedCollection.this[int index] - { - get - { - return this[index]; - } - } - /// /// Gets a read-only reference to the value for a given index into the list. /// diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs index a83976402ebcad..03eae53582c15c 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs @@ -14,7 +14,7 @@ public sealed partial class ImmutableList /// A node in the AVL tree storing this set. /// [DebuggerDisplay("{_key}")] - internal sealed class Node : IBinaryTree, IEnumerable + internal sealed class Node : IEnumerable { /// /// The default empty node. @@ -120,31 +120,11 @@ public bool IsEmpty /// public Node? Left => _left; - /// - /// Gets the left branch of this node. - /// - IBinaryTree? IBinaryTree.Left => _left; - /// /// Gets the right branch of this node. /// public Node? Right => _right; - /// - /// Gets the right branch of this node. - /// - IBinaryTree? IBinaryTree.Right => _right; - - /// - /// Gets the left branch of this node. - /// - IBinaryTree? IBinaryTree.Left => _left; - - /// - /// Gets the right branch of this node. - /// - IBinaryTree? IBinaryTree.Right => _right; - /// /// Gets the value represented by the current node. /// @@ -260,7 +240,7 @@ private ref readonly T ItemRefUnchecked(int index) /// The starting index within that should be captured by the node tree. /// The number of elements from that should be captured by the node tree. /// The root of the created node tree. - internal static Node NodeTreeFromList(IOrderedCollection items, int start, int length) + internal static Node NodeTreeFromList(IReadOnlyList items, int start, int length) { Requires.NotNull(items, nameof(items)); Requires.Range(start >= 0, nameof(start)); @@ -597,7 +577,7 @@ internal Node Sort(Comparison comparison) var array = new T[this.Count]; this.CopyTo(array); Array.Sort(array, comparison); - return NodeTreeFromList(array.AsOrderedCollection(), 0, this.Count); + return NodeTreeFromList(array.AsReadOnlyList(), 0, this.Count); } /// @@ -636,7 +616,7 @@ internal Node Sort(int index, int count, IComparer? comparer) var array = new T[this.Count]; this.CopyTo(array); Array.Sort(array, index, count, comparer); - return NodeTreeFromList(array.AsOrderedCollection(), 0, this.Count); + return NodeTreeFromList(array.AsReadOnlyList(), 0, this.Count); } /// @@ -1578,7 +1558,7 @@ private static Node CreateRange(IEnumerable keys) return other._root; } - IOrderedCollection list = keys.AsOrderedCollection(); + IReadOnlyList list = keys.AsReadOnlyList(); return NodeTreeFromList(list, 0, list.Count); } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs index e962b19d1a786a..cdca30b5dd91ca 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs @@ -16,7 +16,7 @@ namespace System.Collections.Immutable [CollectionBuilder(typeof(ImmutableList), nameof(ImmutableList.Create))] [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableEnumerableDebuggerProxy<>))] - public sealed partial class ImmutableList : IImmutableList, IList, IList, IOrderedCollection, IImmutableListQueries, IStrongEnumerable.Enumerator> + public sealed partial class ImmutableList : IImmutableList, IList, IList, IStrongEnumerable.Enumerator> { /// /// An empty immutable list. @@ -180,15 +180,6 @@ private ImmutableList(Node root) #endregion - #region IOrderedCollection Indexers - - /// - /// Gets the element in the collection at a given index. - /// - T IOrderedCollection.this[int index] => this[index]; - - #endregion - #region Public methods /// @@ -1097,11 +1088,6 @@ T IList.this[int index] /// public Enumerator GetEnumerator() => new Enumerator(_root); - /// - /// Returns the root of the list - /// - internal Node Root => _root; - /// /// Creates a new sorted set wrapper for a node tree. /// @@ -1191,7 +1177,7 @@ private static ImmutableList CreateRange(IEnumerable items) // index into that sequence like a list, so the one possible piece of // garbage produced is a temporary array to store the list while // we build the tree. - IOrderedCollection list = items.AsOrderedCollection(); + IReadOnlyList list = items.AsReadOnlyList(); if (list.Count == 0) { return Empty; diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs index 76330f19c18c8b..280682da09e557 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs @@ -14,7 +14,7 @@ public sealed partial class ImmutableSortedDictionary /// A node in the AVL tree storing this map. /// [DebuggerDisplay("{_key} = {_value}")] - internal sealed class Node : IBinaryTree>, IEnumerable> + internal sealed class Node : IEnumerable> { /// /// The default empty node. @@ -105,22 +105,6 @@ public bool IsEmpty } } - /// - /// Gets the left branch of this node. - /// - IBinaryTree>? IBinaryTree>.Left - { - get { return _left; } - } - - /// - /// Gets the right branch of this node. - /// - IBinaryTree>? IBinaryTree>.Right - { - get { return _right; } - } - /// /// Gets the height of the tree beneath this node. /// @@ -131,27 +115,11 @@ public bool IsEmpty /// public Node? Left { get { return _left; } } - /// - /// Gets the left branch of this node. - /// - IBinaryTree? IBinaryTree.Left - { - get { return _left; } - } - /// /// Gets the right branch of this node. /// public Node? Right { get { return _right; } } - /// - /// Gets the right branch of this node. - /// - IBinaryTree? IBinaryTree.Right - { - get { return _right; } - } - /// /// Gets the value represented by the current node. /// @@ -160,14 +128,6 @@ public KeyValuePair Value get { return new KeyValuePair(_key, _value); } } - /// - /// Gets the number of elements contained by this node and below. - /// - int IBinaryTree.Count - { - get { throw new NotSupportedException(); } - } - /// /// Gets the keys. /// @@ -272,7 +232,7 @@ internal static Node NodeTreeFromSortedDictionary(SortedDictionary { Requires.NotNull(dictionary, nameof(dictionary)); - IOrderedCollection> list = dictionary.AsOrderedCollection(); + IReadOnlyList> list = dictionary.AsReadOnlyList(); return NodeTreeFromList(list, 0, list.Count); } @@ -626,7 +586,7 @@ private static Node MakeBalanced(Node tree) /// The starting index within that should be captured by the node tree. /// The number of elements from that should be captured by the node tree. /// The root of the created node tree. - private static Node NodeTreeFromList(IOrderedCollection> items, int start, int length) + private static Node NodeTreeFromList(IReadOnlyList> items, int start, int length) { Requires.NotNull(items, nameof(items)); Requires.Range(start >= 0, nameof(start)); diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs index d44687253fa527..ab7d775fd5c37c 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs @@ -16,7 +16,7 @@ namespace System.Collections.Immutable /// The type of the value. [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableDictionaryDebuggerProxy<,>))] - public sealed partial class ImmutableSortedDictionary : IImmutableDictionary, ISortKeyCollection, IDictionary, IDictionary + public sealed partial class ImmutableSortedDictionary : IImmutableDictionary, IDictionary, IDictionary { /// /// An empty sorted dictionary with default sort and equality comparers. @@ -177,14 +177,6 @@ public IComparer KeyComparer #endregion - /// - /// Gets the root node (for testing purposes). - /// - internal Node Root - { - get { return _root; } - } - #region IImmutableMap Indexers /// diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs index 1aa94496f43a74..2f0a963972d081 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs @@ -28,7 +28,7 @@ public sealed partial class ImmutableSortedSet /// [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableSortedSetBuilderDebuggerProxy<>))] - public sealed class Builder : ISortKeyCollection, IReadOnlyCollection, ISet, ICollection + public sealed class Builder : IReadOnlyCollection, ISet, ICollection { /// /// The root of the binary tree that stores the collection. Contents are typically not entirely frozen. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs index 8021f77ce38d0c..f81689196614e5 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs @@ -13,7 +13,7 @@ public sealed partial class ImmutableSortedSet /// A node in the AVL tree storing this set. /// [DebuggerDisplay("{_key}")] - internal sealed class Node : IBinaryTree, IEnumerable + internal sealed class Node : IEnumerable { /// /// The default empty node. @@ -119,14 +119,6 @@ public Node? Left get { return _left; } } - /// - /// Gets the left branch of this node. - /// - IBinaryTree? IBinaryTree.Left - { - get { return _left; } - } - /// /// Gets the right branch of this node. /// @@ -135,30 +127,6 @@ public Node? Right get { return _right; } } - /// - /// Gets the right branch of this node. - /// - IBinaryTree? IBinaryTree.Right - { - get { return _right; } - } - - /// - /// Gets the left branch of this node. - /// - IBinaryTree? IBinaryTree.Left - { - get { return _left; } - } - - /// - /// Gets the right branch of this node. - /// - IBinaryTree? IBinaryTree.Right - { - get { return _right; } - } - /// /// Gets the value represented by the current node. /// @@ -750,7 +718,7 @@ private static Node MakeBalanced(Node tree) /// The starting index within that should be captured by the node tree. /// The number of elements from that should be captured by the node tree. /// The root of the created node tree. - internal static Node NodeTreeFromList(IOrderedCollection items, int start, int length) + internal static Node NodeTreeFromList(IReadOnlyList items, int start, int length) { Requires.NotNull(items, nameof(items)); Debug.Assert(start >= 0); diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs index ee1d369c6ea144..b6f3bf81b0f105 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs @@ -21,9 +21,9 @@ namespace System.Collections.Immutable [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableEnumerableDebuggerProxy<>))] #if NET - public sealed partial class ImmutableSortedSet : IImmutableSet, ISortKeyCollection, IReadOnlySet, IReadOnlyList, IList, ISet, IList, IStrongEnumerable.Enumerator> + public sealed partial class ImmutableSortedSet : IImmutableSet, IReadOnlySet, IReadOnlyList, IList, ISet, IList, IStrongEnumerable.Enumerator> #else - public sealed partial class ImmutableSortedSet : IImmutableSet, ISortKeyCollection, IReadOnlyList, IList, ISet, IList, IStrongEnumerable.Enumerator> + public sealed partial class ImmutableSortedSet : IImmutableSet, IReadOnlyList, IList, ISet, IList, IStrongEnumerable.Enumerator> #endif { /// @@ -119,26 +119,14 @@ public int Count #endregion - #region ISortKeyCollection Properties - /// - /// See the interface. + /// See the interface. /// public IComparer KeyComparer { get { return _comparer; } } - #endregion - - /// - /// Gets the root node (for testing purposes). - /// - internal IBinaryTree Root - { - get { return _root; } - } - #region IReadOnlyList Indexers /// @@ -1169,7 +1157,7 @@ private ImmutableSortedSet LeafToRootRefill(IEnumerable addedItems) list.RemoveRange(index, list.Count - index); // Use the now sorted list of unique items to construct a new sorted set. - Node root = Node.NodeTreeFromList(list.AsOrderedCollection(), 0, list.Count); + Node root = Node.NodeTreeFromList(list.AsReadOnlyList(), 0, list.Count); return this.Wrap(root); } @@ -1227,7 +1215,7 @@ private ImmutableSortedSet LeafToRootRefill(ReadOnlySpan addedItems) list.RemoveRange(index, list.Count - index); // Use the now sorted list of unique items to construct a new sorted set. - Node root = Node.NodeTreeFromList(list.AsOrderedCollection(), 0, list.Count); + Node root = Node.NodeTreeFromList(list.AsReadOnlyList(), 0, list.Count); return this.Wrap(root); } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs index 08b011fc160ec5..115f7403986b46 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs @@ -19,7 +19,7 @@ namespace System.Collections.Immutable /// the comparer's implementation. /// [DebuggerDisplay("{_key} = {_value}")] - internal sealed partial class SortedInt32KeyNode : IBinaryTree + internal sealed partial class SortedInt32KeyNode { /// /// The default empty node. @@ -114,24 +114,6 @@ private SortedInt32KeyNode(int key, TValue value, SortedInt32KeyNode lef /// public SortedInt32KeyNode? Right { get { return _right; } } - /// - /// Gets the left branch of this node. - /// - IBinaryTree? IBinaryTree.Left { get { return _left; } } - - /// - /// Gets the right branch of this node. - /// - IBinaryTree? IBinaryTree.Right { get { return _right; } } - - /// - /// Gets the number of elements contained by this node and below. - /// - int IBinaryTree.Count - { - get { throw new NotSupportedException(); } - } - /// /// Gets the value represented by the current node. /// diff --git a/src/libraries/System.Collections.Immutable/tests/BinaryTreeProxy.cs b/src/libraries/System.Collections.Immutable/tests/BinaryTreeProxy.cs new file mode 100644 index 00000000000000..d1eba56cbbb825 --- /dev/null +++ b/src/libraries/System.Collections.Immutable/tests/BinaryTreeProxy.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Collections.Immutable.Tests +{ + /// + /// Defines a proxy class for accessing non-public binary tree implementation details. + /// + public sealed class BinaryTreeProxy(object underlyingValue, Type underlyingType) + { + public int Height => (int)GetProperty(nameof(Height)); + public bool IsEmpty => (bool)GetProperty(nameof(IsEmpty)); + public int Count => (int)GetProperty(nameof(Count)); + public BinaryTreeProxy? Left => GetProperty(nameof(Left)) is { } leftValue + ? new(leftValue, underlyingType) + : null; + + public BinaryTreeProxy? Right => GetProperty(nameof(Right)) is { } rightValue + ? new(rightValue, underlyingType) + : null; + + private object? GetProperty(string propertyName) + => underlyingType.GetProperty(propertyName)!.GetValue(underlyingValue); + } +} diff --git a/src/libraries/System.Collections.Immutable/tests/Frozen/KeyAnalyzerTests.cs b/src/libraries/System.Collections.Immutable/tests/Frozen/KeyAnalyzerTests.cs index 00a16bf3cef54d..7e82e00390f093 100644 --- a/src/libraries/System.Collections.Immutable/tests/Frozen/KeyAnalyzerTests.cs +++ b/src/libraries/System.Collections.Immutable/tests/Frozen/KeyAnalyzerTests.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; using System.Collections.Generic; using System.Linq; using Xunit; diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs index 6bac524a42851c..841bfc451e332d 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs @@ -57,7 +57,7 @@ public void NormalConstructionValueType() [Fact] public void NormalConstructionRefType() { - var builder = new ImmutableArray.Builder(3); + var builder = ImmutableArray.CreateBuilder(); Assert.Equal(0, builder.Count); Assert.False(((ICollection)builder).IsReadOnly); for (int i = 0; i < builder.Count; i++) @@ -77,7 +77,7 @@ public void NormalConstructionRefType() [Fact] public void AddRangeIEnumerable() { - var builder = new ImmutableArray.Builder(2); + var builder = ImmutableArray.CreateBuilder(2); builder.AddRange((IEnumerable)new[] { 1 }); Assert.Equal(1, builder.Count); @@ -119,8 +119,8 @@ public void Add() [Fact] public void AddRangeBuilder() { - ImmutableArray.Builder builder1 = new ImmutableArray.Builder(2); - ImmutableArray.Builder builder2 = new ImmutableArray.Builder(2); + ImmutableArray.Builder builder1 = ImmutableArray.CreateBuilder(2); + ImmutableArray.Builder builder2 = ImmutableArray.CreateBuilder(2); builder1.AddRange(builder2); Assert.Equal(0, builder1.Count); @@ -137,7 +137,7 @@ public void AddRangeBuilder() [Fact] public void AddRangeImmutableArray() { - ImmutableArray.Builder builder1 = new ImmutableArray.Builder(2); + ImmutableArray.Builder builder1 = ImmutableArray.CreateBuilder(2); ImmutableArray array = ImmutableArray.Create(1, 2, 3); builder1.AddRange(array); @@ -154,8 +154,8 @@ public void AddRangeImmutableArray() Assert.Throws(() => builder1.AddRange(default(ImmutableArray))); builder1.AddRange(default(ImmutableArray), 42); - var builder2 = new ImmutableArray.Builder(); - builder2.AddRange(default(ImmutableArray)); + var builder2 = ImmutableArray.CreateBuilder(); + AssertExtensions.Throws(() => builder2.AddRange(default(ImmutableArray))); AssertExtensions.Throws("items", () => builder2.AddRange((ImmutableArray.Builder)null)); } @@ -164,7 +164,7 @@ public void AddRangeImmutableArray() public void AddRangeDerivedArray(string[] builderElements, string[] rangeElements, string[] expectedResult) { // Initialize builder - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(builderElements); // AddRange @@ -179,7 +179,7 @@ public void AddRangeDerivedArray(string[] builderElements, string[] rangeElement public void AddRangeSpan(string[] builderElements, string[] rangeElements, string[] expectedResult) { // Initialize builder - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(builderElements); // AddRange @@ -194,7 +194,7 @@ public void AddRangeSpan(string[] builderElements, string[] rangeElements, strin public void AddRangeDerivedSpan(string[] builderElements, string[] rangeElements, string[] expectedResult) { // Initialize builder - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(builderElements); // AddRange @@ -209,7 +209,7 @@ public void AddRangeDerivedSpan(string[] builderElements, string[] rangeElements public void AddRangeDerivedImmutableArray(string[] builderElements, string[] rangeElements, string[] expectedResult) { // Initialize builder - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(builderElements); // AddRange @@ -224,11 +224,11 @@ public void AddRangeDerivedImmutableArray(string[] builderElements, string[] ran public void AddRangeDerivedBuilder(string[] builderElements, string[] rangeElements, string[] expectedResult) { // Initialize builder - var builderBase = new ImmutableArray.Builder(); + var builderBase = ImmutableArray.CreateBuilder(); builderBase.AddRange(builderElements); // Prepare another builder to add - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(rangeElements); // AddRange @@ -241,7 +241,7 @@ public void AddRangeDerivedBuilder(string[] builderElements, string[] rangeEleme [Fact] public void Contains() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); Assert.False(builder.Contains(1)); builder.Add(1); Assert.True(builder.Contains(1)); @@ -283,7 +283,7 @@ public void LastIndexOf() [Fact] public void Insert() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(1, 2, 3); builder.Insert(1, 4); builder.Insert(4, 5); @@ -295,7 +295,7 @@ public void Insert() [Fact] public void InsertRange() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.InsertRange(0, Enumerable.Range(1, 4)); Assert.Equal(new[] { 1, 2, 3, 4 }, builder); @@ -303,7 +303,7 @@ public void InsertRange() builder.InsertRange(1, Enumerable.Range(5, 2)); Assert.Equal(new[] { 1, 5, 6, 2, 3, 4 }, builder); - builder.InsertRange(0, new ImmutableArray(new int[] { 7, 8 })); + builder.InsertRange(0, ImmutableArray.Create(new int[] { 7, 8 })); Assert.Equal(new[] { 7, 8, 1, 5, 6, 2, 3, 4 }, builder); AssertExtensions.Throws("index", () => builder.InsertRange(-1, Enumerable.Range(1, 2))); @@ -317,7 +317,7 @@ public void InsertRange() [Fact] public void Remove() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(1, 2, 3, 4); Assert.True(builder.Remove(1)); Assert.False(builder.Remove(6)); @@ -346,7 +346,7 @@ public void Remove_EqualityComparer() [Fact] public void RemoveAt() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(1, 2, 3, 4); builder.RemoveAt(0); AssertExtensions.Throws("index", () => builder.RemoveAt(-1)); @@ -363,7 +363,7 @@ public void RemoveAt() [Fact] public void RemoveRange_ValueType() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(1, 2, 3, 4, 5); builder.RemoveRange(1, 2); @@ -386,7 +386,7 @@ public void RemoveRange_ValueType() [Fact] public void RemoveRange_ReferenceType() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(new GenericParameterHelper(1), new GenericParameterHelper(2), new GenericParameterHelper(3), new GenericParameterHelper(4)); builder.RemoveRange(1, 2); @@ -408,7 +408,7 @@ public void RemoveRange_EqualityComparer() [Fact] public void RemoveAll() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(Enumerable.Range(1, 8)); builder.RemoveAll(n => n % 2 == 0); @@ -418,7 +418,7 @@ public void RemoveAll() [Fact] public void ReverseContents() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(1, 2, 3, 4); builder.Reverse(); Assert.Equal(new[] { 4, 3, 2, 1 }, builder); @@ -443,7 +443,7 @@ public void ReverseContents() [Fact] public void Sort() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(2, 4, 1, 3); builder.Sort(); Assert.Equal(new[] { 1, 2, 3, 4 }, builder); @@ -452,7 +452,7 @@ public void Sort() [Fact] public void Sort_Comparison() { - var builder = new ImmutableArray.Builder(4); + var builder = ImmutableArray.CreateBuilder(4); builder.Sort((x, y) => y.CompareTo(x)); Assert.Equal(Array.Empty(), builder); @@ -491,17 +491,17 @@ public void SortOneElementArray() { int[] resultantArray = new[] { 4 }; - var builder1 = new ImmutableArray.Builder(); + var builder1 = ImmutableArray.CreateBuilder(); builder1.Add(4); builder1.Sort(); Assert.Equal(resultantArray, builder1); - var builder2 = new ImmutableArray.Builder(); + var builder2 = ImmutableArray.CreateBuilder(); builder2.Add(4); builder2.Sort(Comparer.Default); Assert.Equal(resultantArray, builder2); - var builder3 = new ImmutableArray.Builder(); + var builder3 = ImmutableArray.CreateBuilder(); builder3.Add(4); builder3.Sort(0, 1, Comparer.Default); Assert.Equal(resultantArray, builder3); @@ -510,7 +510,7 @@ public void SortOneElementArray() [Fact] public void SortRange() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(2, 4, 1, 3); AssertExtensions.Throws("index", () => builder.Sort(-1, 2, Comparer.Default)); AssertExtensions.Throws("count", () => builder.Sort(1, 4, Comparer.Default)); @@ -526,8 +526,8 @@ public void SortRange() [Fact] public void SortComparer() { - var builder1 = new ImmutableArray.Builder(); - var builder2 = new ImmutableArray.Builder(); + var builder1 = ImmutableArray.CreateBuilder(); + var builder2 = ImmutableArray.CreateBuilder(); builder1.AddRange("c", "B", "a"); builder2.AddRange("c", "B", "a"); builder1.Sort(StringComparer.OrdinalIgnoreCase); @@ -539,7 +539,7 @@ public void SortComparer() [Fact] public void Count() { - var builder = new ImmutableArray.Builder(3); + var builder = ImmutableArray.CreateBuilder(3); // Initial count is at zero, which is less than capacity. Assert.Equal(0, builder.Count); @@ -566,7 +566,7 @@ public void Count() [Fact] public void CountContract() { - var builder = new ImmutableArray.Builder(100); + var builder = ImmutableArray.CreateBuilder(100); builder.AddRange(Enumerable.Range(1, 100)); builder.Count = 10; Assert.Equal(Enumerable.Range(1, 10), builder); @@ -577,7 +577,7 @@ public void CountContract() [Fact] public void IndexSetter() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); Assert.Throws(() => builder[0] = 1); Assert.Throws(() => builder[-1] = 1); @@ -597,7 +597,7 @@ public void IndexSetter() [Fact] public void ToImmutable() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(1, 2, 3); ImmutableArray array = builder.ToImmutable(); @@ -617,7 +617,7 @@ public void ToImmutable() [Fact] public void ToImmutableArray() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.AddRange(0, 1, 2); ImmutableArray array = builder.ToImmutableArray(); @@ -705,7 +705,7 @@ public void CopyToSpan() [Fact] public void Clear() { - var builder = new ImmutableArray.Builder(2); + var builder = ImmutableArray.CreateBuilder(2); builder.Add(1); builder.Add(1); builder.Clear(); @@ -716,7 +716,7 @@ public void Clear() [Fact] public void MutationsSucceedAfterToImmutable() { - var builder = new ImmutableArray.Builder(1); + var builder = ImmutableArray.CreateBuilder(1); builder.Add(1); ImmutableArray immutable = builder.ToImmutable(); builder[0] = 0; @@ -727,11 +727,11 @@ public void MutationsSucceedAfterToImmutable() [Fact] public void Enumerator() { - var empty = new ImmutableArray.Builder(0); + var empty = ImmutableArray.CreateBuilder(0); IEnumerator enumerator = empty.GetEnumerator(); Assert.False(enumerator.MoveNext()); - var manyElements = new ImmutableArray.Builder(3); + var manyElements = ImmutableArray.CreateBuilder(3); manyElements.AddRange(1, 2, 3); enumerator = manyElements.GetEnumerator(); @@ -748,11 +748,11 @@ public void Enumerator() [Fact] public void IEnumerator() { - var empty = new ImmutableArray.Builder(0); + var empty = ImmutableArray.CreateBuilder(0); IEnumerator enumerator = ((IEnumerable)empty).GetEnumerator(); Assert.False(enumerator.MoveNext()); - var manyElements = new ImmutableArray.Builder(3); + var manyElements = ImmutableArray.CreateBuilder(3); manyElements.AddRange(1, 2, 3); enumerator = ((IEnumerable)manyElements).GetEnumerator(); @@ -1108,7 +1108,7 @@ public static void TestDebuggerAttributes_Null() [Fact] public void ItemRef() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.Add(1); builder.Add(2); builder.Add(3); @@ -1126,7 +1126,7 @@ public void ItemRef() [Fact] public void ItemRef_OutOfBounds() { - var builder = new ImmutableArray.Builder(); + var builder = ImmutableArray.CreateBuilder(); builder.Add(1); builder.Add(2); builder.Add(3); @@ -1143,7 +1143,7 @@ private static ImmutableArray.Builder CreateBuilderWithCount(int count) protected override IEnumerable GetEnumerableOf(params T[] contents) { - var builder = new ImmutableArray.Builder(contents.Length); + var builder = ImmutableArray.CreateBuilder(contents.Length); builder.AddRange(contents); return builder; } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.nonnetstandard.cs index c30e0738f377cf..d6ff8b49bf4b30 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.nonnetstandard.cs @@ -2,9 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Reflection; using Xunit; namespace System.Collections.Immutable.Tests @@ -25,14 +22,14 @@ public void EnumeratorWithHashCollisionsTest() this.EnumeratorTestHelper(emptyMap); } - internal override IBinaryTree GetRootNode(IImmutableDictionary dictionary) + internal override BinaryTreeProxy GetRootNode(IImmutableDictionary dictionary) { - return ((ImmutableDictionary)dictionary).Root; + return ((ImmutableDictionary)dictionary).GetBinaryTreeProxy(); } private void EmptyTestHelperHash(IImmutableDictionary empty, TKey someKey) { - Assert.Same(EqualityComparer.Default, ((IHashKeyCollection)empty).KeyComparer); + Assert.Same(EqualityComparer.Default, ((ImmutableDictionary)empty).KeyComparer); } } } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTestBase.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTestBase.nonnetstandard.cs index 97d1c2cfbed2bd..2396991a33a776 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTestBase.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTestBase.nonnetstandard.cs @@ -262,7 +262,7 @@ protected void EnumeratorTestHelper(IImmutableDictionary(() => manualEnum.Current); } - internal abstract IBinaryTree GetRootNode(IImmutableDictionary dictionary); + internal abstract BinaryTreeProxy GetRootNode(IImmutableDictionary dictionary); private static void KeysOrValuesTestHelper(ICollection collection, T containedValue) { @@ -287,7 +287,7 @@ private static void KeysOrValuesTestHelper(ICollection collection, T conta private void VerifyAvlTreeState(IImmutableDictionary dictionary) { - IBinaryTree rootNode = this.GetRootNode(dictionary); + BinaryTreeProxy rootNode = this.GetRootNode(dictionary); rootNode.VerifyBalanced(); rootNode.VerifyHeightIsWithinTolerance(dictionary.Count); } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableHashSetTest.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableHashSetTest.nonnetstandard.cs index 9ea7ce3d5c5577..a8e7547945d652 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableHashSetTest.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableHashSetTest.nonnetstandard.cs @@ -2,9 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Reflection; using Xunit; namespace System.Collections.Immutable.Tests @@ -24,9 +21,9 @@ public void TryGetValueTest() this.TryGetValueTestHelper(ImmutableHashSet.Empty.WithComparer(StringComparer.OrdinalIgnoreCase)); } - internal override IBinaryTree GetRootNode(IImmutableSet set) + internal override BinaryTreeProxy GetRootNode(IImmutableSet set) { - return ((ImmutableHashSet)set).Root; + return ((ImmutableHashSet)set).GetBinaryTreeProxy(); } /// @@ -42,7 +39,7 @@ private void EmptyTestHelper(IImmutableSet emptySet, T value, IEqualityCom this.EmptyTestHelper(emptySet); Assert.Same(emptySet, emptySet.ToImmutableHashSet(comparer)); - Assert.Same(comparer ?? EqualityComparer.Default, ((IHashKeyCollection)emptySet).KeyComparer); + Assert.Same(comparer ?? EqualityComparer.Default, ((ImmutableHashSet)emptySet).KeyComparer); if (comparer == null) { diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs index 01bad4efa1b5a5..741dfbd085cde4 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs @@ -435,7 +435,8 @@ public static void TestDebuggerAttributes_Null() public void ItemRef() { var list = new[] { 1, 2, 3 }.ToImmutableList(); - var builder = new ImmutableList.Builder(list); + var builder = ImmutableList.CreateBuilder(); + builder.AddRange(list); ref readonly int safeRef = ref builder.ItemRef(1); ref int unsafeRef = ref Unsafe.AsRef(in safeRef); @@ -451,7 +452,8 @@ public void ItemRef() public void ItemRef_OutOfBounds() { var list = new[] { 1, 2, 3 }.ToImmutableList(); - var builder = new ImmutableList.Builder(list); + var builder = ImmutableList.CreateBuilder(); + builder.AddRange(list); Assert.Throws(() => builder.ItemRef(5)); } @@ -483,7 +485,7 @@ public void ToImmutableList() protected override IEnumerable GetEnumerableOf(params T[] contents) { - return ImmutableList.Empty.AddRange((ReadOnlySpan)contents).ToBuilder(); + return ImmutableList.Empty.AddRange(contents).ToBuilder(); } protected override void RemoveAllTestHelper(ImmutableList list, Predicate test) @@ -506,9 +508,9 @@ protected override void ReverseTestHelper(ImmutableList list, int index, i Assert.Equal(expected, builder.ToList()); } - internal override IImmutableListQueries GetListQuery(ImmutableList list) + internal override ImmutableListQueries GetListQuery(ImmutableList list) { - return list.ToBuilder(); + return new ImmutableListBuilderQuery(list.ToBuilder()); } protected override List SortTestHelper(ImmutableList list) @@ -538,5 +540,29 @@ protected override List SortTestHelper(ImmutableList list, int index, i builder.Sort(index, count, comparer); return builder.ToImmutable().ToList(); } + + private sealed class ImmutableListBuilderQuery(ImmutableList.Builder list) : ImmutableListQueries(list) + { + public override int BinarySearch(T item) => list.BinarySearch(item); + public override int BinarySearch(T item, IComparer? comparer) => list.BinarySearch(item, comparer); + public override int BinarySearch(int index, int count, T item, IComparer? comparer) => list.BinarySearch(index, count, item, comparer); + public override ImmutableList ConvertAll(Func converter) => list.ConvertAll(converter); + public override void CopyTo(T[] array) => list.CopyTo(array); + public override void CopyTo(T[] array, int arrayIndex) => list.CopyTo(array, arrayIndex); + public override void CopyTo(int index, T[] array, int arrayIndex, int count) => list.CopyTo(index, array, arrayIndex, count); + public override bool Exists(Predicate match) => list.Exists(match); + public override T? Find(Predicate match) => list.Find(match); + public override ImmutableList FindAll(Predicate match) => list.FindAll(match); + public override int FindIndex(Predicate match) => list.FindIndex(match); + public override int FindIndex(int startIndex, Predicate match) => list.FindIndex(startIndex, match); + public override int FindIndex(int startIndex, int count, Predicate match) => list.FindIndex(startIndex, count, match); + public override T? FindLast(Predicate match) => list.FindLast(match); + public override int FindLastIndex(Predicate match) => list.FindLastIndex(match); + public override int FindLastIndex(int startIndex, Predicate match) => list.FindLastIndex(startIndex, match); + public override int FindLastIndex(int startIndex, int count, Predicate match) => list.FindLastIndex(startIndex, count, match); + public override void ForEach(Action action) => list.ForEach(action); + public override ImmutableList GetRange(int index, int count) => list.GetRange(index, count); + public override bool TrueForAll(Predicate match) => list.TrueForAll(match); + } } } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListQueries.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListQueries.cs new file mode 100644 index 00000000000000..eedfda01ce54c6 --- /dev/null +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListQueries.cs @@ -0,0 +1,62 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; + +namespace System.Collections.Immutable.Tests +{ + /// + /// Defines a bridging API for testing various immutable list implementations. + /// + public abstract class ImmutableListQueries(IList underlyingList) : ICollection, IList + { + public int Count => underlyingList.Count; + public T this[int index] => underlyingList[index]; + public abstract ImmutableList ConvertAll(Func converter); + public abstract void ForEach(Action action); + public abstract ImmutableList GetRange(int index, int count); + public abstract void CopyTo(T[] array); + public abstract void CopyTo(T[] array, int arrayIndex); + public abstract void CopyTo(int index, T[] array, int arrayIndex, int count); + public abstract bool Exists(Predicate match); + public abstract T? Find(Predicate match); + public abstract ImmutableList FindAll(Predicate match); + public abstract int FindIndex(Predicate match); + public abstract int FindIndex(int startIndex, Predicate match); + public abstract int FindIndex(int startIndex, int count, Predicate match); + public abstract T? FindLast(Predicate match); + public abstract int FindLastIndex(Predicate match); + public abstract int FindLastIndex(int startIndex, Predicate match); + public abstract int FindLastIndex(int startIndex, int count, Predicate match); + public abstract bool TrueForAll(Predicate match); + public abstract int BinarySearch(T item); + public abstract int BinarySearch(T item, IComparer? comparer); + public abstract int BinarySearch(int index, int count, T item, IComparer? comparer); + public IEnumerator GetEnumerator() => underlyingList.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => underlyingList.GetEnumerator(); + + + void ICollection.Add(T item) => underlyingList.Add(item); + void ICollection.Clear() => underlyingList.Clear(); + bool ICollection.Contains(T item) => underlyingList.Contains(item); + bool ICollection.Remove(T item) => underlyingList.Remove(item); + int IList.Add(object? value) => ((IList)underlyingList).Add(value); + void IList.Clear() => ((IList)underlyingList).Clear(); + bool IList.Contains(object? value) => ((IList)underlyingList).Contains(value); + int IList.IndexOf(object? value) => ((IList)underlyingList).IndexOf(value); + void IList.Insert(int index, object? value) => ((IList)underlyingList).Insert(index, value); + void IList.Remove(object? value) => ((IList)underlyingList).Remove(value); + void IList.RemoveAt(int index) => ((IList)underlyingList).RemoveAt(index); + void ICollection.CopyTo(Array array, int index) => ((ICollection)underlyingList).CopyTo(array, index); + bool ICollection.IsReadOnly => underlyingList.IsReadOnly; + bool IList.IsFixedSize => ((IList)underlyingList).IsFixedSize; + bool IList.IsReadOnly => ((IList)underlyingList).IsReadOnly; + bool ICollection.IsSynchronized => ((IList)underlyingList).IsSynchronized; + object ICollection.SyncRoot => ((IList)underlyingList).SyncRoot; + object? IList.this[int index] + { + get => underlyingList[index]; + set => underlyingList[index] = (T)value!; + } + } +} diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs index b3e2e8bc30bc3f..84fb7392466bef 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs @@ -206,7 +206,7 @@ public void AddRangeBalanceTest() Assert.Equal(Enumerable.Range(1, expectedTotalSize), list); - list.Root.VerifyHeightIsWithinTolerance(); + list.GetBinaryTreeProxy().VerifyHeightIsWithinTolerance(); } [Fact] @@ -235,10 +235,10 @@ public void InsertRangeRandomBalanceTest() list.InsertRange(startPosition, values); Assert.Equal(list, immutableList); - immutableList.Root.VerifyBalanced(); + immutableList.GetBinaryTreeProxy().VerifyBalanced(); } - immutableList.Root.VerifyHeightIsWithinTolerance(); + immutableList.GetBinaryTreeProxy().VerifyHeightIsWithinTolerance(); } [Fact] @@ -879,14 +879,14 @@ protected override List SortTestHelper(ImmutableList list, int index, i return list.Sort(index, count, comparer).ToList(); } - internal override IImmutableListQueries GetListQuery(ImmutableList list) + internal override ImmutableListQueries GetListQuery(ImmutableList list) { - return list; + return new ImmutableListQuery(list); } private static void VerifyBalanced(ImmutableList tree) { - tree.Root.VerifyBalanced(); + tree.GetBinaryTreeProxy().VerifyBalanced(); } private struct Person @@ -907,5 +907,29 @@ public int GetHashCode(Person obj) return obj.Name.GetHashCode(); } } + + private sealed class ImmutableListQuery(ImmutableList list) : ImmutableListQueries(list) + { + public override int BinarySearch(T item) => list.BinarySearch(item); + public override int BinarySearch(T item, IComparer? comparer) => list.BinarySearch(item, comparer); + public override int BinarySearch(int index, int count, T item, IComparer? comparer) => list.BinarySearch(index, count, item, comparer); + public override ImmutableList ConvertAll(Func converter) => list.ConvertAll(converter); + public override void CopyTo(T[] array) => list.CopyTo(array); + public override void CopyTo(T[] array, int arrayIndex) => list.CopyTo(array, arrayIndex); + public override void CopyTo(int index, T[] array, int arrayIndex, int count) => list.CopyTo(index, array, arrayIndex, count); + public override bool Exists(Predicate match) => list.Exists(match); + public override T? Find(Predicate match) => list.Find(match); + public override ImmutableList FindAll(Predicate match) => list.FindAll(match); + public override int FindIndex(Predicate match) => list.FindIndex(match); + public override int FindIndex(int startIndex, Predicate match) => list.FindIndex(startIndex, match); + public override int FindIndex(int startIndex, int count, Predicate match) => list.FindIndex(startIndex, count, match); + public override T? FindLast(Predicate match) => list.FindLast(match); + public override int FindLastIndex(Predicate match) => list.FindLastIndex(match); + public override int FindLastIndex(int startIndex, Predicate match) => list.FindLastIndex(startIndex, match); + public override int FindLastIndex(int startIndex, int count, Predicate match) => list.FindLastIndex(startIndex, count, match); + public override void ForEach(Action action) => list.ForEach(action); + public override ImmutableList GetRange(int index, int count) => list.GetRange(index, count); + public override bool TrueForAll(Predicate match) => list.TrueForAll(match); + } } } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs index 09eeac7214e530..86640628603a78 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs @@ -14,7 +14,7 @@ public abstract class ImmutableListTestBase : SimpleElementImmutablesTestBase protected static readonly Func ContainsFunc = (l, v) => l.Contains(v); protected static readonly Func RemoveFunc = (l, v) => { l.Remove(v); return l.Count; }; - internal abstract IImmutableListQueries GetListQuery(ImmutableList list); + internal abstract ImmutableListQueries GetListQuery(ImmutableList list); [Fact] public void CopyToEmptyTest() @@ -29,7 +29,7 @@ public void CopyToEmptyTest() [Fact] public void CopyToTest() { - IImmutableListQueries listQuery = this.GetListQuery(ImmutableList.Create(1, 2)); + ImmutableListQueries listQuery = this.GetListQuery(ImmutableList.Create(1, 2)); var list = (IEnumerable)listQuery; var array = new int[2]; @@ -397,7 +397,7 @@ public void SortTest() public void BinarySearch() { var basis = new List(Enumerable.Range(1, 50).Select(n => n * 2)); - IImmutableListQueries query = this.GetListQuery(basis.ToImmutableList()); + ImmutableListQueries query = this.GetListQuery(basis.ToImmutableList()); for (int value = basis.First() - 1; value <= basis.Last() + 1; value++) { int expected = basis.BinarySearch(value); @@ -436,7 +436,7 @@ private void BinarySearchPartialSortedListHelper(ImmutableArray inputData, int max = inputData[sortedIndex + sortedLength - 1]; var basis = new List(inputData); - IImmutableListQueries query = this.GetListQuery(inputData.ToImmutableList()); + ImmutableListQueries query = this.GetListQuery(inputData.ToImmutableList()); for (int value = min - 1; value <= max + 1; value++) { for (int index = sortedIndex; index < sortedIndex + sortedLength; index++) // make sure the index we pass in is always within the sorted range in the list. diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableSetTest.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableSetTest.nonnetstandard.cs index 821e0841920824..34b6ded17bdf05 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableSetTest.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableSetTest.nonnetstandard.cs @@ -26,13 +26,9 @@ public void SymmetricExceptTest() [Fact] public void EnumeratorTest() { - IComparer comparer = null; IImmutableSet set = this.Empty(); - var sortedSet = set as ISortKeyCollection; - if (sortedSet != null) - { - comparer = sortedSet.KeyComparer; - } + Assert.True(set is ImmutableSortedSet or ImmutableHashSet); + IComparer? comparer = (set as ImmutableSortedSet)?.KeyComparer; this.EnumeratorTestHelper(set, comparer, 3, 5, 1); double[] data = this.GenerateDummyFillData(); @@ -54,7 +50,7 @@ public void UnionTest() this.UnionTestHelper(this.Empty().Union(new[] { 2 }), Enumerable.Range(0, 1000).ToArray()); } - internal abstract IBinaryTree GetRootNode(IImmutableSet set); + internal abstract BinaryTreeProxy GetRootNode(IImmutableSet set); protected void TryGetValueTestHelper(IImmutableSet set) { @@ -134,7 +130,7 @@ private void UnionTestHelper(IImmutableSet set, params T[] values) private void VerifyAvlTreeState(IImmutableSet set) { - IBinaryTree rootNode = this.GetRootNode(set); + BinaryTreeProxy rootNode = this.GetRootNode(set); rootNode.VerifyBalanced(); rootNode.VerifyHeightIsWithinTolerance(set.Count); } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.nonnetstandard.cs index f3a0cacefd03d1..970977ee806519 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.nonnetstandard.cs @@ -13,9 +13,9 @@ namespace System.Collections.Immutable.Tests { public partial class ImmutableSortedDictionaryTest : ImmutableDictionaryTestBase { - internal override IBinaryTree GetRootNode(IImmutableDictionary dictionary) + internal override BinaryTreeProxy GetRootNode(IImmutableDictionary dictionary) { - return ((ImmutableSortedDictionary)dictionary).Root; + return ((ImmutableSortedDictionary)dictionary).GetBinaryTreeProxy(); } } } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderDebuggerProxyTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderDebuggerProxyTest.cs index 9b91a8bc37b30e..b9bdcda1f5da75 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderDebuggerProxyTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderDebuggerProxyTest.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Reflection; using Xunit; namespace System.Collections.Immutable.Tests @@ -15,10 +16,14 @@ public void DoesNotCacheContents() { ImmutableSortedSet set = ImmutableSortedSet.Empty.Add(1); ImmutableSortedSet.Builder builder = set.ToBuilder(); - var debuggerProxy = new ImmutableSortedSetBuilderDebuggerProxy(builder); - _ = debuggerProxy.Contents; // view the contents to trigger caching + DebuggerTypeProxyAttribute debuggerProxyAttribute = builder.GetType().GetCustomAttribute(); + Type debuggerProxyType = Type.GetType(debuggerProxyAttribute.ProxyTypeName).MakeGenericType(typeof(int)); + object debuggerProxy = Activator.CreateInstance(debuggerProxyType, [builder]); + _ = GetContents(); // view the contents to trigger caching builder.Add(2); - Assert.Equal(builder.ToArray(), debuggerProxy.Contents); + Assert.Equal(builder.ToArray(), GetContents()); + + ICollection GetContents() => (ICollection)debuggerProxyType.GetProperty("Contents").GetValue(debuggerProxy); } } } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.nonnetstandard.cs index 7b8dc5b37787dd..1ce51125d38e99 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.nonnetstandard.cs @@ -16,7 +16,11 @@ public partial class ImmutableSortedSetBuilderTest : ImmutablesTestBase public void ItemRef() { var array = new[] { 1, 2, 3 }.ToImmutableSortedSet(); - var builder = new ImmutableSortedSet.Builder(array); + var builder = ImmutableSortedSet.CreateBuilder(); + foreach (int item in array) + { + builder.Add(item); + } ref readonly int safeRef = ref builder.ItemRef(1); ref int unsafeRef = ref Unsafe.AsRef(in safeRef); @@ -32,7 +36,11 @@ public void ItemRef() public void ItemRef_OutOfBounds() { var array = new[] { 1, 2, 3 }.ToImmutableSortedSet(); - var builder = new ImmutableSortedSet.Builder(array); + var builder = ImmutableSortedSet.CreateBuilder(); + foreach (int item in array) + { + builder.Add(item); + } Assert.Throws(() => builder.ItemRef(5)); } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetTest.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetTest.nonnetstandard.cs index 32a91e0ebc3afa..7df72d18b19d78 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetTest.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedSetTest.nonnetstandard.cs @@ -26,9 +26,9 @@ public void TryGetValueTest() this.TryGetValueTestHelper(ImmutableSortedSet.Empty.WithComparer(StringComparer.OrdinalIgnoreCase)); } - internal override IBinaryTree GetRootNode(IImmutableSet set) + internal override BinaryTreeProxy GetRootNode(IImmutableSet set) { - return ((ImmutableSortedSet)set).Root; + return ((ImmutableSortedSet)set).GetBinaryTreeProxy(); } /// @@ -44,7 +44,7 @@ private void EmptyTestHelper(IImmutableSet emptySet, T value, IComparer this.EmptyTestHelper(emptySet); Assert.Same(emptySet, emptySet.ToImmutableSortedSet(comparer)); - Assert.Same(comparer ?? Comparer.Default, ((ISortKeyCollection)emptySet).KeyComparer); + Assert.Same(comparer ?? Comparer.Default, ((ImmutableSortedSet)emptySet).KeyComparer); IImmutableSet reemptied = emptySet.Add(value).Clear(); Assert.Same(reemptied, reemptied.ToImmutableSortedSet(comparer)); //, "Getting the empty set from a non-empty instance did not preserve the comparer."); diff --git a/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj b/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj index cc324a605ee68f..3f734f107c98b9 100644 --- a/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj +++ b/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj @@ -3,6 +3,7 @@ $(NoWarn);0436 $(NetCoreAppCurrent);$(NetFrameworkMinimum) true + true @@ -10,7 +11,13 @@ + + + + + + @@ -29,6 +36,7 @@ + diff --git a/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.cs b/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.cs index 6415cd41d5a57c..b96d74ed5d8fb5 100644 --- a/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.cs +++ b/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Reflection; using Xunit; using Xunit.Sdk; @@ -33,5 +34,16 @@ internal static void ValidateDefaultThisBehavior(ReadOnlySpan span, throw ThrowsException.ForNoException(typeof(NullReferenceException)); } + + internal static BinaryTreeProxy GetBinaryTreeProxy(this IReadOnlyCollection value) + { + FieldInfo rootField = value.GetType().GetField("_root", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.NotNull(rootField); + + object root = rootField.GetValue(value); + Assert.NotNull(root); + + return new BinaryTreeProxy(root, root.GetType()); + } } } diff --git a/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.nonnetstandard.cs b/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.nonnetstandard.cs index 7ea0f04c8cb887..3ed71c361a4d69 100644 --- a/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.nonnetstandard.cs +++ b/src/libraries/System.Collections.Immutable/tests/TestExtensionsMethods.nonnetstandard.cs @@ -36,7 +36,7 @@ internal static IDictionary ToBuilder(this IImmutabl /// Verifies that a binary tree is balanced according to AVL rules. /// /// The root node of the binary tree. - internal static void VerifyBalanced(this IBinaryTree node) + internal static void VerifyBalanced(this BinaryTreeProxy node) { if (node.Left != null) { @@ -66,8 +66,8 @@ internal static void VerifyBalanced(this IBinaryTree node) /// Verifies that a binary tree is no taller than necessary to store the data if it were optimally balanced. /// /// The root node. - /// The number of nodes in the tree. May be null if is functional. - internal static void VerifyHeightIsWithinTolerance(this IBinaryTree node, int? count = null) + /// The number of nodes in the tree. May be null if is functional. + internal static void VerifyHeightIsWithinTolerance(this BinaryTreeProxy node, int? count = null) { // http://en.wikipedia.org/wiki/AVL_tree double heightMustBeLessThan = Math.Log(2, s_GoldenRatio) * Math.Log(Math.Sqrt(5) * ((count ?? node.Count) + 2), 2) - 2;