-// TODO: add tests for Comparer and HashCodeProvider\r
+// HashtableTest.cs - NUnit Test Cases for the System.Collections.Hashtable class\r
+//\r
+//\r
+// (C) Ximian, Inc. http://www.ximian.com\r
+// \r
\r
\r
using System;\r
using System.Collections;\r
\r
+using System.IO;\r
+using System.Runtime.Serialization;\r
+using System.Runtime.Serialization.Formatters;\r
+using System.Runtime.Serialization.Formatters.Binary;\r
+\r
using NUnit.Framework;\r
\r
\r
\r
-namespace Testsuite.System.Collections {\r
+namespace MonoTests.System.Collections {\r
+\r
\r
+/// <summary>Hashtable test.</summary>\r
+[TestFixture]\r
+public class HashtableTest : Assertion {\r
\r
- /// <summary>Hashtable test.</summary>\r
- public class HashtableTest {\r
- public static ITest Suite {\r
- get {\r
- TestSuite suite= new TestSuite("All Hashtable Tests");\r
- suite.AddTest(BasicOperationsTest.Suite);\r
- return suite;\r
+ [Test]\r
+ public void TestCtor1() {\r
+ Hashtable h = new Hashtable();\r
+ AssertNotNull("No hash table", h);\r
+ }\r
+\r
+ [Test]\r
+ public void TestCtor2() {\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable((IDictionary) null);\r
+ } catch (ArgumentNullException) {\r
+ errorThrown = true;\r
+ }\r
+ Assert("null hashtable error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ string[] keys = {"this", "is", "a", "test"};\r
+ char[] values = {'a', 'b', 'c', 'd'};\r
+ Hashtable h1 = new Hashtable();\r
+ for (int i = 0; i < keys.Length; i++) {\r
+ h1[keys[i]] = values[i];\r
+ }\r
+ Hashtable h2 = new Hashtable(h1);\r
+ for (int i = 0; i < keys.Length; i++) {\r
+ AssertEquals("No match for key " + keys[i],\r
+ values[i], h2[keys[i]]);\r
}\r
}\r
}\r
\r
+ [Test]\r
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
+ public void TestCtor3 ()\r
+ {\r
+ Hashtable h = new Hashtable ();\r
+ Hashtable hh = new Hashtable (h, Single.NaN);\r
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (ArgumentException))]\r
+ public void TestCtor4 ()\r
+ {\r
+ Hashtable ht = new Hashtable (Int32.MaxValue, 0.1f, null, null);\r
+ }\r
+\r
+ [Test]\r
+ public void TestCtor5 ()\r
+ {\r
+ // tests if negative capacity throws exception\r
+ try {\r
+ Hashtable ht = new Hashtable (-10, 0.1f, null, null);\r
+ Assert("must throw ArgumentOutOfRange exception, param: capacity", false);\r
+ } catch (ArgumentOutOfRangeException e) {\r
+ Assert("ParamName is not capacity", e.ParamName == "capacity");\r
+ }\r
\r
+ // tests if loadFactor out of range throws exception (low)\r
+ try {\r
+ Hashtable ht = new Hashtable (100, 0.01f, null, null);\r
+ Assert("must throw ArgumentOutOfRange exception, param: loadFactor, too low value", false);\r
+ } catch (ArgumentOutOfRangeException e) \r
+ {\r
+ Assert("ParamName is not loadFactor",e.ParamName == "loadFactor");\r
+ }\r
\r
+ // tests if loadFactor out of range throws exception (high)\r
+ try \r
+ {\r
+ Hashtable ht = new Hashtable (100, 2f, null, null);\r
+ Assert("must throw ArgumentOutOfRange exception, param: loadFactor, too high value", false);\r
+ } \r
+ catch (ArgumentOutOfRangeException e) \r
+ {\r
+ Assert("ParamName is not loadFactor", e.ParamName == "loadFactor");\r
+ }\r
\r
- public class BasicOperationsTest : TestCase {\r
+ }\r
\r
- protected Hashtable ht;\r
- private static Random rnd;\r
+ // TODO - Ctors for capacity and load (how to test? any access?)\r
+ // TODO - Ctors with IComparer, IHashCodeProvider, Serialization\r
+\r
+ [Test] \r
+ public void TestCount() {\r
+ Hashtable h = new Hashtable();\r
+ AssertEquals("new table - count zero", 0, h.Count);\r
+ int max = 100;\r
+ for (int i = 1; i <= max; i++) {\r
+ h[i] = i;\r
+ AssertEquals("Count wrong for " + i,\r
+ i, h.Count);\r
+ }\r
+ for (int i = 1; i <= max; i++) {\r
+ h[i] = i * 2;\r
+ AssertEquals("Count shouldn't change at " + i,\r
+ max, h.Count);\r
+ }\r
+ }\r
\r
- public BasicOperationsTest(String name) : base(name) {}\r
+ [Test] \r
+ public void TestIsFixedSize() {\r
+ Hashtable h = new Hashtable();\r
+ AssertEquals("hashtable not fixed by default",\r
+ false, h.IsFixedSize);\r
+ // TODO - any way to get a fixed-size hashtable?\r
+ }\r
\r
- protected override void SetUp() {\r
- ht=new Hashtable();\r
- rnd=new Random();\r
+ public void TestIsReadOnly() {\r
+ Hashtable h = new Hashtable();\r
+ AssertEquals("hashtable not read-only by default",\r
+ false, h.IsReadOnly);\r
+ // TODO - any way to get a read-only hashtable?\r
+ }\r
+\r
+ [Test] \r
+ public void TestIsSynchronized ()\r
+ {\r
+ Hashtable h = new Hashtable ();\r
+ Assert ("hashtable not synched by default", !h.IsSynchronized);\r
+\r
+ Hashtable h2 = Hashtable.Synchronized (h);\r
+ Assert ("hashtable should by synched", h2.IsSynchronized);\r
+\r
+ Hashtable h3 = (Hashtable) h2.Clone ();\r
+ Assert ("Cloned Hashtable should by synched", h3.IsSynchronized);\r
+ }\r
+\r
+ [Test]\r
+ public void TestItem() {\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ Object o = h[null];\r
+ } catch (ArgumentNullException e) {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not \"key\"", "key", e.ParamName);\r
+ }\r
+ Assert("null hashtable error not thrown", \r
+ errorThrown);\r
+ }\r
+ // TODO - if read-only and/or fixed-size is possible,\r
+ // test 'NotSupportedException' here\r
+\r
+ {\r
+ Hashtable h = new Hashtable();\r
+ int max = 100;\r
+ for (int i = 1; i <= max; i++) {\r
+ h[i] = i;\r
+ AssertEquals("value wrong for " + i,\r
+ i, h[i]);\r
+ }\r
+ }\r
+ }\r
+\r
+ [Test]\r
+ public void TestKeys() {\r
+ string[] keys = {"this", "is", "a", "test"};\r
+ string[] keys2 = {"new", "keys"};\r
+ char[] values1 = {'a', 'b', 'c', 'd'};\r
+ char[] values2 = {'e', 'f', 'g', 'h'};\r
+ ICollection keysReference, keysReference2;\r
+ Hashtable h1 = new Hashtable();\r
+ for (int i = 0; i < keys.Length; i++) {\r
+ h1[keys[i]] = values1[i];\r
}\r
+ AssertEquals("keys wrong size",\r
+ keys.Length, h1.Keys.Count);\r
+ for (int i = 0; i < keys.Length; i++) {\r
+ h1[keys[i]] = values2[i];\r
+ }\r
+ AssertEquals("keys wrong size 2",\r
+ keys.Length, h1.Keys.Count);\r
+\r
+ // MS .NET Always returns the same reference when calling Keys property\r
+ keysReference = h1.Keys;\r
+ keysReference2 = h1.Keys;\r
+ AssertEquals("keys references differ", keysReference, keysReference2);\r
\r
- public static ITest Suite {\r
- get {\r
- return new TestSuite(typeof(BasicOperationsTest));\r
+ for (int i = 0; i < keys2.Length; i++) \r
+ {\r
+ h1[keys2[i]] = values2[i];\r
+ }\r
+ AssertEquals("keys wrong size 3",\r
+ keys.Length+keys2.Length, h1.Keys.Count);\r
+ AssertEquals("keys wrong size 4",\r
+ keys.Length+keys2.Length, keysReference.Count);\r
+ }\r
+\r
+ // TODO - SyncRoot\r
+ [Test] \r
+ public void TestValues() {\r
+ string[] keys = {"this", "is", "a", "test"};\r
+ char[] values1 = {'a', 'b', 'c', 'd'};\r
+ char[] values2 = {'e', 'f', 'g', 'h'};\r
+ Hashtable h1 = new Hashtable();\r
+ for (int i = 0; i < keys.Length; i++) {\r
+ h1[keys[i]] = values1[i];\r
+ }\r
+ AssertEquals("values wrong size",\r
+ keys.Length, h1.Values.Count);\r
+ for (int i = 0; i < keys.Length; i++) {\r
+ h1[keys[i]] = values2[i];\r
+ }\r
+ AssertEquals("values wrong size 2",\r
+ keys.Length, h1.Values.Count);\r
+\r
+ // MS .NET Always returns the same reference when calling Values property\r
+ ICollection valuesReference1 = h1.Values;\r
+ ICollection valuesReference2 = h1.Values;\r
+ AssertEquals("values references differ", valuesReference1, valuesReference2);\r
+ }\r
+\r
+ [Test]\r
+ public void TestAdd() {\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ h.Add(null, "huh?");\r
+ } catch (ArgumentNullException e) {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not 'key'", "key", e.ParamName);\r
+ }\r
+ Assert("null add error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ h.Add('a', 1);\r
+ h.Add('a', 2);\r
+ } catch (ArgumentException) {\r
+ errorThrown = true;\r
+ }\r
+ Assert("re-add error not thrown", \r
+ errorThrown);\r
+ }\r
+ // TODO - hit NotSupportedException\r
+ {\r
+ Hashtable h = new Hashtable();\r
+ int max = 100;\r
+ for (int i = 1; i <= max; i++) {\r
+ h.Add(i, i);\r
+ AssertEquals("value wrong for " + i,\r
+ i, h[i]);\r
}\r
}\r
+ }\r
\r
+ [Test]\r
+ public void TestClear() {\r
+ // TODO - hit NotSupportedException\r
+ Hashtable h = new Hashtable();\r
+ AssertEquals("new table - count zero", 0, h.Count);\r
+ int max = 100;\r
+ for (int i = 1; i <= max; i++) {\r
+ h[i] = i;\r
+ }\r
+ Assert("table don't gots stuff", h.Count > 0);\r
+ h.Clear();\r
+ AssertEquals("Table should be cleared",\r
+ 0, h.Count);\r
+ }\r
\r
+ [Test]\r
+ public void TestClone() {\r
+ {\r
+ char[] c1 = {'a', 'b', 'c'};\r
+ char[] c2 = {'d', 'e', 'f'};\r
+ Hashtable h1 = new Hashtable();\r
+ for (int i = 0; i < c1.Length; i++) {\r
+ h1[c1[i]] = c2[i];\r
+ }\r
+ Hashtable h2 = (Hashtable)h1.Clone();\r
+ AssertNotNull("got no clone!", h2);\r
+ AssertNotNull("clone's got nothing!", h2[c1[0]]);\r
+ for (int i = 0; i < c1.Length; i++) {\r
+ AssertEquals("Hashtable match", \r
+ h1[c1[i]], h2[c1[i]]);\r
+ }\r
+ }\r
+ {\r
+ char[] c1 = {'a', 'b', 'c'};\r
+ char[] c20 = {'1', '2'};\r
+ char[] c21 = {'3', '4'};\r
+ char[] c22 = {'5', '6'};\r
+ char[][] c2 = {c20, c21, c22};\r
+ Hashtable h1 = new Hashtable();\r
+ for (int i = 0; i < c1.Length; i++) {\r
+ h1[c1[i]] = c2[i];\r
+ }\r
+ Hashtable h2 = (Hashtable)h1.Clone();\r
+ AssertNotNull("got no clone!", h2);\r
+ AssertNotNull("clone's got nothing!", h2[c1[0]]);\r
+ for (int i = 0; i < c1.Length; i++) {\r
+ AssertEquals("Hashtable match", \r
+ h1[c1[i]], h2[c1[i]]);\r
+ }\r
\r
- private void SetDefaultData() {\r
- ht.Clear();\r
- ht.Add("k1","another");\r
- ht.Add("k2","yet");\r
- ht.Add("k3","hashtable");\r
+ ((char[])h1[c1[0]])[0] = 'z';\r
+ AssertEquals("shallow copy", h1[c1[0]], h2[c1[0]]);\r
}\r
+ }\r
\r
+ [Test]\r
+ public void TestContains() {\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ bool result = h.Contains(null);\r
+ } catch (ArgumentNullException e) {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not 'key'", "key", e.ParamName);\r
+ }\r
+ Assert("null add error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ Hashtable h = new Hashtable();\r
+ for (int i = 0; i < 10000; i += 2) \r
+ {\r
+ h[i] = i;\r
+ }\r
+ for (int i = 0; i < 10000; i += 2) \r
+ {\r
+ Assert("hashtable must contain"+i.ToString(), h.Contains(i));\r
+ Assert("hashtable does not contain "+((int)(i+1)).ToString(), !h.Contains(i+1));\r
+ }\r
+ }\r
+ }\r
\r
- public void TestAddRemoveClear() {\r
- ht.Clear();\r
- Assert(ht.Count==0);\r
+ [Test]\r
+ public void TestContainsKey() {\r
+ {\r
+ bool errorThrown = false;\r
+ try \r
+ {\r
+ Hashtable h = new Hashtable();\r
+ bool result = h.Contains(null);\r
+ } \r
+ catch (ArgumentNullException e) \r
+ {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not 'key'", "key", e.ParamName);\r
+ }\r
+ Assert("null add error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ Hashtable h = new Hashtable();\r
+ for (int i = 0; i < 1000; i += 2) \r
+ {\r
+ h[i] = i;\r
+ }\r
+ for (int i = 0; i < 1000; i += 2) \r
+ {\r
+ Assert("hashtable must contain"+i.ToString(), h.Contains(i));\r
+ Assert("hashtable does not contain "+((int)(i+1)).ToString(), !h.Contains(i+1));\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ [Test] \r
+ public void TestContainsValue() {\r
+ {\r
+ Hashtable h = new Hashtable();\r
+ h['a'] = "blue";\r
+ Assert("blue? it's in there!", \r
+ h.ContainsValue("blue"));\r
+ Assert("green? no way!", \r
+ !h.ContainsValue("green"));\r
+ Assert("null? no way!", \r
+ !h.ContainsValue(null));\r
+ h['b'] = null;\r
+ Assert("null? it's in there!", \r
+ h.ContainsValue(null));\r
\r
- SetDefaultData();\r
- Assert(ht.Count==3);\r
+ }\r
+ }\r
\r
- bool thrown=false;\r
+ [Test] \r
+ public void TestCopyTo() {\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ h.CopyTo(null, 0);\r
+ } catch (ArgumentNullException e) {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not \"array\"", "array", e.ParamName); \r
+ }\r
+ Assert("null hashtable error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ Object[] o = new Object[1];\r
+ h.CopyTo(o, -1);\r
+ } catch (ArgumentOutOfRangeException e) {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not \"arrayIndex\"", "arrayIndex", e.ParamName);\r
+ }\r
+ Assert("out of range error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ Object[,] o = new Object[1,1];\r
+ h.CopyTo(o, 1);\r
+ } catch (ArgumentException) {\r
+ errorThrown = true;\r
+ }\r
+ Assert("multi-dim array error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ h['a'] = 1; // no error if table is empty\r
+ Object[] o = new Object[5];\r
+ h.CopyTo(o, 5);\r
+ } catch (ArgumentException) {\r
+ errorThrown = true;\r
+ }\r
+ Assert("no room in array error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ h['a'] = 1;\r
+ h['b'] = 2;\r
+ h['c'] = 2;\r
+ Object[] o = new Object[2];\r
+ h.CopyTo(o, 0);\r
+ } catch (ArgumentException) {\r
+ errorThrown = true;\r
+ }\r
+ Assert("table too big error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ bool errorThrown = false;\r
try {\r
- ht.Add("k2","cool");\r
- } catch (ArgumentException) {thrown=true;}\r
- Assert("Must throw ArgumentException!",thrown);\r
+ Hashtable h = new Hashtable();\r
+ h["blue"] = 1;\r
+ h["green"] = 2;\r
+ h["red"] = 3;\r
+ Char[] o = new Char[3];\r
+ h.CopyTo(o, 0);\r
+ } catch (InvalidCastException) {\r
+ errorThrown = true;\r
+ }\r
+ Assert("invalid cast error not thrown", \r
+ errorThrown);\r
+ }\r
\r
- ht["k2"]="cool";\r
- Assert(ht.Count==3);\r
- Assert(ht["k2"].Equals("cool"));\r
+ {\r
+ Hashtable h = new Hashtable();\r
+ h['a'] = 1;\r
+ h['b'] = 2;\r
+ DictionaryEntry[] o = new DictionaryEntry[2];\r
+ h.CopyTo(o,0);\r
+ AssertEquals("first copy fine.", 'a', o[0].Key);\r
+ AssertEquals("first copy fine.", 1, o[0].Value);\r
+ AssertEquals("second copy fine.", 'b', o[1].Key);\r
+ AssertEquals("second copy fine.", 2, o[1].Value);\r
+ }\r
+ }\r
\r
+ [Test] \r
+ public void TestGetEnumerator() {\r
+ String[] s1 = {"this", "is", "a", "test"};\r
+ Char[] c1 = {'a', 'b', 'c', 'd'};\r
+ Hashtable h1 = new Hashtable();\r
+ for (int i = 0; i < s1.Length; i++) {\r
+ h1[s1[i]] = c1[i];\r
+ }\r
+ IDictionaryEnumerator en = h1.GetEnumerator();\r
+ AssertNotNull("No enumerator", en);\r
+ \r
+ for (int i = 0; i < s1.Length; i++) {\r
+ en.MoveNext();\r
+ Assert("Not enumerating for " + en.Key, \r
+ Array.IndexOf(s1, en.Key) >= 0);\r
+ Assert("Not enumerating for " + en.Value, \r
+ Array.IndexOf(c1, en.Value) >= 0);\r
}\r
+ }\r
\r
- public void TestCopyTo() {\r
- SetDefaultData();\r
- Object[] entries=new Object[ht.Count];\r
- ht.CopyTo(entries,0);\r
- Assert("Not an entry.",entries[0] is DictionaryEntry);\r
+ // TODO - GetObjectData\r
+ // TODO - OnDeserialization\r
+ [Test]\r
+ public void TestSerialization () {\r
+ Random r = new Random();\r
+ string filename = "hashtable_" + r.Next(99999).ToString() + ".dat";\r
+ Hashtable table1 = new Hashtable();\r
+ Hashtable table2;\r
+ Stream str;\r
+ BinaryFormatter formatter = new BinaryFormatter();\r
+\r
+ for (int i = 0; i < 100; i++) {\r
+ table1[i] = "TestString Key: " + i.ToString();\r
+ }\r
+ str = File.OpenWrite(filename);\r
+ formatter.Serialize(str, table1);\r
+ str.Close();\r
+\r
+ str = File.OpenRead(filename);\r
+ table2 = (Hashtable) formatter.Deserialize(str);\r
+ str.Close();\r
+\r
+ File.Delete(filename);\r
+\r
+ bool result;\r
+ foreach (DictionaryEntry de in table1) {\r
+ int key1 = (int) de.Key;\r
+ string val1 = (string) de.Value;\r
+ string val2 = (string) table2[key1];\r
+ if (val2 != val1) {\r
+ result = false;\r
+ }\r
}\r
+ result = true;\r
\r
+ Assert("Binary Serialization Error", result);\r
+ }\r
\r
- public void TestUnderHeavyLoad() {\r
- Console.WriteLine("Testing "+ht);\r
- ht.Clear();\r
- int max=100000;\r
- String[] cache=new String[max*2];\r
- int n=0;\r
+ [Test] \r
+ public void TestRemove() {\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = new Hashtable();\r
+ h.Remove(null);\r
+ } catch (ArgumentNullException e) {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not \"key\"", "key", e.ParamName);\r
+ }\r
+ Assert("null hashtable error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ string[] keys = {"this", "is", "a", "test"};\r
+ char[] values = {'a', 'b', 'c', 'd'};\r
+ Hashtable h = new Hashtable();\r
+ for (int i = 0; i < keys.Length; i++) {\r
+ h[keys[i]] = values[i];\r
+ }\r
+ AssertEquals("not enough in table",\r
+ 4, h.Count);\r
+ h.Remove("huh?");\r
+ AssertEquals("not enough in table",\r
+ 4, h.Count);\r
+ h.Remove("this");\r
+ AssertEquals("Wrong count in table",\r
+ 3, h.Count);\r
+ h.Remove("this");\r
+ AssertEquals("Wrong count in table",\r
+ 3, h.Count);\r
+ }\r
+ }\r
\r
- for (int i=0;i<max;i++) {\r
- int id=rnd.Next()&0xFFFF;\r
- String key=""+id+"-key-"+id;\r
- String val="value-"+id;\r
- if (ht[key]==null) {\r
- ht[key]=val;\r
- cache[n]=key;\r
- cache[n+max]=val;\r
- n++;\r
- }\r
+ [Test] \r
+ public void TestSynchronized() {\r
+ {\r
+ bool errorThrown = false;\r
+ try {\r
+ Hashtable h = Hashtable.Synchronized(null);\r
+ } catch (ArgumentNullException e) {\r
+ errorThrown = true;\r
+ AssertEquals("ParamName is not \"table\"", "table", e.ParamName);\r
}\r
+ Assert("null hashtable error not thrown", \r
+ errorThrown);\r
+ }\r
+ {\r
+ Hashtable h = new Hashtable();\r
+ Assert("hashtable not synced by default", \r
+ !h.IsSynchronized);\r
+ Hashtable h2 = Hashtable.Synchronized(h);\r
+ Assert("hashtable should by synced", \r
+ h2.IsSynchronized);\r
+ }\r
+ }\r
+ \r
+ \r
+ protected Hashtable ht;\r
+ private static Random rnd;\r
+ \r
+ [SetUp]\r
+ public void SetUp() {\r
+ ht=new Hashtable();\r
+ rnd=new Random();\r
+ }\r
+ \r
+ private void SetDefaultData() {\r
+ ht.Clear();\r
+ ht.Add("k1","another");\r
+ ht.Add("k2","yet");\r
+ ht.Add("k3","hashtable");\r
+ }\r
+ \r
+ [Test]\r
+ public void TestAddRemoveClear() {\r
+ ht.Clear();\r
+ Assert(ht.Count==0);\r
+ \r
+ SetDefaultData();\r
+ Assert(ht.Count==3);\r
+ \r
+ bool thrown=false;\r
+ try {\r
+ ht.Add("k2","cool");\r
+ } catch (ArgumentException) {thrown=true;}\r
+ Assert("Must throw ArgumentException!",thrown);\r
+ \r
+ ht["k2"]="cool";\r
+ Assert(ht.Count==3);\r
+ Assert(ht["k2"].Equals("cool"));\r
+ \r
+ }\r
\r
- Assert(ht.Count==n);\r
+ [Test] \r
+ public void TestCopyTo2() {\r
+ SetDefaultData();\r
+ Object[] entries=new Object[ht.Count];\r
+ ht.CopyTo(entries,0);\r
+ Assert("Not an entry.",entries[0] is DictionaryEntry);\r
+ }\r
\r
- for (int i=0;i<n;i++) {\r
+ [Test]\r
+ public void CopyTo_Empty ()\r
+ {\r
+ Hashtable ht = new Hashtable ();\r
+ AssertEquals ("Count", 0, ht.Count);\r
+ object[] array = new object [ht.Count];\r
+ ht.CopyTo (array, 0);\r
+ }\r
+ \r
+ [Test] \r
+ public void TestUnderHeavyLoad() {\r
+ ht.Clear();\r
+ int max=100000;\r
+ String[] cache=new String[max*2];\r
+ int n=0;\r
+ \r
+ for (int i=0;i<max;i++) {\r
+ int id=rnd.Next()&0xFFFF;\r
+ String key=""+id+"-key-"+id;\r
+ String val="value-"+id;\r
+ if (ht[key]==null) {\r
+ ht[key]=val;\r
+ cache[n]=key;\r
+ cache[n+max]=val;\r
+ n++;\r
+ }\r
+ }\r
+ \r
+ Assert(ht.Count==n);\r
+ \r
+ for (int i=0;i<n;i++) {\r
+ String key=cache[i];\r
+ String val=ht[key] as String;\r
+ String err="ht[\""+key+"\"]=\""+val+\r
+ "\", expected \""+cache[i+max]+"\"";\r
+ Assert(err,val!=null && val.Equals(cache[i+max]));\r
+ }\r
+ \r
+ int r1=(n/3);\r
+ int r2=r1+(n/5);\r
+ \r
+ for (int i=r1;i<r2;i++) {\r
+ ht.Remove(cache[i]);\r
+ }\r
+ \r
+ \r
+ for (int i=0;i<n;i++) {\r
+ if (i>=r1 && i<r2) {\r
+ Assert(ht[cache[i]]==null);\r
+ } else {\r
String key=cache[i];\r
String val=ht[key] as String;\r
String err="ht[\""+key+"\"]=\""+val+\r
- "\", expected \""+cache[i+max]+"\"";\r
+ "\", expected \""+cache[i+max]+"\"";\r
Assert(err,val!=null && val.Equals(cache[i+max]));\r
}\r
+ }\r
+ \r
+ ICollection keys=ht.Keys;\r
+ int nKeys=0;\r
+ foreach (Object key in keys) {\r
+ Assert((key as String) != null);\r
+ nKeys++;\r
+ }\r
+ Assert(nKeys==ht.Count);\r
+\r
+ \r
+ ICollection vals=ht.Values;\r
+ int nVals=0;\r
+ foreach (Object val in vals) {\r
+ Assert((val as String) != null);\r
+ nVals++;\r
+ }\r
+ Assert(nVals==ht.Count);\r
+ \r
+ }\r
\r
- int r1=(n/3);\r
- int r2=r1+(n/5);\r
+ \r
+ /// <summary>\r
+ /// Test hashtable with CaseInsensitiveHashCodeProvider\r
+ /// and CaseInsensitive comparer.\r
+ /// </summary>\r
+ [Test] \r
+ public void TestCaseInsensitive ()\r
+ {\r
+ // Not very meaningfull test, just to make\r
+ // sure that hcp is set properly set.\r
+ Hashtable ciHashtable = new Hashtable(11,1.0f,CaseInsensitiveHashCodeProvider.Default,CaseInsensitiveComparer.Default);\r
+ ciHashtable ["key1"] = "value";\r
+ ciHashtable ["key2"] = "VALUE";\r
+ Assert(ciHashtable ["key1"].Equals ("value"));\r
+ Assert(ciHashtable ["key2"].Equals ("VALUE"));\r
+\r
+ ciHashtable ["KEY1"] = "new_value";\r
+ Assert(ciHashtable ["key1"].Equals ("new_value"));\r
\r
- for (int i=r1;i<r2;i++) {\r
- ht.Remove(cache[i]);\r
- }\r
+ }\r
\r
+ [Test]\r
+ public void TestCopyConstructor ()\r
+ {\r
+ SetDefaultData ();\r
\r
- for (int i=0;i<n;i++) {\r
- if (i>=r1 && i<r2) {\r
- Assert(ht[cache[i]]==null);\r
- } else {\r
- String key=cache[i];\r
- String val=ht[key] as String;\r
- String err="ht[\""+key+"\"]=\""+val+\r
- "\", expected \""+cache[i+max]+"\"";\r
- Assert(err,val!=null && val.Equals(cache[i+max]));\r
- }\r
- }\r
+ Hashtable htCopy = new Hashtable (ht);\r
\r
- ICollection keys=ht.Keys;\r
- int nKeys=0;\r
- foreach (Object key in keys) {\r
- Assert((key as String) != null);\r
- nKeys++;\r
- }\r
- Assert(nKeys==ht.Count);\r
+ Assert(ht.Count == htCopy.Count);\r
+ }\r
\r
+ [Test]\r
+ public void TestEnumerator ()\r
+ {\r
+ SetDefaultData ();\r
\r
- ICollection vals=ht.Values;\r
- int nVals=0;\r
- foreach (Object val in vals) {\r
- Assert((val as String) != null);\r
- nVals++;\r
- }\r
- Assert(nVals==ht.Count);\r
+ IEnumerator e = ht.GetEnumerator ();\r
\r
- }\r
+ while (e.MoveNext ()) {}\r
\r
+ Assert (!e.MoveNext ());\r
+\r
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (ArgumentNullException))]\r
+ public void GetObjectData_NullSerializationInfo () \r
+ {\r
+ SetDefaultData ();\r
+ ht.GetObjectData (null, new StreamingContext ());\r
}\r
}\r
+}\r