96a5eecddf076f8890a29a138b564616cc72ee83
[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
12 using NUnit.Framework;
13
14 namespace MonoTests.System.Collections
15 {
16         [TestFixture]
17         public class DictionaryBaseTest
18         {
19                 public class ConcreteDictionary : DictionaryBase
20                 {
21                         public bool onInsertFired;
22                         public bool onInsertCompleteFired;
23                         public bool onValidateFired;
24                         public bool onValidateExist;
25                         public bool onRemoveFired;
26                         public bool onRemoveExist;
27                         public bool onRemoveCompleteFired;
28                         public bool onClearFired;
29                         public bool onClearCompleteFired;
30                         public bool onSetFired;
31                         public bool onSetExist;
32                         public bool onSetCompleteFired;
33                         public bool onGetFired;
34                         public bool onGetExist;
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                                         ((IDictionary) this).Add (j, j * 2);
55                                 ClearFlags();
56                         }
57
58                         public IDictionary BaseDictionary {
59                                 get { return this.Dictionary; }
60                         }
61
62                         public void ClearFlags ()
63                         {
64                                 onInsertFired = false;
65                                 onInsertCompleteFired = false;
66                                 onValidateFired = false;
67                                 onRemoveFired = false;
68                                 onRemoveCompleteFired = false;
69                                 onClearFired = false;
70                                 onClearCompleteFired = false;
71                                 onSetFired = false;
72                                 onSetCompleteFired = false;
73                                 onGetFired = false;
74                         }
75
76                         protected override void OnValidate (object key, object value)
77                         {
78                                 onValidateFired = true;
79                                 if (key != null)
80                                         Assert.AreEqual (onValidateExist, BaseDictionary.Contains (key));
81                                 if (onValidateMustThrowException)
82                                         throw new Exception ();
83                                 base.OnValidate (key, value);
84                         }
85
86                         protected override void OnInsert (object key, object value)
87                         {
88                                 onInsertFired = true;
89                                 Assert.IsFalse (BaseDictionary.Contains (key));
90                                 if (onInsertMustThrowException)
91                                         throw new Exception ();
92                                 base.OnInsert (key, value);
93                         }
94
95                         protected override void OnInsertComplete (object key, object value)
96                         {
97                                 onInsertCompleteFired = true;
98                                 Assert.IsTrue (BaseDictionary.Contains (key));
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                                 Assert.AreEqual (onRemoveExist, BaseDictionary.Contains (key));
108                                 if (onRemoveMustThrowException)
109                                         throw new Exception ();
110                                 base.OnRemove (key, value);
111                         }
112
113                         protected override void OnRemoveComplete (object key, object value)
114                         {
115                                 onRemoveCompleteFired = true;
116                                 Assert.IsFalse (BaseDictionary.Contains (key));
117                                 if (onRemoveCompleteMustThrowException)
118                                         throw new Exception ();
119                                 base.OnRemoveComplete (key, value);
120                         }
121
122                         protected override void OnClear ()
123                         {
124                                 onClearFired = true;
125                                 if (onClearMustThrowException)
126                                         throw new Exception ();
127                                 base.OnClear ();
128                         }
129
130                         protected override void OnClearComplete ()
131                         {
132                                 onClearCompleteFired = true;
133                                 Assert.AreEqual (0, BaseDictionary.Count);
134                                 if (onClearCompleteMustThrowException)
135                                         throw new Exception ();
136                                 base.OnClearComplete ();
137                         }
138
139                         protected override object OnGet (object key, object currentValue)
140                         {
141                                 onGetFired = true;
142                                 Assert.AreEqual (onGetExist, BaseDictionary.Contains (key));
143                                 if (onGetMustThrowException)
144                                         throw new Exception ();
145                                 return base.OnGet (key, currentValue);
146                         }
147
148                         protected override void OnSet (object key, object oldValue, object newValue)
149                         {
150                                 onSetFired = true;
151                                 Assert.AreEqual (onSetExist, BaseDictionary.Contains (key));
152                                 if (onSetMustThrowException)
153                                         throw new Exception ();
154                                 base.OnSet (key, oldValue, newValue);
155                         }
156
157                         protected override void OnSetComplete (object key, object oldValue, object newValue)
158                         {
159                                 onSetCompleteFired = true;
160                                 Assert.IsTrue (BaseDictionary.Contains (key));
161                                 if (onSetCompleteMustThrowException)
162                                         throw new Exception ();
163                                 base.OnSetComplete (key, oldValue, newValue);
164                         }
165                 }
166
167                 [Test]
168                 public void Add ()
169                 {
170                         ConcreteDictionary myDictionary = new ConcreteDictionary (10);
171                         myDictionary.BaseDictionary.Add(100, 1);
172
173                         Assert.IsTrue (myDictionary.onInsertFired, "#1");
174                         Assert.IsTrue (myDictionary.onInsertCompleteFired, "#2");
175                         Assert.IsTrue (myDictionary.onValidateFired, "#3");
176                         Assert.IsFalse (myDictionary.onRemoveFired, "#4");
177                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#5");
178                         Assert.IsFalse (myDictionary.onClearFired, "#6");
179                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#7");
180                         Assert.IsFalse (myDictionary.onSetFired, "#8");
181                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#9");
182                         Assert.IsFalse (myDictionary.onGetFired, "#10");
183                         Assert.AreEqual (11, myDictionary.Count, "#11");
184                         myDictionary.onGetExist = true;
185                         Assert.AreEqual (1, myDictionary.BaseDictionary [100], "#12");
186                 }
187
188                 [Test]
189                 public void AddOnValidateExcept ()
190                 {
191                         ConcreteDictionary myDictionary = new ConcreteDictionary (30);
192                         myDictionary.onValidateMustThrowException = true;
193
194                         try {
195                                 myDictionary.BaseDictionary.Add(111,222);
196                                 Assert.Fail ("#A1");
197                         } catch (AssertionException) {
198                                 throw;
199                         } catch (Exception ex) {
200                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
201                                 Assert.IsNull (ex.InnerException, "#A3");
202                                 Assert.IsNotNull (ex.Message, "#A4");
203                         }
204
205                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
206                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
207                         Assert.IsTrue (myDictionary.onValidateFired, "#B3");
208                         Assert.IsFalse (myDictionary.onRemoveFired, "#B4");
209                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
210                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
211                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
212                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
213                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
214                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
215                         Assert.AreEqual (30, myDictionary.Count, "#B11");
216                 }
217
218                 [Test]
219                 public void AddOnInsertExcept ()
220                 {
221                         ConcreteDictionary myDictionary = new ConcreteDictionary (30);
222                         myDictionary.onInsertMustThrowException = true;
223
224                         try {
225                                 myDictionary.BaseDictionary.Add (666, 222);
226                                 Assert.Fail ("#A1");
227                         } catch (AssertionException) {
228                                 throw;
229                         } catch (Exception ex) {
230                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
231                                 Assert.IsNull (ex.InnerException, "#A3");
232                                 Assert.IsNotNull (ex.Message, "#A4");
233                         }
234
235                         Assert.IsTrue (myDictionary.onValidateFired, "#B1");
236                         Assert.IsTrue (myDictionary.onInsertFired, "#B2");
237                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B3");
238                         Assert.AreEqual (30, myDictionary.Count, "#B4");
239                 }
240
241                 [Test]
242                 public void AddOnInsertCompleteExcept ()
243                 {
244                         ConcreteDictionary myDictionary = new ConcreteDictionary (5);
245                         myDictionary.onInsertCompleteMustThrowException = true;
246
247                         try {
248                                 myDictionary.BaseDictionary.Add (888, 999);
249                                 Assert.Fail ("#A1");
250                         } catch (AssertionException) {
251                                 throw;
252                         } catch (Exception ex) {
253                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
254                                 Assert.IsNull (ex.InnerException, "#A3");
255                                 Assert.IsNotNull (ex.Message, "#A4");
256                         }
257
258                         Assert.IsTrue (myDictionary.onValidateFired, "#B1");
259                         Assert.IsTrue (myDictionary.onInsertFired, "#B2");
260                         Assert.IsTrue (myDictionary.onInsertCompleteFired, "#B3");
261                         Assert.AreEqual (5, myDictionary.Count, "#B4");
262                 }
263
264                 [Test]
265                 public void AddNullKey ()
266                 {
267                         ConcreteDictionary myDictionary = new ConcreteDictionary ();
268
269                         try {
270                                 myDictionary.BaseDictionary.Add (null, 11);
271                                 Assert.Fail ("#A1");
272                         } catch (ArgumentNullException ex) {
273                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
274                                 Assert.IsNull (ex.InnerException, "#A3");
275                                 Assert.IsNotNull (ex.Message, "#A4");
276                                 Assert.AreEqual ("key", ex.ParamName, "#A5");
277                         }
278
279                         Assert.IsTrue (myDictionary.onValidateFired, "#B1");
280                         Assert.IsTrue (myDictionary.onInsertFired, "#B2");
281                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B3");
282                 }
283
284                 [Test]
285                 public void Clear ()
286                 {
287                         ConcreteDictionary myDictionary = new ConcreteDictionary (30);
288                         myDictionary.Clear();
289
290                         Assert.IsTrue (myDictionary.onClearFired, "#1");
291                         Assert.IsTrue (myDictionary.onClearCompleteFired, "#2");
292                         Assert.AreEqual (0, myDictionary.Count, "#3");
293                 }
294
295                 [Test]
296                 public void ClearOnClearExcept ()
297                 {
298                         ConcreteDictionary myDictionary = new ConcreteDictionary (30);
299                         myDictionary.onClearMustThrowException = true;
300
301                         try {
302                                 myDictionary.Clear ();
303                                 Assert.Fail ("#A1");
304                         } catch (AssertionException) {
305                                 throw;
306                         } catch (Exception ex) {
307                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
308                                 Assert.IsNull (ex.InnerException, "#A3");
309                                 Assert.IsNotNull (ex.Message, "#A4");
310                         }
311
312                         Assert.IsTrue (myDictionary.onClearFired, "#B1");
313                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B2");
314                         Assert.AreEqual (30, myDictionary.Count, "#B3");
315                 }
316
317                 [Test]
318                 public void ClearOnClearCompleteExcept ()
319                 {
320                         ConcreteDictionary myDictionary = new ConcreteDictionary (30);
321                         myDictionary.onClearCompleteMustThrowException = true;
322
323                         try {
324                                 myDictionary.Clear();
325                                 Assert.Fail ("#A1");
326                         } catch (AssertionException) {
327                                 throw;
328                         } catch (Exception ex) {
329                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
330                                 Assert.IsNull (ex.InnerException, "#A3");
331                                 Assert.IsNotNull (ex.Message, "#A4");
332                         }
333
334                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
335                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
336                         Assert.IsFalse (myDictionary.onValidateFired, "#B3");
337                         Assert.IsFalse (myDictionary.onRemoveFired, "#B4");
338                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
339                         Assert.IsTrue (myDictionary.onClearFired, "#B6");
340                         Assert.IsTrue (myDictionary.onClearCompleteFired, "#B7");
341                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
342                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
343                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
344                         Assert.AreEqual (0, myDictionary.Count, "#B11");
345                 }
346
347                 [Test]
348                 public void Count ()
349                 {
350                         ConcreteDictionary myDictionary = new ConcreteDictionary (19);
351                         Assert.AreEqual (19, myDictionary.Count);
352                 }
353
354                 [Test]
355                 public void Remove ()
356                 {
357                         ConcreteDictionary myDictionary = new ConcreteDictionary(8);
358                         myDictionary.onValidateExist = true;
359                         myDictionary.onRemoveExist = true;
360                         myDictionary.BaseDictionary.Remove (5);
361
362                         Assert.IsFalse (myDictionary.onInsertFired, "#1");
363                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#2");
364                         Assert.IsTrue (myDictionary.onValidateFired, "#3");
365                         Assert.IsTrue (myDictionary.onRemoveFired, "#4");
366                         Assert.IsTrue (myDictionary.onRemoveCompleteFired, "#5");
367                         Assert.IsFalse (myDictionary.onClearFired, "#6");
368                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#7");
369                         Assert.IsFalse (myDictionary.onSetFired, "#8");
370                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#9");
371                         Assert.IsFalse (myDictionary.onGetFired, "#10");
372                         Assert.AreEqual (7, myDictionary.Count, "#11");
373                         Assert.IsNull (myDictionary.BaseDictionary [5], "#12");
374                 }
375
376                 [Test]
377                 public void RemoveOnValidateExcept ()
378                 {
379                         ConcreteDictionary myDictionary = new ConcreteDictionary (28);
380                         myDictionary.onValidateExist = true;
381                         myDictionary.onValidateMustThrowException = true;
382
383                         try {
384                                 myDictionary.BaseDictionary.Remove (11);
385                                 Assert.Fail ("#A1");
386                         } catch (AssertionException) {
387                                 throw;
388                         } catch (Exception ex) {
389                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
390                                 Assert.IsNull (ex.InnerException, "#A3");
391                                 Assert.IsNotNull (ex.Message, "#A4");
392                         }
393
394                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
395                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
396                         Assert.IsTrue (myDictionary.onValidateFired, "#B3");
397                         Assert.IsFalse (myDictionary.onRemoveFired, "#B4");
398                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
399                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
400                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
401                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
402                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
403                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
404                         Assert.AreEqual (28, myDictionary.Count, "#B11");
405                         myDictionary.onGetExist = true;
406                         Assert.AreEqual (22, myDictionary.BaseDictionary [11], "#B12");
407                 }
408
409                 [Test]
410                 public void RemoveOnRemoveExcept ()
411                 {
412                         ConcreteDictionary myDictionary = new ConcreteDictionary (28);
413                         myDictionary.onValidateExist = true;
414                         myDictionary.onRemoveExist = true;
415                         myDictionary.onRemoveMustThrowException = true;
416
417                         try {
418                                 myDictionary.BaseDictionary.Remove (11);
419                                 Assert.Fail ("#A1");
420                         } catch (AssertionException) {
421                                 throw;
422                         } catch (Exception ex) {
423                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
424                                 Assert.IsNull (ex.InnerException, "#A3");
425                                 Assert.IsNotNull (ex.Message, "#A4");
426                         }
427
428                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
429                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
430                         Assert.IsTrue (myDictionary.onValidateFired, "#B3");
431                         Assert.IsTrue (myDictionary.onRemoveFired, "#B4");
432                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
433                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
434                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
435                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
436                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
437                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
438                         Assert.AreEqual (28, myDictionary.Count, "#B11");
439                         myDictionary.onGetExist = true;
440                         Assert.AreEqual (22, myDictionary.BaseDictionary [11], "#B12");
441                 }
442
443                 [Test]
444                 public void RemoveOnRemoveCompleteExcept ()
445                 {
446                         ConcreteDictionary myDictionary = new ConcreteDictionary (28);
447                         myDictionary.onValidateExist = true;
448                         myDictionary.onRemoveExist = true;
449                         myDictionary.onRemoveCompleteMustThrowException = true;
450
451                         try {
452                                 myDictionary.BaseDictionary.Remove (11);
453                                 Assert.Fail ("#A1");
454                         } catch (AssertionException) {
455                                 throw;
456                         } catch (Exception ex) {
457                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
458                                 Assert.IsNull (ex.InnerException, "#A3");
459                                 Assert.IsNotNull (ex.Message, "#A4");
460                         }
461
462                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
463                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
464                         Assert.IsTrue (myDictionary.onValidateFired, "#B3");
465                         Assert.IsTrue (myDictionary.onRemoveFired, "#B4");
466                         Assert.IsTrue (myDictionary.onRemoveCompleteFired, "#B5");
467                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
468                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
469                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
470                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
471                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
472 #if NET_2_0
473                         myDictionary.onGetExist = true;
474                         Assert.AreEqual (28, myDictionary.Count, "#B11");
475                         Assert.AreEqual (22, myDictionary.BaseDictionary [11], "#B12");
476 #else
477                         Assert.AreEqual (27, myDictionary.Count, "#B11");
478                         Assert.IsNull (myDictionary.BaseDictionary [11], "#B12");
479 #endif
480                 }
481
482                 [Test]
483                 public void RemoveKeyNotInDictionary ()
484                 {
485                         ConcreteDictionary myDictionary = new ConcreteDictionary (28);
486                         myDictionary.BaseDictionary.Remove (80);
487
488                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
489                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
490 #if NET_2_0
491                         Assert.IsFalse (myDictionary.onValidateFired, "#1");
492                         Assert.IsFalse (myDictionary.onRemoveFired, "#2");
493                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#3");
494 #else
495                         Assert.IsTrue (myDictionary.onValidateFired, "#1");
496                         Assert.IsTrue (myDictionary.onRemoveFired, "#2");
497                         Assert.IsTrue (myDictionary.onRemoveCompleteFired, "#3");
498 #endif
499                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
500                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
501                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
502                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
503                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
504                 }
505
506                 [Test]
507                 public void Items ()
508                 {
509                         ConcreteDictionary myDictionary = new ConcreteDictionary (19);
510                         myDictionary.onGetExist = true;
511
512                         for (int i = 0; i < 19; i++)
513                                 Assert.AreEqual (i * 2, (int) myDictionary.BaseDictionary [i]);
514                 }
515
516                 [Test]
517                 public void Contains ()
518                 {
519                         ConcreteDictionary myDictionary = new ConcreteDictionary (14);
520                         for (int i = 0; i < 14; i++)
521                                 Assert.IsTrue (myDictionary.BaseDictionary.Contains (i), "Must contain " + i);
522                         for (int i = 14; i < 34; i++)
523                                 Assert.IsFalse (myDictionary.BaseDictionary.Contains (i), "Must not contain " + i);
524                 }
525
526                 [Test]
527                 public void GetEnumerator ()
528                 {
529                         ConcreteDictionary myDictionary = new ConcreteDictionary (4);
530                         Assert.IsNotNull (myDictionary.GetEnumerator ());
531                 }
532
533                 [Test]
534                 public void Keys ()
535                 {
536                         ConcreteDictionary myDictionary = new ConcreteDictionary (5);
537                         ICollection keys = myDictionary.BaseDictionary.Keys;
538
539                         int total = 0;
540                         foreach (int i in keys)
541                                 total += i;
542                         Assert.AreEqual (10, total, "#1");
543                         Assert.AreEqual (5, keys.Count, "#2");
544                 }
545
546                 [Test]
547                 public void Values ()
548                 {
549                         ConcreteDictionary myDictionary = new ConcreteDictionary (5);
550                         ICollection values = myDictionary.BaseDictionary.Values;
551
552                         int total = 0;
553                         foreach (int i in values)
554                                 total += i;
555                         Assert.AreEqual (20, total, "#1");
556                         Assert.AreEqual (5, values.Count, "#2");
557                 }
558
559                 [Test]
560                 public void Get ()
561                 {
562                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
563                         myDictionary.onGetExist = true;
564                         int v = (int) myDictionary.BaseDictionary[10];
565
566                         Assert.IsFalse (myDictionary.onInsertFired, "#1");
567                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#2");
568                         Assert.IsFalse (myDictionary.onValidateFired, "#3");
569                         Assert.IsFalse (myDictionary.onRemoveFired, "#4");
570                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#5");
571                         Assert.IsFalse (myDictionary.onClearFired, "#6");
572                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#7");
573                         Assert.IsFalse (myDictionary.onSetFired, "#8");
574                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#9");
575                         Assert.IsTrue (myDictionary.onGetFired, "#10");
576                         Assert.AreEqual (20, v, "#11");
577                 }
578
579                 [Test]
580                 public void GetOnGetExcept ()
581                 {
582                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
583                         myDictionary.onGetExist = true;
584                         myDictionary.onGetMustThrowException = true;
585
586                         try {
587                                 int v = (int) myDictionary.BaseDictionary [10];
588                                 Assert.Fail ("#1:" + v);
589                         } catch (AssertionException) {
590                                 throw;
591                         } catch (Exception ex) {
592                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
593                                 Assert.IsNull (ex.InnerException, "#A3");
594                                 Assert.IsNotNull (ex.Message, "#A4");
595                         }
596
597                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
598                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
599                         Assert.IsFalse (myDictionary.onValidateFired, "#B3");
600                         Assert.IsFalse (myDictionary.onRemoveFired, "#B4");
601                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
602                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
603                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
604                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
605                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
606                         Assert.IsTrue (myDictionary.onGetFired, "#B10");
607                 }
608
609                 [Test]
610                 public void GetNoKey ()
611                 {
612                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
613                         Assert.IsNull (myDictionary.BaseDictionary [100]);
614                 }
615
616                 [Test]
617                 public void Set ()
618                 {
619                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
620                         myDictionary.onValidateExist = true;
621                         myDictionary.onSetExist = true;
622                         myDictionary.BaseDictionary[10] = 50;
623
624                         Assert.IsFalse (myDictionary.onInsertFired, "#1");
625                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#2");
626                         Assert.IsTrue (myDictionary.onValidateFired, "#3");
627                         Assert.IsFalse (myDictionary.onRemoveFired, "#4");
628                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#5");
629                         Assert.IsFalse (myDictionary.onClearFired, "#6");
630                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#7");
631                         Assert.IsTrue (myDictionary.onSetFired, "#8");
632                         Assert.IsTrue (myDictionary.onSetCompleteFired, "#9");
633                         Assert.IsFalse (myDictionary.onGetFired, "#10");
634                         myDictionary.onGetExist = true;
635                         Assert.AreEqual (50, myDictionary.BaseDictionary [10], "#11");
636                 }
637
638                 [Test]
639                 public void SetNewKey ()
640                 {
641                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
642                         myDictionary.BaseDictionary[111] = 222;
643
644                         Assert.IsFalse (myDictionary.onInsertFired, "#1");
645                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#2");
646                         Assert.IsTrue (myDictionary.onValidateFired, "#3");
647                         Assert.IsFalse (myDictionary.onRemoveFired, "#4");
648                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#5");
649                         Assert.IsFalse (myDictionary.onClearFired, "#6");
650                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#7");
651                         Assert.IsTrue (myDictionary.onSetFired, "#8");
652                         Assert.IsTrue (myDictionary.onSetCompleteFired, "#9");
653                         Assert.IsFalse (myDictionary.onGetFired, "#10");
654                         myDictionary.onGetExist = true;
655                         Assert.AreEqual (222, myDictionary.BaseDictionary [111], "#11");
656                         Assert.AreEqual (19, myDictionary.Count, "#12");
657                 }
658
659                 [Test]
660                 public void SetOnValidateExcept()
661                 {
662                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
663                         myDictionary.onValidateExist = true;
664                         myDictionary.onValidateMustThrowException = true;
665                         
666                         try {
667                                 myDictionary.BaseDictionary[10] = 50;
668                                 Assert.Fail ("#A1");
669                         } catch (AssertionException) {
670                                 throw;
671                         } catch (Exception ex) {
672                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
673                                 Assert.IsNull (ex.InnerException, "#A3");
674                                 Assert.IsNotNull (ex.Message, "#A4");
675                         }
676
677                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
678                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
679                         Assert.IsTrue (myDictionary.onValidateFired, "#B3");
680                         Assert.IsFalse (myDictionary.onRemoveFired, "#B4");
681                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
682                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
683                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
684                         Assert.IsFalse (myDictionary.onSetFired, "#B8");
685                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
686                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
687                         myDictionary.onGetExist = true;
688                         Assert.AreEqual (20, myDictionary.BaseDictionary [10], "#B11");
689                 }
690
691                 [Test]
692                 public void SetOnSetExcept()
693                 {
694                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
695                         myDictionary.onValidateExist = true;
696                         myDictionary.onSetExist = true;
697                         myDictionary.onSetMustThrowException = true;
698                         
699                         try {
700                                 myDictionary.BaseDictionary[10] = 50;
701                                 Assert.Fail ("#A1");
702                         } catch (AssertionException) {
703                                 throw;
704                         } catch (Exception ex) {
705                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
706                                 Assert.IsNull (ex.InnerException, "#A3");
707                                 Assert.IsNotNull (ex.Message, "#A4");
708                         }
709
710                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
711                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
712                         Assert.IsTrue (myDictionary.onValidateFired, "#B3");
713                         Assert.IsFalse (myDictionary.onRemoveFired, "#B4");
714                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
715                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
716                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
717                         Assert.IsTrue (myDictionary.onSetFired, "#B8");
718                         Assert.IsFalse (myDictionary.onSetCompleteFired, "#B9");
719                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
720                         myDictionary.onGetExist = true;
721                         Assert.AreEqual (20, myDictionary.BaseDictionary [10], "#B11");
722                 }
723
724                 [Test]
725                 public void SetOnSetCompleteExcept() 
726                 {
727                         ConcreteDictionary myDictionary = new ConcreteDictionary (18);
728                         myDictionary.onValidateExist = true;
729                         myDictionary.onSetExist = true;
730                         myDictionary.onSetCompleteMustThrowException = true;
731                         
732                         try {
733                                 myDictionary.BaseDictionary[10] = 50;
734                                 Assert.Fail ("#A1");
735                         } catch (AssertionException) {
736                                 throw;
737                         } catch (Exception ex) {
738                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#A2");
739                                 Assert.IsNull (ex.InnerException, "#A3");
740                                 Assert.IsNotNull (ex.Message, "#A4");
741                         }
742
743                         Assert.IsFalse (myDictionary.onInsertFired, "#B1");
744                         Assert.IsFalse (myDictionary.onInsertCompleteFired, "#B2");
745                         Assert.IsTrue (myDictionary.onValidateFired, "#B3");
746                         Assert.IsFalse (myDictionary.onRemoveFired, "#B4");
747                         Assert.IsFalse (myDictionary.onRemoveCompleteFired, "#B5");
748                         Assert.IsFalse (myDictionary.onClearFired, "#B6");
749                         Assert.IsFalse (myDictionary.onClearCompleteFired, "#B7");
750                         Assert.IsTrue (myDictionary.onSetFired, "#B8");
751                         Assert.IsTrue (myDictionary.onSetCompleteFired, "#B9");
752                         Assert.IsFalse (myDictionary.onGetFired, "#B10");
753                         myDictionary.onGetExist = true;
754                         Assert.AreEqual (20, myDictionary.BaseDictionary [10], "#B11");
755                 }
756
757                 [Test]
758                 public void IsReadOnly() 
759                 {
760                         ConcreteDictionary myDictionary = new ConcreteDictionary (1);
761                         Assert.IsFalse (myDictionary.BaseDictionary.IsReadOnly);
762                 }
763
764                 [Test]
765                 public void IsFixedSize() 
766                 {
767                         ConcreteDictionary myDictionary = new ConcreteDictionary (1);
768                         Assert.IsFalse (myDictionary.BaseDictionary.IsFixedSize);
769                 }
770
771                 [Test]
772                 public void DictionaryProperty()
773                 {
774                         ConcreteDictionary myDictionary = new ConcreteDictionary (1);
775                         Assert.AreEqual (myDictionary, myDictionary.BaseDictionary);
776                 }
777
778                 public class NullDictionary : DictionaryBase
779                 {
780                         protected override object OnGet (object key, object currentValue)
781                         {
782                                 return null;
783                         }
784                 }
785
786                 [Test]
787                 public void NullDictionary_Get ()
788                 {
789                         IDictionary dictionary = new NullDictionary ();
790                         dictionary ["a"] = "b";
791                         Assert.AreEqual ("b", dictionary ["a"]);
792                 }
793
794                 public class ModifyDictionary : DictionaryBase
795                 {
796                         protected override object OnGet (object key, object currentValue)
797                         {
798                                 (this as IDictionary) [key] = key;
799                                 return key;
800                         }
801                 }
802
803                 [Test]
804                 public void ModifyDictionary_Get ()
805                 {
806                         IDictionary dictionary = new ModifyDictionary ();
807                         dictionary ["a"] = "b";
808 #if NET_2_0
809                         // first time we return "b" - because the value was cached
810                         Assert.AreEqual ("b", dictionary ["a"], "#1");
811 #else
812                         Assert.AreEqual ("a", dictionary ["a"], "#1");
813 #endif
814                         // second time we return "a" - because it's the value in the dictionary
815                         Assert.AreEqual ("a", dictionary ["a"], "#2");
816                 }
817
818                 public class ThrowDictionary : DictionaryBase
819                 {
820                         protected override object OnGet (object key, object currentValue)
821                         {
822                                 throw new ArgumentException ((string) key, (string) currentValue);
823                         }
824                 }
825
826                 [Test]
827                 public void ThrowDictionary_Get ()
828                 {
829                         IDictionary dictionary = new ThrowDictionary ();
830                         dictionary ["a"] = "b";
831
832                         try {
833                                 object value = dictionary ["a"];
834                                 Assert.Fail ("#1: " + value);
835                         } catch (ArgumentException ex) {
836                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
837                                 Assert.IsNull (ex.InnerException, "#3");
838                                 Assert.IsNotNull (ex.Message, "#4");
839                                 Assert.IsTrue (ex.Message.StartsWith ("a"), "#5");
840                                 Assert.AreEqual ("b", ex.ParamName, "#6");
841                         }
842                 }
843         }
844 }