2 // System.Collections.DictionaryBase
3 // Test suite for System.Collections.DictionaryBase
6 // Carlos Alberto Barcenilla (barce@frlp.utn.edu.ar)
10 using System
.Collections
;
11 using NUnit
.Framework
;
13 namespace MonoTests
.System
.Collections
17 public class DictionaryBaseTest
: Assertion
19 static void Main(string[] args
)
23 public class ConcreteDictionary
: DictionaryBase
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
;
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
;
47 public ConcreteDictionary()
51 public ConcreteDictionary(int i
)
53 for (int j
= 0; j
< i
; j
++)
55 ((IDictionary
) this).Add(j
, j
*2);
61 public IDictionary BaseDictionary
63 get { return this.Dictionary; }
66 public void ClearFlags()
68 onInsertFired
= false;
69 onInsertCompleteFired
= false;
70 onValidateFired
= false;
71 onRemoveFired
= false;
72 onRemoveCompleteFired
= false;
74 onClearCompleteFired
= false;
76 onSetCompleteFired
= false;
80 protected override void OnValidate(object key
, object value)
82 onValidateFired
= true;
83 if (onValidateMustThrowException
)
84 throw new Exception();
85 base.OnValidate (key
, value);
88 protected override void OnInsert(object key
, object value)
91 if (onInsertMustThrowException
)
92 throw new Exception();
93 base.OnInsert (key
, value);
96 protected override void OnInsertComplete(object key
, object value)
98 onInsertCompleteFired
= true;
99 if (onInsertCompleteMustThrowException
)
100 throw new Exception();
101 base.OnInsertComplete (key
, value);
104 protected override void OnRemove(object key
, object value)
106 onRemoveFired
= true;
107 if (onRemoveMustThrowException
)
108 throw new Exception();
109 base.OnRemove (key
, value);
112 protected override void OnRemoveComplete(object key
, object value)
114 onRemoveCompleteFired
= true;
115 if (onRemoveCompleteMustThrowException
)
116 throw new Exception();
117 base.OnRemoveComplete (key
, value);
121 protected override void OnClear()
124 if (onClearMustThrowException
)
125 throw new Exception();
129 protected override void OnClearComplete()
131 onClearCompleteFired
= true;
132 if (onClearCompleteMustThrowException
)
133 throw new Exception();
134 base.OnClearComplete ();
137 protected override object OnGet(object key
, object currentValue
)
140 if (onGetMustThrowException
)
141 throw new Exception();
142 return base.OnGet (key
, currentValue
);
145 protected override void OnSet(object key
, object oldValue
, object newValue
)
148 if (onSetMustThrowException
)
149 throw new Exception();
150 base.OnSet (key
, oldValue
, newValue
);
153 protected override void OnSetComplete(object key
, object oldValue
, object newValue
)
155 onSetCompleteFired
= true;
156 if (onSetCompleteMustThrowException
)
157 throw new Exception();
158 base.OnSetComplete (key
, oldValue
, newValue
);
165 ConcreteDictionary myDictionary
= new ConcreteDictionary(10);
166 myDictionary
.BaseDictionary
.Add(100, 1);
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]);
176 public void AddOnValidateExcept()
178 bool exceptionThrown
= false;
180 ConcreteDictionary myDictionary
= new ConcreteDictionary(30);
181 myDictionary
.onValidateMustThrowException
= true;
184 myDictionary
.BaseDictionary
.Add(111,222);
186 exceptionThrown
= true;
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
);
198 public void AddOnInsertExcept()
200 bool exceptionThrown
= false;
202 ConcreteDictionary myDictionary
= new ConcreteDictionary(30);
203 myDictionary
.onInsertMustThrowException
= true;
206 myDictionary
.BaseDictionary
.Add(666,222);
208 exceptionThrown
= true;
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
);
220 public void AddOnInsertCompleteExcept()
222 bool exceptionThrown
= false;
224 ConcreteDictionary myDictionary
= new ConcreteDictionary(5);
225 myDictionary
.onInsertCompleteMustThrowException
= true;
228 myDictionary
.BaseDictionary
.Add(888,999);
230 exceptionThrown
= true;
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
);
242 public void AddNullKey()
244 bool exceptionThrown
= false;
246 ConcreteDictionary myDictionary
= new ConcreteDictionary();
248 myDictionary
.BaseDictionary
.Add(null, 11);
249 } catch (ArgumentNullException
) {
250 exceptionThrown
= true;
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
);
262 ConcreteDictionary myDictionary
= new ConcreteDictionary(30);
263 myDictionary
.Clear();
265 Assert("OnClear must be fired", myDictionary
.onClearFired
);
266 Assert("OnClearComplete must be fired", myDictionary
.onClearCompleteFired
);
267 AssertEquals("Count", 0, myDictionary
.Count
);
271 public void ClearOnClearExcept()
273 bool exceptionThrown
= false;
275 ConcreteDictionary myDictionary
= new ConcreteDictionary(30);
276 myDictionary
.onClearMustThrowException
= true;
279 myDictionary
.Clear();
281 exceptionThrown
= true;
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
);
292 public void ClearOnClearCompleteExcept()
294 bool exceptionThrown
= false;
296 ConcreteDictionary myDictionary
= new ConcreteDictionary(30);
297 myDictionary
.onClearCompleteMustThrowException
= true;
300 myDictionary
.Clear();
302 exceptionThrown
= true;
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
);
315 ConcreteDictionary myDictionary
= new ConcreteDictionary(19);
316 AssertEquals(19, myDictionary
.Count
);
322 ConcreteDictionary myDictionary
= new ConcreteDictionary(8);
323 myDictionary
.BaseDictionary
.Remove(5);
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]);
333 public void RemoveOnValidateExcept()
335 bool exceptionThrown
= false;
337 ConcreteDictionary myDictionary
= new ConcreteDictionary(28);
338 myDictionary
.onValidateMustThrowException
= true;
341 myDictionary
.BaseDictionary
.Remove(11);
343 exceptionThrown
= true;
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]);
356 public void RemoveOnRemoveExcept()
358 bool exceptionThrown
= false;
360 ConcreteDictionary myDictionary
= new ConcreteDictionary(28);
361 myDictionary
.onRemoveMustThrowException
= true;
364 myDictionary
.BaseDictionary
.Remove(11);
366 exceptionThrown
= true;
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]);
379 public void RemoveOnRemoveCompleteExcept()
381 bool exceptionThrown
= false;
383 ConcreteDictionary myDictionary
= new ConcreteDictionary(28);
384 myDictionary
.onRemoveCompleteMustThrowException
= true;
388 myDictionary
.BaseDictionary
.Remove(11);
392 exceptionThrown
= true;
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]);
407 public void RemoveKeyNotInDictionary()
410 ConcreteDictionary myDictionary
= new ConcreteDictionary(28);
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
);
421 ConcreteDictionary myDictionary
= new ConcreteDictionary(19);
422 for (int i
= 0; i
< 19; i
++)
424 AssertEquals(i
*2, (int) myDictionary
.BaseDictionary
[i
]);
429 public void Contains()
431 ConcreteDictionary myDictionary
= new ConcreteDictionary(14);
432 for (int i
= 0; i
< 14; i
++)
434 Assert("Must contain " + i
, myDictionary
.BaseDictionary
.Contains(i
));
436 for (int i
= 14; i
< 34; i
++)
438 Assert("Must not contain " + i
, !myDictionary
.BaseDictionary
.Contains(i
));
443 public void GetEnumerator()
445 ConcreteDictionary myDictionary
= new ConcreteDictionary(4);
447 AssertNotNull(myDictionary
.GetEnumerator());
453 ConcreteDictionary myDictionary
= new ConcreteDictionary(5);
454 ICollection keys
= myDictionary
.BaseDictionary
.Keys
;
457 foreach (int i
in keys
)
459 AssertEquals(10, total
);
460 AssertEquals(5, keys
.Count
);
466 ConcreteDictionary myDictionary
= new ConcreteDictionary(5);
467 ICollection values
= myDictionary
.BaseDictionary
.Values
;
470 foreach (int i
in values
)
472 AssertEquals(20, total
);
473 AssertEquals(5, values
.Count
);
479 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
480 int v
= (int) myDictionary
.BaseDictionary
[10];
482 Assert("OnGet must be fired", myDictionary
.onGetFired
);
487 public void GetOnGetExcept()
489 bool exceptionThrown
= false;
491 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
492 myDictionary
.onGetMustThrowException
= true;
495 int v
= (int) myDictionary
.BaseDictionary
[10];
497 exceptionThrown
= true;
499 Assert("Exception must be thrown", exceptionThrown
);
500 Assert("OnGet must be fired", myDictionary
.onGetFired
);
505 public void GetNoKey()
507 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
508 AssertNull(myDictionary
.BaseDictionary
[100]);
514 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
515 myDictionary
.BaseDictionary
[10] = 50;
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]);
524 public void SetNewKey()
526 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
527 myDictionary
.BaseDictionary
[111] = 222;
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
);
539 public void SetOnValidateExcept()
541 bool exceptionThrown
= false;
543 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
544 myDictionary
.onValidateMustThrowException
= true;
547 myDictionary
.BaseDictionary
[10] = 50;
549 exceptionThrown
= true;
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]);
560 public void SetOnSetExcept()
562 bool exceptionThrown
= false;
564 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
565 myDictionary
.onSetMustThrowException
= true;
568 myDictionary
.BaseDictionary
[10] = 50;
570 exceptionThrown
= true;
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]);
581 public void SetOnSetCompleteExcept()
583 bool exceptionThrown
= false;
585 ConcreteDictionary myDictionary
= new ConcreteDictionary(18);
586 myDictionary
.onSetCompleteMustThrowException
= true;
589 myDictionary
.BaseDictionary
[10] = 50;
591 exceptionThrown
= true;
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]);
602 public void IsReadOnly()
604 ConcreteDictionary myDictionary
= new ConcreteDictionary(1);
605 Assert(!myDictionary
.BaseDictionary
.IsReadOnly
);
609 public void IsFixedSize()
611 ConcreteDictionary myDictionary
= new ConcreteDictionary(1);
612 Assert(!myDictionary
.BaseDictionary
.IsFixedSize
);
616 public void DictionaryProperty()
618 ConcreteDictionary myDictionary
= new ConcreteDictionary(1);
619 AssertEquals(myDictionary
, myDictionary
.BaseDictionary
);