1 // HashtableTest.cs - NUnit Test Cases for the System.Collections.Hashtable class
\r
4 // (C) Ximian, Inc. http://www.ximian.com
\r
9 using System.Collections;
\r
12 using System.Runtime.Serialization;
\r
13 using System.Runtime.Serialization.Formatters;
\r
14 using System.Runtime.Serialization.Formatters.Binary;
\r
16 using NUnit.Framework;
\r
20 namespace MonoTests.System.Collections {
\r
23 /// <summary>Hashtable test.</summary>
\r
25 public class HashtableTest : Assertion {
\r
28 public void TestCtor1() {
\r
29 Hashtable h = new Hashtable();
\r
30 AssertNotNull("No hash table", h);
\r
34 public void TestCtor2() {
\r
36 bool errorThrown = false;
\r
38 Hashtable h = new Hashtable((IDictionary) null);
\r
39 } catch (ArgumentNullException) {
\r
42 Assert("null hashtable error not thrown",
\r
46 string[] keys = {"this", "is", "a", "test"};
\r
47 char[] values = {'a', 'b', 'c', 'd'};
\r
48 Hashtable h1 = new Hashtable();
\r
49 for (int i = 0; i < keys.Length; i++) {
\r
50 h1[keys[i]] = values[i];
\r
52 Hashtable h2 = new Hashtable(h1);
\r
53 for (int i = 0; i < keys.Length; i++) {
\r
54 AssertEquals("No match for key " + keys[i],
\r
55 values[i], h2[keys[i]]);
\r
61 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
62 public void TestCtor3 ()
\r
64 Hashtable h = new Hashtable ();
\r
65 Hashtable hh = new Hashtable (h, Single.NaN);
\r
69 [ExpectedException (typeof (ArgumentException))]
\r
70 public void TestCtor4 ()
\r
72 Hashtable ht = new Hashtable (Int32.MaxValue, 0.1f, null, null);
\r
76 public void TestCtor5 ()
\r
78 // tests if negative capacity throws exception
\r
80 Hashtable ht = new Hashtable (-10, 0.1f, null, null);
\r
81 Assert("must throw ArgumentOutOfRange exception, param: capacity", false);
\r
82 } catch (ArgumentOutOfRangeException e) {
\r
83 Assert("ParamName is not capacity", e.ParamName == "capacity");
\r
86 // tests if loadFactor out of range throws exception (low)
\r
88 Hashtable ht = new Hashtable (100, 0.01f, null, null);
\r
89 Assert("must throw ArgumentOutOfRange exception, param: loadFactor, too low value", false);
\r
90 } catch (ArgumentOutOfRangeException e)
\r
92 Assert("ParamName is not loadFactor",e.ParamName == "loadFactor");
\r
95 // tests if loadFactor out of range throws exception (high)
\r
98 Hashtable ht = new Hashtable (100, 2f, null, null);
\r
99 Assert("must throw ArgumentOutOfRange exception, param: loadFactor, too high value", false);
\r
101 catch (ArgumentOutOfRangeException e)
\r
103 Assert("ParamName is not loadFactor", e.ParamName == "loadFactor");
\r
108 // TODO - Ctors for capacity and load (how to test? any access?)
\r
109 // TODO - Ctors with IComparer, IHashCodeProvider, Serialization
\r
112 public void TestCount() {
\r
113 Hashtable h = new Hashtable();
\r
114 AssertEquals("new table - count zero", 0, h.Count);
\r
116 for (int i = 1; i <= max; i++) {
\r
118 AssertEquals("Count wrong for " + i,
\r
121 for (int i = 1; i <= max; i++) {
\r
123 AssertEquals("Count shouldn't change at " + i,
\r
129 public void TestIsFixedSize() {
\r
130 Hashtable h = new Hashtable();
\r
131 AssertEquals("hashtable not fixed by default",
\r
132 false, h.IsFixedSize);
\r
133 // TODO - any way to get a fixed-size hashtable?
\r
136 public void TestIsReadOnly() {
\r
137 Hashtable h = new Hashtable();
\r
138 AssertEquals("hashtable not read-only by default",
\r
139 false, h.IsReadOnly);
\r
140 // TODO - any way to get a read-only hashtable?
\r
144 public void TestIsSynchronized ()
\r
146 Hashtable h = new Hashtable ();
\r
147 Assert ("hashtable not synched by default", !h.IsSynchronized);
\r
149 Hashtable h2 = Hashtable.Synchronized (h);
\r
150 Assert ("hashtable should by synched", h2.IsSynchronized);
\r
152 Hashtable h3 = (Hashtable) h2.Clone ();
\r
153 Assert ("Cloned Hashtable should by synched", h3.IsSynchronized);
\r
157 public void TestItem() {
\r
159 bool errorThrown = false;
\r
161 Hashtable h = new Hashtable();
\r
162 Object o = h[null];
\r
163 } catch (ArgumentNullException e) {
\r
164 errorThrown = true;
\r
165 AssertEquals("ParamName is not \"key\"", "key", e.ParamName);
\r
167 Assert("null hashtable error not thrown",
\r
170 // TODO - if read-only and/or fixed-size is possible,
\r
171 // test 'NotSupportedException' here
\r
174 Hashtable h = new Hashtable();
\r
176 for (int i = 1; i <= max; i++) {
\r
178 AssertEquals("value wrong for " + i,
\r
185 public void TestKeys() {
\r
186 string[] keys = {"this", "is", "a", "test"};
\r
187 string[] keys2 = {"new", "keys"};
\r
188 char[] values1 = {'a', 'b', 'c', 'd'};
\r
189 char[] values2 = {'e', 'f', 'g', 'h'};
\r
190 ICollection keysReference, keysReference2;
\r
191 Hashtable h1 = new Hashtable();
\r
192 for (int i = 0; i < keys.Length; i++) {
\r
193 h1[keys[i]] = values1[i];
\r
195 AssertEquals("keys wrong size",
\r
196 keys.Length, h1.Keys.Count);
\r
197 for (int i = 0; i < keys.Length; i++) {
\r
198 h1[keys[i]] = values2[i];
\r
200 AssertEquals("keys wrong size 2",
\r
201 keys.Length, h1.Keys.Count);
\r
203 // MS .NET Always returns the same reference when calling Keys property
\r
204 keysReference = h1.Keys;
\r
205 keysReference2 = h1.Keys;
\r
206 AssertEquals("keys references differ", keysReference, keysReference2);
\r
208 for (int i = 0; i < keys2.Length; i++)
\r
210 h1[keys2[i]] = values2[i];
\r
212 AssertEquals("keys wrong size 3",
\r
213 keys.Length+keys2.Length, h1.Keys.Count);
\r
214 AssertEquals("keys wrong size 4",
\r
215 keys.Length+keys2.Length, keysReference.Count);
\r
220 public void TestValues() {
\r
221 string[] keys = {"this", "is", "a", "test"};
\r
222 char[] values1 = {'a', 'b', 'c', 'd'};
\r
223 char[] values2 = {'e', 'f', 'g', 'h'};
\r
224 Hashtable h1 = new Hashtable();
\r
225 for (int i = 0; i < keys.Length; i++) {
\r
226 h1[keys[i]] = values1[i];
\r
228 AssertEquals("values wrong size",
\r
229 keys.Length, h1.Values.Count);
\r
230 for (int i = 0; i < keys.Length; i++) {
\r
231 h1[keys[i]] = values2[i];
\r
233 AssertEquals("values wrong size 2",
\r
234 keys.Length, h1.Values.Count);
\r
236 // MS .NET Always returns the same reference when calling Values property
\r
237 ICollection valuesReference1 = h1.Values;
\r
238 ICollection valuesReference2 = h1.Values;
\r
239 AssertEquals("values references differ", valuesReference1, valuesReference2);
\r
243 public void TestAdd() {
\r
245 bool errorThrown = false;
\r
247 Hashtable h = new Hashtable();
\r
248 h.Add(null, "huh?");
\r
249 } catch (ArgumentNullException e) {
\r
250 errorThrown = true;
\r
251 AssertEquals("ParamName is not 'key'", "key", e.ParamName);
\r
253 Assert("null add error not thrown",
\r
257 bool errorThrown = false;
\r
259 Hashtable h = new Hashtable();
\r
262 } catch (ArgumentException) {
\r
263 errorThrown = true;
\r
265 Assert("re-add error not thrown",
\r
268 // TODO - hit NotSupportedException
\r
270 Hashtable h = new Hashtable();
\r
272 for (int i = 1; i <= max; i++) {
\r
274 AssertEquals("value wrong for " + i,
\r
281 public void TestClear() {
\r
282 // TODO - hit NotSupportedException
\r
283 Hashtable h = new Hashtable();
\r
284 AssertEquals("new table - count zero", 0, h.Count);
\r
286 for (int i = 1; i <= max; i++) {
\r
289 Assert("table don't gots stuff", h.Count > 0);
\r
291 AssertEquals("Table should be cleared",
\r
296 public void TestClone() {
\r
298 char[] c1 = {'a', 'b', 'c'};
\r
299 char[] c2 = {'d', 'e', 'f'};
\r
300 Hashtable h1 = new Hashtable();
\r
301 for (int i = 0; i < c1.Length; i++) {
\r
304 Hashtable h2 = (Hashtable)h1.Clone();
\r
305 AssertNotNull("got no clone!", h2);
\r
306 AssertNotNull("clone's got nothing!", h2[c1[0]]);
\r
307 for (int i = 0; i < c1.Length; i++) {
\r
308 AssertEquals("Hashtable match",
\r
309 h1[c1[i]], h2[c1[i]]);
\r
313 char[] c1 = {'a', 'b', 'c'};
\r
314 char[] c20 = {'1', '2'};
\r
315 char[] c21 = {'3', '4'};
\r
316 char[] c22 = {'5', '6'};
\r
317 char[][] c2 = {c20, c21, c22};
\r
318 Hashtable h1 = new Hashtable();
\r
319 for (int i = 0; i < c1.Length; i++) {
\r
322 Hashtable h2 = (Hashtable)h1.Clone();
\r
323 AssertNotNull("got no clone!", h2);
\r
324 AssertNotNull("clone's got nothing!", h2[c1[0]]);
\r
325 for (int i = 0; i < c1.Length; i++) {
\r
326 AssertEquals("Hashtable match",
\r
327 h1[c1[i]], h2[c1[i]]);
\r
330 ((char[])h1[c1[0]])[0] = 'z';
\r
331 AssertEquals("shallow copy", h1[c1[0]], h2[c1[0]]);
\r
336 public void TestContains() {
\r
338 bool errorThrown = false;
\r
340 Hashtable h = new Hashtable();
\r
341 bool result = h.Contains(null);
\r
342 } catch (ArgumentNullException e) {
\r
343 errorThrown = true;
\r
344 AssertEquals("ParamName is not 'key'", "key", e.ParamName);
\r
346 Assert("null add error not thrown",
\r
350 Hashtable h = new Hashtable();
\r
351 for (int i = 0; i < 10000; i += 2)
\r
355 for (int i = 0; i < 10000; i += 2)
\r
357 Assert("hashtable must contain"+i.ToString(), h.Contains(i));
\r
358 Assert("hashtable does not contain "+((int)(i+1)).ToString(), !h.Contains(i+1));
\r
364 public void TestContainsKey() {
\r
366 bool errorThrown = false;
\r
369 Hashtable h = new Hashtable();
\r
370 bool result = h.Contains(null);
\r
372 catch (ArgumentNullException e)
\r
374 errorThrown = true;
\r
375 AssertEquals("ParamName is not 'key'", "key", e.ParamName);
\r
377 Assert("null add error not thrown",
\r
381 Hashtable h = new Hashtable();
\r
382 for (int i = 0; i < 1000; i += 2)
\r
386 for (int i = 0; i < 1000; i += 2)
\r
388 Assert("hashtable must contain"+i.ToString(), h.Contains(i));
\r
389 Assert("hashtable does not contain "+((int)(i+1)).ToString(), !h.Contains(i+1));
\r
396 public void TestContainsValue() {
\r
398 Hashtable h = new Hashtable();
\r
400 Assert("blue? it's in there!",
\r
401 h.ContainsValue("blue"));
\r
402 Assert("green? no way!",
\r
403 !h.ContainsValue("green"));
\r
404 Assert("null? no way!",
\r
405 !h.ContainsValue(null));
\r
407 Assert("null? it's in there!",
\r
408 h.ContainsValue(null));
\r
414 public void TestCopyTo() {
\r
416 bool errorThrown = false;
\r
418 Hashtable h = new Hashtable();
\r
420 } catch (ArgumentNullException e) {
\r
421 errorThrown = true;
\r
422 AssertEquals("ParamName is not \"array\"", "array", e.ParamName);
\r
424 Assert("null hashtable error not thrown",
\r
428 bool errorThrown = false;
\r
430 Hashtable h = new Hashtable();
\r
431 Object[] o = new Object[1];
\r
433 } catch (ArgumentOutOfRangeException e) {
\r
434 errorThrown = true;
\r
435 AssertEquals("ParamName is not \"arrayIndex\"", "arrayIndex", e.ParamName);
\r
437 Assert("out of range error not thrown",
\r
441 bool errorThrown = false;
\r
443 Hashtable h = new Hashtable();
\r
444 Object[,] o = new Object[1,1];
\r
446 } catch (ArgumentException) {
\r
447 errorThrown = true;
\r
449 Assert("multi-dim array error not thrown",
\r
453 bool errorThrown = false;
\r
455 Hashtable h = new Hashtable();
\r
456 h['a'] = 1; // no error if table is empty
\r
457 Object[] o = new Object[5];
\r
459 } catch (ArgumentException) {
\r
460 errorThrown = true;
\r
462 Assert("no room in array error not thrown",
\r
466 bool errorThrown = false;
\r
468 Hashtable h = new Hashtable();
\r
472 Object[] o = new Object[2];
\r
474 } catch (ArgumentException) {
\r
475 errorThrown = true;
\r
477 Assert("table too big error not thrown",
\r
481 bool errorThrown = false;
\r
483 Hashtable h = new Hashtable();
\r
487 Char[] o = new Char[3];
\r
489 } catch (InvalidCastException) {
\r
490 errorThrown = true;
\r
492 Assert("invalid cast error not thrown",
\r
497 Hashtable h = new Hashtable();
\r
500 DictionaryEntry[] o = new DictionaryEntry[2];
\r
502 #if TARGET_JVM // Hashtable is not an ordered collection!
\r
503 if (o[0].Key.Equals('b')) {
\r
504 DictionaryEntry v = o[0];
\r
508 #endif // TARGET_JVM
\r
509 AssertEquals("first copy fine.", 'a', o[0].Key);
\r
510 AssertEquals("first copy fine.", 1, o[0].Value);
\r
511 AssertEquals("second copy fine.", 'b', o[1].Key);
\r
512 AssertEquals("second copy fine.", 2, o[1].Value);
\r
517 public void TestGetEnumerator() {
\r
518 String[] s1 = {"this", "is", "a", "test"};
\r
519 Char[] c1 = {'a', 'b', 'c', 'd'};
\r
520 Hashtable h1 = new Hashtable();
\r
521 for (int i = 0; i < s1.Length; i++) {
\r
524 IDictionaryEnumerator en = h1.GetEnumerator();
\r
525 AssertNotNull("No enumerator", en);
\r
527 for (int i = 0; i < s1.Length; i++) {
\r
529 Assert("Not enumerating for " + en.Key,
\r
530 Array.IndexOf(s1, en.Key) >= 0);
\r
531 Assert("Not enumerating for " + en.Value,
\r
532 Array.IndexOf(c1, en.Value) >= 0);
\r
537 public void TestSerialization () {
\r
538 Hashtable table1 = new Hashtable();
\r
540 Stream str = new MemoryStream ();
\r
541 BinaryFormatter formatter = new BinaryFormatter();
\r
543 for (int i = 0; i < 100; i++)
\r
544 table1[i] = "TestString Key: " + i.ToString();
\r
546 formatter.Serialize (str, table1);
\r
548 table2 = (Hashtable) formatter.Deserialize (str);
\r
551 foreach (DictionaryEntry de in table1)
\r
552 AssertEquals (de.Value, table2 [de.Key]);
\r
556 [Category ("TargetJvmNotWorking")]
\r
557 public void TestSerialization2 () {
\r
558 // Test from bug #70570
\r
559 MemoryStream stream = new MemoryStream();
\r
560 BinaryFormatter formatter = new BinaryFormatter();
\r
562 Hashtable table = new Hashtable();
\r
563 table.Add (new Bug(), "Hello");
\r
565 formatter.Serialize(stream, table);
\r
566 stream.Position = 0;
\r
567 table = (Hashtable) formatter.Deserialize(stream);
\r
568 AssertEquals ("#1", 1, table.Count);
\r
572 public void TestRemove() {
\r
574 bool errorThrown = false;
\r
576 Hashtable h = new Hashtable();
\r
578 } catch (ArgumentNullException e) {
\r
579 errorThrown = true;
\r
580 AssertEquals("ParamName is not \"key\"", "key", e.ParamName);
\r
582 Assert("null hashtable error not thrown",
\r
586 string[] keys = {"this", "is", "a", "test"};
\r
587 char[] values = {'a', 'b', 'c', 'd'};
\r
588 Hashtable h = new Hashtable();
\r
589 for (int i = 0; i < keys.Length; i++) {
\r
590 h[keys[i]] = values[i];
\r
592 AssertEquals("not enough in table",
\r
595 AssertEquals("not enough in table",
\r
598 AssertEquals("Wrong count in table",
\r
601 AssertEquals("Wrong count in table",
\r
607 public void TestSynchronized() {
\r
609 bool errorThrown = false;
\r
611 Hashtable h = Hashtable.Synchronized(null);
\r
612 } catch (ArgumentNullException e) {
\r
613 errorThrown = true;
\r
614 AssertEquals("ParamName is not \"table\"", "table", e.ParamName);
\r
616 Assert("null hashtable error not thrown",
\r
620 Hashtable h = new Hashtable();
\r
621 Assert("hashtable not synced by default",
\r
622 !h.IsSynchronized);
\r
623 Hashtable h2 = Hashtable.Synchronized(h);
\r
624 Assert("hashtable should by synced",
\r
625 h2.IsSynchronized);
\r
630 protected Hashtable ht;
\r
631 private static Random rnd;
\r
634 public void SetUp() {
\r
635 ht=new Hashtable();
\r
639 private void SetDefaultData() {
\r
641 ht.Add("k1","another");
\r
642 ht.Add("k2","yet");
\r
643 ht.Add("k3","hashtable");
\r
647 public void TestAddRemoveClear() {
\r
649 Assert(ht.Count==0);
\r
652 Assert(ht.Count==3);
\r
656 ht.Add("k2","cool");
\r
657 } catch (ArgumentException) {thrown=true;}
\r
658 Assert("Must throw ArgumentException!",thrown);
\r
661 Assert(ht.Count==3);
\r
662 Assert(ht["k2"].Equals("cool"));
\r
667 public void TestCopyTo2() {
\r
669 Object[] entries=new Object[ht.Count];
\r
670 ht.CopyTo(entries,0);
\r
671 Assert("Not an entry.",entries[0] is DictionaryEntry);
\r
675 public void CopyTo_Empty ()
\r
677 Hashtable ht = new Hashtable ();
\r
678 AssertEquals ("Count", 0, ht.Count);
\r
679 object[] array = new object [ht.Count];
\r
680 ht.CopyTo (array, 0);
\r
684 public void TestUnderHeavyLoad() {
\r
687 String[] cache=new String[max*2];
\r
690 for (int i=0;i<max;i++) {
\r
691 int id=rnd.Next()&0xFFFF;
\r
692 String key=""+id+"-key-"+id;
\r
693 String val="value-"+id;
\r
694 if (ht[key]==null) {
\r
702 Assert(ht.Count==n);
\r
704 for (int i=0;i<n;i++) {
\r
705 String key=cache[i];
\r
706 String val=ht[key] as String;
\r
707 String err="ht[\""+key+"\"]=\""+val+
\r
708 "\", expected \""+cache[i+max]+"\"";
\r
709 Assert(err,val!=null && val.Equals(cache[i+max]));
\r
715 for (int i=r1;i<r2;i++) {
\r
716 ht.Remove(cache[i]);
\r
720 for (int i=0;i<n;i++) {
\r
721 if (i>=r1 && i<r2) {
\r
722 Assert(ht[cache[i]]==null);
\r
724 String key=cache[i];
\r
725 String val=ht[key] as String;
\r
726 String err="ht[\""+key+"\"]=\""+val+
\r
727 "\", expected \""+cache[i+max]+"\"";
\r
728 Assert(err,val!=null && val.Equals(cache[i+max]));
\r
732 ICollection keys=ht.Keys;
\r
734 foreach (Object key in keys) {
\r
735 Assert((key as String) != null);
\r
738 Assert(nKeys==ht.Count);
\r
741 ICollection vals=ht.Values;
\r
743 foreach (Object val in vals) {
\r
744 Assert((val as String) != null);
\r
747 Assert(nVals==ht.Count);
\r
753 /// Test hashtable with CaseInsensitiveHashCodeProvider
\r
754 /// and CaseInsensitive comparer.
\r
757 public void TestCaseInsensitive ()
\r
759 // Not very meaningfull test, just to make
\r
760 // sure that hcp is set properly set.
\r
761 Hashtable ciHashtable = new Hashtable(11,1.0f,CaseInsensitiveHashCodeProvider.Default,CaseInsensitiveComparer.Default);
\r
762 ciHashtable ["key1"] = "value";
\r
763 ciHashtable ["key2"] = "VALUE";
\r
764 Assert(ciHashtable ["key1"].Equals ("value"));
\r
765 Assert(ciHashtable ["key2"].Equals ("VALUE"));
\r
767 ciHashtable ["KEY1"] = "new_value";
\r
768 Assert(ciHashtable ["key1"].Equals ("new_value"));
\r
773 public void TestCopyConstructor ()
\r
777 Hashtable htCopy = new Hashtable (ht);
\r
779 Assert(ht.Count == htCopy.Count);
\r
783 public void TestEnumerator ()
\r
787 IEnumerator e = ht.GetEnumerator ();
\r
789 while (e.MoveNext ()) {}
\r
791 Assert (!e.MoveNext ());
\r
796 [ExpectedException (typeof (ArgumentNullException))]
\r
797 public void GetObjectData_NullSerializationInfo ()
\r
800 ht.GetObjectData (null, new StreamingContext ());
\r
805 [Category ("NotDotNet")] // .NET raises InvalidOperationException.
\r
806 public void SyncHashtable_ICollectionsGetEnumerator ()
\r
808 Hashtable hashtable = Hashtable.Synchronized (new Hashtable ());
\r
809 hashtable["a"] = 1;
\r
810 //IEnumerator e = (hashtable.Clone() as
\r
811 IEnumerator e = (hashtable as ICollection).GetEnumerator ();
\r
814 DictionaryEntry de = (DictionaryEntry) e.Current;
\r
818 public void SerializableSubClasses ()
\r
820 Hashtable ht = new Hashtable ();
\r
822 Assert ("Keys.IsSerializable", ht.Keys.GetType ().IsSerializable);
\r
823 Assert ("Values.IsSerializable", ht.Values.GetType ().IsSerializable);
\r
824 Assert ("GetEnumerator.IsSerializable", ht.GetEnumerator ().GetType ().IsSerializable);
\r
825 Assert ("Synchronized.IsSerializable", Hashtable.Synchronized (ht).GetType ().IsSerializable);
\r
830 public class Bug :ISerializable {
\r
833 private sealed class InnerClassSerializationHelper : IObjectReference {
\r
834 public object GetRealObject( StreamingContext context )
\r
840 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context )
\r
842 info.SetType( typeof(InnerClassSerializationHelper) );
\r