| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- // HashtableTest.cs - NUnit Test Cases for the System.Collections.Hashtable class
- //
- //
- // (C) Ximian, Inc. http://www.ximian.com
- //
- using System;
- using System.Collections;
- using NUnit.Framework;
- namespace MonoTests.System.Collections {
- /// <summary>Hashtable test.</summary>
- [TestFixture]
- public class HashtableTest : TestCase {
- public void TestCtor1() {
- Hashtable h = new Hashtable();
- AssertNotNull("No hash table", h);
- }
- public void TestCtor2() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable(null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null hashtable error not thrown",
- errorThrown);
- }
- {
- string[] keys = {"this", "is", "a", "test"};
- char[] values = {'a', 'b', 'c', 'd'};
- Hashtable h1 = new Hashtable();
- for (int i = 0; i < keys.Length; i++) {
- h1[keys[i]] = values[i];
- }
- Hashtable h2 = new Hashtable(h1);
- for (int i = 0; i < keys.Length; i++) {
- AssertEquals("No match for key " + keys[i],
- values[i], h2[keys[i]]);
- }
- }
- }
- // TODO - Ctors for capacity and load (how to test? any access?)
- // TODO - Ctors with IComparer, IHashCodeProvider, Serialization
-
- public void TestCount() {
- Hashtable h = new Hashtable();
- AssertEquals("new table - count zero", 0, h.Count);
- int max = 100;
- for (int i = 1; i <= max; i++) {
- h[i] = i;
- AssertEquals("Count wrong for " + i,
- i, h.Count);
- }
- for (int i = 1; i <= max; i++) {
- h[i] = i * 2;
- AssertEquals("Count shouldn't change at " + i,
- max, h.Count);
- }
- }
- public void TestIsFixedSize() {
- Hashtable h = new Hashtable();
- AssertEquals("hashtable not fixed by default",
- false, h.IsFixedSize);
- // TODO - any way to get a fixed-size hashtable?
- }
- public void TestIsReadOnly() {
- Hashtable h = new Hashtable();
- AssertEquals("hashtable not read-only by default",
- false, h.IsReadOnly);
- // TODO - any way to get a read-only hashtable?
- }
- public void TestIsSynchronized() {
- Hashtable h = new Hashtable();
- Assert("hashtable not synced by default", !h.IsSynchronized);
- Hashtable h2 = Hashtable.Synchronized(h);
- Assert("hashtable should by synced", h2.IsSynchronized);
- }
- public void TestItem() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- Object o = h[null];
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null hashtable error not thrown",
- errorThrown);
- }
- // TODO - if read-only and/or fixed-size is possible,
- // test 'NotSupportedException' here
- {
- Hashtable h = new Hashtable();
- int max = 100;
- for (int i = 1; i <= max; i++) {
- h[i] = i;
- AssertEquals("value wrong for " + i,
- i, h[i]);
- }
- }
- }
- public void TestKeys() {
- string[] keys = {"this", "is", "a", "test"};
- char[] values1 = {'a', 'b', 'c', 'd'};
- char[] values2 = {'e', 'f', 'g', 'h'};
- Hashtable h1 = new Hashtable();
- for (int i = 0; i < keys.Length; i++) {
- h1[keys[i]] = values1[i];
- }
- AssertEquals("keys wrong size",
- keys.Length, h1.Keys.Count);
- for (int i = 0; i < keys.Length; i++) {
- h1[keys[i]] = values2[i];
- }
- AssertEquals("keys wrong size 2",
- keys.Length, h1.Keys.Count);
- }
- // TODO - SyncRoot
- public void TestValues() {
- string[] keys = {"this", "is", "a", "test"};
- char[] values1 = {'a', 'b', 'c', 'd'};
- char[] values2 = {'e', 'f', 'g', 'h'};
- Hashtable h1 = new Hashtable();
- for (int i = 0; i < keys.Length; i++) {
- h1[keys[i]] = values1[i];
- }
- AssertEquals("values wrong size",
- keys.Length, h1.Values.Count);
- for (int i = 0; i < keys.Length; i++) {
- h1[keys[i]] = values2[i];
- }
- AssertEquals("values wrong size 2",
- keys.Length, h1.Values.Count);
- }
-
- public void TestAdd() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- h.Add(null, "huh?");
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null add error not thrown",
- errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- h.Add('a', 1);
- h.Add('a', 2);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert("re-add error not thrown",
- errorThrown);
- }
- // TODO - hit NotSupportedException
- {
- Hashtable h = new Hashtable();
- int max = 100;
- for (int i = 1; i <= max; i++) {
- h.Add(i, i);
- AssertEquals("value wrong for " + i,
- i, h[i]);
- }
- }
- }
- public void TestClear() {
- // TODO - hit NotSupportedException
- Hashtable h = new Hashtable();
- AssertEquals("new table - count zero", 0, h.Count);
- int max = 100;
- for (int i = 1; i <= max; i++) {
- h[i] = i;
- }
- Assert("table don't gots stuff", h.Count > 0);
- h.Clear();
- AssertEquals("Table should be cleared",
- 0, h.Count);
- }
- public void TestClone() {
- {
- char[] c1 = {'a', 'b', 'c'};
- char[] c2 = {'d', 'e', 'f'};
- Hashtable h1 = new Hashtable();
- for (int i = 0; i < c1.Length; i++) {
- h1[c1[i]] = c2[i];
- }
- Hashtable h2 = (Hashtable)h1.Clone();
- AssertNotNull("got no clone!", h2);
- AssertNotNull("clone's got nothing!", h2[c1[0]]);
- for (int i = 0; i < c1.Length; i++) {
- AssertEquals("Hashtable match",
- h1[c1[i]], h2[c1[i]]);
- }
- }
- {
- char[] c1 = {'a', 'b', 'c'};
- char[] c20 = {'1', '2'};
- char[] c21 = {'3', '4'};
- char[] c22 = {'5', '6'};
- char[][] c2 = {c20, c21, c22};
- Hashtable h1 = new Hashtable();
- for (int i = 0; i < c1.Length; i++) {
- h1[c1[i]] = c2[i];
- }
- Hashtable h2 = (Hashtable)h1.Clone();
- AssertNotNull("got no clone!", h2);
- AssertNotNull("clone's got nothing!", h2[c1[0]]);
- for (int i = 0; i < c1.Length; i++) {
- AssertEquals("Hashtable match",
- h1[c1[i]], h2[c1[i]]);
- }
- ((char[])h1[c1[0]])[0] = 'z';
- AssertEquals("shallow copy", h1[c1[0]], h2[c1[0]]);
- }
- }
- public void TestContains() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- bool result = h.Contains(null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null add error not thrown",
- errorThrown);
- }
- {
- Hashtable h = new Hashtable();
- h['a'] = "blue";
- Assert("'a'? it's in there!", h.Contains('a'));
- Assert("'b'? no way!", !h.Contains('b'));
- }
- }
- public void TestContainsKey() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- bool result = h.ContainsKey(null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null add error not thrown",
- errorThrown);
- }
- {
- Hashtable h = new Hashtable();
- h['a'] = "blue";
- Assert("'a'? it's in there!", h.ContainsKey('a'));
- Assert("'b'? no way!", !h.ContainsKey('b'));
- }
- }
-
- public void TestContainsValue() {
- {
- Hashtable h = new Hashtable();
- h['a'] = "blue";
- Assert("blue? it's in there!",
- h.ContainsValue("blue"));
- Assert("green? no way!",
- !h.ContainsValue("green"));
- }
- }
- public void TestCopyTo() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- h.CopyTo(null, 0);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null hashtable error not thrown",
- errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- Object[] o = new Object[1];
- h.CopyTo(o, -1);
- } catch (ArgumentOutOfRangeException) {
- errorThrown = true;
- }
- Assert("out of range error not thrown",
- errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- Object[,] o = new Object[1,1];
- h.CopyTo(o, 1);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert("multi-dim array error not thrown",
- errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- h['a'] = 1; // no error if table is empty
- Object[] o = new Object[5];
- h.CopyTo(o, 5);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert("no room in array error not thrown",
- errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- h['a'] = 1;
- h['b'] = 2;
- h['c'] = 2;
- Object[] o = new Object[2];
- h.CopyTo(o, 0);
- } catch (ArgumentException) {
- errorThrown = true;
- }
- Assert("table too big error not thrown",
- errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- h["blue"] = 1;
- h["green"] = 2;
- h["red"] = 3;
- Char[] o = new Char[3];
- h.CopyTo(o, 0);
- } catch (InvalidCastException) {
- errorThrown = true;
- }
- Assert("invalid cast error not thrown",
- errorThrown);
- }
- {
- Hashtable h = new Hashtable();
- h['a'] = 1;
- h['b'] = 2;
- DictionaryEntry[] o = new DictionaryEntry[2];
- h.CopyTo(o,0);
- AssertEquals("first copy fine.", 'a', o[0].Key);
- AssertEquals("first copy fine.", 1, o[0].Value);
- AssertEquals("second copy fine.", 'b', o[1].Key);
- AssertEquals("second copy fine.", 2, o[1].Value);
- }
- }
- public void TestGetEnumerator() {
- String[] s1 = {"this", "is", "a", "test"};
- Char[] c1 = {'a', 'b', 'c', 'd'};
- Hashtable h1 = new Hashtable();
- for (int i = 0; i < s1.Length; i++) {
- h1[s1[i]] = c1[i];
- }
- IDictionaryEnumerator en = h1.GetEnumerator();
- AssertNotNull("No enumerator", en);
-
- for (int i = 0; i < s1.Length; i++) {
- en.MoveNext();
- Assert("Not enumerating for " + en.Key,
- Array.IndexOf(s1, en.Key) >= 0);
- Assert("Not enumerating for " + en.Value,
- Array.IndexOf(c1, en.Value) >= 0);
- }
- }
- // TODO - GetObjectData
- // TODO - OnDeserialization
- public void TestRemove() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = new Hashtable();
- h.Remove(null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null hashtable error not thrown",
- errorThrown);
- }
- {
- string[] keys = {"this", "is", "a", "test"};
- char[] values = {'a', 'b', 'c', 'd'};
- Hashtable h = new Hashtable();
- for (int i = 0; i < keys.Length; i++) {
- h[keys[i]] = values[i];
- }
- AssertEquals("not enough in table",
- 4, h.Count);
- h.Remove("huh?");
- AssertEquals("not enough in table",
- 4, h.Count);
- h.Remove("this");
- AssertEquals("Wrong count in table",
- 3, h.Count);
- h.Remove("this");
- AssertEquals("Wrong count in table",
- 3, h.Count);
- }
- }
- public void TestSynchronized() {
- {
- bool errorThrown = false;
- try {
- Hashtable h = Hashtable.Synchronized(null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert("null hashtable error not thrown",
- errorThrown);
- }
- {
- Hashtable h = new Hashtable();
- Assert("hashtable not synced by default",
- !h.IsSynchronized);
- Hashtable h2 = Hashtable.Synchronized(h);
- Assert("hashtable should by synced",
- h2.IsSynchronized);
- }
- }
-
-
- protected Hashtable ht;
- private static Random rnd;
-
- [SetUp]
- protected override void SetUp() {
- ht=new Hashtable();
- rnd=new Random();
- }
-
- private void SetDefaultData() {
- ht.Clear();
- ht.Add("k1","another");
- ht.Add("k2","yet");
- ht.Add("k3","hashtable");
- }
-
-
- public void TestAddRemoveClear() {
- ht.Clear();
- Assert(ht.Count==0);
-
- SetDefaultData();
- Assert(ht.Count==3);
-
- bool thrown=false;
- try {
- ht.Add("k2","cool");
- } catch (ArgumentException) {thrown=true;}
- Assert("Must throw ArgumentException!",thrown);
-
- ht["k2"]="cool";
- Assert(ht.Count==3);
- Assert(ht["k2"].Equals("cool"));
-
- }
-
- public void TestCopyTo2() {
- SetDefaultData();
- Object[] entries=new Object[ht.Count];
- ht.CopyTo(entries,0);
- Assert("Not an entry.",entries[0] is DictionaryEntry);
- }
-
-
- public void TestUnderHeavyLoad() {
- ht.Clear();
- int max=100000;
- String[] cache=new String[max*2];
- int n=0;
-
- for (int i=0;i<max;i++) {
- int id=rnd.Next()&0xFFFF;
- String key=""+id+"-key-"+id;
- String val="value-"+id;
- if (ht[key]==null) {
- ht[key]=val;
- cache[n]=key;
- cache[n+max]=val;
- n++;
- }
- }
-
- Assert(ht.Count==n);
-
- for (int i=0;i<n;i++) {
- String key=cache[i];
- String val=ht[key] as String;
- String err="ht[\""+key+"\"]=\""+val+
- "\", expected \""+cache[i+max]+"\"";
- Assert(err,val!=null && val.Equals(cache[i+max]));
- }
-
- int r1=(n/3);
- int r2=r1+(n/5);
-
- for (int i=r1;i<r2;i++) {
- ht.Remove(cache[i]);
- }
-
-
- for (int i=0;i<n;i++) {
- if (i>=r1 && i<r2) {
- Assert(ht[cache[i]]==null);
- } else {
- String key=cache[i];
- String val=ht[key] as String;
- String err="ht[\""+key+"\"]=\""+val+
- "\", expected \""+cache[i+max]+"\"";
- Assert(err,val!=null && val.Equals(cache[i+max]));
- }
- }
-
- ICollection keys=ht.Keys;
- int nKeys=0;
- foreach (Object key in keys) {
- Assert((key as String) != null);
- nKeys++;
- }
- Assert(nKeys==ht.Count);
-
- ICollection vals=ht.Values;
- int nVals=0;
- foreach (Object val in vals) {
- Assert((val as String) != null);
- nVals++;
- }
- Assert(nVals==ht.Count);
-
- }
-
- [TestFixture]
- public class HashtableTest2 : TestCase {
- protected Hashtable ht;
- private static Random rnd;
-
- [SetUp]
- protected override void SetUp ()
- {
- ht=new Hashtable ();
- rnd=new Random ();
- }
-
- private void SetDefaultData ()
- {
- ht.Clear ();
- ht.Add ("k1","another");
- ht.Add ("k2","yet");
- ht.Add ("k3","hashtable");
- }
-
- public void TestAddRemoveClear ()
- {
- ht.Clear ();
- Assert (ht.Count == 0);
-
- SetDefaultData ();
- Assert (ht.Count == 3);
- bool thrown=false;
- try {
- ht.Add ("k2","cool");
- } catch (ArgumentException) {thrown=true;}
- Assert("Must throw ArgumentException!",thrown);
- ht["k2"]="cool";
- Assert(ht.Count == 3);
- Assert(ht["k2"].Equals("cool"));
- }
- public void TestCopyTo ()
- {
- SetDefaultData ();
- Object[] entries=new Object[ht.Count];
- ht.CopyTo (entries,0);
- Assert("Not an entry.",entries[0] is DictionaryEntry);
- }
- public void TestUnderHeavyLoad ()
- {
- ht.Clear ();
- int max=100000;
- String[] cache=new String[max*2];
- int n=0;
- for (int i=0;i<max;i++) {
- int id=rnd.Next()&0xFFFF;
- String key=""+id+"-key-"+id;
- String val="value-"+id;
- if (ht[key]==null) {
- ht[key]=val;
- cache[n]=key;
- cache[n+max]=val;
- n++;
- }
- }
- Assert(ht.Count==n);
- for (int i=0;i<n;i++) {
- String key=cache[i];
- String val=ht[key] as String;
- String err="ht[\""+key+"\"]=\""+val+
- "\", expected \""+cache[i+max]+"\"";
- Assert(err,val!=null && val.Equals(cache[i+max]));
- }
- int r1=(n/3);
- int r2=r1+(n/5);
- for (int i=r1;i<r2;i++) {
- ht.Remove(cache[i]);
- }
- for (int i=0;i<n;i++) {
- if (i>=r1 && i<r2) {
- Assert(ht[cache[i]]==null);
- } else {
- String key=cache[i];
- String val=ht[key] as String;
- String err="ht[\""+key+"\"]=\""+val+
- "\", expected \""+cache[i+max]+"\"";
- Assert(err,val!=null && val.Equals(cache[i+max]));
- }
- }
- ICollection keys=ht.Keys;
- int nKeys=0;
- foreach (Object key in keys) {
- Assert((key as String) != null);
- nKeys++;
- }
- Assert(nKeys==ht.Count);
- ICollection vals=ht.Values;
- int nVals=0;
- foreach (Object val in vals) {
- Assert((val as String) != null);
- nVals++;
- }
- Assert(nVals==ht.Count);
-
- }
-
- /// <summary>
- /// Test hashtable with CaseInsensitiveHashCodeProvider
- /// and CaseInsensitive comparer.
- /// </summary>
- public void TestCaseInsensitive ()
- {
- // Not very meaningfull test, just to make
- // sure that hcp is set properly set.
- Hashtable ciHashtable = new Hashtable(11,1.0f,CaseInsensitiveHashCodeProvider.Default,CaseInsensitiveComparer.Default);
- ciHashtable ["key1"] = "value";
- ciHashtable ["key2"] = "VALUE";
- Assert(ciHashtable ["key1"].Equals ("value"));
- Assert(ciHashtable ["key2"].Equals ("VALUE"));
- ciHashtable ["KEY1"] = "new_value";
- Assert(ciHashtable ["key1"].Equals ("new_value"));
- }
- public void TestCopyConstructor ()
- {
- SetDefaultData ();
- Hashtable htCopy = new Hashtable (ht);
- Assert(ht.Count == htCopy.Count);
- }
- public void TestEnumerator ()
- {
- SetDefaultData ();
- IEnumerator e = ht.GetEnumerator ();
- while (e.MoveNext ()) {}
- Assert (!e.MoveNext ());
- }
-
- }
-
-
- }
- }
|