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     /// <summary>
12     /// Contains tests that ensure the correctness of the Dictionary class.
13     /// </summary>
14     public abstract class SortedList_Generic_Tests<TKey, TValue> : IDictionary_Generic_Tests<TKey, TValue>
15     {
16         #region IDictionary<TKey, TValue> Helper Methods
17 
GenericIDictionaryFactory()18         protected override IDictionary<TKey, TValue> GenericIDictionaryFactory()
19         {
20             return new SortedList<TKey, TValue>();
21         }
22 
23         [Theory]
24         [MemberData(nameof(ValidCollectionSizes))]
Enumerator_MoveNext_AfterDisposal(int count)25         public override void Enumerator_MoveNext_AfterDisposal(int count)
26         {
27             // Disposal of the enumerator is treated the same as a Reset call
28             IEnumerator<KeyValuePair<TKey, TValue>> enumerator = GenericIEnumerableFactory(count).GetEnumerator();
29             for (int i = 0; i < count; i++)
30                 enumerator.MoveNext();
31             enumerator.Dispose();
32             if (count > 0)
33                 Assert.True(enumerator.MoveNext());
34         }
35 
36         protected override Type ICollection_Generic_CopyTo_IndexLargerThanArrayCount_ThrowType => typeof(ArgumentOutOfRangeException);
37 
38         #endregion
39 
40         #region Constructor_IComparer
41 
42         [Theory]
43         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Constructor_IComparer(int count)44         public void SortedList_Generic_Constructor_IComparer(int count)
45         {
46             IComparer<TKey> comparer = GetKeyIComparer();
47             IDictionary<TKey, TValue> source = GenericIDictionaryFactory(count);
48             SortedList<TKey, TValue> copied = new SortedList<TKey, TValue>(source, comparer);
49             Assert.Equal(source, copied);
50             Assert.Equal(comparer, copied.Comparer);
51         }
52 
53         #endregion
54 
55         #region Constructor_IDictionary
56 
57         [Theory]
58         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Constructor_IDictionary(int count)59         public void SortedList_Generic_Constructor_IDictionary(int count)
60         {
61             IDictionary<TKey, TValue> source = GenericIDictionaryFactory(count);
62             IDictionary<TKey, TValue> copied = new SortedList<TKey, TValue>(source);
63             Assert.Equal(source, copied);
64         }
65 
66         [Theory]
67         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Constructor_NullIDictionary_ThrowsArgumentNullException(int count)68         public void SortedList_Generic_Constructor_NullIDictionary_ThrowsArgumentNullException(int count)
69         {
70             Assert.Throws<ArgumentNullException>(() => new SortedList<TKey, TValue>((IDictionary<TKey, TValue>)null));
71         }
72 
73         #endregion
74 
75         #region Constructor_IDictionary_IComparer
76 
77         [Theory]
78         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Constructor_IDictionary_IComparer(int count)79         public void SortedList_Generic_Constructor_IDictionary_IComparer(int count)
80         {
81             IComparer<TKey> comparer = GetKeyIComparer();
82             IDictionary<TKey, TValue> source = GenericIDictionaryFactory(count);
83             SortedList<TKey, TValue> copied = new SortedList<TKey, TValue>(source, comparer);
84             Assert.Equal(source, copied);
85             Assert.Equal(comparer, copied.Comparer);
86         }
87 
88         #endregion
89 
90         #region Constructor_int
91 
92         [Theory]
93         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Constructor_int(int count)94         public void SortedList_Generic_Constructor_int(int count)
95         {
96             SortedList<TKey, TValue> dictionary = new SortedList<TKey, TValue>(count);
97             Assert.Equal(0, dictionary.Count);
98             Assert.Equal(count, dictionary.Capacity);
99         }
100 
101         [Theory]
102         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Constructor_NegativeCapacity_ThrowsArgumentOutOfRangeException(int count)103         public void SortedList_Generic_Constructor_NegativeCapacity_ThrowsArgumentOutOfRangeException(int count)
104         {
105             Assert.Throws<ArgumentOutOfRangeException>(() => new SortedList<TKey, TValue>(-1));
106             Assert.Throws<ArgumentOutOfRangeException>(() => new SortedList<TKey, TValue>(int.MinValue));
107         }
108 
109         #endregion
110 
111         #region Constructor_int_IComparer
112 
113         [Theory]
114         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Constructor_int_IComparer(int count)115         public void SortedList_Generic_Constructor_int_IComparer(int count)
116         {
117             IComparer<TKey> comparer = GetKeyIComparer();
118             SortedList<TKey, TValue> dictionary = new SortedList<TKey, TValue>(count, comparer);
119             Assert.Equal(0, dictionary.Count);
120             Assert.Equal(comparer, dictionary.Comparer);
121             Assert.Equal(count, dictionary.Capacity);
122         }
123 
124         #endregion
125 
126         #region Capacity
127 
128         [Theory]
129         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Capacity_setRoundTrips(int count)130         public void SortedList_Generic_Capacity_setRoundTrips(int count)
131         {
132             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
133             dictionary.Capacity = count * 2;
134             Assert.Equal(count * 2, dictionary.Capacity);
135 
136             dictionary.Capacity = count * 2 + 16000;
137             Assert.Equal(count * 2 + 16000, dictionary.Capacity);
138         }
139 
140         [Theory]
141         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Capacity_NegativeValue_ThrowsArgumentOutOfRangeException(int count)142         public void SortedList_Generic_Capacity_NegativeValue_ThrowsArgumentOutOfRangeException(int count)
143         {
144             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
145             int capacityBefore = dictionary.Capacity;
146             AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => dictionary.Capacity = -1);
147             Assert.Equal(capacityBefore, dictionary.Capacity);
148         }
149 
150         [Theory]
151         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Capacity_LessThanCount_ThrowsArgumentOutOfRangeException(int count)152         public void SortedList_Generic_Capacity_LessThanCount_ThrowsArgumentOutOfRangeException(int count)
153         {
154             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory();
155             for (int i = 0; i < count; i++)
156             {
157                 AddToCollection(dictionary, 1);
158                 AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => dictionary.Capacity = i);
159             }
160         }
161 
162         [Theory]
163         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Capacity_GrowsDuringAdds(int count)164         public void SortedList_Generic_Capacity_GrowsDuringAdds(int count)
165         {
166             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory();
167             int capacity = 4;
168             for (int i = 0; i < count; i++)
169             {
170                 AddToCollection(dictionary, 1);
171 
172                 //if the array needs to grow, it doubles the size
173                 if (i == capacity)
174                     capacity *= 2;
175                 if (i <= capacity + 1)
176                     Assert.Equal(capacity, dictionary.Capacity);
177                 else
178                     Assert.Equal(i, dictionary.Capacity);
179             }
180         }
181 
182         [Theory]
183         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Capacity_ClearDoesntTrim(int count)184         public void SortedList_Generic_Capacity_ClearDoesntTrim(int count)
185         {
186             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory();
187             int capacity = 4;
188             for (int i = 0; i < count; i++)
189             {
190                 AddToCollection(dictionary, 1);
191 
192                 //if the array needs to grow, it doubles the size
193                 if (i == capacity)
194                     capacity *= 2;
195             }
196             dictionary.Clear();
197             if (count == 0)
198                 Assert.Equal(0, dictionary.Capacity);
199             else
200                 Assert.Equal(capacity, dictionary.Capacity);
201         }
202 
203         [Theory]
204         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_Capacity_ClearTrimsToInitialCapacity(int count)205         public void SortedList_Generic_Capacity_ClearTrimsToInitialCapacity(int count)
206         {
207             SortedList<TKey, TValue> dictionary = new SortedList<TKey, TValue>(count);
208             AddToCollection(dictionary, count);
209             dictionary.Clear();
210             Assert.Equal(count, dictionary.Capacity);
211         }
212 
213         #endregion
214 
215         #region ContainsValue
216 
217         [Theory]
218         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_ContainsValue_NotPresent(int count)219         public void SortedList_Generic_ContainsValue_NotPresent(int count)
220         {
221             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
222             int seed = 4315;
223             TValue notPresent = CreateTValue(seed++);
224             while (dictionary.Values.Contains(notPresent))
225                 notPresent = CreateTValue(seed++);
226             Assert.False(dictionary.ContainsValue(notPresent));
227         }
228 
229         [Theory]
230         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_ContainsValue_Present(int count)231         public void SortedList_Generic_ContainsValue_Present(int count)
232         {
233             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
234             int seed = 4315;
235             KeyValuePair<TKey, TValue> notPresent = CreateT(seed++);
236             while (dictionary.Contains(notPresent))
237                 notPresent = CreateT(seed++);
238             dictionary.Add(notPresent.Key, notPresent.Value);
239             Assert.True(dictionary.ContainsValue(notPresent.Value));
240         }
241 
242         [Theory]
243         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_ContainsValue_DefaultValueNotPresent(int count)244         public void SortedList_Generic_ContainsValue_DefaultValueNotPresent(int count)
245         {
246             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
247             Assert.False(dictionary.ContainsValue(default(TValue)));
248         }
249 
250         [Theory]
251         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_ContainsValue_DefaultValuePresent(int count)252         public void SortedList_Generic_ContainsValue_DefaultValuePresent(int count)
253         {
254             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
255             int seed = 4315;
256             TKey notPresent = CreateTKey(seed++);
257             while (dictionary.ContainsKey(notPresent))
258                 notPresent = CreateTKey(seed++);
259             dictionary.Add(notPresent, default(TValue));
260             Assert.True(dictionary.ContainsValue(default(TValue)));
261         }
262 
263         #endregion
264 
265         #region IndexOfKey
266 
267         [Theory]
268         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_IndexOf_DefaultKeyNotContainedInSortedList(int count)269         public void SortedList_Generic_IndexOf_DefaultKeyNotContainedInSortedList(int count)
270         {
271             if (DefaultValueAllowed)
272             {
273                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
274                 TKey key = default(TKey);
275                 if (dictionary.ContainsKey(key))
276                 {
277                     if (IsReadOnly)
278                         return;
279                     dictionary.Remove(key);
280                 }
281                 Assert.Equal(-1, dictionary.IndexOfKey(key));
282             }
283         }
284 
285         [Theory]
286         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_IndexOfKey_EachKey(int count)287         public void SortedList_Generic_IndexOfKey_EachKey(int count)
288         {
289             // Assumes no duplicate elements contained in the dictionary returned by GenericIListFactory
290             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
291             IList<TKey> keys = dictionary.Keys;
292             Assert.All(Enumerable.Range(0, count), index =>
293             {
294                 Assert.Equal(index, dictionary.IndexOfKey(keys[index]));
295             });
296         }
297 
298         #endregion
299 
300         #region IndexOfValue
301 
302         [Theory]
303         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_IndexOfValue_DefaultValueNotContainedInList(int count)304         public void SortedList_Generic_IndexOfValue_DefaultValueNotContainedInList(int count)
305         {
306             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
307             TValue value = default(TValue);
308             while (dictionary.ContainsValue(value))
309             {
310                 if (IsReadOnly)
311                     return;
312                 dictionary.RemoveAt(dictionary.IndexOfValue(value));
313             }
314             Assert.Equal(-1, dictionary.IndexOfValue(value));
315         }
316 
317         [Theory]
318         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_IndexOfValue_DefaultValueContainedInList(int count)319         public void SortedList_Generic_IndexOfValue_DefaultValueContainedInList(int count)
320         {
321             if (!IsReadOnly)
322             {
323                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
324                 TKey key = GetNewKey(dictionary);
325                 TValue value = default(TValue);
326                 while (dictionary.ContainsValue(value))
327                     dictionary.RemoveAt(dictionary.IndexOfValue(value));
328 
329                 List<TKey> keys = dictionary.Keys.ToList();
330                 keys.Add(key);
331                 keys.Sort();
332                 int expectedIndex = keys.IndexOf(key);
333                 dictionary.Add(key, value);
334                 Assert.Equal(expectedIndex, dictionary.IndexOfValue(value));
335             }
336         }
337 
338         [Theory]
339         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_IndexOfValue_ValueInCollectionMultipleTimes(int count)340         public void SortedList_Generic_IndexOfValue_ValueInCollectionMultipleTimes(int count)
341         {
342             if (!IsReadOnly)
343             {
344                 int seed = 53214;
345                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
346                 TKey key1 = CreateTKey(seed++);
347                 TKey key2 = CreateTKey(seed++);
348                 TValue value = CreateTValue(seed++);
349                 while (dictionary.ContainsKey(key1))
350                     key1 = CreateTKey(seed++);
351                 while (key1.Equals(key2) || dictionary.ContainsKey(key2))
352                     key2 = CreateTKey(seed++);
353                 while (dictionary.ContainsValue(value))
354                     dictionary.RemoveAt(dictionary.IndexOfValue(value));
355 
356                 List<TKey> keys = dictionary.Keys.ToList();
357                 keys.Add(key1);
358                 keys.Add(key2);
359                 keys.Sort();
360                 int expectedIndex = Math.Min(keys.IndexOf(key1), keys.IndexOf(key2));
361 
362                 dictionary.Add(key1, value);
363                 dictionary.Add(key2, value);
364                 Assert.Equal(expectedIndex, dictionary.IndexOfValue(value));
365             }
366         }
367 
368         [Theory]
369         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_IndexOfValue_EachValue(int count)370         public void SortedList_Generic_IndexOfValue_EachValue(int count)
371         {
372             // Assumes no duplicate elements contained in the dictionary returned by GenericIListFactory
373             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
374             IList<TKey> keys = dictionary.Keys;
375             Assert.All(Enumerable.Range(0, count), index =>
376             {
377                 Assert.Equal(index, dictionary.IndexOfValue(dictionary[keys[index]]));
378             });
379         }
380 
381         #endregion
382 
383         #region RemoveAt
384 
RemoveAt(SortedList<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> element)385         private void RemoveAt(SortedList<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> element)
386         {
387             dictionary.RemoveAt(dictionary.IndexOfKey(element.Key));
388         }
389 
390         [Theory]
391         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_RemoveAt_OnReadOnlySortedList_ThrowsNotSupportedException(int count)392         public void SortedList_Generic_RemoveAt_OnReadOnlySortedList_ThrowsNotSupportedException(int count)
393         {
394             if (IsReadOnly)
395             {
396                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
397                 Assert.Throws<NotSupportedException>(() => RemoveAt(dictionary, CreateT(34543)));
398             }
399         }
400 
401         [Theory]
402         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_RemoveAt_NonDefaultValueContainedInCollection(int count)403         public void SortedList_Generic_RemoveAt_NonDefaultValueContainedInCollection(int count)
404         {
405             if (!IsReadOnly)
406             {
407                 int seed = count * 251;
408                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
409                 KeyValuePair<TKey, TValue> pair = CreateT(seed++);
410                 if (!dictionary.ContainsKey(pair.Key))
411                 {
412                     dictionary.Add(pair.Key, pair.Value);
413                     count++;
414                 }
415                 RemoveAt(dictionary, pair);
416                 Assert.Equal(count - 1, dictionary.Count);
417             }
418         }
419 
420         [Theory]
421         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_RemoveAt_EveryValue(int count)422         public void SortedList_Generic_RemoveAt_EveryValue(int count)
423         {
424             if (!IsReadOnly)
425             {
426                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
427                 Assert.All(dictionary.ToList(), value =>
428                 {
429                     RemoveAt(dictionary, value);
430                 });
431                 Assert.Empty(dictionary);
432             }
433         }
434 
435         [Theory]
436         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_RemoveAt_OutOfRangeValues(int count)437         public void SortedList_Generic_RemoveAt_OutOfRangeValues(int count)
438         {
439             if (!IsReadOnly)
440             {
441                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(count);
442                 Assert.Throws<ArgumentOutOfRangeException>(() => dictionary.RemoveAt(-1));
443                 Assert.Throws<ArgumentOutOfRangeException>(() => dictionary.RemoveAt(int.MinValue));
444                 Assert.Throws<ArgumentOutOfRangeException>(() => dictionary.RemoveAt(count));
445                 Assert.Throws<ArgumentOutOfRangeException>(() => dictionary.RemoveAt(count + 1));
446             }
447         }
448 
449         #endregion
450 
451         #region TrimExcess
452 
453         [Theory]
454         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_TrimExcess_OnValidSortedListThatHasntBeenRemovedFrom(int dictionaryLength)455         public void SortedList_Generic_TrimExcess_OnValidSortedListThatHasntBeenRemovedFrom(int dictionaryLength)
456         {
457             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(dictionaryLength);
458             dictionary.TrimExcess();
459         }
460 
461         [Theory]
462         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_TrimExcess_Repeatedly(int dictionaryLength)463         public void SortedList_Generic_TrimExcess_Repeatedly(int dictionaryLength)
464         {
465             SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(dictionaryLength);
466             List<KeyValuePair<TKey, TValue>> expected = dictionary.ToList();
467             dictionary.TrimExcess();
468             dictionary.TrimExcess();
469             dictionary.TrimExcess();
470             Assert.True(dictionary.SequenceEqual(expected, GetIEqualityComparer()));
471         }
472 
473         [Theory]
474         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_TrimExcess_AfterRemovingOneElement(int dictionaryLength)475         public void SortedList_Generic_TrimExcess_AfterRemovingOneElement(int dictionaryLength)
476         {
477             if (dictionaryLength > 0)
478             {
479                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(dictionaryLength);
480                 List<KeyValuePair<TKey, TValue>> expected = dictionary.ToList();
481                 KeyValuePair<TKey, TValue> elementToRemove = dictionary.ElementAt(0);
482 
483                 dictionary.TrimExcess();
484                 Assert.True(dictionary.Remove(elementToRemove.Key));
485                 expected.Remove(elementToRemove);
486                 dictionary.TrimExcess();
487 
488                 Assert.True(dictionary.SequenceEqual(expected, GetIEqualityComparer()));
489             }
490         }
491 
492         [Theory]
493         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_TrimExcess_AfterClearingAndAddingSomeElementsBack(int dictionaryLength)494         public void SortedList_Generic_TrimExcess_AfterClearingAndAddingSomeElementsBack(int dictionaryLength)
495         {
496             if (dictionaryLength > 0)
497             {
498                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(dictionaryLength);
499                 dictionary.TrimExcess();
500                 dictionary.Clear();
501                 dictionary.TrimExcess();
502                 Assert.Equal(0, dictionary.Count);
503 
504                 AddToCollection(dictionary, dictionaryLength / 10);
505                 dictionary.TrimExcess();
506                 Assert.Equal(dictionaryLength / 10, dictionary.Count);
507             }
508         }
509 
510         [Theory]
511         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_TrimExcess_AfterClearingAndAddingAllElementsBack(int dictionaryLength)512         public void SortedList_Generic_TrimExcess_AfterClearingAndAddingAllElementsBack(int dictionaryLength)
513         {
514             if (dictionaryLength > 0)
515             {
516                 SortedList<TKey, TValue> dictionary = (SortedList<TKey, TValue>)GenericIDictionaryFactory(dictionaryLength);
517                 dictionary.TrimExcess();
518                 dictionary.Clear();
519                 dictionary.TrimExcess();
520                 Assert.Equal(0, dictionary.Count);
521 
522                 AddToCollection(dictionary, dictionaryLength);
523                 dictionary.TrimExcess();
524                 Assert.Equal(dictionaryLength, dictionary.Count);
525             }
526         }
527 
528         #endregion
529 
530         #region Ordering
531 
532         [Theory]
533         [MemberData(nameof(ValidCollectionSizes))]
SortedList_Generic_DictionaryIsProperlySortedAccordingToComparer(int setLength)534         public void SortedList_Generic_DictionaryIsProperlySortedAccordingToComparer(int setLength)
535         {
536             SortedList<TKey, TValue> set = (SortedList<TKey, TValue>)GenericIDictionaryFactory(setLength);
537             List<KeyValuePair<TKey, TValue>> expected = set.ToList();
538             expected.Sort(GetIComparer());
539             int expectedIndex = 0;
540             foreach (KeyValuePair<TKey, TValue> value in set)
541                 Assert.Equal(expected[expectedIndex++], value);
542         }
543 
544         #endregion
545 
546         #region IReadOnlyDictionary<TKey, TValue>.Keys
547 
548         [Theory]
549         [MemberData(nameof(ValidCollectionSizes))]
IReadOnlyDictionary_Generic_Keys_ContainsAllCorrectKeys(int count)550         public void IReadOnlyDictionary_Generic_Keys_ContainsAllCorrectKeys(int count)
551         {
552             IDictionary<TKey, TValue> dictionary = GenericIDictionaryFactory(count);
553             IEnumerable<TKey> expected = dictionary.Select((pair) => pair.Key);
554             IEnumerable<TKey> keys = ((IReadOnlyDictionary<TKey, TValue>)dictionary).Keys;
555             Assert.True(expected.SequenceEqual(keys));
556         }
557 
558         [Theory]
559         [MemberData(nameof(ValidCollectionSizes))]
IReadOnlyDictionary_Generic_Values_ContainsAllCorrectValues(int count)560         public void IReadOnlyDictionary_Generic_Values_ContainsAllCorrectValues(int count)
561         {
562             IDictionary<TKey, TValue> dictionary = GenericIDictionaryFactory(count);
563             IEnumerable<TValue> expected = dictionary.Select((pair) => pair.Value);
564             IEnumerable<TValue> values = ((IReadOnlyDictionary<TKey, TValue>)dictionary).Values;
565             Assert.True(expected.SequenceEqual(values));
566         }
567 
568         #endregion
569     }
570 }
571