Clear() public method

public Clear ( ) : void
return void
示例#1
0
        /// <summary>Removes all items from the <see cref="Cache"/> object.</summary>
        public void Clear()
        {
            _rwl.AcquireWriterLock(LOCK_TIMEOUT_MSECS);
            try
            {
                if (_data.Count > 0)
                {
                    foreach (object key in _data.Keys)
                    {
                        if (key != null)
                        {
                            CacheCollectionElement element = _data[key] as CacheCollectionElement;
                            if (element != null)
                            {
                                element.IsCollectable = true;
                            }
                        }
                    }

                    _data.Clear();
                }
            }
            finally
            {
                _rwl.ReleaseWriterLock();
            }
        }
示例#2
0
		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)");
		}
示例#3
0
 public void CloseAllConnections()
 {
     lock (m_Connections)
     {
         // close all pending connections;
         foreach (s2hConnection conn in m_Connections.Values)
         {
             try
             {
                 conn.Close();
             }
             catch (Exception) {}
         }
         m_Connections.Clear();
     }
 }
示例#4
0
        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"));
            }
        }
示例#5
0
        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();

            // [] Contains on empty dictionary

            Assert.Throws<ArgumentNullException>(() => { hd.Contains(null); });


            if (hd.Contains("some_string"))
            {
                Assert.False(true, string.Format("Error, empty dictionary contains some_object"));
            }
            if (hd.Contains(new Hashtable()))
            {
                Assert.False(true, string.Format("Error, empty dictionary contains some_object"));
            }

            // [] simple strings and Contains()
            //

            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++)
            {
                if (!hd.Contains(keysShort[i]))
                {
                    Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysShort[i]));
                }
            }

            cnt = hd.Count;
            len = valuesLong.Length;
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }
            if (hd.Count != len + cnt)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len + cnt));
            }
            // verify new keys
            for (int i = 0; i < len; i++)
            {
                if (!hd.Contains(keysLong[i]))
                {
                    Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysLong[i]));
                }
            }
            // verify old keys
            for (int i = 0; i < valuesShort.Length; i++)
            {
                if (!hd.Contains(keysShort[i]))
                {
                    Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysShort[i]));
                }
            }


            //
            // [] Intl strings and Contains()
            //

            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;
            }


            hd.Clear();
            for (int i = 0; i < len; i++)
            {
                hd.Add(intlValues[i + len], intlValues[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++)
            {
                //
                if (!hd.Contains(intlValues[i + len]))
                {
                    Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, intlValues[i + len]));
                }
            }


            //
            // [] Case sensitivity
            // by default HybridDictionary is case-sensitive
            //


            hd.Clear();
            len = valuesLong.Length;
            //
            // will use first half of array as valuesShort and second half as keysShort
            //
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);     // adding uppercase strings
            }

            //
            for (int i = 0; i < len; i++)
            {
                // uppercase key
                if (!hd.Contains(keysLong[i]))
                {
                    Assert.False(true, string.Format("Error, doesn't contain added uppercase \"{1}\"", i, keysLong[i]));
                }

                // lowercase key
                if (hd.Contains(keysLong[i].ToUpper()))
                {
                    Assert.False(true, string.Format("Error, contains uppercase \"{1}\" - should not", i, keysLong[i].ToUpper()));
                }
            }

            //  [] different_in_casing_only keys and Contains()
            //

            hd.Clear();
            string[] ks = { "Key", "kEy", "keY" };
            len = ks.Length;
            for (int i = 0; i < len; i++)
            {
                hd.Add(ks[i], "Value" + i);
            }
            if (hd.Count != len)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len));
            }

            if (hd.Contains("Value0"))
            {
                Assert.False(true, string.Format("Error, returned true when should not"));
            }
            for (int i = 0; i < len; i++)
            {
                if (!hd.Contains(ks[i]))
                {
                    Assert.False(true, string.Format("Error, returned false when true expected", i));
                }
            }

            cnt = hd.Count;
            len = valuesLong.Length;
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }
            if (hd.Count != len + cnt)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len + cnt));
            }
            // verify new keys
            for (int i = 0; i < len; i++)
            {
                if (!hd.Contains(keysLong[i]))
                {
                    Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysLong[i]));
                }
            }
            // verify old keys
            for (int i = 0; i < ks.Length; i++)
            {
                if (!hd.Contains(ks[i]))
                {
                    Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, ks[i]));
                }
            }


            //
            //   [] Contains(null) - for filled dictionary
            //
            len = valuesShort.Length;
            hd.Clear();
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysShort[i], valuesShort[i]);
            }

            Assert.Throws<ArgumentNullException>(() => { hd.Contains(null); });

            // [] Contains() for case-insensitive comparer dictionary
            //

            hd = new HybridDictionary(true);
            hd.Clear();
            len = ks.Length;
            hd.Add(ks[0], "Value0");

            for (int i = 1; i < len; i++)
            {
                Assert.Throws<ArgumentException>(() => { hd.Add(ks[i], "Value" + i); });
            }
            if (hd.Count != 1)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, 1));
            }

            if (hd.Contains("Value0"))
            {
                Assert.False(true, string.Format("Error, returned true when should not"));
            }
            for (int i = 0; i < len; i++)
            {
                if (!hd.Contains(ks[i]))
                {
                    Assert.False(true, string.Format("Error, returned false when true expected", i));
                }
            }
            if (!hd.Contains("KEY"))
            {
                Assert.False(true, string.Format("Error, returned false non-existing-cased key"));
            }

            // [] few not_overriding_Equals objects and Contains()
            //

            hd = new HybridDictionary();
            hd.Clear();
            Hashtable[] lbl = new Hashtable[2];
            lbl[0] = new Hashtable();
            lbl[1] = new Hashtable();
            ArrayList[] b = new ArrayList[2];
            b[0] = new ArrayList();
            b[1] = new ArrayList();
            hd.Add(lbl[0], b[0]);
            hd.Add(lbl[1], b[1]);

            Assert.Throws<ArgumentException>(() => { hd.Add(lbl[0], "Hello"); });

            if (hd.Count != 2)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, 2));
            }

            if (!hd.Contains(lbl[0]))
            {
                Assert.False(true, string.Format("Error, returned false when true expected"));
            }
            if (!hd.Contains(lbl[1]))
            {
                Assert.False(true, string.Format("Error, returned false when true expected"));
            }
            if (hd.Contains(new Hashtable()))
            {
                Assert.False(true, string.Format("Error, returned true when false expected"));
            }

            // [] many not_overriding_Equals objects and Contains()
            //

            hd = new HybridDictionary();
            hd.Clear();
            int num = 40;
            lbl = new Hashtable[num];
            b = new ArrayList[num];
            for (int i = 0; i < num; i++)
            {
                lbl[i] = new Hashtable();
                b[i] = new ArrayList();
                hd.Add(lbl[i], b[i]);
            }

            Assert.Throws<ArgumentException>(() => { hd.Add(lbl[0], "Hello"); });


            if (hd.Count != num)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, num));
            }

            for (int i = 0; i < num; i++)
            {
                if (!hd.Contains(lbl[i]))
                {
                    Assert.False(true, string.Format("Error, returned false when true expected", i));
                }
            }
            if (hd.Contains(new Hashtable()))
            {
                Assert.False(true, string.Format("Error, returned true when false expected"));
            }

            // [] few not_overriding_Equals structs and Contains()

            hd = new HybridDictionary();
            SpecialStruct s = new SpecialStruct();
            s.Num = 1;
            s.Wrd = "one";
            SpecialStruct s1 = new SpecialStruct();
            s1.Num = 1;
            s1.Wrd = "one";
            hd.Add(s, "first");
            hd.Add(s1, "second");
            if (hd.Count != 2)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, 2));
            }
            if (!hd.Contains(s))
            {
                Assert.False(true, string.Format("Error, returned false when true expected"));
            }
            if (!hd.Contains(s1))
            {
                Assert.False(true, string.Format("Error, returned false when true expected"));
            }

            // [] many not_overriding_Equals structs and Contains()

            hd = new HybridDictionary();
            SpecialStruct[] ss = new SpecialStruct[num];
            for (int i = 0; i < num; i++)
            {
                ss[i] = new SpecialStruct();
                ss[i].Num = i;
                ss[i].Wrd = "value" + i;
                hd.Add(ss[i], "item" + i);
            }
            if (hd.Count != num)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, num));
            }
            for (int i = 0; i < num; i++)
            {
                if (!hd.Contains(ss[i]))
                {
                    Assert.False(true, string.Format("Error, returned false when true expected", i));
                }
            }
            s = new SpecialStruct();
            s.Num = 1;
            s.Wrd = "value1";
            if (hd.Contains(s))
            {
                Assert.False(true, string.Format("Error, returned true when false expected"));
            }
        }
示例#6
0
文件: Gamemap.cs 项目: sgf/Yupi
        /// <summary>
        ///     Removes from map.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="handleGameItem">if set to <c>true</c> [handle game item].</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        internal bool RemoveFromMap(RoomItem item, bool handleGameItem)
        {
            RemoveSpecialItem(item);
            if (_room.GotSoccer())
                _room.GetSoccer().OnGateRemove(item);
            bool result = false;
            foreach (Point current in item.GetCoords.Where(current => RemoveCoordinatedItem(item, current)))
                result = true;
            HybridDictionary hybridDictionary = new HybridDictionary();
            foreach (Point current2 in item.GetCoords)
            {
                int point = CoordinatesFormatter.PointToInt(current2);
                if (CoordinatedItems.Contains(point))
                {
                    List<RoomItem> value = (List<RoomItem>) CoordinatedItems[point];
                    if (!hybridDictionary.Contains(current2))
                        hybridDictionary.Add(current2, value);
                }
                SetDefaultValue(current2.X, current2.Y);
            }
            foreach (Point point2 in hybridDictionary.Keys)
            {
                List<RoomItem> list = (List<RoomItem>) hybridDictionary[point2];
                foreach (RoomItem current3 in list)
                    ConstructMapForItem(current3, point2);
            }
            if (GuildGates.ContainsKey(item.Coordinate))
                GuildGates.Remove(item.Coordinate);
            _room.GetRoomItemHandler().OnHeightMapUpdate(hybridDictionary.Keys);
            hybridDictionary.Clear();

            return result;
        }
示例#7
0
        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"));
            }
        }
示例#8
0
        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"));
                }
            }
        }
示例#9
0
    // 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;
    }
示例#10
0
        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 "));
            }
        }
示例#11
0
        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
            Object itm;         // Item

            // 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();

            // [] get Item() on empty dictionary
            //
            cnt = hd.Count;
            Assert.Throws<ArgumentNullException>(() => { itm = hd[null]; });

            cnt = hd.Count;
            itm = hd["some_string"];
            if (itm != null)
            {
                Assert.False(true, string.Format("Error, returned non-null for Item(some_string)"));
            }

            cnt = hd.Count;
            itm = hd[new Hashtable()];
            if (itm != null)
            {
                Assert.False(true, string.Format("Error, returned non-null for Item(some_object)"));
            }

            // [] get Item() 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;
                itm = hd[keysShort[i]];
                if (String.Compare(itm.ToString(), valuesShort[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item", i));
                }
            }

            // [] get Item() 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;
                itm = hd[keysLong[i]];
                if (String.Compare(itm.ToString(), valuesLong[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item", i));
                }
            }


            // [] get Item() 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;
                itm = hd[intlValues[i + len]];
                if (string.Compare(itm.ToString(), intlValues[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item", i));
                }
            }

            // [] get Item() 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;
                itm = hd[intlValues[i + len]];
                if (String.Compare(itm.ToString(), intlValues[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item", i));
                }
            }


            //
            // [] 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
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item", i));
                }
                if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) == 0)
                {
                    Assert.False(true, string.Format("Error, returned lowercase when added uppercase", 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
                itm = hd[keysLong[i].ToLower()];
                if (itm != null)
                {
                    Assert.False(true, string.Format("Error, returned non-null for 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
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item", i));
                }
                if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) == 0)
                {
                    Assert.False(true, string.Format("Error, returned lowercase when added uppercase", 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
                itm = hd[keysLong[i].ToLower()];
                if (itm != null)
                {
                    Assert.False(true, string.Format("Error, returned non-null for lowercase key", i));
                }
            }


            //
            // [] get Item() in 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++)
            {
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item for uppercase key", i));
                }
                itm = hd[keysLong[i].ToLower()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item - for lowercase key", i));
                }
            }

            // [] get Item() in 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++)
            {
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item for uppercase key", i));
                }
                itm = hd[keysLong[i].ToLower()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0)
                {
                    Assert.False(true, string.Format("Error, returned wrong item for lowercase key", i));
                }
            }


            //
            //   [] get Item(null) on filled HD - list
            //
            hd = new HybridDictionary();
            len = valuesShort.Length;
            hd.Clear();
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysShort[i], valuesShort[i]);
            }

            Assert.Throws<ArgumentNullException>(() => { itm = hd[null]; });

            //   [] get Item(null) on filled HD - hashtable
            //
            hd = new HybridDictionary();
            len = valuesLong.Length;
            hd.Clear();
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }

            Assert.Throws<ArgumentNullException>(() => { itm = hd[null]; });

            //
            //   [] get Item(cpecial_object) on 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++)
            {
                itm = hd[lbl[i]];
                if (!itm.Equals(b[i]))
                {
                    Assert.False(true, string.Format("Error, returned wrong special object"));
                }
            }

            //   [] get Item(cpecial_object) on 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++)
            {
                itm = hd[lbl[i]];
                if (!itm.Equals(b[i]))
                {
                    Assert.False(true, string.Format("Error, returned wrong special object"));
                }
            }
        }
示例#12
0
        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));
            }
        }
示例#13
0
        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
            //-----------------------------------------------------------------

            // [] simple strings

            hd = new HybridDictionary();


            for (int i = 0; i < valuesShort.Length; i++)
            {
                cnt = hd.Count;
                hd.Add(keysShort[i], valuesShort[i]);
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (String.Compare(hd[keysShort[i]].ToString(), valuesShort[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysShort[i]], valuesShort[i]));
                }
            }

            // increase the number of items
            for (int i = 0; i < valuesLong.Length; i++)
            {
                cnt = hd.Count;
                hd.Add(keysLong[i], valuesLong[i]);
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i]], valuesLong[i]));
                }
            }

            //
            // [] Intl strings
            //
            int len = valuesShort.Length;
            hd.Clear();
            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;
            }

            Boolean caseInsensitive = false;
            for (int i = 0; i < len * 2; i++)
            {
                if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper())
                    caseInsensitive = true;
            }


            //
            // will use first half of array as valuesShort and second half as keysShort
            //
            for (int i = 0; i < len; i++)
            {
                cnt = hd.Count;

                hd.Add(intlValues[i + len], intlValues[i]);
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (String.Compare(hd[intlValues[i + len]].ToString(), intlValues[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[intlValues[i + len]], intlValues[i]));
                }
            }

            // increase the number of items
            for (int i = 0; i < valuesLong.Length; i++)
            {
                cnt = hd.Count;
                hd.Add(keysLong[i], intlValues[1]);
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (String.Compare(hd[keysLong[i]].ToString(), intlValues[1]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i]], intlValues[1]));
                }
            }

            //
            // [] Case sensitivity
            // Casing doesn't change ( keysShort are not converted to lower!)
            //
            string[] intlValuesLower = new string[len * 2];

            // fill array with unique strings
            //
            for (int i = 0; i < len * 2; i++)
            {
                intlValues[i] = intlValues[i].ToUpper();
            }

            for (int i = 0; i < len * 2; i++)
            {
                intlValuesLower[i] = intlValues[i].ToLower();
            }

            hd.Clear();
            //
            // will use first half of array as valuesShort and second half as keysShort
            //
            for (int i = 0; i < len; i++)
            {
                cnt = hd.Count;

                hd.Add(intlValues[i + len], intlValues[i]);
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (hd[intlValues[i + len]] == null)
                {
                    Assert.False(true, string.Format("Error, returned null", i));
                }
                else
                {
                    if (!hd[intlValues[i + len]].Equals(intlValues[i]))
                    {
                        Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[intlValues[i + len]], intlValues[i]));
                    }
                }

                // verify that dictionary doesn't contains lowercase item
                //
                if (!caseInsensitive && hd[intlValuesLower[i + len]] != null)
                {
                    Assert.False(true, string.Format("Error, returned non-null", i));
                }
            }

            //
            //   [] Add multiple valuesShort with the same key
            //   Add multiple valuesShort with the same key - ArgumentException expected
            //

            hd.Clear();
            len = valuesShort.Length;
            string k = "keykey";
            hd.Add(k, "value");
            Assert.Throws<ArgumentException>(() => { hd.Add(k, "newvalue"); });

            //
            // [] Add null value
            //

            cnt = hd.Count;
            k = "kk";
            hd.Add(k, null);

            if (hd.Count != cnt + 1)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + 1));
            }

            // verify that dictionary contains null
            //
            if (hd[k] != null)
            {
                Assert.False(true, string.Format("Error, returned non-null on place of null"));
            }

            //
            // [] Add item with null key
            // Add item with null key - ArgumentNullException expected
            //

            cnt = hd.Count;
            Assert.Throws<ArgumentNullException>(() => { hd.Add(null, "item"); });

            //
            // [] Add duplicate values
            //
            hd.Clear();
            for (int i = 0; i < valuesShort.Length; i++)
            {
                cnt = hd.Count;
                hd.Add(keysShort[i], "value");
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (!hd[keysShort[i]].Equals("value"))
                {
                    Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysShort[i]], "value"));
                }
            }
            // verify Keys and Values

            if (hd.Keys.Count != valuesShort.Length)
            {
                Assert.False(true, string.Format("Error, Keys contains {0} instead of {1}", hd.Keys.Count, valuesShort.Length));
            }
            if (hd.Values.Count != valuesShort.Length)
            {
                Assert.False(true, string.Format("Error, Values contains {0} instead of {1}", hd.Values.Count, valuesShort.Length));
            }

            //
            //  [] add many simple strings
            //
            hd = new HybridDictionary();
            for (int i = 0; i < valuesLong.Length; i++)
            {
                cnt = hd.Count;
                hd.Add(keysLong[i], valuesLong[i]);
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0)
                {
                    Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i]], valuesLong[i]));
                }
            }

            // increase the number of items
            for (int i = 0; i < valuesLong.Length; i++)
            {
                cnt = hd.Count;
                hd.Add(keysLong[i] + "_", valuesLong[i] + i);
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1));
                }

                //  access the item
                //
                if (String.Compare(hd[keysLong[i] + "_"].ToString(), valuesLong[i] + i) != 0)
                {
                    Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i] + "_"], valuesLong[i] + i));
                }
            }
        }
示例#14
0
        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];

            // string [] destination;
            Array destination;

            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();

            // []  Copy empty dictionary into empty array
            //
            destination = Array.CreateInstance(typeof(Object), 0);

            Assert.Throws<ArgumentOutOfRangeException>(() => { hd.CopyTo(destination, -1); });

            hd.CopyTo(destination, 0);

            // exception even when copying empty dictionary
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, 1); });

            //  [] Copy empty dictionary into filled array
            //
            destination = Array.CreateInstance(typeof(Object), valuesShort.Length);
            for (int i = 0; i < valuesShort.Length; i++)
            {
                destination.SetValue(valuesShort[i], i);
            }
            hd.CopyTo(destination, 0);
            if (destination.Length != valuesShort.Length)
            {
                Assert.False(true, string.Format("Error, altered array after copying empty dictionary"));
            }
            if (destination.Length == valuesShort.Length)
            {
                for (int i = 0; i < valuesShort.Length; i++)
                {
                    if (String.Compare(destination.GetValue(i).ToString(), valuesShort[i]) != 0)
                    {
                        Assert.False(true, string.Format("Error, altered item {0} after copying empty dictionary", i));
                    }
                }
            }


            //  [] few simple strings and CopyTo(Array, 0)
            //

            hd.Clear();
            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));
            }

            destination = Array.CreateInstance(typeof(Object), len);
            hd.CopyTo(destination, 0);
            //
            //
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //

                if (String.Compare(hd[keysShort[i]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), hd[keysShort[i]]));
                }
                if (String.Compare(keysShort[i], ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), keysShort[i]));
                }
            }

            //  [] few simple strings and CopyTo(Array, middle_index)
            //


            hd.Clear();

            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, valuesShort.Length));
            }

            destination = Array.CreateInstance(typeof(Object), len * 2);
            hd.CopyTo(destination, len);

            //
            //
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                if (String.Compare(hd[keysShort[i]].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, hd[keysShort[i]]));
                }
                // verify keysShort
                if (String.Compare(keysShort[i], ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, keysShort[i]));
                }
            }

            //  [] many simple strings and CopyTo(Array, 0)
            //

            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));
            }

            destination = Array.CreateInstance(typeof(Object), len);
            hd.CopyTo(destination, 0);
            //
            //
            IDictionaryEnumerator en = hd.GetEnumerator();
            en.MoveNext();
            // items are copied in the same order they are enumerated
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;

                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), hd[k]));
                }
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), k.ToString()));
                }
                en.MoveNext();
            }

            //  [] many simple strings and CopyTo(Array, middle_index)
            //


            hd.Clear();

            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));
            }

            destination = Array.CreateInstance(typeof(Object), len * 2);
            hd.CopyTo(destination, len);

            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, hd[k]));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, k));
                }
                en.MoveNext();
            }

            //
            // [] many Intl strings and CopyTo(Array, 0)
            //

            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;
            }

            Boolean caseInsensitive = false;
            for (int i = 0; i < len * 2; i++)
            {
                if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper())
                    caseInsensitive = true;
            }

            hd.Clear();
            for (int i = 0; i < len; i++)
            {
                hd.Add(intlValues[i + len], intlValues[i]);
            }
            if (hd.Count != (len))
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len));
            }

            destination = Array.CreateInstance(typeof(Object), len);
            hd.CopyTo(destination, 0);
            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, hd[k]));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, k));
                }
                en.MoveNext();
            }


            //
            // [] many Intl strings and CopyTo(Array, middle_index)
            //


            destination = Array.CreateInstance(typeof(Object), len * 2);
            hd.CopyTo(destination, len);

            //
            // order of items is the same as they were in dictionary
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, hd[k]));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, k));
                }
                en.MoveNext();
            }

            // [] few Intl strings and CopyTo(Array, 0)
            //

            int len1 = valuesShort.Length;

            hd.Clear();
            for (int i = 0; i < len1; i++)
            {
                hd.Add(intlValues[i + len1], intlValues[i]);
            }
            if (hd.Count != (len1))
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len1));
            }

            destination = Array.CreateInstance(typeof(Object), len1);
            hd.CopyTo(destination, 0);
            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len1; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, hd[k]));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, k));
                }
                en.MoveNext();
            }

            //
            // [] few Intl strings and CopyTo(Array, middle_index)
            //


            destination = Array.CreateInstance(typeof(Object), len1 * 2);
            hd.CopyTo(destination, len1);

            //
            // order of items is the same as they were in dictionary
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len1; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i + len1)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len1)).Value, hd[k]));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i + len1)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len1)).Key, k));
                }
                en.MoveNext();
            }


            //
            // [] Case sensitivity
            //

            string[] intlValuesUpper = new string[len * 2];
            string[] intlValuesLower = new string[len * 2];

            // fill array with unique upper-case strings
            //
            for (int i = 0; i < len * 2; i++)
            {
                string val = intlValues[i].ToUpper();
                while (Array.IndexOf(intlValuesUpper, val) != -1)
                    val = intl.GetRandomString(MAX_LEN).ToUpper();
                intlValuesUpper[i] = val;
            }

            caseInsensitive = false;
            for (int i = 0; i < len * 2; i++)
            {
                intlValuesLower[i] = intlValuesUpper[i].ToLower();
                if (intlValuesLower[i].Length != 0 &&
            intlValuesLower[i] == intlValuesUpper[i])
                    caseInsensitive = true;
            }

            hd.Clear();
            //
            // will use first half of array as values and second half as keys
            //
            for (int i = 0; i < len; i++)
            {
                hd.Add(intlValuesUpper[i + len], intlValuesUpper[i]);     // adding uppercase strings
            }

            destination = Array.CreateInstance(typeof(Object), len);
            hd.CopyTo(destination, 0);

            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, hd[k]));
                }

                if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) > -1)
                {
                    Assert.False(true, string.Format("Error, copied lowercase string"));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0)
                {
                    Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, k));
                }

                if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) > -1)
                {
                    Assert.False(true, string.Format("Error, copied lowercase key"));
                }
                en.MoveNext();
            }

            //  ----------------------------------------------------------------
            //   [] Parameter validation for short HybridDictionary (list)
            //  ----------------------------------------------------------------

            hd = new HybridDictionary();
            for (int i = 0; i < len1; i++)
            {
                hd.Add(keysShort[i], valuesShort[i]);
            }
            //
            //   CopyTo(null, int)
            //
            destination = null;
            Assert.Throws<ArgumentNullException>(() => { hd.CopyTo(destination, 0); });

            //
            //   CopyTo(Array, -1)
            //

            destination = Array.CreateInstance(typeof(Object), 2);
            Assert.Throws<ArgumentOutOfRangeException>(() => { hd.CopyTo(destination, -1); });

            //
            //   CopyTo(Array, upperBound+1)
            //
            cnt = hd.Count;

            destination = Array.CreateInstance(typeof(Object), cnt);
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt); });

            //
            //   CopyTo(Array, upperBound+2)
            //
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt + 1); });

            //
            //   CopyTo(Array, not_enough_space)
            //
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt / 2); });

            //
            //   CopyTo(multidim_Array, 0)
            //

            Array dest = new String[cnt, cnt];
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(dest, 0); });

            //
            //   CopyTo(wrong_type, 0)
            //

            dest = Array.CreateInstance(typeof(ArrayList), cnt);
            Assert.Throws<System.InvalidCastException>(() => { hd.CopyTo(dest, 0); });
            //
            //   CopyTo(Array, upperBound+1) - copy empty dictionary - no exception
            //
            hd.Clear();

            destination = Array.CreateInstance(typeof(Object), len);
            hd.CopyTo(destination, len);


            //  ----------------------------------------------------------------
            //   [] Parameter validation for long HybridDictionary (hashtable)
            //  ----------------------------------------------------------------

            hd = new HybridDictionary();
            len = valuesLong.Length;
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }
            //
            //   CopyTo(null, int)
            //
            destination = null;
            Assert.Throws<ArgumentNullException>(() => { hd.CopyTo(destination, 0); });
            //
            //   CopyTo(Array, -1)
            //

            destination = Array.CreateInstance(typeof(Object), 2);
            Assert.Throws<ArgumentOutOfRangeException>(() => { hd.CopyTo(destination, -1); });

            //
            //   CopyTo(Array, upperBound+1)
            //
            cnt = hd.Count;

            destination = Array.CreateInstance(typeof(Object), cnt);
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt); });

            //
            //   CopyTo(Array, upperBound+2)
            //
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt + 1); });

            //
            //   CopyTo(Array, not_enough_space)
            //
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt / 2); });

            //
            //   CopyTo(multidim_Array, 0)
            //

            dest = Array.CreateInstance(typeof(string), cnt, cnt);
            Assert.Throws<ArgumentException>(() => { hd.CopyTo(dest, 0); });

            //
            //   CopyTo(wrong_type, 0)
            //

            dest = Array.CreateInstance(typeof(ArrayList), cnt);
            Assert.Throws<System.InvalidCastException>(() => { hd.CopyTo(dest, 0); });

            //
            //  [] CopyTo() for few not_overriding_Equals objects
            //

            hd.Clear();
            int num = 2;
            Hashtable[] lbl = new Hashtable[num];
            ArrayList[] b = new ArrayList[num];
            for (int i = 0; i < num; i++)
            {
                lbl[i] = new Hashtable();
                b[i] = new ArrayList();
                hd.Add(lbl[i], b[i]);
            }

            destination = Array.CreateInstance(typeof(Object), num);
            hd.CopyTo(destination, 0);
            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < num; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (!hd[k].Equals(((DictionaryEntry)destination.GetValue(i)).Value))
                {
                    Assert.False(true, string.Format("Error, failed to copy {0}th entry", i));
                }
                // verify keysShort
                if (!k.Equals(((DictionaryEntry)destination.GetValue(i)).Key))
                {
                    Assert.False(true, string.Format("Error, failed to copy {0} entry", i));
                }
                en.MoveNext();
            }

            //  [] CopyTo() for many not_overriding_Equals objects
            //
            hd.Clear();
            num = 40;
            lbl = new Hashtable[num];
            b = new ArrayList[num];
            for (int i = 0; i < num; i++)
            {
                lbl[i] = new Hashtable();
                b[i] = new ArrayList();
                hd.Add(lbl[i], b[i]);
            }

            destination = Array.CreateInstance(typeof(Object), num);
            hd.CopyTo(destination, 0);
            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < num; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (!hd[k].Equals(((DictionaryEntry)destination.GetValue(i)).Value))
                {
                    Assert.False(true, string.Format("Error, failed to copy {0}th entry", i));
                }
                // verify keysShort
                if (!k.Equals(((DictionaryEntry)destination.GetValue(i)).Key))
                {
                    Assert.False(true, string.Format("Error, failed to copy {0} entry", i));
                }
                en.MoveNext();
            }

            //
            //  [] CopyTo() - for short case-insensitive HybridDictionary
            //
            hd = new HybridDictionary(true);
            len = 3;
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }

            destination = Array.CreateInstance(typeof(Object), len);
            hd.CopyTo(destination, 0);
            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) < 0)
                {
                    Assert.False(true, string.Format("Error, failed to copy {0}th entry when case-insensitive", i));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) < 0)
                {
                    Assert.False(true, string.Format("Error, failed to copy {0} entry when case-insensitive", i));
                }
                en.MoveNext();
            }

            //  [] CopyTo() - for long case-insensitive HybridDictionary
            //
            hd = new HybridDictionary(true);
            len = valuesLong.Length;
            for (int i = 0; i < len; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }

            destination = Array.CreateInstance(typeof(Object), len);
            hd.CopyTo(destination, 0);
            //
            //
            en = hd.GetEnumerator();
            en.MoveNext();
            for (int i = 0; i < len; i++)
            {
                // verify that dictionary is copied correctly
                //
                Object k = en.Key;
                if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) < 0)
                {
                    Assert.False(true, string.Format("Error, failed to copy {0}th entry when case-insensitive", i));
                }
                // verify keysShort
                if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) < 0)
                {
                    Assert.False(true, string.Format("Error, failed to copy {0} entry when case-insensitive", i));
                }
                en.MoveNext();
            }
        }
示例#15
0
 internal bool RemoveFromMap(RoomItem item, bool handleGameItem)
 {
     if (handleGameItem)
     {
         this.RemoveSpecialItem(item);
     }
     if (this.room.GotSoccer())
     {
         this.room.GetSoccer().onGateRemove(item);
     }
     bool result = false;
     foreach (Point current in item.GetCoords)
     {
         if (this.RemoveCoordinatedItem(item, current))
         {
             result = true;
         }
     }
     HybridDictionary HybridDictionary = new HybridDictionary();
     foreach (Point current2 in item.GetCoords)
     {
         Point point = new Point(current2.X, current2.Y);
         if (this.mCoordinatedItems.Contains(point))
         {
             List<RoomItem> value = (List<RoomItem>)this.mCoordinatedItems[point];
             if (!HybridDictionary.Contains(current2))
             {
                 HybridDictionary.Add(current2, value);
             }
         }
         this.SetDefaultValue(current2.X, current2.Y);
     }
     foreach (Point point2 in HybridDictionary.Keys)
     {
         if (HybridDictionary.Contains(point2))
         {
             List<RoomItem> list = (List<RoomItem>)HybridDictionary[point2];
             foreach (RoomItem current3 in list)
             {
                 this.ConstructMapForItem(current3, point2);
             }
         }
     }
     room.GetRoomItemHandler().OnHeightmapUpdate(HybridDictionary.Keys);
     HybridDictionary.Clear();
     HybridDictionary = null;
     return result;
 }
示例#16
0
        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
            Object itm;         // Item

            // 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();

            // [] set Item() on empty dictionary
            //
            cnt = hd.Count;
            try
            {
                hd[null] = valuesShort[0];
                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["some_string"] = valuesShort[0];
            if (hd.Count != cnt + 1)
            {
                Assert.False(true, string.Format("Error, failed to add Item(some_string)"));
            }
            itm = hd["some_string"];
            if (String.Compare(itm.ToString(), valuesShort[0]) != 0)
            {
                Assert.False(true, string.Format("Error, added wrong Item(some_string)"));
            }

            cnt = hd.Count;
            Hashtable l = new Hashtable();
            ArrayList bb = new ArrayList();
            hd[l] = bb;
            if (hd.Count != cnt + 1)
            {
                Assert.False(true, string.Format("Error, failed to add Item(some_object)"));
            }
            itm = hd[l];
            if (!itm.Equals(bb))
            {
                Assert.False(true, string.Format("Error, returned wrong Item(some_object)"));
            }

            // [] set Item() on short dictionary with simple strings
            //

            hd.Clear();
            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[keysShort[i]] = valuesLong[0];
                if (hd.Count != cnt)
                {
                    Assert.False(true, string.Format("Error, added item instead of setting", i));
                }
                itm = hd[keysShort[i]];
                if (String.Compare(itm.ToString(), valuesLong[0]) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set item", i));
                }
            }
            // should add non-existing item
            cnt = hd.Count;
            hd[keysLong[0]] = valuesLong[0];
            if (hd.Count != cnt + 1)
            {
                Assert.False(true, string.Format("Error, didn't add non-existing item"));
            }
            itm = hd[keysLong[0]];
            if (String.Compare(itm.ToString(), valuesLong[0]) != 0)
            {
                Assert.False(true, string.Format("Error, failed to set item"));
            }

            // [] set Item() 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[keysLong[i]] = valuesShort[0];
                if (hd.Count != cnt)
                {
                    Assert.False(true, string.Format("Error, added item instead of setting", i));
                }
                itm = hd[keysLong[i]];
                if (String.Compare(itm.ToString(), valuesShort[0]) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set item", i));
                }
            }
            // should add non-existing item
            cnt = hd.Count;
            hd[keysShort[0]] = valuesShort[0];
            if (hd.Count != cnt + 1)
            {
                Assert.False(true, string.Format("Error, didn't add non-existing item"));
            }
            itm = hd[keysShort[0]];
            if (String.Compare(itm.ToString(), valuesShort[0]) != 0)
            {
                Assert.False(true, string.Format("Error, failed to set item"));
            }


            //
            // [] set Item() on long dictionary with Intl strings
            // Intl strings
            //

            len = valuesLong.Length;
            string[] intlValues = new string[len * 2 + 1];

            // fill array with unique strings
            //
            for (int i = 0; i < len * 2 + 1; i++)
            {
                string val = intl.GetRandomString(MAX_LEN);
                while (Array.IndexOf(intlValues, val) != -1)
                    val = intl.GetRandomString(MAX_LEN);
                intlValues[i] = val;
            }
            string toSet = intlValues[len * 2];      // string to set

            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[intlValues[i + len]] = toSet;
                if (hd.Count != cnt)
                {
                    Assert.False(true, string.Format("Error, added item instead of setting", i));
                }
                itm = hd[intlValues[i + len]];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set item", i));
                }
            }

            // [] set Item() 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[intlValues[i + len]] = toSet;
                if (hd.Count != cnt)
                {
                    Assert.False(true, string.Format("Error, added item instead of setting", i));
                }
                itm = hd[intlValues[i + len]];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, returned 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
                hd[keysLong[i].ToUpper()] = toSet;
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set", 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   - should add lowercase key
            for (int i = 0; i < len; i++)
            {
                // lowercase key
                cnt = hd.Count;
                hd[keysLong[i].ToLower()] = toSet;
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, failed to add when setting", i));
                }
                itm = hd[keysLong[i].ToLower()];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set item", i));
                }
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to preserve item", 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
                hd[keysLong[i].ToUpper()] = toSet;
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set", 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   - should add lowercase key
            for (int i = 0; i < len; i++)
            {
                // lowercase key
                cnt = hd.Count;
                hd[keysLong[i].ToLower()] = toSet;
                if (hd.Count != cnt + 1)
                {
                    Assert.False(true, string.Format("Error, failed to add when setting", i));
                }
                itm = hd[keysLong[i].ToLower()];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set item", i));
                }
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to preserve item", i));
                }
            }

            // [] set Item() 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++)
            {
                hd[keysLong[i].ToUpper()] = toSet;
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set via uppercase key", i));
                }
                hd[keysLong[i].ToLower()] = valuesLong[0];
                itm = hd[keysLong[i].ToLower()];
                if (String.Compare(itm.ToString(), valuesLong[0]) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set via lowercase key", i));
                }
            }

            // [] set Item() 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++)
            {
                hd[keysLong[i].ToUpper()] = toSet;
                itm = hd[keysLong[i].ToUpper()];
                if (String.Compare(itm.ToString(), toSet) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set via uppercase key", i));
                }
                hd[keysLong[i].ToLower()] = valuesLong[0];
                itm = hd[keysLong[i].ToLower()];
                if (String.Compare(itm.ToString(), valuesLong[0]) != 0)
                {
                    Assert.False(true, string.Format("Error, failed to set via lowercase key", i));
                }
            }


            //
            //  [] set Item(null) on 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[null] = toSet;
                Assert.False(true, string.Format("Error, no exception"));
            }
            catch (ArgumentNullException)
            {
            }
            catch (Exception e)
            {
                Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString()));
            }

            //  [] set Item(null) on 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[null] = toSet;
                Assert.False(true, string.Format("Error, no exception"));
            }
            catch (ArgumentNullException)
            {
            }
            catch (Exception e)
            {
                Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString()));
            }

            //  [] set Item(special_object) on filled HD - list
            //
            hd = new HybridDictionary();

            hd.Clear();
            len = 2;
            ArrayList[] b = new ArrayList[len];
            Hashtable[] lbl = new Hashtable[len];
            SortedList cb = new SortedList();
            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[lbl[i]] = cb;
                if (hd.Count != cnt)
                {
                    Assert.False(true, string.Format("Error, added special object instead of setting"));
                }
                itm = hd[lbl[i]];
                if (!itm.Equals(cb))
                {
                    Assert.False(true, string.Format("Error, failed to set special object"));
                }
            }

            cnt = hd.Count;
            hd[cb] = lbl[0];
            if (hd.Count != cnt + 1)
            {
                Assert.False(true, string.Format("Error, failed to add non-existing special object"));
            }
            itm = hd[cb];
            if (!itm.Equals(lbl[0]))
            {
                Assert.False(true, string.Format("Error, failed to set special object"));
            }

            //  [] set Item(special_object) on 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[lbl[i]] = cb;
                if (hd.Count != cnt)
                {
                    Assert.False(true, string.Format("Error, added special object instead of setting"));
                }
                itm = hd[lbl[i]];
                if (!itm.Equals(cb))
                {
                    Assert.False(true, string.Format("Error, failed to set special object"));
                }
            }
            cnt = hd.Count;
            hd[cb] = lbl[0];
            if (hd.Count != cnt + 1)
            {
                Assert.False(true, string.Format("Error, failed to add non-existing special object"));
            }
            itm = hd[cb];
            if (!itm.Equals(lbl[0]))
            {
                Assert.False(true, string.Format("Error, failed to set special object"));
            }

            //  [] set Item() to null on filled HD - list
            //
            hd = new HybridDictionary();

            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));
            }

            cnt = hd.Count;
            hd[keysShort[0]] = null;
            if (hd.Count != cnt)
            {
                Assert.False(true, string.Format("Error, added entry instead of setting"));
            }
            itm = hd[keysShort[0]];
            if (itm != null)
            {
                Assert.False(true, string.Format("Error, failed to set to null"));
            }

            //  [] set Item() to null on filled HD - hashtable
            //
            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));
            }

            cnt = hd.Count;
            hd[keysLong[0]] = null;
            if (hd.Count != cnt)
            {
                Assert.False(true, string.Format("Error, added entry instead of setting"));
            }
            itm = hd[keysLong[0]];
            if (itm != null)
            {
                Assert.False(true, string.Format("Error, failed to set to null"));
            }
        }
示例#17
0
        public void Test01()
        {
            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
            for (int i = 0; i < BIG_LENGTH; i++)
            {
                valuesLong[i] = "Item" + i;
                keysLong[i] = "keY" + i;
            }

            // [] HybridDictionary is constructed as expected
            //-----------------------------------------------------------------

            hd = new HybridDictionary();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after default ctor", hd.Count, 0));
            }

            //
            // [] Clear empty dictionary and check Count
            //
            hd.Clear();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0));
            }

            cnt = hd.Keys.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
            cnt = hd.Values.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0));
            }


            //  [] Add simple strings (list) - Count -  Clear() - Count
            //
            cnt = hd.Count;
            for (int i = 0; i < valuesShort.Length; i++)
            {
                hd.Add(keysShort[i], valuesShort[i]);
            }
            if (hd.Count != valuesShort.Length)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length));
            }

            hd.Clear();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0));
            }

            cnt = hd.Keys.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
            cnt = hd.Values.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0));
            }

            //   Hashtable
            //  [] Add simple strings (hashtable) - Count -  Clear() - Count
            //
            cnt = hd.Count;
            for (int i = 0; i < valuesLong.Length; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }
            if (hd.Count != valuesLong.Length)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesLong.Length));
            }

            hd.Clear();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0));
            }

            cnt = hd.Keys.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
            cnt = hd.Values.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
        }
示例#18
0
        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();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after default ctor", hd.Count, 0));
            }

            // [] Clear() on empty dictionary
            //
            hd.Clear();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0));
            }

            cnt = hd.Keys.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
            cnt = hd.Values.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0));
            }


            //  [] Add simple strings and Clear()
            //
            cnt = hd.Count;
            for (int i = 0; i < valuesShort.Length; i++)
            {
                hd.Add(keysShort[i], valuesShort[i]);
            }
            if (hd.Count != valuesShort.Length)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length));
            }

            hd.Clear();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0));
            }

            cnt = hd.Keys.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
            cnt = hd.Values.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0));
            }


            //
            // [] Add Intl strings and Clear()
            //
            int len = valuesShort.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;
            }

            //   Add items
            //
            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));
            }

            hd.Clear();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0));
            }

            cnt = hd.Keys.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
            cnt = hd.Values.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0));
            }

            //
            //  [] Add many simple strings and Clear()
            //
            hd.Clear();
            for (int i = 0; i < valuesLong.Length; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }
            if (hd.Count != valuesLong.Length)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesLong.Length));
            }

            hd.Clear();
            cnt = hd.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0));
            }

            cnt = hd.Keys.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0));
            }
            cnt = hd.Values.Count;
            if (cnt != 0)
            {
                Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0));
            }

            // Clear should clear underlying collection's items
            hd = new HybridDictionary();

            for (int i = 0; i < 100; i++)
                hd.Add("key_" + i, "val_" + i);
            ICollection icol1 = hd.Keys;
            ICollection icol2 = hd.Values;
            hd.Clear();

            if (icol1.Count != 0)
            {
                Assert.False(true, string.Format("Error, icol1.Count wrong, expected {0} got {1}", 0, icol1.Count));
            }
            if (icol2.Count != 0)
            {
                Assert.False(true, string.Format("Error, icol2.Count wrong, expected {0} got {1}", 0, icol2.Count));
            }
        }
示例#19
0
        public void Test01()
        {
            HybridDictionary hd;
            int cnt;

            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.IsSynchronized should return false
            //-----------------------------------------------------------------

            hd = new HybridDictionary();

            // [] on empty dictionary
            //
            if (hd.IsSynchronized)
            {
                Assert.False(true, string.Format("Error, returned true for empty dictionary"));
            }

            // [] on short filled dictionary
            //

            hd.Clear();
            cnt = valuesShort.Length;
            for (int i = 0; i < cnt; i++)
            {
                hd.Add(keysShort[i], valuesShort[i]);
            }
            if (hd.Count != cnt)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt));
            }
            if (hd.IsSynchronized)
            {
                Assert.False(true, string.Format("Error, returned true for short filled dictionary"));
            }

            // [] on long filled dictionary
            //

            hd.Clear();
            cnt = valuesLong.Length;
            for (int i = 0; i < cnt; i++)
            {
                hd.Add(keysLong[i], valuesLong[i]);
            }
            if (hd.Count != cnt)
            {
                Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt));
            }
            if (hd.IsSynchronized)
            {
                Assert.False(true, string.Format("Error, returned true for long filled dictionary"));
            }
        }
		private void BindMemberCombo (IClass c)
		{
			if (!ClassBrowserVisible)
				return;

			int position = 0;
			int activeIndex = -1;
			
			// find out where the current cursor position is and set the combos.
			int line;
			int column;
			this.GetLineColumnFromPosition(this.CursorPosition, out line, out column);
			
			UpdateComboTip (classCombo, c);
			membersCombo.Changed -= new EventHandler (MemberChanged);
			// Clear down all our local stores.
			
			membersCombo.Model = null;
			memberStore.Clear();
			UpdateComboTip (membersCombo, null);
				
			HybridDictionary methodMap = new HybridDictionary();
			
			Gdk.Pixbuf pix;
			
			ArrayList members = new ArrayList ();
			members.AddRange (c.Methods);
			members.AddRange (c.Properties);
			members.AddRange (c.Fields);
			members.Sort (new LanguageItemComparer ());
			
			// Add items to the member drop down 
			
			foreach (IMember mem in members)
			{
				pix = ImageService.GetPixbuf(IdeApp.Services.Icons.GetIcon (mem), IconSize.Menu); 
				
				// Add the member to the list
				MonoDevelop.Projects.Ambience.Ambience am = se.View.GetAmbience ();
				string displayName = am.Convert (mem, MonoDevelop.Projects.Ambience.ConversionFlags.UseIntrinsicTypeNames |
				                                      MonoDevelop.Projects.Ambience.ConversionFlags.ShowParameters |
				                                      MonoDevelop.Projects.Ambience.ConversionFlags.ShowParameterNames |
				                                      MonoDevelop.Projects.Ambience.ConversionFlags.ShowGenericParameters);
				memberStore.AppendValues (pix, displayName, mem);
				
				// Check if the current cursor position in inside this member
				if (IsMemberSelected (mem, line, column)) {
					UpdateComboTip (membersCombo, mem);
					activeIndex = position;
				}
				
				position++;
			}
			membersCombo.Model = memberStore;
			
			// don't need method map anymore
			methodMap.Clear ();
			
			// set active the method the cursor is in
			membersCombo.Active = activeIndex;
			membersCombo.Changed += new EventHandler (MemberChanged);
		}