/// <summary> /// Inserts an item into the <see cref="Cache"/> object with a specified lifetime using a cache key to reference its location. /// </summary> /// <param name="key">The cache key used to reference the item.</param> /// <param name="value">The object to be inserted into the cache.</param> /// <param name="lifetime">The lifetime of the inserted item (i.e. is it expendable or not).</param> public void Insert(object key, object value, CacheItemLifetime lifetime) { if (key != null) { _rwl.AcquireWriterLock(LOCK_TIMEOUT_MSECS); try { // remove any existing item for this key CacheCollectionElement element = _data[key] as CacheCollectionElement; if (element != null) { element.IsCollectable = true; _data.Remove(key); } // add new item element = new CacheCollectionElement(value, lifetime); _data[key] = element; } finally { _rwl.ReleaseWriterLock(); } } else { throw new ArgumentNullException("key"); } }
public void All () { HybridDictionary dict = new HybridDictionary (true); dict.Add ("CCC", "ccc"); dict.Add ("BBB", "bbb"); dict.Add ("fff", "fff"); dict ["EEE"] = "eee"; dict ["ddd"] = "ddd"; Assert.AreEqual (5, dict.Count, "#1"); Assert.AreEqual ("eee", dict["eee"], "#2"); dict.Add ("CCC2", "ccc"); dict.Add ("BBB2", "bbb"); dict.Add ("fff2", "fff"); dict ["EEE2"] = "eee"; dict ["ddd2"] = "ddd"; dict ["xxx"] = "xxx"; dict ["yyy"] = "yyy"; Assert.AreEqual (12, dict.Count, "#3"); Assert.AreEqual ("eee", dict["eee"], "#4"); dict.Remove ("eee"); Assert.AreEqual (11, dict.Count, "Removed/Count"); Assert.IsFalse (dict.Contains ("eee"), "Removed/Contains(xxx)"); DictionaryEntry[] entries = new DictionaryEntry [11]; dict.CopyTo (entries, 0); Assert.IsTrue (dict.Contains ("xxx"), "Contains(xxx)"); dict.Clear (); Assert.AreEqual (0, dict.Count, "Cleared/Count"); Assert.IsFalse (dict.Contains ("xxx"), "Cleared/Contains(xxx)"); }
public void RemoveClient(string auth) { s2hClient Client = GetClient(auth); if (Client != null) { lock (m_Clients) { string [] connectionHandles = Client.ConnectionHandles; foreach (string s in connectionHandles) { m_DataExchanger.CloseConnection(s); } Client.CloseAllConnections(); m_Clients.Remove(auth); } } }
public bool CloseConnection(string handle) { lock (m_Connections) { //System.Diagnostics.Debug.WriteLine("Connections before:" + m_Connections.Count.ToString()); if (m_Connections.Contains(handle)) { s2hConnection conn = (s2hConnection)m_Connections[handle]; m_Connections.Remove(handle); try { conn.Close(); } catch (Exception) {} //System.Diagnostics.Debug.WriteLine("Connections after:" + m_Connections.Count.ToString()); return(true); } } return(false); }
public void Test01() { const int BIG_LENGTH = 100; HybridDictionary hd; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; Array arr; ICollection vs; // Values collection int ind; for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); // [] for empty dictionary // if (hd.Count > 0) hd.Clear(); if (hd.Values.Count != 0) { Assert.False(true, string.Format("Error, returned Values.Count = {0}", hd.Values.Count)); } // [] for short filled dictionary // int len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } vs = hd.Values; if (vs.Count != len) { Assert.False(true, string.Format("Error, returned Values.Count = {0}", vs.Count)); } arr = Array.CreateInstance(typeof(Object), len); vs.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, valuesShort[i]); if (ind < 0) { Assert.False(true, string.Format("Error, Values doesn't contain \"{1}\" value. Search result: {2}", i, valuesShort[i], ind)); } } // [] for long filled dictionary // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } vs = hd.Values; if (vs.Count != len) { Assert.False(true, string.Format("Error, returned Values.Count = {0}", vs.Count)); } arr = Array.CreateInstance(typeof(Object), len); vs.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, valuesLong[i]); if (ind < 0) { Assert.False(true, string.Format("Error, Values doesn't contain \"{1}\" value. Search result: {2}", i, valuesLong[i], ind)); } } // // [] get Values on dictionary with different_in_casing_only keys - list // hd.Clear(); string intlStr = "intlStr"; hd.Add("keykey", intlStr); // 1st key hd.Add("keyKey", intlStr); // 2nd key if (hd.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, 2)); } // get Values // vs = hd.Values; if (vs.Count != hd.Count) { Assert.False(true, string.Format("Error, returned Values.Count = {0}", vs.Count)); } arr = Array.CreateInstance(typeof(Object), 2); vs.CopyTo(arr, 0); ind = Array.IndexOf(arr, intlStr); if (ind < 0) { Assert.False(true, string.Format("Error, Values doesn't contain {0} value", intlStr)); } // // [] get Values on dictionary with different_in_casing_only keys - hashtable // hd.Clear(); hd.Add("keykey", intlStr); // 1st key for (int i = 0; i < BIG_LENGTH; i++) { hd.Add(keysLong[i], valuesLong[i]); } hd.Add("keyKey", intlStr); // 2nd key if (hd.Count != BIG_LENGTH + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, BIG_LENGTH + 2)); } // get Values // vs = hd.Values; if (vs.Count != hd.Count) { Assert.False(true, string.Format("Error, returned Values.Count = {0}", vs.Count)); } arr = Array.CreateInstance(typeof(Object), BIG_LENGTH + 2); vs.CopyTo(arr, 0); for (int i = 0; i < BIG_LENGTH; i++) { if (Array.IndexOf(arr, valuesLong[i]) < 0) { Assert.False(true, string.Format("Error, Values doesn't contain {0} value", valuesLong[i], i)); } } ind = Array.IndexOf(arr, intlStr); if (ind < 0) { Assert.False(true, string.Format("Error, Values doesn't contain {0} value", intlStr)); } // // [] Change long dictionary and check Values // hd.Clear(); len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } vs = hd.Values; if (vs.Count != len) { Assert.False(true, string.Format("Error, returned Values.Count = {0}", vs.Count)); } hd.Remove(keysLong[0]); if (hd.Count != len - 1) { Assert.False(true, string.Format("Error, didn't remove element")); } if (vs.Count != len - 1) { Assert.False(true, string.Format("Error, Values were not updated after removal")); } arr = Array.CreateInstance(typeof(Object), hd.Count); vs.CopyTo(arr, 0); ind = Array.IndexOf(arr, valuesLong[0]); if (ind >= 0) { Assert.False(true, string.Format("Error, Values still contains removed value " + ind)); } hd.Add(keysLong[0], "new item"); if (hd.Count != len) { Assert.False(true, string.Format("Error, didn't add element")); } if (vs.Count != len) { Assert.False(true, string.Format("Error, Values were not updated after addition")); } arr = Array.CreateInstance(typeof(Object), hd.Count); vs.CopyTo(arr, 0); ind = Array.IndexOf(arr, "new item"); if (ind < 0) { Assert.False(true, string.Format("Error, Values doesn't contain added value ")); } // // [] Change short dictionary and check Values // hd.Clear(); len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } vs = hd.Values; if (vs.Count != len) { Assert.False(true, string.Format("Error, returned Values.Count = {0}", vs.Count)); } hd.Remove(keysShort[0]); if (hd.Count != len - 1) { Assert.False(true, string.Format("Error, didn't remove element")); } if (vs.Count != len - 1) { Assert.False(true, string.Format("Error, Values were not updated after removal")); } arr = Array.CreateInstance(typeof(Object), hd.Count); vs.CopyTo(arr, 0); ind = Array.IndexOf(arr, valuesShort[0]); if (ind >= 0) { Assert.False(true, string.Format("Error, Values still contains removed value " + ind)); } hd.Add(keysShort[0], "new item"); if (hd.Count != len) { Assert.False(true, string.Format("Error, didn't add element")); } if (vs.Count != len) { Assert.False(true, string.Format("Error, Values were not updated after addition")); } arr = Array.CreateInstance(typeof(Object), hd.Count); vs.CopyTo(arr, 0); ind = Array.IndexOf(arr, "new item"); if (ind < 0) { Assert.False(true, string.Format("Error, Values doesn't contain added value ")); } }
public void Test01() { HybridDictionary hd; IDictionaryEnumerator en; DictionaryEntry curr; // Enumerator.Current value DictionaryEntry de; // Enumerator.Entry value Object k; // Enumerator.Key value Object v; // Enumerator.Value const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary GetEnumerator() //----------------------------------------------------------------- hd = new HybridDictionary(); // [] Enumerator for empty dictionary // en = hd.GetEnumerator(); IEnumerator en2; en2 = ((IEnumerable)hd).GetEnumerator(); string type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } // // MoveNext should return false // bool res = en.MoveNext(); if (res) { Assert.False(true, string.Format("Error, MoveNext returned true")); } // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // --------------------------------------------------------- // [] Enumerator for Filled dictionary - list // --------------------------------------------------------- // for (int i = 0; i < valuesShort.Length; i++) { hd.Add(keysShort[i], valuesShort[i]); } en = hd.GetEnumerator(); type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } // // MoveNext should return true // for (int i = 0; i < hd.Count; i++) { res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false", i)); } curr = (DictionaryEntry)en.Current; de = en.Entry; // //enumerator enumerates in different than added order // so we'll check Contains // if (!hd.Contains(curr.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (!hd.Contains(en.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (!hd.Contains(de.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain Entry.Key from enumerator", i)); } if (String.Compare(hd[curr.Key.ToString()].ToString(), curr.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, Value for current Key is different in dictionary", i)); } if (String.Compare(hd[de.Key.ToString()].ToString(), de.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, Entry.Value for current Entry.Key is different in dictionary", i)); } if (String.Compare(hd[en.Key.ToString()].ToString(), en.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, En-tor.Value for current En-tor.Key is different in dictionary", i)); } // while we didn't MoveNext, Current should return the same value DictionaryEntry curr1 = (DictionaryEntry)en.Current; if (!curr.Equals(curr1)) { Assert.False(true, string.Format("Error, second call of Current returned different result", i)); } DictionaryEntry de1 = en.Entry; if (!de.Equals(de1)) { Assert.False(true, string.Format("Error, second call of Entry returned different result", i)); } } // next MoveNext should bring us outside of the collection // res = en.MoveNext(); res = en.MoveNext(); if (res) { Assert.False(true, string.Format("Error, MoveNext returned true")); } // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // Attempt to get Entry should result in exception // Assert.Throws<InvalidOperationException>(() => { de = en.Entry; }); // // Attempt to get Key should result in exception // Assert.Throws<InvalidOperationException>(() => { k = en.Key; }); // // Attempt to get Value should result in exception // Assert.Throws<InvalidOperationException>(() => { v = en.Value; }); en.Reset(); // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // Attempt to get Entry should result in exception // Assert.Throws<InvalidOperationException>(() => { de = en.Entry; }); // --------------------------------------------------------- // [] Enumerator for Filled dictionary - hashtable // --------------------------------------------------------- // for (int i = 0; i < valuesLong.Length; i++) { hd.Add(keysLong[i], valuesLong[i]); } en = hd.GetEnumerator(); type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } // // MoveNext should return true // for (int i = 0; i < hd.Count; i++) { res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false", i)); } curr = (DictionaryEntry)en.Current; de = en.Entry; // //enumerator enumerates in different than added order // so we'll check Contains // if (!hd.Contains(curr.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (!hd.Contains(en.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (!hd.Contains(de.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain Entry.Key from enumerator", i)); } if (String.Compare(hd[curr.Key.ToString()].ToString(), curr.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, Value for current Key is different in dictionary", i)); } if (String.Compare(hd[de.Key.ToString()].ToString(), de.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, Entry.Value for current Entry.Key is different in dictionary", i)); } if (String.Compare(hd[en.Key.ToString()].ToString(), en.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, En-tor.Value for current En-tor.Key is different in dictionary", i)); } // while we didn't MoveNext, Current should return the same value DictionaryEntry curr1 = (DictionaryEntry)en.Current; if (!curr.Equals(curr1)) { Assert.False(true, string.Format("Error, second call of Current returned different result", i)); } DictionaryEntry de1 = en.Entry; if (!de.Equals(de1)) { Assert.False(true, string.Format("Error, second call of Entry returned different result", i)); } } // next MoveNext should bring us outside of the collection // res = en.MoveNext(); res = en.MoveNext(); if (res) { Assert.False(true, string.Format("Error, MoveNext returned true")); } // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // Attempt to get Entry should result in exception // Assert.Throws<InvalidOperationException>(() => { de = en.Entry; }); // // Attempt to get Key should result in exception // Assert.Throws<InvalidOperationException>(() => { k = en.Key; }); // // Attempt to get Value should result in exception // Assert.Throws<InvalidOperationException>(() => { v = en.Value; }); en.Reset(); // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // Attempt to get Entry should result in exception // Assert.Throws<InvalidOperationException>(() => { de = en.Entry; }); //========================================================= // -------------------------------------------------------- // // Modify dictionary when enumerating // // [] Enumerator and short modified HD (list) // hd.Clear(); if (hd.Count < 1) { for (int i = 0; i < valuesShort.Length; i++) { hd.Add(keysShort[i], valuesShort[i]); } } en = hd.GetEnumerator(); res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false")); } curr = (DictionaryEntry)en.Current; de = en.Entry; k = en.Key; v = en.Value; int cnt = hd.Count; hd.Remove(keysShort[0]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item DictionaryEntry curr2 = (DictionaryEntry)en.Current; if (!curr.Equals(curr2)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // will return just removed item DictionaryEntry de2 = en.Entry; if (!de.Equals(de2)) { Assert.False(true, string.Format("Error, Entry returned different value after modification")); } // will return just removed item Object k2 = en.Key; if (!k.Equals(k2)) { Assert.False(true, string.Format("Error, Key returned different value after modification")); } // will return just removed item Object v2 = en.Value; if (!v.Equals(v2)) { Assert.False(true, string.Format("Error, Value returned different value after modification")); } // exception expected Assert.Throws<InvalidOperationException>(() => { res = en.MoveNext(); }); // ========================================================== // // // [] Enumerator and long modified HD (hashtable) // hd.Clear(); if (hd.Count < 1) { for (int i = 0; i < valuesLong.Length; i++) { hd.Add(keysLong[i], valuesLong[i]); } } en = hd.GetEnumerator(); res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false")); } curr = (DictionaryEntry)en.Current; de = en.Entry; k = en.Key; v = en.Value; cnt = hd.Count; hd.Remove(keysLong[0]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item DictionaryEntry curr3 = (DictionaryEntry)en.Current; if (!curr.Equals(curr3)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // will return just removed item DictionaryEntry de3 = en.Entry; if (!de.Equals(de3)) { Assert.False(true, string.Format("Error, Entry returned different value after modification")); } // will return just removed item Object k3 = en.Key; if (!k.Equals(k3)) { Assert.False(true, string.Format("Error, Key returned different value after modification")); } // will return just removed item Object v3 = en.Value; if (!v.Equals(v3)) { Assert.False(true, string.Format("Error, Value returned different value after modification")); } // exception expected Assert.Throws<InvalidOperationException>(() => { res = en.MoveNext(); }); // // Modify collection when enumerated beyond the end // // [] Enumerator and short HD (list) modified after enumerating beyond the end // hd.Clear(); for (int i = 0; i < valuesShort.Length; i++) { hd.Add(keysShort[i], valuesShort[i]); } en = hd.GetEnumerator(); for (int i = 0; i < hd.Count; i++) { en.MoveNext(); } curr = (DictionaryEntry)en.Current; de = en.Entry; k = en.Key; v = en.Value; cnt = hd.Count; hd.Remove(keysShort[0]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item DictionaryEntry curr4 = (DictionaryEntry)en.Current; if (!curr.Equals(curr4)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // will return just removed item DictionaryEntry de4 = en.Entry; if (!de.Equals(de4)) { Assert.False(true, string.Format("Error, Entry returned different value after modification")); } // will return just removed item Object k4 = en.Key; if (!k.Equals(k4)) { Assert.False(true, string.Format("Error, Key returned different value after modification")); } // will return just removed item Object v4 = en.Value; if (!v.Equals(v4)) { Assert.False(true, string.Format("Error, Value returned different value after modification")); } // exception expected Assert.Throws<InvalidOperationException>(() => { res = en.MoveNext(); }); // // [] Enumerator and long HD (hashtable) modified after enumerating beyond the end hd.Clear(); for (int i = 0; i < valuesLong.Length; i++) { hd.Add(keysLong[i], valuesLong[i]); } en = hd.GetEnumerator(); for (int i = 0; i < hd.Count; i++) { en.MoveNext(); } curr = (DictionaryEntry)en.Current; de = en.Entry; k = en.Key; v = en.Value; cnt = hd.Count; hd.Remove(keysLong[0]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item DictionaryEntry curr5 = (DictionaryEntry)en.Current; if (!curr.Equals(curr5)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // will return just removed item DictionaryEntry de5 = en.Entry; if (!de.Equals(de5)) { Assert.False(true, string.Format("Error, Entry returned different value after modification")); } // will return just removed item Object k5 = en.Key; if (!k.Equals(k5)) { Assert.False(true, string.Format("Error, Key returned different value after modification")); } // will return just removed item Object v5 = en.Value; if (!v.Equals(v5)) { Assert.False(true, string.Format("Error, Value returned different value after modification")); } // exception expected Assert.Throws<InvalidOperationException>(() => { res = en.MoveNext(); }); // --------------------------------------------------------- // [] Enumerator for empty case-insensitive dictionary // --------------------------------------------------------- // hd = new HybridDictionary(true); en = hd.GetEnumerator(); type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } }
public void Test01() { HybridDictionary hd; const int BIG_LENGTH = 100; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int len; for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); if (hd == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", hd.Count)); } if (hd["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } System.Collections.ICollection keys = hd.Keys; if (keys.Count != 0) { Assert.False(true, string.Format("Error, Keys contains {0} keys after default ctor", keys.Count)); } System.Collections.ICollection values = hd.Values; if (values.Count != 0) { Assert.False(true, string.Format("Error, Values contains {0} items after default ctor", values.Count)); } // // [] Add(string, string) // hd.Add("Name", "Value"); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } if (String.Compare(hd["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // by default should be case sensitive if (hd["NAME"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value for uppercase key")); } // // [] Clear() // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } if (hd["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] numerous Add(string, string) // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value", i)); } if (hd[keysLong[i].ToUpper()] != null) { Assert.False(true, string.Format("Error, Item() returned non-null for uppercase key", i)); } } // // [] Clear() for dictionary with multiple entries // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } // // [] few elements not overriding Equals() // hd.Clear(); Hashtable[] lbls = new Hashtable[2]; ArrayList[] bs = new ArrayList[2]; lbls[0] = new Hashtable(); lbls[1] = new Hashtable(); bs[0] = new ArrayList(); bs[1] = new ArrayList(); hd.Add(lbls[0], bs[0]); hd.Add(lbls[1], bs[1]); if (hd.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", hd.Count)); } if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } if (!hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, doesn't contain 2nd special item")); } if (hd.Values.Count != 2) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of 2", hd.Values.Count)); } hd.Remove(lbls[1]); if (hd.Count != 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, failed to remove special item")); } // // [] many elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[BIG_LENGTH]; bs = new ArrayList[BIG_LENGTH]; for (int i = 0; i < BIG_LENGTH; i++) { lbls[i] = new Hashtable(); bs[i] = new ArrayList(); hd.Add(lbls[i], bs[i]); } if (hd.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, BIG_LENGTH)); } for (int i = 0; i < BIG_LENGTH; i++) { if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } } if (hd.Values.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of {1}", hd.Values.Count, BIG_LENGTH)); } hd.Remove(lbls[0]); if (hd.Count != BIG_LENGTH - 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, failed to remove special item")); } }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); // [] Remove() on empty dictionary // cnt = hd.Count; try { hd.Remove(null); Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } cnt = hd.Count; hd.Remove("some_string"); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } cnt = hd.Count; hd.Remove(new Hashtable()); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } // // [] Remove() on short dictionary with simple strings // cnt = hd.Count; int len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysShort[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysShort[i])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } // remove second time hd.Remove(keysShort[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed when Remove() second time", i)); } } // [] Remove() on long dictionary with simple strings // hd.Clear(); cnt = hd.Count; len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysLong[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } // remove second time hd.Remove(keysLong[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed when Remove() second time", i)); } } // // [] Remove() on long dictionary with Intl strings // Intl strings // len = valuesLong.Length; string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; hd.Remove(intlValues[i + len]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // [] Remove() on short dictionary with Intl strings // len = valuesShort.Length; hd.Clear(); cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; hd.Remove(intlValues[i + len]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // Case sensitivity // [] Case sensitivity - hashtable // len = valuesLong.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToUpper())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = hd.Count; hd.Remove(keysLong[i].ToLower()); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, failed: removed item using lowercase key", i)); } } // // [] Case sensitivity - list len = valuesShort.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToUpper())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = hd.Count; hd.Remove(keysLong[i].ToLower()); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, failed: removed item using lowercase key", i)); } } // // [] Remove() on case-insensitive HD - list // hd = new HybridDictionary(true); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToLower())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Remove() on case-insensitive HD - hashtable // hd = new HybridDictionary(true); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToLower())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Remove(null) from filled HD - list // hd = new HybridDictionary(); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } try { hd.Remove(null); Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // // [] Remove(null) from filled HD - hashtable // hd = new HybridDictionary(); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } try { hd.Remove(null); Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // // [] Remove(special_object) from filled HD - list // hd = new HybridDictionary(); hd.Clear(); len = 2; ArrayList[] b = new ArrayList[len]; Hashtable[] lbl = new Hashtable[len]; for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(lbl[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove special object")); } if (hd.Contains(lbl[i])) { Assert.False(true, string.Format("Error, removed wrong special object")); } } // // [] Remove(special_object) from filled HD - hashtable // hd = new HybridDictionary(); hd.Clear(); len = 40; b = new ArrayList[len]; lbl = new Hashtable[len]; for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(lbl[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove special object")); } if (hd.Contains(lbl[i])) { Assert.False(true, string.Format("Error, removed wrong special object")); } } }
public void Test01() { HybridDictionary hd; const int BIG_LENGTH = 100; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int len; for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- // [] Capacity 0, Case-sensitive ctor hd = new HybridDictionary(0, false); if (hd == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", hd.Count)); } if (hd["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } // // [] Add(string, string) // // should be able to add keys that differ only in casing hd.Add("Name", "Value"); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } if (String.Compare(hd["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } hd.Add("NaMe", "Value"); if (hd.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", hd.Count)); } if (String.Compare(hd["NaMe"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // by default should be case sensitive if (hd["NAME"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value for uppercase key")); } // // [] Clear() short dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } if (hd["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] numerous Add(string, string) // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value", i)); } if (hd[keysLong[i].ToUpper()] != null) { Assert.False(true, string.Format("Error, Item() returned non-null for uppercase key", i)); } } // // [] Clear() long dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } // // [] few elements not overriding Equals() // hd.Clear(); Hashtable[] lbls = new Hashtable[2]; ArrayList[] bs = new ArrayList[2]; lbls[0] = new Hashtable(); lbls[1] = new Hashtable(); bs[0] = new ArrayList(); bs[1] = new ArrayList(); hd.Add(lbls[0], bs[0]); hd.Add(lbls[1], bs[1]); if (hd.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", hd.Count)); } if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } if (!hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, doesn't contain 2nd special item")); } if (hd.Values.Count != 2) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of 2", hd.Values.Count)); } hd.Remove(lbls[1]); if (hd.Count != 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, failed to remove special item")); } // // [] many elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[BIG_LENGTH]; bs = new ArrayList[BIG_LENGTH]; for (int i = 0; i < BIG_LENGTH; i++) { lbls[i] = new Hashtable(); bs[i] = new ArrayList(); hd.Add(lbls[i], bs[i]); } if (hd.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, BIG_LENGTH)); } for (int i = 0; i < BIG_LENGTH; i++) { if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } } if (hd.Values.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of {1}", hd.Values.Count, BIG_LENGTH)); } hd.Remove(lbls[0]); if (hd.Count != BIG_LENGTH - 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, failed to remove special item")); } // ---------------------------------------------------------------- //----------------------------------------------------------------- // [] Capacity 10 - Case-sensitive hd = new HybridDictionary(10, false); if (hd == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", hd.Count)); } if (hd["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } // // [] Add(string, string) // // should be able to add keys that differ only in casing hd.Add("Name", "Value"); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } if (String.Compare(hd["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } hd.Add("NaMe", "Value"); if (hd.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", hd.Count)); } if (String.Compare(hd["NaMe"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // by default should be case sensitive if (hd["NAME"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value for uppercase key")); } // // [] Clear() short dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } if (hd["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] numerous Add(string, string) // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value", i)); } if (hd[keysLong[i].ToUpper()] != null) { Assert.False(true, string.Format("Error, Item() returned non-null for uppercase key", i)); } } // // [] Clear() long dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } // // [] few elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[2]; bs = new ArrayList[2]; lbls[0] = new Hashtable(); lbls[1] = new Hashtable(); bs[0] = new ArrayList(); bs[1] = new ArrayList(); hd.Add(lbls[0], bs[0]); hd.Add(lbls[1], bs[1]); if (hd.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", hd.Count)); } if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } if (!hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, doesn't contain 2nd special item")); } if (hd.Values.Count != 2) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of 2", hd.Values.Count)); } hd.Remove(lbls[1]); if (hd.Count != 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, failed to remove special item")); } // // [] many elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[BIG_LENGTH]; bs = new ArrayList[BIG_LENGTH]; for (int i = 0; i < BIG_LENGTH; i++) { lbls[i] = new Hashtable(); bs[i] = new ArrayList(); hd.Add(lbls[i], bs[i]); } if (hd.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, BIG_LENGTH)); } for (int i = 0; i < BIG_LENGTH; i++) { if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } } if (hd.Values.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of {1}", hd.Values.Count, BIG_LENGTH)); } hd.Remove(lbls[0]); if (hd.Count != BIG_LENGTH - 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, failed to remove special item")); } // --------------------------------------------------------------- //---------------------------------------------------------------- // [] Capacity 100 - case-sensitive hd = new HybridDictionary(100, false); if (hd == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", hd.Count)); } if (hd["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } // // [] Add(string, string) // // should be able to add keys that differ only in casing hd.Add("Name", "Value"); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } if (String.Compare(hd["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } hd.Add("NaMe", "Value"); if (hd.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", hd.Count)); } if (String.Compare(hd["NaMe"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // by default should be case sensitive if (hd["NAME"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value for uppercase key")); } // // [] Clear() short dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } if (hd["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] numerous Add(string, string) // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value", i)); } if (hd[keysLong[i].ToUpper()] != null) { Assert.False(true, string.Format("Error, Item() returned non-null for uppercase key", i)); } } // // [] Clear() long dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } // // [] few elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[2]; bs = new ArrayList[2]; lbls[0] = new Hashtable(); lbls[1] = new Hashtable(); bs[0] = new ArrayList(); bs[1] = new ArrayList(); hd.Add(lbls[0], bs[0]); hd.Add(lbls[1], bs[1]); if (hd.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", hd.Count)); } if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } if (!hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, doesn't contain 2nd special item")); } if (hd.Values.Count != 2) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of 2", hd.Values.Count)); } hd.Remove(lbls[1]); if (hd.Count != 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[1])) { Assert.False(true, string.Format("Error, failed to remove special item")); } // // [] many elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[BIG_LENGTH]; bs = new ArrayList[BIG_LENGTH]; for (int i = 0; i < BIG_LENGTH; i++) { lbls[i] = new Hashtable(); bs[i] = new ArrayList(); hd.Add(lbls[i], bs[i]); } if (hd.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, BIG_LENGTH)); } for (int i = 0; i < BIG_LENGTH; i++) { if (!hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } } if (hd.Values.Count != BIG_LENGTH) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of {1}", hd.Values.Count, BIG_LENGTH)); } hd.Remove(lbls[0]); if (hd.Count != BIG_LENGTH - 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (hd.Contains(lbls[0])) { Assert.False(true, string.Format("Error, failed to remove special item")); } // **************************************************************// ///// Case-insensitive ctor /////////////////////////////////////// // [] Capacity 0 - Case-insensitive ctor hd = new HybridDictionary(0, true); if (hd == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", hd.Count)); } if (hd["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } // // [] Add(string, string) // hd.Add("Name", "Value"); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } if (String.Compare(hd["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // should not allow keys that differ only in casing Assert.Throws<ArgumentException>(() => { hd.Add("NaMe", "vl"); }); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } // we created case-insensitive - should find this key if (String.Compare(hd["NAME"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value for uppercase key")); } // // [] Clear() short dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } if (hd["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] numerous Add(string, string) // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value", i)); } // should have case-insensitive dictionary if (String.Compare(hd[keysLong[i].ToUpper()].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value for uppercase key", i)); } } // // [] Clear() long dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } // // [] few elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[2]; bs = new ArrayList[2]; lbls[0] = new Hashtable(); lbls[1] = new Hashtable(); bs[0] = new ArrayList(); bs[1] = new ArrayList(); hd.Add(lbls[0], bs[0]); // should get ArgumentException here Assert.Throws<ArgumentException>(() => { hd.Add(lbls[1], bs[1]); }); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } // --------------------------------------------------------------- // --------------------------------------------------------------- // [] Capacity 10 - case-insensitive hd = new HybridDictionary(10, true); if (hd == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", hd.Count)); } if (hd["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } // // [] Add(string, string) // hd.Add("Name", "Value"); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } if (String.Compare(hd["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // should not allow keys that differ only in casing Assert.Throws<ArgumentException>(() => { hd.Add("NaMe", "vl"); }); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } // we created case-insensitive - should find this key if (String.Compare(hd["NAME"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value for uppercase key")); } // // [] Clear() short dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } if (hd["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] numerous Add(string, string) // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value", i)); } // should have case-insensitive dictionary if (String.Compare(hd[keysLong[i].ToUpper()].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value for uppercase key", i)); } } // // [] Clear() long dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } // // [] few elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[2]; bs = new ArrayList[2]; lbls[0] = new Hashtable(); lbls[1] = new Hashtable(); bs[0] = new ArrayList(); bs[1] = new ArrayList(); hd.Add(lbls[0], bs[0]); // should get ArgumentException here Assert.Throws<ArgumentException>(() => { hd.Add(lbls[1], bs[1]); }); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } // --------------------------------------------------------------- // --------------------------------------------------------------- // [] Capacity 100, case-insensitive hd = new HybridDictionary(100, true); if (hd == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", hd.Count)); } if (hd["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } // // [] Add(string, string) // hd.Add("Name", "Value"); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } if (String.Compare(hd["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // should not allow keys that differ only in casing Assert.Throws<ArgumentException>(() => { hd.Add("NaMe", "vl"); }); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } // we created case-insensitive - should find this key if (String.Compare(hd["NAME"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value for uppercase key")); } // // [] Clear() // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } if (hd["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] numerous Add(string, string) // len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value", i)); } // should have case-insensitive dictionary if (String.Compare(hd[keysLong[i].ToUpper()].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value for uppercase key", i)); } } // // [] Clear() long dictionary // hd.Clear(); if (hd.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", hd.Count)); } // // [] few elements not overriding Equals() // hd.Clear(); lbls = new Hashtable[2]; bs = new ArrayList[2]; lbls[0] = new Hashtable(); lbls[1] = new Hashtable(); bs[0] = new ArrayList(); bs[1] = new ArrayList(); hd.Add(lbls[0], bs[0]); // should get ArgumentException here Assert.Throws<ArgumentException>(() => { hd.Add(lbls[1], bs[1]); }); if (hd.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", hd.Count)); } }
// Get all namespaces in the local or scope from the last parent private IDictionary GetNamespacesInScope(NamespaceScope scope) { int i = 0; switch (scope) { case NamespaceScope.All: i = 0; break; case NamespaceScope.Local: i = _lastDecl; int lastScopeCount = _nsDeclarations[i].ScopeCount; while (_nsDeclarations[i].ScopeCount == lastScopeCount) i--; i++; break; } HybridDictionary dict = new HybridDictionary(_lastDecl -i + 1); for (; i < _lastDecl; i++) { string prefix = _nsDeclarations[i].Prefix; string xmlNamespace = _nsDeclarations[i].Uri; Debug.Assert(prefix != null); if (xmlNamespace.Length > 0 || prefix.Length > 0) { dict[prefix] = xmlNamespace; } else { // default namespace redeclared to "" -> remove from list dict.Remove(prefix); } } return dict; }
private static void ReleaseInstanceDataForTriggerBinding( BindingBase binding, HybridDictionary instanceValues, EventHandler<BindingValueChangedEventArgs> handler) { BindingExpressionBase bindingExpr = (BindingExpressionBase)instanceValues[binding]; if (bindingExpr != null) { bindingExpr.ValueChanged -= handler; bindingExpr.Detach(); instanceValues.Remove(binding); } }
// // This method // 1. Adds or removes per-instance state on the container/child (push model) // 2. Processes a single value that needs per-instance storage // internal static void ProcessInstanceValue( DependencyObject target, int childIndex, HybridDictionary instanceValues, DependencyProperty dp, int i, bool apply) { // If we get this far, it's because there's a value // in the property value list of an active style that requires // per-instance storage. The initialization (CreateInstaceData) // should have created the InstanceValues hashtable by now. Debug.Assert(instanceValues != null, "InstanceValues hashtable should have been created at initialization time."); InstanceValueKey key = new InstanceValueKey(childIndex, dp.GlobalIndex, i); if (apply) { // Store a sentinel value in per-instance StyleData. // The actual value is created only on demand. instanceValues[key] = NotYetApplied; } else { // Remove the instance value from the table object value = instanceValues[key]; instanceValues.Remove(key); Expression expr; Freezable freezable; if ((expr = value as Expression)!= null) { // if the instance value is an expression, detach it expr.OnDetach(target, dp); } else if ((freezable = value as Freezable)!= null) { // if the instance value is a Freezable, remove its // inheritance context target.RemoveSelfAsInheritanceContext(freezable, dp); } } }
// Updated 2004-01-29 by CraigAndera public IEnumerable AllTopicsForNewsletter(AbsoluteTopicName newsletter) { // HybridDictionary switches between using a ListDictionary and a Hashtable // depending on the size of the collection - should be a good choice since we don't // know how big the collection of topic names will be HybridDictionary answer = new HybridDictionary(); ContentBase cb = ContentBaseForNewsletter(newsletter); foreach (string s in TheFederation.GetTopicListPropertyValue(newsletter, "Topics")) { // If the wildcard appears, ignore all the other topics listed - include every topic if (s == "*") { answer.Clear(); foreach (AbsoluteTopicName atn in cb.AllTopics(false)) { answer.Add(atn.Fullname, atn); } // No need to continue iterating after we find the wildcard break; } else { RelativeTopicName rel = new RelativeTopicName(s); foreach (AbsoluteTopicName atn in cb.AllAbsoluteTopicNamesThatExist(rel)) { answer.Add(atn.Fullname, atn); } } } // Now we need to remove any topics that appear in the Exclude field foreach (string s in TheFederation.GetTopicListPropertyValue(newsletter, "Exclude")) { RelativeTopicName rel = new RelativeTopicName(s); foreach (AbsoluteTopicName atn in cb.AllAbsoluteTopicNamesThatExist(rel)) { answer.Remove(atn.Fullname); } } // Do the same for "Excludes", since it's hard to remember which one to use foreach (string s in TheFederation.GetTopicListPropertyValue(newsletter, "Excludes")) { RelativeTopicName rel = new RelativeTopicName(s); foreach (AbsoluteTopicName atn in cb.AllAbsoluteTopicNamesThatExist(rel)) { answer.Remove(atn.Fullname); } } return answer.Values; }
void IToolboxService.ResetToolbox() { IMxUIService service = (IMxUIService) this._serviceProvider.GetService(typeof(IMxUIService)); if (service.ShowMessage("All toolbox customizations in all sections will be lost.\r\nAre you sure you want to continue?", "Reset Toolbox", MessageBoxIcon.Exclamation, MessageBoxButtons.YesNo, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { IToolboxService service2 = this; ICollection is2 = this.ReadToolboxFromConfig(); IDictionary dictionary = new HybridDictionary(true); foreach (ToolboxSection section in this._sections) { dictionary[section.Name] = section; } foreach (ToolboxSection section2 in is2) { ToolboxSection section3 = (ToolboxSection) dictionary[section2.Name]; if (section3 != null) { section3.ClearToolboxDataItems(); foreach (ToolboxDataItem item in section2.ToolboxDataItems) { section3.AddToolboxDataItem(item, false); } section3.FireItemsChanged(); dictionary.Remove(section2.Name); continue; } service2.AddToolboxSection(section2); } bool flag = false; foreach (ToolboxSection section4 in dictionary.Values) { if (section4 == service2.ActiveSection) { flag = true; } service2.RemoveToolboxSection(section4); } if (flag) { if (this._sections.Count > 0) { service2.ActiveSection = (ToolboxSection) this._sections[0]; } else { service2.ActiveSection = null; } } } }
private ICollection GetCollectionDelta(ICollection original, ICollection modified) { if (((original != null) && (modified != null)) && (original.Count != 0)) { IEnumerator enumerator = modified.GetEnumerator(); if (enumerator != null) { IDictionary dictionary = new HybridDictionary(); foreach (object obj2 in original) { if (dictionary.Contains(obj2)) { int num = (int) dictionary[obj2]; dictionary[obj2] = ++num; } else { dictionary.Add(obj2, 1); } } ArrayList list = null; for (int i = 0; (i < modified.Count) && enumerator.MoveNext(); i++) { object current = enumerator.Current; if (dictionary.Contains(current)) { if (list == null) { list = new ArrayList(); enumerator.Reset(); for (int j = 0; (j < i) && enumerator.MoveNext(); j++) { list.Add(enumerator.Current); } enumerator.MoveNext(); } int num4 = (int) dictionary[current]; if (--num4 == 0) { dictionary.Remove(current); } else { dictionary[current] = num4; } } else if (list != null) { list.Add(current); } } if (list != null) { return list; } } } return modified; }