**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Collections / DictionaryBaseTest.cs
blobad7b637db6e5596989c27e3f0b2f8affa4e1c8c0
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 //
9 using System;
10 using System.Collections;
11 using NUnit.Framework;
13 namespace MonoTests.System.Collections
16 [TestFixture]
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);
58 ClearFlags();
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;
73 onClearFired = false;
74 onClearCompleteFired = false;
75 onSetFired = false;
76 onSetCompleteFired = false;
77 onGetFired = 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)
90 onInsertFired = true;
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()
123 onClearFired = true;
124 if (onClearMustThrowException)
125 throw new Exception();
126 base.OnClear ();
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)
139 onGetFired = true;
140 if (onGetMustThrowException)
141 throw new Exception();
142 return base.OnGet (key, currentValue);
145 protected override void OnSet(object key, object oldValue, object newValue)
147 onSetFired = true;
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);
162 [Test]
163 public void Add()
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]);
175 [Test]
176 public void AddOnValidateExcept()
178 bool exceptionThrown = false;
180 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
181 myDictionary.onValidateMustThrowException = true;
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);
197 [Test]
198 public void AddOnInsertExcept()
200 bool exceptionThrown = false;
202 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
203 myDictionary.onInsertMustThrowException = true;
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);
219 [Test]
220 public void AddOnInsertCompleteExcept()
222 bool exceptionThrown = false;
224 ConcreteDictionary myDictionary = new ConcreteDictionary(5);
225 myDictionary.onInsertCompleteMustThrowException = true;
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);
241 [Test]
242 public void AddNullKey()
244 bool exceptionThrown = false;
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);
259 [Test]
260 public void Clear()
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);
270 [Test]
271 public void ClearOnClearExcept()
273 bool exceptionThrown = false;
275 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
276 myDictionary.onClearMustThrowException = true;
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);
291 [Test]
292 public void ClearOnClearCompleteExcept()
294 bool exceptionThrown = false;
296 ConcreteDictionary myDictionary = new ConcreteDictionary(30);
297 myDictionary.onClearCompleteMustThrowException = true;
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);
312 [Test]
313 public void Count()
315 ConcreteDictionary myDictionary = new ConcreteDictionary(19);
316 AssertEquals(19, myDictionary.Count);
319 [Test]
320 public void Remove()
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]);
332 [Test]
333 public void RemoveOnValidateExcept()
335 bool exceptionThrown = false;
337 ConcreteDictionary myDictionary = new ConcreteDictionary(28);
338 myDictionary.onValidateMustThrowException = true;
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]);
355 [Test]
356 public void RemoveOnRemoveExcept()
358 bool exceptionThrown = false;
360 ConcreteDictionary myDictionary = new ConcreteDictionary(28);
361 myDictionary.onRemoveMustThrowException = true;
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]);
378 [Test]
379 public void RemoveOnRemoveCompleteExcept()
381 bool exceptionThrown = false;
383 ConcreteDictionary myDictionary = new ConcreteDictionary(28);
384 myDictionary.onRemoveCompleteMustThrowException = true;
386 try
388 myDictionary.BaseDictionary.Remove(11);
390 catch
392 exceptionThrown = true;
394 finally
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]);
406 [Test]
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);
418 [Test]
419 public void Items()
421 ConcreteDictionary myDictionary = new ConcreteDictionary(19);
422 for (int i = 0; i < 19; i++)
424 AssertEquals(i*2, (int) myDictionary.BaseDictionary[i]);
428 [Test]
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));
442 [Test]
443 public void GetEnumerator()
445 ConcreteDictionary myDictionary = new ConcreteDictionary(4);
447 AssertNotNull(myDictionary.GetEnumerator());
450 [Test]
451 public void Keys()
453 ConcreteDictionary myDictionary = new ConcreteDictionary(5);
454 ICollection keys = myDictionary.BaseDictionary.Keys;
456 int total = 0;
457 foreach (int i in keys)
458 total += i;
459 AssertEquals(10, total);
460 AssertEquals(5, keys.Count);
463 [Test]
464 public void Values()
466 ConcreteDictionary myDictionary = new ConcreteDictionary(5);
467 ICollection values = myDictionary.BaseDictionary.Values;
469 int total = 0;
470 foreach (int i in values)
471 total += i;
472 AssertEquals(20, total);
473 AssertEquals(5, values.Count);
476 [Test]
477 public void Get()
479 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
480 int v = (int) myDictionary.BaseDictionary[10];
482 Assert("OnGet must be fired", myDictionary.onGetFired);
483 AssertEquals(v, 20);
486 [Test]
487 public void GetOnGetExcept()
489 bool exceptionThrown = false;
491 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
492 myDictionary.onGetMustThrowException = true;
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);
504 [Test]
505 public void GetNoKey()
507 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
508 AssertNull(myDictionary.BaseDictionary[100]);
511 [Test]
512 public void Set()
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]);
523 [Test]
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);
538 [Test]
539 public void SetOnValidateExcept()
541 bool exceptionThrown = false;
543 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
544 myDictionary.onValidateMustThrowException = true;
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]);
559 [Test]
560 public void SetOnSetExcept()
562 bool exceptionThrown = false;
564 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
565 myDictionary.onSetMustThrowException = true;
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]);
580 [Test]
581 public void SetOnSetCompleteExcept()
583 bool exceptionThrown = false;
585 ConcreteDictionary myDictionary = new ConcreteDictionary(18);
586 myDictionary.onSetCompleteMustThrowException = true;
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]);
601 [Test]
602 public void IsReadOnly()
604 ConcreteDictionary myDictionary = new ConcreteDictionary(1);
605 Assert(!myDictionary.BaseDictionary.IsReadOnly);
608 [Test]
609 public void IsFixedSize()
611 ConcreteDictionary myDictionary = new ConcreteDictionary(1);
612 Assert(!myDictionary.BaseDictionary.IsFixedSize);
615 [Test]
616 public void DictionaryProperty()
618 ConcreteDictionary myDictionary = new ConcreteDictionary(1);
619 AssertEquals(myDictionary, myDictionary.BaseDictionary);