* SortedListTest.cs: No longer derive from deprecated Assertion class.
[mono.git] / mcs / class / corlib / Test / System.Collections / DictionaryBaseTest.cs
1 //
2 // System.Collections.DictionaryBase
3 // Test suite for System.Collections.DictionaryBase
4 //
5 // Authors:
6 //      Carlos Alberto Barcenilla (barce@frlp.utn.edu.ar)
7 //
8
9 using System;
10 using System.Collections;
11 using NUnit.Framework;
12
13 namespace MonoTests.System.Collections 
14 {
15
16         [TestFixture]
17         public class DictionaryBaseTest: Assertion
18         {
19                 static void Main(string[] args)
20                 {
21                 }
22
23                 public class ConcreteDictionary : DictionaryBase
24                 {
25                         public bool onInsertFired;
26                         public bool onInsertCompleteFired;
27                         public bool onValidateFired;
28                         public bool onRemoveFired;
29                         public bool onRemoveCompleteFired;
30                         public bool onClearFired;
31                         public bool onClearCompleteFired;
32                         public bool onSetFired;
33                         public bool onSetCompleteFired;
34                         public bool onGetFired;
35
36                         public bool onInsertMustThrowException;
37                         public bool onInsertCompleteMustThrowException;
38                         public bool onValidateMustThrowException;
39                         public bool onRemoveMustThrowException;
40                         public bool onRemoveCompleteMustThrowException;
41                         public bool onClearMustThrowException;
42                         public bool onClearCompleteMustThrowException;
43                         public bool onSetMustThrowException;
44                         public bool onSetCompleteMustThrowException;
45                         public bool onGetMustThrowException;
46
47                         public ConcreteDictionary() 
48                         {
49                         }
50
51                         public ConcreteDictionary(int i) 
52                         {
53                                 for (int j = 0; j < i; j++) 
54                                 {
55                                         ((IDictionary) this).Add(j, j*2);
56                                 }
57
58                                 ClearFlags();
59                         }
60
61             public IDictionary BaseDictionary 
62                         {
63                                 get { return this.Dictionary; }
64                         }
65
66                         public void ClearFlags() 
67                         {
68                                 onInsertFired = false;
69                                 onInsertCompleteFired = false;
70                                 onValidateFired = false;
71                                 onRemoveFired = false;
72                                 onRemoveCompleteFired = false;
73                                 onClearFired = false;
74                                 onClearCompleteFired = false;
75                                 onSetFired = false;
76                                 onSetCompleteFired = false;
77                                 onGetFired = false;
78                         }
79
80                         protected override void OnValidate(object key, object value)
81                         {
82                                 onValidateFired = true;
83                                 if (onValidateMustThrowException)
84                                         throw new Exception();
85                                 base.OnValidate (key, value);
86                         }
87
88                         protected override void OnInsert(object key, object value)
89                         {
90                                 onInsertFired = true;
91                                 if (onInsertMustThrowException)
92                                         throw new Exception();
93                                 base.OnInsert (key, value);
94                         }
95
96                         protected override void OnInsertComplete(object key, object value)
97                         {
98                                 onInsertCompleteFired = true;
99                                 if (onInsertCompleteMustThrowException)
100                                         throw new Exception();
101                                 base.OnInsertComplete (key, value);
102                         }
103
104                         protected override void OnRemove(object key, object value)
105                         {
106                                 onRemoveFired = true;
107                                 if (onRemoveMustThrowException)
108                                         throw new Exception();
109                                 base.OnRemove (key, value);
110                         }
111
112                         protected override void OnRemoveComplete(object key, object value)
113                         {
114                                 onRemoveCompleteFired = true;
115                                 if (onRemoveCompleteMustThrowException)
116                                         throw new Exception();
117                                 base.OnRemoveComplete (key, value);
118                         }
119
120
121                         protected override void OnClear()
122                         {
123                                 onClearFired = true;
124                                 if (onClearMustThrowException)
125                                         throw new Exception();
126                                 base.OnClear ();
127                         }
128
129                         protected override void OnClearComplete()
130                         {
131                                 onClearCompleteFired = true;
132                                 if (onClearCompleteMustThrowException)
133                                         throw new Exception();
134                                 base.OnClearComplete ();
135                         }
136
137                         protected override object OnGet(object key, object currentValue)
138                         {
139                                 onGetFired = true;
140                                 if (onGetMustThrowException)
141                                         throw new Exception();
142                                 return base.OnGet (key, currentValue);
143                         }
144
145                         protected override void OnSet(object key, object oldValue, object newValue)
146                         {
147                                 onSetFired = true;
148                                 if (onSetMustThrowException)
149                                         throw new Exception();
150                                 base.OnSet (key, oldValue, newValue);
151                         }
152
153                         protected override void OnSetComplete(object key, object oldValue, object newValue)
154                         {
155                                 onSetCompleteFired = true;
156                                 if (onSetCompleteMustThrowException)
157                                         throw new Exception();
158                                 base.OnSetComplete (key, oldValue, newValue);
159                         }
160                 }
161
162                 [Test]
163                 public void Add() 
164                 {
165                         ConcreteDictionary myDictionary = new ConcreteDictionary(10);
166                         myDictionary.BaseDictionary.Add(100, 1);
167
168                         Assert("OnValidate must be fired", myDictionary.onValidateFired);
169                         Assert("OnInsert must be fired", myDictionary.onInsertFired);
170                         Assert("OnInsertComplete must be fired", myDictionary.onInsertCompleteFired);
171                         AssertEquals("Count", 11, myDictionary.Count);
172                         AssertEquals(1, myDictionary.BaseDictionary[100]);
173                 }
174
175                 [Test]
176                 public void AddOnValidateExcept() 
177                 {
178                         bool exceptionThrown = false;
179
180                         ConcreteDictionary myDictionary = new ConcreteDictionary(30);
181                         myDictionary.onValidateMustThrowException = true;
182
183                         try {
184                                 myDictionary.BaseDictionary.Add(111,222);
185                         } catch {
186                                 exceptionThrown = true;
187                         } finally {
188                                 Assert("Exception must be thrown", exceptionThrown);
189                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
190                                 Assert("OnInsert must not be fired", !myDictionary.onInsertFired);
191                                 Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
192                                 AssertEquals("Count", 30, myDictionary.Count);
193                         }
194
195                 }
196
197                 [Test]
198                 public void AddOnInsertExcept() 
199                 {
200                         bool exceptionThrown = false;
201
202                         ConcreteDictionary myDictionary = new ConcreteDictionary(30);
203                         myDictionary.onInsertMustThrowException = true;
204
205                         try {
206                                 myDictionary.BaseDictionary.Add(666,222);
207                         } catch {
208                                 exceptionThrown = true;
209                         } finally {
210                                 Assert("Exception must be thrown", exceptionThrown);
211                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
212                                 Assert("OnInsert must be fired", myDictionary.onInsertFired);
213                                 Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
214                                 AssertEquals("Count", 30, myDictionary.Count);
215                         }
216
217                 }
218
219                 [Test]
220                 public void AddOnInsertCompleteExcept() 
221                 {
222                         bool exceptionThrown = false;
223
224                         ConcreteDictionary myDictionary = new ConcreteDictionary(5);
225                         myDictionary.onInsertCompleteMustThrowException = true;
226
227                         try {
228                                 myDictionary.BaseDictionary.Add(888,999);
229                         } catch {
230                                 exceptionThrown = true;
231                         } finally {
232                                 Assert("Exception must be thrown", exceptionThrown);
233                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
234                                 Assert("OnInsert must be fired", myDictionary.onInsertFired);
235                                 Assert("OnInsertComplete must be fired", myDictionary.onInsertCompleteFired);
236                                 AssertEquals("Count", 5, myDictionary.Count);
237                         }
238
239                 }
240
241                 [Test]
242                 public void AddNullKey() 
243                 {
244                         bool exceptionThrown = false;
245
246                         ConcreteDictionary myDictionary = new ConcreteDictionary();
247                         try {
248                                 myDictionary.BaseDictionary.Add(null, 11);
249                         } catch (ArgumentNullException) {
250                                 exceptionThrown = true;
251                         } finally {
252                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
253                                 Assert("OnInsert must be fired", myDictionary.onInsertFired);
254                                 Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
255                                 Assert("ArgumentNullException must be thrown", exceptionThrown);
256                         }
257                 }
258
259                 [Test]
260                 public void Clear() 
261                 {
262                         ConcreteDictionary myDictionary = new ConcreteDictionary(30);
263                         myDictionary.Clear();
264
265                         Assert("OnClear must be fired", myDictionary.onClearFired);
266                         Assert("OnClearComplete must be fired", myDictionary.onClearCompleteFired);
267                         AssertEquals("Count", 0, myDictionary.Count);
268                 }
269
270                 [Test]
271                 public void ClearOnClearExcept() 
272                 {
273                         bool exceptionThrown = false;
274
275                         ConcreteDictionary myDictionary = new ConcreteDictionary(30);
276                         myDictionary.onClearMustThrowException = true;
277
278                         try {
279                                 myDictionary.Clear();
280                         } catch {
281                                 exceptionThrown = true;
282                         } finally {
283                                 Assert("Exception must be thrown", exceptionThrown);
284                                 Assert("OnClear must be fired", myDictionary.onClearFired);
285                                 Assert("OnClearComplete must not be fired", !myDictionary.onClearCompleteFired);
286                                 AssertEquals("Count", 30, myDictionary.Count);
287                         }
288
289                 }
290
291                 [Test]
292                 public void ClearOnClearCompleteExcept() 
293                 {
294                         bool exceptionThrown = false;
295
296                         ConcreteDictionary myDictionary = new ConcreteDictionary(30);
297                         myDictionary.onClearCompleteMustThrowException = true;
298
299                         try {
300                                 myDictionary.Clear();
301                         } catch {
302                                 exceptionThrown = true;
303                         } finally {
304                                 Assert("Exception must be thrown", exceptionThrown);
305                                 Assert("OnClear must be fired", myDictionary.onClearFired);
306                                 Assert("OnClearComplete must be fired", myDictionary.onClearCompleteFired);
307                                 AssertEquals("Count", 0, myDictionary.Count);
308                         }
309
310                 }
311
312                 [Test]
313                 public void Count() 
314                 {
315                         ConcreteDictionary myDictionary = new ConcreteDictionary(19);
316                         AssertEquals(19, myDictionary.Count);
317                 }
318
319                 [Test]
320                 public void Remove() 
321                 {
322                         ConcreteDictionary myDictionary = new ConcreteDictionary(8);
323                         myDictionary.BaseDictionary.Remove(5);
324
325                         Assert("OnValidate must be fired", myDictionary.onValidateFired);
326                         Assert("OnRemove must be fired", myDictionary.onRemoveFired);
327                         Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
328                         AssertEquals("Count", 7, myDictionary.Count);
329                         AssertEquals(null, myDictionary.BaseDictionary[5]);
330                 }
331
332                 [Test]
333                 public void RemoveOnValidateExcept() 
334                 {
335                         bool exceptionThrown = false;
336
337                         ConcreteDictionary myDictionary = new ConcreteDictionary(28);
338                         myDictionary.onValidateMustThrowException = true;
339
340                         try {
341                                 myDictionary.BaseDictionary.Remove(11);
342                         } catch {
343                                 exceptionThrown = true;
344                         } finally {
345                                 Assert("Exception must be thrown in this test", exceptionThrown);
346                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
347                                 Assert("OnRemove must not be fired", !myDictionary.onRemoveFired);
348                                 Assert("OnRemoveComplete must not be fired", !myDictionary.onRemoveCompleteFired);
349                                 AssertEquals("Count", 28, myDictionary.Count);
350                                 AssertEquals(22, myDictionary.BaseDictionary[11]);
351                         }
352
353                 }
354
355                 [Test]
356                 public void RemoveOnRemoveExcept() 
357                 {
358                         bool exceptionThrown = false;
359
360                         ConcreteDictionary myDictionary = new ConcreteDictionary(28);
361                         myDictionary.onRemoveMustThrowException = true;
362
363                         try {
364                                 myDictionary.BaseDictionary.Remove(11);
365                         } catch {
366                                 exceptionThrown = true;
367                         } finally {
368                                 Assert("Exception must be thrown", exceptionThrown);
369                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
370                                 Assert("OnRemove must be fired", myDictionary.onRemoveFired);
371                                 Assert("OnRemoveComplete must not be fired", !myDictionary.onRemoveCompleteFired);
372                                 AssertEquals("Count", 28, myDictionary.Count);
373                                 AssertEquals(22, myDictionary.BaseDictionary[11]);
374                         }
375
376                 }
377
378                 [Test]
379                 public void RemoveOnRemoveCompleteExcept() 
380                 {
381                         bool exceptionThrown = false;
382
383                         ConcreteDictionary myDictionary = new ConcreteDictionary(28);
384                         myDictionary.onRemoveCompleteMustThrowException = true;
385
386                         try 
387                         {
388                                 myDictionary.BaseDictionary.Remove(11);
389                         } 
390                         catch 
391                         {
392                                 exceptionThrown = true;
393                         } 
394                         finally 
395                         {
396                                 Assert("Exception must be thrown", exceptionThrown);
397                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
398                                 Assert("OnRemove must be fired", myDictionary.onRemoveFired);
399                                 Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
400                                 AssertEquals("Count", 27, myDictionary.Count);
401                                 AssertEquals(null, myDictionary.BaseDictionary[11]);
402                         }
403
404                 }
405
406                 [Test]
407                 public void RemoveKeyNotInDictionary() 
408                 {
409                 
410                         ConcreteDictionary myDictionary = new ConcreteDictionary(28);
411
412                         myDictionary.BaseDictionary.Remove(80);
413                         Assert("OnValidate must be fired", myDictionary.onValidateFired);
414                         Assert("OnRemove must be fired", myDictionary.onRemoveFired);
415                         Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
416                 }
417
418                 [Test]
419                 public void Items() 
420                 {
421                         ConcreteDictionary myDictionary = new ConcreteDictionary(19);
422                         for (int i = 0; i < 19; i++) 
423                         {
424                                 AssertEquals(i*2, (int) myDictionary.BaseDictionary[i]);
425                         }
426                 }
427
428                 [Test]
429                 public void Contains() 
430                 {
431                         ConcreteDictionary myDictionary = new ConcreteDictionary(14);
432                         for (int i = 0; i < 14; i++) 
433                         {
434                                 Assert("Must contain " + i, myDictionary.BaseDictionary.Contains(i));
435                         }
436                         for (int i = 14; i < 34; i++) 
437                         {
438                                 Assert("Must not contain " + i, !myDictionary.BaseDictionary.Contains(i));
439                         }
440                 }
441
442                 [Test]
443                 public void GetEnumerator() 
444                 {
445                         ConcreteDictionary myDictionary = new ConcreteDictionary(4);
446                         
447                         AssertNotNull(myDictionary.GetEnumerator());
448                 }
449
450                 [Test]
451                 public void Keys() 
452                 {
453                         ConcreteDictionary myDictionary = new ConcreteDictionary(5);
454                         ICollection keys = myDictionary.BaseDictionary.Keys;
455
456                         int total = 0;
457                         foreach (int i in keys) 
458                                 total += i;
459                         AssertEquals(10, total);
460                         AssertEquals(5, keys.Count);
461                 }
462
463                 [Test]
464                 public void Values() 
465                 {
466                         ConcreteDictionary myDictionary = new ConcreteDictionary(5);
467                         ICollection values = myDictionary.BaseDictionary.Values;
468
469                         int total = 0;
470                         foreach (int i in values) 
471                                 total += i;
472                         AssertEquals(20, total);
473                         AssertEquals(5, values.Count);
474                 }
475
476                 [Test]
477                 public void Get() 
478                 {
479                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
480                         int v = (int) myDictionary.BaseDictionary[10];
481
482                         Assert("OnGet must be fired", myDictionary.onGetFired);
483                         AssertEquals(v, 20);
484                 }
485
486                 [Test]
487                 public void GetOnGetExcept() 
488                 {
489                         bool exceptionThrown = false;
490
491                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
492                         myDictionary.onGetMustThrowException = true;
493
494                         try {
495                                 int v = (int) myDictionary.BaseDictionary[10];
496                         } catch {
497                                 exceptionThrown = true;
498                         } finally {
499                                 Assert("Exception must be thrown", exceptionThrown);
500                                 Assert("OnGet must be fired", myDictionary.onGetFired);
501                         }
502                 }
503
504                 [Test]
505                 public void GetNoKey() 
506                 {
507                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
508                         AssertNull(myDictionary.BaseDictionary[100]);
509                 }
510
511                 [Test]
512                 public void Set() 
513                 {
514                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
515                         myDictionary.BaseDictionary[10] = 50;
516
517                         Assert("OnValidate must be fired", myDictionary.onValidateFired);
518                         Assert("OnSet must be fired", myDictionary.onSetFired);
519                         Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
520                         AssertEquals(50, myDictionary.BaseDictionary[10]);
521                 }
522
523                 [Test]
524                 public void SetNewKey() 
525                 {
526                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
527                         myDictionary.BaseDictionary[111] = 222;
528
529                         Assert("OnValidate must be fired", myDictionary.onValidateFired);
530                         Assert("OnSet must be fired", myDictionary.onSetFired);
531                         Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
532                         Assert("OnInsert must not be fired", !myDictionary.onInsertFired);
533                         Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
534                         AssertEquals(222, myDictionary.BaseDictionary[111]);
535                         AssertEquals(19, myDictionary.Count);
536                 }
537
538                 [Test]
539                 public void SetOnValidateExcept() 
540                 {
541                         bool exceptionThrown = false;
542
543                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
544                         myDictionary.onValidateMustThrowException = true;
545                         
546                         try {
547                                 myDictionary.BaseDictionary[10] = 50;
548                         } catch {
549                                 exceptionThrown = true;
550                         } finally {
551                                 Assert("Exception must be thrown", exceptionThrown);
552                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
553                                 Assert("OnSet must not be fired", !myDictionary.onSetFired);
554                                 Assert("OnSetComplete not must be fired", !myDictionary.onSetCompleteFired);
555                                 AssertEquals(20, myDictionary.BaseDictionary[10]);
556                         }
557                 }
558
559                 [Test]
560                 public void SetOnSetExcept() 
561                 {
562                         bool exceptionThrown = false;
563
564                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
565                         myDictionary.onSetMustThrowException = true;
566                         
567                         try {
568                                 myDictionary.BaseDictionary[10] = 50;
569                         } catch {
570                                 exceptionThrown = true;
571                         } finally {
572                                 Assert("Exception must be thrown", exceptionThrown);
573                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
574                                 Assert("OnSet must be fired", myDictionary.onSetFired);
575                                 Assert("OnSetComplete must not be fired", !myDictionary.onSetCompleteFired);
576                                 AssertEquals(20, myDictionary.BaseDictionary[10]);
577                         }
578                 }
579
580                 [Test]
581                 public void SetOnSetCompleteExcept() 
582                 {
583                         bool exceptionThrown = false;
584
585                         ConcreteDictionary myDictionary = new ConcreteDictionary(18);
586                         myDictionary.onSetCompleteMustThrowException = true;
587                         
588                         try {
589                                 myDictionary.BaseDictionary[10] = 50;
590                         } catch {
591                                 exceptionThrown = true;
592                         } finally {
593                                 Assert("Exception must be thrown", exceptionThrown);
594                                 Assert("OnValidate must be fired", myDictionary.onValidateFired);
595                                 Assert("OnSet must be fired", myDictionary.onSetFired);
596                                 Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
597                                 AssertEquals(20, myDictionary.BaseDictionary[10]);
598                         }
599                 }
600
601                 [Test]
602                 public void IsReadOnly() 
603                 {
604                         ConcreteDictionary myDictionary = new ConcreteDictionary(1);
605                         Assert(!myDictionary.BaseDictionary.IsReadOnly);
606                 }
607
608                 [Test]
609                 public void IsFixedSize() 
610                 {
611                         ConcreteDictionary myDictionary = new ConcreteDictionary(1);
612                         Assert(!myDictionary.BaseDictionary.IsFixedSize);
613                 }
614
615                 [Test]
616                 public void DictionaryProperty()
617                 {
618                         ConcreteDictionary myDictionary = new ConcreteDictionary(1);
619                         AssertEquals(myDictionary, myDictionary.BaseDictionary);
620                 }
621         }
622 }