2 // System.Collections.DictionaryBase
\r
3 // Test suite for System.Collections.DictionaryBase
\r
6 // Carlos Alberto Barcenilla (barce@frlp.utn.edu.ar)
\r
10 using System.Collections;
\r
11 using NUnit.Framework;
\r
13 namespace MonoTests.System.Collections
\r
17 public class DictionaryBaseTest: Assertion
\r
19 static void Main(string[] args)
\r
23 public class ConcreteDictionary : DictionaryBase
\r
25 public bool onInsertFired;
\r
26 public bool onInsertCompleteFired;
\r
27 public bool onValidateFired;
\r
28 public bool onRemoveFired;
\r
29 public bool onRemoveCompleteFired;
\r
30 public bool onClearFired;
\r
31 public bool onClearCompleteFired;
\r
32 public bool onSetFired;
\r
33 public bool onSetCompleteFired;
\r
34 public bool onGetFired;
\r
36 public bool onInsertMustThrowException;
\r
37 public bool onInsertCompleteMustThrowException;
\r
38 public bool onValidateMustThrowException;
\r
39 public bool onRemoveMustThrowException;
\r
40 public bool onRemoveCompleteMustThrowException;
\r
41 public bool onClearMustThrowException;
\r
42 public bool onClearCompleteMustThrowException;
\r
43 public bool onSetMustThrowException;
\r
44 public bool onSetCompleteMustThrowException;
\r
45 public bool onGetMustThrowException;
\r
47 public ConcreteDictionary()
\r
51 public ConcreteDictionary(int i)
\r
53 for (int j = 0; j < i; j++)
\r
55 ((IDictionary) this).Add(j, j*2);
\r
61 public IDictionary BaseDictionary
\r
63 get { return this.Dictionary; }
\r
66 public void ClearFlags()
\r
68 onInsertFired = false;
\r
69 onInsertCompleteFired = false;
\r
70 onValidateFired = false;
\r
71 onRemoveFired = false;
\r
72 onRemoveCompleteFired = false;
\r
73 onClearFired = false;
\r
74 onClearCompleteFired = false;
\r
76 onSetCompleteFired = false;
\r
80 protected override void OnValidate(object key, object value)
\r
82 onValidateFired = true;
\r
83 if (onValidateMustThrowException)
\r
84 throw new Exception();
\r
85 base.OnValidate (key, value);
\r
88 protected override void OnInsert(object key, object value)
\r
90 onInsertFired = true;
\r
91 if (onInsertMustThrowException)
\r
92 throw new Exception();
\r
93 base.OnInsert (key, value);
\r
96 protected override void OnInsertComplete(object key, object value)
\r
98 onInsertCompleteFired = true;
\r
99 if (onInsertCompleteMustThrowException)
\r
100 throw new Exception();
\r
101 base.OnInsertComplete (key, value);
\r
104 protected override void OnRemove(object key, object value)
\r
106 onRemoveFired = true;
\r
107 if (onRemoveMustThrowException)
\r
108 throw new Exception();
\r
109 base.OnRemove (key, value);
\r
112 protected override void OnRemoveComplete(object key, object value)
\r
114 onRemoveCompleteFired = true;
\r
115 if (onRemoveCompleteMustThrowException)
\r
116 throw new Exception();
\r
117 base.OnRemoveComplete (key, value);
\r
121 protected override void OnClear()
\r
123 onClearFired = true;
\r
124 if (onClearMustThrowException)
\r
125 throw new Exception();
\r
129 protected override void OnClearComplete()
\r
131 onClearCompleteFired = true;
\r
132 if (onClearCompleteMustThrowException)
\r
133 throw new Exception();
\r
134 base.OnClearComplete ();
\r
137 protected override object OnGet(object key, object currentValue)
\r
140 if (onGetMustThrowException)
\r
141 throw new Exception();
\r
142 return base.OnGet (key, currentValue);
\r
145 protected override void OnSet(object key, object oldValue, object newValue)
\r
148 if (onSetMustThrowException)
\r
149 throw new Exception();
\r
150 base.OnSet (key, oldValue, newValue);
\r
153 protected override void OnSetComplete(object key, object oldValue, object newValue)
\r
155 onSetCompleteFired = true;
\r
156 if (onSetCompleteMustThrowException)
\r
157 throw new Exception();
\r
158 base.OnSetComplete (key, oldValue, newValue);
\r
165 ConcreteDictionary myDictionary = new ConcreteDictionary(10);
\r
166 myDictionary.BaseDictionary.Add(100, 1);
\r
168 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
169 Assert("OnInsert must be fired", myDictionary.onInsertFired);
\r
170 Assert("OnInsertComplete must be fired", myDictionary.onInsertCompleteFired);
\r
171 AssertEquals("Count", 11, myDictionary.Count);
\r
172 AssertEquals(1, myDictionary.BaseDictionary[100]);
\r
176 public void AddOnValidateExcept()
\r
178 bool exceptionThrown = false;
\r
180 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
\r
181 myDictionary.onValidateMustThrowException = true;
\r
184 myDictionary.BaseDictionary.Add(111,222);
\r
186 exceptionThrown = true;
\r
188 Assert("Exception must be thrown", exceptionThrown);
\r
189 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
190 Assert("OnInsert must not be fired", !myDictionary.onInsertFired);
\r
191 Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
\r
192 AssertEquals("Count", 30, myDictionary.Count);
\r
198 public void AddOnInsertExcept()
\r
200 bool exceptionThrown = false;
\r
202 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
\r
203 myDictionary.onInsertMustThrowException = true;
\r
206 myDictionary.BaseDictionary.Add(666,222);
\r
208 exceptionThrown = true;
\r
210 Assert("Exception must be thrown", exceptionThrown);
\r
211 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
212 Assert("OnInsert must be fired", myDictionary.onInsertFired);
\r
213 Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
\r
214 AssertEquals("Count", 30, myDictionary.Count);
\r
220 public void AddOnInsertCompleteExcept()
\r
222 bool exceptionThrown = false;
\r
224 ConcreteDictionary myDictionary = new ConcreteDictionary(5);
\r
225 myDictionary.onInsertCompleteMustThrowException = true;
\r
228 myDictionary.BaseDictionary.Add(888,999);
\r
230 exceptionThrown = true;
\r
232 Assert("Exception must be thrown", exceptionThrown);
\r
233 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
234 Assert("OnInsert must be fired", myDictionary.onInsertFired);
\r
235 Assert("OnInsertComplete must be fired", myDictionary.onInsertCompleteFired);
\r
236 AssertEquals("Count", 5, myDictionary.Count);
\r
242 public void AddNullKey()
\r
244 bool exceptionThrown = false;
\r
246 ConcreteDictionary myDictionary = new ConcreteDictionary();
\r
248 myDictionary.BaseDictionary.Add(null, 11);
\r
249 } catch (ArgumentNullException) {
\r
250 exceptionThrown = true;
\r
252 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
253 Assert("OnInsert must be fired", myDictionary.onInsertFired);
\r
254 Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
\r
255 Assert("ArgumentNullException must be thrown", exceptionThrown);
\r
260 public void Clear()
\r
262 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
\r
263 myDictionary.Clear();
\r
265 Assert("OnClear must be fired", myDictionary.onClearFired);
\r
266 Assert("OnClearComplete must be fired", myDictionary.onClearCompleteFired);
\r
267 AssertEquals("Count", 0, myDictionary.Count);
\r
271 public void ClearOnClearExcept()
\r
273 bool exceptionThrown = false;
\r
275 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
\r
276 myDictionary.onClearMustThrowException = true;
\r
279 myDictionary.Clear();
\r
281 exceptionThrown = true;
\r
283 Assert("Exception must be thrown", exceptionThrown);
\r
284 Assert("OnClear must be fired", myDictionary.onClearFired);
\r
285 Assert("OnClearComplete must not be fired", !myDictionary.onClearCompleteFired);
\r
286 AssertEquals("Count", 30, myDictionary.Count);
\r
292 public void ClearOnClearCompleteExcept()
\r
294 bool exceptionThrown = false;
\r
296 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
\r
297 myDictionary.onClearCompleteMustThrowException = true;
\r
300 myDictionary.Clear();
\r
302 exceptionThrown = true;
\r
304 Assert("Exception must be thrown", exceptionThrown);
\r
305 Assert("OnClear must be fired", myDictionary.onClearFired);
\r
306 Assert("OnClearComplete must be fired", myDictionary.onClearCompleteFired);
\r
307 AssertEquals("Count", 0, myDictionary.Count);
\r
313 public void Count()
\r
315 ConcreteDictionary myDictionary = new ConcreteDictionary(19);
\r
316 AssertEquals(19, myDictionary.Count);
\r
320 public void Remove()
\r
322 ConcreteDictionary myDictionary = new ConcreteDictionary(8);
\r
323 myDictionary.BaseDictionary.Remove(5);
\r
325 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
326 Assert("OnRemove must be fired", myDictionary.onRemoveFired);
\r
327 Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
\r
328 AssertEquals("Count", 7, myDictionary.Count);
\r
329 AssertEquals(null, myDictionary.BaseDictionary[5]);
\r
333 public void RemoveOnValidateExcept()
\r
335 bool exceptionThrown = false;
\r
337 ConcreteDictionary myDictionary = new ConcreteDictionary(28);
\r
338 myDictionary.onValidateMustThrowException = true;
\r
341 myDictionary.BaseDictionary.Remove(11);
\r
343 exceptionThrown = true;
\r
345 Assert("Exception must be thrown in this test", exceptionThrown);
\r
346 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
347 Assert("OnRemove must not be fired", !myDictionary.onRemoveFired);
\r
348 Assert("OnRemoveComplete must not be fired", !myDictionary.onRemoveCompleteFired);
\r
349 AssertEquals("Count", 28, myDictionary.Count);
\r
350 AssertEquals(22, myDictionary.BaseDictionary[11]);
\r
356 public void RemoveOnRemoveExcept()
\r
358 bool exceptionThrown = false;
\r
360 ConcreteDictionary myDictionary = new ConcreteDictionary(28);
\r
361 myDictionary.onRemoveMustThrowException = true;
\r
364 myDictionary.BaseDictionary.Remove(11);
\r
366 exceptionThrown = true;
\r
368 Assert("Exception must be thrown", exceptionThrown);
\r
369 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
370 Assert("OnRemove must be fired", myDictionary.onRemoveFired);
\r
371 Assert("OnRemoveComplete must not be fired", !myDictionary.onRemoveCompleteFired);
\r
372 AssertEquals("Count", 28, myDictionary.Count);
\r
373 AssertEquals(22, myDictionary.BaseDictionary[11]);
\r
379 public void RemoveOnRemoveCompleteExcept()
\r
381 bool exceptionThrown = false;
\r
383 ConcreteDictionary myDictionary = new ConcreteDictionary(28);
\r
384 myDictionary.onRemoveCompleteMustThrowException = true;
\r
388 myDictionary.BaseDictionary.Remove(11);
\r
392 exceptionThrown = true;
\r
396 Assert("Exception must be thrown", exceptionThrown);
\r
397 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
398 Assert("OnRemove must be fired", myDictionary.onRemoveFired);
\r
399 Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
\r
400 AssertEquals("Count", 27, myDictionary.Count);
\r
401 AssertEquals(null, myDictionary.BaseDictionary[11]);
\r
407 public void RemoveKeyNotInDictionary()
\r
410 ConcreteDictionary myDictionary = new ConcreteDictionary(28);
\r
412 myDictionary.BaseDictionary.Remove(80);
\r
413 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
414 Assert("OnRemove must be fired", myDictionary.onRemoveFired);
\r
415 Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
\r
419 public void Items()
\r
421 ConcreteDictionary myDictionary = new ConcreteDictionary(19);
\r
422 for (int i = 0; i < 19; i++)
\r
424 AssertEquals(i*2, (int) myDictionary.BaseDictionary[i]);
\r
429 public void Contains()
\r
431 ConcreteDictionary myDictionary = new ConcreteDictionary(14);
\r
432 for (int i = 0; i < 14; i++)
\r
434 Assert("Must contain " + i, myDictionary.BaseDictionary.Contains(i));
\r
436 for (int i = 14; i < 34; i++)
\r
438 Assert("Must not contain " + i, !myDictionary.BaseDictionary.Contains(i));
\r
443 public void GetEnumerator()
\r
445 ConcreteDictionary myDictionary = new ConcreteDictionary(4);
\r
447 AssertNotNull(myDictionary.GetEnumerator());
\r
451 public void Keys()
\r
453 ConcreteDictionary myDictionary = new ConcreteDictionary(5);
\r
454 ICollection keys = myDictionary.BaseDictionary.Keys;
\r
457 foreach (int i in keys)
\r
459 AssertEquals(10, total);
\r
460 AssertEquals(5, keys.Count);
\r
464 public void Values()
\r
466 ConcreteDictionary myDictionary = new ConcreteDictionary(5);
\r
467 ICollection values = myDictionary.BaseDictionary.Values;
\r
470 foreach (int i in values)
\r
472 AssertEquals(20, total);
\r
473 AssertEquals(5, values.Count);
\r
479 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
480 int v = (int) myDictionary.BaseDictionary[10];
\r
482 Assert("OnGet must be fired", myDictionary.onGetFired);
\r
483 AssertEquals(v, 20);
\r
487 public void GetOnGetExcept()
\r
489 bool exceptionThrown = false;
\r
491 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
492 myDictionary.onGetMustThrowException = true;
\r
495 int v = (int) myDictionary.BaseDictionary[10];
\r
497 exceptionThrown = true;
\r
499 Assert("Exception must be thrown", exceptionThrown);
\r
500 Assert("OnGet must be fired", myDictionary.onGetFired);
\r
505 public void GetNoKey()
\r
507 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
508 AssertNull(myDictionary.BaseDictionary[100]);
\r
514 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
515 myDictionary.BaseDictionary[10] = 50;
\r
517 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
518 Assert("OnSet must be fired", myDictionary.onSetFired);
\r
519 Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
\r
520 AssertEquals(50, myDictionary.BaseDictionary[10]);
\r
524 public void SetNewKey()
\r
526 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
527 myDictionary.BaseDictionary[111] = 222;
\r
529 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
530 Assert("OnSet must be fired", myDictionary.onSetFired);
\r
531 Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
\r
532 Assert("OnInsert must not be fired", !myDictionary.onInsertFired);
\r
533 Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
\r
534 AssertEquals(222, myDictionary.BaseDictionary[111]);
\r
535 AssertEquals(19, myDictionary.Count);
\r
539 public void SetOnValidateExcept()
\r
541 bool exceptionThrown = false;
\r
543 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
544 myDictionary.onValidateMustThrowException = true;
\r
547 myDictionary.BaseDictionary[10] = 50;
\r
549 exceptionThrown = true;
\r
551 Assert("Exception must be thrown", exceptionThrown);
\r
552 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
553 Assert("OnSet must not be fired", !myDictionary.onSetFired);
\r
554 Assert("OnSetComplete not must be fired", !myDictionary.onSetCompleteFired);
\r
555 AssertEquals(20, myDictionary.BaseDictionary[10]);
\r
560 public void SetOnSetExcept()
\r
562 bool exceptionThrown = false;
\r
564 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
565 myDictionary.onSetMustThrowException = true;
\r
568 myDictionary.BaseDictionary[10] = 50;
\r
570 exceptionThrown = true;
\r
572 Assert("Exception must be thrown", exceptionThrown);
\r
573 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
574 Assert("OnSet must be fired", myDictionary.onSetFired);
\r
575 Assert("OnSetComplete must not be fired", !myDictionary.onSetCompleteFired);
\r
576 AssertEquals(20, myDictionary.BaseDictionary[10]);
\r
581 public void SetOnSetCompleteExcept()
\r
583 bool exceptionThrown = false;
\r
585 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
\r
586 myDictionary.onSetCompleteMustThrowException = true;
\r
589 myDictionary.BaseDictionary[10] = 50;
\r
591 exceptionThrown = true;
\r
593 Assert("Exception must be thrown", exceptionThrown);
\r
594 Assert("OnValidate must be fired", myDictionary.onValidateFired);
\r
595 Assert("OnSet must be fired", myDictionary.onSetFired);
\r
596 Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
\r
597 AssertEquals(20, myDictionary.BaseDictionary[10]);
\r
602 public void IsReadOnly()
\r
604 ConcreteDictionary myDictionary = new ConcreteDictionary(1);
\r
605 Assert(!myDictionary.BaseDictionary.IsReadOnly);
\r
609 public void IsFixedSize()
\r
611 ConcreteDictionary myDictionary = new ConcreteDictionary(1);
\r
612 Assert(!myDictionary.BaseDictionary.IsFixedSize);
\r
616 public void DictionaryProperty()
\r
618 ConcreteDictionary myDictionary = new ConcreteDictionary(1);
\r
619 AssertEquals(myDictionary, myDictionary.BaseDictionary);
\r