forked from dotnet/runtime
/
IDictionary.NonGeneric.Tests.cs
882 lines (802 loc) · 33.5 KB
/
IDictionary.NonGeneric.Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
// 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.Linq;
using Xunit;
namespace System.Collections.Tests
{
/// <summary>
/// Contains tests that ensure the correctness of any class that implements the nongeneric
/// IDictionary interface
/// </summary>
public abstract partial class IDictionary_NonGeneric_Tests : ICollection_NonGeneric_Tests
{
#region IDictionary Helper Methods
/// <summary>
/// To be implemented in the concrete Dictionary test classes. Creates an instance of TValue that
/// is dependent only on the seed passed as input and will return the same value on repeated
/// calls with the same seed.
/// </summary>
protected virtual object CreateTKey(int seed)
{
if (seed % 2 == 0)
{
int stringLength = seed % 10 + 5;
Random rand = new Random(seed);
byte[] bytes = new byte[stringLength];
rand.NextBytes(bytes);
return Convert.ToBase64String(bytes);
}
else
{
Random rand = new Random(seed);
return rand.Next();
}
}
/// <summary>
/// To be implemented in the concrete Dictionary test classes. Creates an instance of TValue that
/// is dependent only on the seed passed as input and will return the same value on repeated
/// calls with the same seed.
/// </summary>
protected virtual object CreateTValue(int seed)
{
return CreateTKey(seed);
}
/// <summary>
/// Creates an instance of an IDictionary that can be used for testing.
/// </summary>
/// <returns>An instance of an IDictionary that can be used for testing.</returns>
protected abstract IDictionary NonGenericIDictionaryFactory();
/// <summary>
/// Creates an instance of an IDictionary that can be used for testing.
/// </summary>
/// <param name="count">The number of items that the returned IDictionary contains.</param>
/// <returns>An instance of an IDictionary that can be used for testing.</returns>
protected virtual IDictionary NonGenericIDictionaryFactory(int count)
{
IDictionary collection = NonGenericIDictionaryFactory();
AddToCollection(collection, count);
return collection;
}
/// <summary>
/// Helper method to get a key that doesn't already exist within the dictionary
/// </summary>
/// <param name="dictionary"></param>
protected object GetNewKey(IDictionary dictionary)
{
int seed = 840;
object missingKey = CreateTKey(seed++);
while (dictionary.Contains(missingKey) || missingKey.Equals(null))
missingKey = CreateTKey(seed++);
return missingKey;
}
/// <summary>
/// Used in IDictionary_NonGeneric_Values_ModifyingTheDictionaryUpdatesTheCollection and
/// IDictionary_NonGeneric_Keys_ModifyingTheDictionaryUpdatesTheCollection.
/// Some collections (e.g ConcurrentDictionary) use iterators in the Keys and Values properties,
/// and do not respond to updates in the base collection.
/// </summary>
protected virtual bool IDictionary_NonGeneric_Keys_Values_ModifyingTheDictionaryUpdatesTheCollection => true;
/// <summary>
/// Used in IDictionary_NonGeneric_Values_Enumeration_ParentDictionaryModifiedInvalidatesEnumerator and
/// IDictionary_NonGeneric_Keys_Enumeration_ParentDictionaryModifiedInvalidatesEnumerator.
/// Some collections (e.g. ConcurrentDictionary) do not throw an InvalidOperationException
/// when enumerating the Keys or Values property and the parent is modified.
/// </summary>
protected virtual bool IDictionary_NonGeneric_Keys_Values_ParentDictionaryModifiedInvalidates => true;
protected virtual bool ExpectedIsFixedSize => false;
#endregion
#region ICollection Helper Methods
protected override ICollection NonGenericICollectionFactory()
{
return NonGenericIDictionaryFactory();
}
protected override bool DuplicateValuesAllowed => false;
protected override bool NullAllowed => false;
protected override bool Enumerator_Current_UndefinedOperation_Throws => true;
protected override Type ICollection_NonGeneric_CopyTo_ArrayOfEnumType_ThrowType => typeof(ArgumentException);
protected override void AddToCollection(ICollection collection, int numberOfItemsToAdd)
{
Assert.False(IsReadOnly);
int seed = 12353;
IDictionary casted = (IDictionary)collection;
int initialCount = casted.Count;
while ((casted.Count - initialCount) < numberOfItemsToAdd)
{
object key = CreateTKey(seed++);
object value = CreateTValue(seed++);
while (casted.Contains(key) || Enumerable.Contains(InvalidValues, key))
key = CreateTKey(seed++);
casted.Add(key, value);
}
}
/// <summary>
/// Returns a set of ModifyEnumerable delegates that modify the enumerable passed to them.
/// </summary>
protected override IEnumerable<ModifyEnumerable> GetModifyEnumerables(ModifyOperation operations)
{
if ((operations & ModifyOperation.Add) == ModifyOperation.Add)
{
yield return (IEnumerable enumerable) =>
{
IDictionary casted = ((IDictionary)enumerable);
casted.Add(CreateTKey(12), CreateTValue(5123));
return true;
};
}
if ((operations & ModifyOperation.Insert) == ModifyOperation.Insert)
{
yield return (IEnumerable enumerable) =>
{
IDictionary casted = ((IDictionary)enumerable);
casted[CreateTKey(541)] = CreateTValue(12);
return true;
};
}
if ((operations & ModifyOperation.Overwrite) == ModifyOperation.Overwrite)
{
yield return (IEnumerable enumerable) =>
{
IDictionary casted = ((IDictionary)enumerable);
if (casted.Count > 0)
{
IEnumerator keys = casted.Keys.GetEnumerator();
keys.MoveNext();
casted[keys.Current] = CreateTValue(12);
return true;
}
return false;
};
}
if ((operations & ModifyOperation.Remove) == ModifyOperation.Remove)
{
yield return (IEnumerable enumerable) =>
{
IDictionary casted = ((IDictionary)enumerable);
if (casted.Count > 0)
{
IEnumerator keys = casted.Keys.GetEnumerator();
keys.MoveNext();
casted.Remove(keys.Current);
return true;
}
return false;
};
}
if ((operations & ModifyOperation.Clear) == ModifyOperation.Clear)
{
yield return (IEnumerable enumerable) =>
{
IDictionary casted = ((IDictionary)enumerable);
if (casted.Count > 0)
{
casted.Clear();
return true;
}
return false;
};
}
}
/// <summary>
/// Used in IDictionary_NonGeneric_Keys_Enumeration_Reset and IDictionary_NonGeneric_Values_Enumeration_Reset.
/// Typically, the support for Reset in enumerators for the Keys and Values depend on the support for it
/// in the parent dictionary. However, some collections (e.g. ConcurrentDictionary) don't.
/// </summary>
protected virtual bool IDictionary_NonGeneric_Keys_Values_Enumeration_ResetImplemented => ResetImplemented;
#endregion
#region IsFixedSize
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_IsFixedSize_Validity(int count)
{
IDictionary collection = NonGenericIDictionaryFactory(count);
Assert.Equal(ExpectedIsFixedSize, collection.IsFixedSize);
}
#endregion
#region IsReadOnly
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_IsReadOnly_Validity(int count)
{
IDictionary collection = NonGenericIDictionaryFactory(count);
Assert.Equal(IsReadOnly, collection.IsReadOnly);
}
#endregion
#region Item Getter
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemGet_NullKey(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
if (!NullAllowed)
{
Assert.Throws<ArgumentNullException>(() => dictionary[null]);
}
else
{
object value = CreateTValue(3452);
dictionary[null] = value;
Assert.Equal(value, dictionary[null]);
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemGet_MissingNonNullKey_ThrowsKeyNotFoundException(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
Assert.Null(dictionary[missingKey]);
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemGet_MissingNullKey_ThrowsKeyNotFoundException(int count)
{
if (NullAllowed)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = null;
if (dictionary.Contains(missingKey))
dictionary.Remove(missingKey);
Assert.Null(dictionary[missingKey]);
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemGet_PresenobjectReturnsCorrecobject(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
foreach (DictionaryEntry pair in dictionary)
{
Assert.Equal(pair.Value, dictionary[pair.Key]);
}
}
#endregion
#region Item Setter
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemSet_NullKey(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
if (!NullAllowed)
{
Assert.Throws<ArgumentNullException>(() => dictionary[null] = CreateTValue(3));
}
else
{
object value = CreateTValue(3452);
dictionary[null] = value;
Assert.Equal(value, dictionary[null]);
}
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemSet_OnReadOnlyDictionary_ThrowsNotSupportedException(int count)
{
if (IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
Assert.Throws<NotSupportedException>(() => dictionary[missingKey] = CreateTValue(5312));
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemSet_AddsNewValueWhenNotPresent(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
dictionary[missingKey] = CreateTValue(543);
Assert.Equal(count + 1, dictionary.Count);
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_ItemSet_ReplacesExistingValueWhenPresent(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object existingKey = GetNewKey(dictionary);
dictionary.Add(existingKey, CreateTValue(5342));
object newValue = CreateTValue(1234);
dictionary[existingKey] = newValue;
Assert.Equal(count + 1, dictionary.Count);
Assert.Equal(newValue, dictionary[existingKey]);
}
}
#endregion
#region Keys
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Keys_ContainsAllCorrectobjects(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object[] expected = new object[count];
dictionary.Keys.CopyTo(expected, 0);
int i = 0;
foreach (object key in dictionary.Keys)
Assert.Equal(expected[i++], key);
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Keys_ModifyingTheDictionaryUpdatesTheCollection(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
ICollection keys = dictionary.Keys;
int previousCount = keys.Count;
dictionary.Clear();
if (IDictionary_NonGeneric_Keys_Values_ModifyingTheDictionaryUpdatesTheCollection)
{
Assert.Empty(keys);
}
else
{
Assert.Equal(previousCount, keys.Count);
}
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Keys_Enumeration_ParentDictionaryModifiedInvalidatesEnumerator(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
ICollection keys = dictionary.Keys;
IEnumerator keysEnum = keys.GetEnumerator();
dictionary.Add(GetNewKey(dictionary), CreateTValue(3432));
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : IDictionary_NonGeneric_Keys_Values_ParentDictionaryModifiedInvalidates)
{
Assert.Throws<InvalidOperationException>(() => keysEnum.MoveNext());
Assert.Throws<InvalidOperationException>(() => keysEnum.Reset());
}
else
{
keysEnum.MoveNext();
if (count > 0)
{
_ = keysEnum.Current;
}
keysEnum.Reset();
}
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Keys_Enumeration_Reset(int count)
{
IEnumerator enumerator = NonGenericIDictionaryFactory(count).Keys.GetEnumerator();
if (IDictionary_NonGeneric_Keys_Values_Enumeration_ResetImplemented)
enumerator.Reset();
else
Assert.Throws<NotSupportedException>(() => enumerator.Reset());
}
#endregion
#region Values
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Values_ContainsAllCorrecobjects(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object[] expected = new object[count];
dictionary.Values.CopyTo(expected, 0);
int i = 0;
foreach (object value in dictionary.Values)
Assert.Equal(expected[i++], value);
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Values_IncludeDuplicatesMultipleTimes(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
List<DictionaryEntry> entries = new List<DictionaryEntry>();
foreach (DictionaryEntry pair in dictionary)
entries.Add(pair);
foreach (DictionaryEntry pair in entries)
{
object missingKey = GetNewKey(dictionary);
dictionary.Add(missingKey, (pair.Value));
}
Assert.Equal(count * 2, dictionary.Values.Count);
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Values_ModifyingTheDictionaryUpdatesTheCollection(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
ICollection values = dictionary.Values;
int previousCount = values.Count;
dictionary.Clear();
if (IDictionary_NonGeneric_Keys_Values_ModifyingTheDictionaryUpdatesTheCollection)
{
Assert.Empty(values);
}
else
{
Assert.Equal(previousCount, values.Count);
}
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public virtual void IDictionary_NonGeneric_Values_Enumeration_ParentDictionaryModifiedInvalidatesEnumerator(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
ICollection values = dictionary.Values;
IEnumerator valuesEnum = values.GetEnumerator();
dictionary.Add(GetNewKey(dictionary), CreateTValue(3432));
if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : IDictionary_NonGeneric_Keys_Values_ParentDictionaryModifiedInvalidates)
{
Assert.Throws<InvalidOperationException>(() => valuesEnum.MoveNext());
Assert.Throws<InvalidOperationException>(() => valuesEnum.Reset());
Assert.Throws<InvalidOperationException>(() => valuesEnum.Current);
}
else
{
valuesEnum.MoveNext();
if (count > 0)
{
_ = valuesEnum.Current;
}
valuesEnum.Reset();
}
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Values_Enumeration_Reset(int count)
{
IEnumerator enumerator = NonGenericIDictionaryFactory(count).Values.GetEnumerator();
if (IDictionary_NonGeneric_Keys_Values_Enumeration_ResetImplemented)
enumerator.Reset();
else
Assert.Throws<NotSupportedException>(() => enumerator.Reset());
}
#endregion
#region Add
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Add_OnReadOnlyDictionary_ThrowsNotSupportedException(int count)
{
if (IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
Assert.Throws<NotSupportedException>(() => dictionary.Add(CreateTKey(0), CreateTValue(0)));
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Add_NullKey_NullValue(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = null;
object value = null;
if (NullAllowed && !IsReadOnly)
{
dictionary.Add(missingKey, value);
Assert.Equal(count + 1, dictionary.Count);
Assert.Equal(value, dictionary[missingKey]);
}
else if (!IsReadOnly)
{
Assert.Throws<ArgumentNullException>(() => dictionary.Add(missingKey, value));
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Add_NullKey_NonNullValue(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = null;
object value = CreateTValue(1456);
if (NullAllowed && !IsReadOnly)
{
dictionary.Add(missingKey, value);
Assert.Equal(count + 1, dictionary.Count);
Assert.Equal(value, dictionary[missingKey]);
}
else if (!IsReadOnly)
{
Assert.Throws<ArgumentNullException>(() => dictionary.Add(missingKey, value));
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Add_NonNullKey_NullValue(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
object value = null;
dictionary.Add(missingKey, value);
Assert.Equal(count + 1, dictionary.Count);
Assert.Equal(value, dictionary[missingKey]);
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Add_NonNullKey_NonNulValue(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
object value = CreateTValue(1342);
dictionary.Add(missingKey, value);
Assert.Equal(count + 1, dictionary.Count);
Assert.Equal(value, dictionary[missingKey]);
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Add_DuplicateKey(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
dictionary.Add(missingKey, CreateTValue(34251));
Assert.Throws<ArgumentException>(() => dictionary.Add(missingKey, CreateTValue(134)));
}
}
#endregion
#region Remove
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Remove_NullKey(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
if (!IsReadOnly)
{
if (!NullAllowed)
{
Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null));
}
else
{
object value = CreateTValue(3452);
dictionary.Add(null, value);
dictionary.Remove(null);
Assert.Null(dictionary[null]);
}
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Remove_ThrowsWhenNotSupported(int count)
{
if (IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
foreach (DictionaryEntry entry in dictionary)
{
Assert.Throws<NotSupportedException>(() => dictionary.Remove(entry.Key));
break;
}
}
}
#endregion
#region Clear
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Clear(int count)
{
IDictionary collection = NonGenericIDictionaryFactory(count);
if (IsReadOnly)
{
Assert.Throws<NotSupportedException>(() => collection.Clear());
Assert.Equal(count, collection.Count);
Assert.Equal(count, collection.Keys.Count);
Assert.Equal(count, collection.Values.Count);
}
else
{
collection.Clear();
Assert.Equal(0, collection.Count);
Assert.Equal(0, collection.Keys.Count);
Assert.Equal(0, collection.Values.Count);
// Make sure we can clear an already cleared dictionary
collection.Clear();
Assert.Equal(0, collection.Count);
Assert.Equal(0, collection.Keys.Count);
Assert.Equal(0, collection.Values.Count);
}
}
#endregion
#region Contains
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Contains_ValidKeyNotContainedInDictionary(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
Assert.False(dictionary.Contains(missingKey));
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Contains_ValidKeyContainedInDictionary(int count)
{
if (!IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = GetNewKey(dictionary);
dictionary.Add(missingKey, CreateTValue(34251));
Assert.True(dictionary.Contains(missingKey));
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Contains_NullKeyNotContainedInDictionary(int count)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
if (NullAllowed)
{
// returns false
object missingKey = null;
while (dictionary.Contains(missingKey))
dictionary.Remove(missingKey);
Assert.False(dictionary.Contains(missingKey));
}
else
{
// throws ArgumentNullException
Assert.Throws<ArgumentNullException>(() => dictionary.Contains(null));
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_Contains_NullKeyContainedInDictionary(int count)
{
if (NullAllowed && !IsReadOnly)
{
IDictionary dictionary = NonGenericIDictionaryFactory(count);
object missingKey = null;
if (!dictionary.Contains(missingKey))
dictionary.Add(missingKey, CreateTValue(5341));
Assert.True(dictionary.Contains(missingKey));
}
}
#endregion
#region IDictionaryEnumerator
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_IDictionaryEnumerator_Current_FromStartToFinish(int count)
{
IDictionaryEnumerator enumerator = NonGenericIDictionaryFactory(count).GetEnumerator();
for (int i = 0; i < 2; i++)
{
int counter = 0;
while (enumerator.MoveNext())
{
object current = enumerator.Current;
object key = enumerator.Key;
object value = enumerator.Value;
DictionaryEntry entry = enumerator.Entry;
Assert.Equal(current, entry);
Assert.Equal(key, entry.Key);
Assert.Equal(value, entry.Value);
counter++;
}
Assert.Equal(count, counter);
if (!ResetImplemented)
{
break;
}
enumerator.Reset();
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_IDictionaryEnumerator_Reset_BeforeIteration_Support(int count)
{
IDictionaryEnumerator enumerator = NonGenericIDictionaryFactory(count).GetEnumerator();
if (ResetImplemented)
enumerator.Reset();
else
Assert.Throws<NotSupportedException>(() => enumerator.Reset());
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void IDictionary_NonGeneric_IDictionaryEnumerator_Current_ReturnsSameValueOnRepeatedCalls(int count)
{
IDictionaryEnumerator enumerator = NonGenericIDictionaryFactory(count).GetEnumerator();
while (enumerator.MoveNext())
{
object current = enumerator.Current;
Assert.Equal(current, enumerator.Current);
Assert.Equal(current, enumerator.Current);
Assert.Equal(current, enumerator.Current);
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public virtual void IDictionary_NonGeneric_IDictionaryEnumerator_Current_BeforeFirstMoveNext_UndefinedBehavior(int count)
{
object current, key, value, entry;
IDictionaryEnumerator enumerator = NonGenericIDictionaryFactory(count).GetEnumerator();
if (Enumerator_Current_UndefinedOperation_Throws)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
Assert.Throws<InvalidOperationException>(() => enumerator.Key);
Assert.Throws<InvalidOperationException>(() => enumerator.Value);
Assert.Throws<InvalidOperationException>(() => enumerator.Entry);
}
else
{
current = enumerator.Current;
key = enumerator.Key;
value = enumerator.Value;
entry = enumerator.Entry;
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public virtual void IDictionary_NonGeneric_IDictionaryEnumerator_Current_AfterEndOfEnumerable_UndefinedBehavior(int count)
{
object current, key, value, entry;
IDictionaryEnumerator enumerator = NonGenericIDictionaryFactory(count).GetEnumerator();
while (enumerator.MoveNext()) ;
if (Enumerator_Current_UndefinedOperation_Throws)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
Assert.Throws<InvalidOperationException>(() => enumerator.Key);
Assert.Throws<InvalidOperationException>(() => enumerator.Value);
Assert.Throws<InvalidOperationException>(() => enumerator.Entry);
}
else
{
current = enumerator.Current;
key = enumerator.Key;
value = enumerator.Value;
entry = enumerator.Entry;
}
}
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public virtual void IDictionary_NonGeneric_IDictionaryEnumerator_Current_ModifiedDuringEnumeration_UndefinedBehavior(int count)
{
Assert.All(GetModifyEnumerables(ModifyEnumeratorThrows), ModifyEnumerable =>
{
object current, key, value, entry;
IDictionary enumerable = NonGenericIDictionaryFactory(count);
IDictionaryEnumerator enumerator = enumerable.GetEnumerator();
if (ModifyEnumerable(enumerable))
{
if (Enumerator_Current_UndefinedOperation_Throws)
{
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
Assert.Throws<InvalidOperationException>(() => enumerator.Key);
Assert.Throws<InvalidOperationException>(() => enumerator.Value);
Assert.Throws<InvalidOperationException>(() => enumerator.Entry);
}
else
{
current = enumerator.Current;
key = enumerator.Key;
value = enumerator.Value;
entry = enumerator.Entry;
}
}
});
}
#endregion
}
}