private void BasicTests (ListDictionary ld) { Assert.AreEqual (0, ld.Count, "Count"); Assert.IsFalse (ld.IsFixedSize, "IsFixedSize"); Assert.IsFalse (ld.IsReadOnly, "IsReadOnly"); Assert.IsFalse (ld.IsSynchronized, "IsSynchronized"); Assert.AreEqual (0, ld.Keys.Count, "Keys"); Assert.AreEqual (0, ld.Values.Count, "Values"); Assert.IsNotNull (ld.SyncRoot, "SyncRoot"); Assert.IsNotNull (ld.GetEnumerator (), "GetEnumerator"); Assert.IsNotNull ((ld as IEnumerable).GetEnumerator (), "IEnumerable.GetEnumerator"); ld.Add ("a", "1"); Assert.AreEqual (1, ld.Count, "Count-1"); Assert.IsTrue (ld.Contains ("a"), "Contains(a)"); Assert.IsFalse (ld.Contains ("1"), "Contains(1)"); ld.Add ("b", null); Assert.AreEqual (2, ld.Count, "Count-2"); Assert.IsNull (ld["b"], "this[b]"); DictionaryEntry[] entries = new DictionaryEntry[2]; ld.CopyTo (entries, 0); ld["b"] = "2"; Assert.AreEqual ("2", ld["b"], "this[b]2"); ld.Remove ("b"); Assert.AreEqual (1, ld.Count, "Count-3"); ld.Clear (); Assert.AreEqual (0, ld.Count, "Count-4"); }
public void Clear() { if (hash != null) { hash.Clear(); hash = null; list = new ListDictionary(); } else if (list != null) { list.Clear(); } }
public void Clear() { if (this.hashtable != null) { var hashtable = this.hashtable; this.hashtable = null; hashtable.Clear(); } if (this.list != null) { ListDictionary list = this.list; this.list = null; list.Clear(); } }
public void Clear() { if (hashtable != null) { Hashtable cachedHashtable = hashtable; hashtable = null; cachedHashtable.Clear(); } if (list != null) { ListDictionary cachedList = list; list = null; cachedList.Clear(); } }
public void Clear() { if (_hashtable != null) { Hashtable cachedHashtable = _hashtable; _hashtable = null; cachedHashtable.Clear(); } if (_list != null) { ListDictionary cachedList = _list; _list = null; cachedList.Clear(); } }
public static void PerformListDictionary() //Diese Methode zeigt wie schnell eine kleine ListDictionary sein kann und wie sehr eine große ListDictionary die Performance beeinträchtigt { System.Collections.Specialized.ListDictionary listDictionary = new System.Collections.Specialized.ListDictionary(); //Da unsere Klasse ebenfalls "ListDictionary" heißt, müssen wir die ListDictionary-Collection bei vollem Namen nennen. Natürlich war das beabsichtigt um den Hintergrund zu erklären ;) Man kann gleichnamige Klassen haben solange sich der Vollständige name bzw der Namespace unterscheidet. Hashtable hashtable = new Hashtable(); //Ein HashTable ist im Prinzip genau wie ein Dictionary nur dass es seinen Inhalt nach dem HashCode sortiert. HashCodes sind Codes die jedes Objekt generiert. Hashtables gelten jedoch als veraltet weshalb ich nicht weiter darauf eingehen werde. string bigHashTableResult = null; string smallDictionaryResult = null; string bigDictionaryResult = null; _isLoading = true; Task.Run(VisibleLoadingCycle); //Hier bedeuted "Task" eine Asynchrone Operation welches parallel zum derzeitigem Hauptprogramm abläuft. Während wir also die Methode "VisibleLoadingCycle" angestoßen haben gehen wir gleichzeitig weiter mit der nächsten Zeile. FillDictionaryWithContent(10, listDictionary); // ^in diesem Kontext bedeuted "Hauptprogramm"/"Mainthread" das programm welches das Konsolenfenster/Kommandofenster verfolgt. WriteAllContentToConsole(listDictionary); _isLoading = false; smallDictionaryResult = _stopwatch.Elapsed.ToString(); _stopwatch.Reset(); //Hier stellen wir die Stopwatch wieder auf 0 listDictionary.Clear(); Console.WriteLine("Drücke 'Enter' um das große ListDictionary zu füllen"); if (Console.ReadKey().Key == ConsoleKey.Enter) { _isLoading = true; Task.Run(VisibleLoadingCycle); FillDictionaryWithContent(100_000, listDictionary); WriteAllContentToConsole(listDictionary); _isLoading = false; bigDictionaryResult = _stopwatch.Elapsed.ToString(); _stopwatch.Reset(); } Console.WriteLine("Drücke 'Enter' um das Hashtable zu füllen"); if (Console.ReadKey().Key == ConsoleKey.Enter) { _isLoading = true; Task.Run(VisibleLoadingCycle); FillDictionaryWithContent(100_000, hashtable); WriteAllContentToConsole(hashtable); _isLoading = false; bigHashTableResult = _stopwatch.Elapsed.ToString(); //Die "stopwatch.Elapsed" Property gibt die Zeitspanne zurück die wir während der Laufzeit der Stopwatch aufgenommen haben } Console.WriteLine(smallDictionaryResult); Console.WriteLine(bigDictionaryResult); Console.WriteLine(bigHashTableResult); }
public void Test01() { IntlStrings intl; ListDictionary ld; Object itm; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] on empty dictionary // cnt = ld.Count; Assert.Throws<ArgumentNullException>(() => { itm = ld[null]; }); cnt = ld.Count; itm = ld["some_string"]; if (itm != null) { Assert.False(true, string.Format("Error, returned non=null")); } cnt = ld.Count; itm = ld[new Hashtable()]; if (itm != null) { Assert.False(true, string.Format("Error, returned non-null")); } // [] get Item() on dictionary filled with simple strings // cnt = ld.Count; int len = values.Length; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, values.Length)); } // for (int i = 0; i < len; i++) { if (!ld.Contains(keys[i])) { Assert.False(true, string.Format("Error, doesn't contain key", i)); } if (String.Compare(ld[keys[i]].ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } // // Intl strings // [] get Item() on dictionary filled with Intl strings // 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; } cnt = ld.Count; for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt + len)); } for (int i = 0; i < len; i++) { // if (!ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain key", i)); } if (String.Compare(ld[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } // // [] Case sensitivity // 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(); } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key if (!ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain key", i)); } if (String.Compare(ld[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = ld.Count; if (!caseInsensitive && ld[intlValuesLower[i + len]] != null) { Assert.False(true, string.Format("Error, failed: returned non-null for lowercase key", i)); } } // // [] get Item() on filled dictionary with case-insensitive comparer ld = new ListDictionary(new InsensitiveComparer()); len = values.Length; ld.Clear(); string kk = "key"; for (int i = 0; i < len; i++) { ld.Add(kk + i, values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } for (int i = 0; i < len; i++) { if (ld[kk.ToUpper() + i] == null) { Assert.False(true, string.Format("Error, returned null for differently cased key", i)); } else { if (String.Compare(ld[kk.ToUpper() + i].ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } } // // [] Item(null) for dilled dictionary // ld = new ListDictionary(); cnt = ld.Count; if (ld.Count < len) { ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } } Assert.Throws<ArgumentNullException>(() => { itm = ld[null]; }); // [] get Item(special_object) // ld = new ListDictionary(); ld.Clear(); ArrayList b = new ArrayList(); ArrayList b1 = new ArrayList(); Hashtable lbl = new Hashtable(); Hashtable lbl1 = new Hashtable(); ld.Add(lbl, b); ld.Add(lbl1, b1); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } if (!ld[lbl].Equals(b)) { Assert.False(true, string.Format("Error, failed to access special object")); } if (!ld[lbl1].Equals(b1)) { Assert.False(true, string.Format("Error, failed to access special object")); } }
public void Clear() { if (this.hashtable != null) { Hashtable hashtable = this.hashtable; this.hashtable = null; hashtable.Clear(); } if (this.list != null) { ListDictionary list = this.list; this.list = null; list.Clear(); } }
public void Test01() { IntlStrings intl; ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] Remove() on empty dictionary // cnt = ld.Count; Assert.Throws<ArgumentNullException>(() => { ld.Remove(null); }); cnt = ld.Count; ld.Remove("some_string"); if (ld.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } cnt = ld.Count; ld.Remove(new Hashtable()); if (ld.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } // [] add simple strings and Remove() // cnt = ld.Count; int len = values.Length; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, values.Length)); } // for (int i = 0; i < len; i++) { cnt = ld.Count; ld.Remove(keys[i]); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } if (ld.Contains(keys[i])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // Intl strings // [] Add Intl strings and Remove() // 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; } cnt = ld.Count; for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = ld.Count; ld.Remove(intlValues[i + len]); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } if (ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Case sensitivity // 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(); } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key cnt = ld.Count; ld.Remove(intlValues[i + len]); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } if (ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = ld.Count; ld.Remove(intlValuesLower[i + len]); if (!caseInsensitive && ld.Count != cnt) { Assert.False(true, string.Format("Error, failed: removed item using lowercase key", i)); } } // // [] Remove() on LD with case-insensitive comparer // ld = new ListDictionary(new InsensitiveComparer()); len = values.Length; ld.Clear(); string kk = "key"; for (int i = 0; i < len; i++) { ld.Add(kk + i, values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } for (int i = 0; i < len; i++) { cnt = ld.Count; ld.Remove(kk.ToUpper() + i); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (ld.Contains(kk + i)) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Remove(null) // ld = new ListDictionary(); cnt = ld.Count; if (ld.Count < len) { ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } } Assert.Throws<ArgumentNullException>(() => { ld.Remove(null); }); ld = new ListDictionary(); ld.Clear(); ArrayList b = new ArrayList(); ArrayList b1 = new ArrayList(); Hashtable lbl = new Hashtable(); Hashtable lbl1 = new Hashtable(); ld.Add(lbl, b); ld.Add(lbl1, b1); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } cnt = ld.Count; ld.Remove(lbl); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove special object")); } if (ld.Contains(lbl)) { Assert.False(true, string.Format("Error, removed wrong special object")); } }
public void Test01() { ListDictionary ld; IDictionaryEnumerator en; DictionaryEntry curr; // Enumerator.Current value DictionaryEntry de; // Enumerator.Entry value Object k; // Enumerator.Key value Object v; // Enumerator.Value // simple string values string[] values = { "a", "aa", "", " ", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; // [] ListDictionary GetEnumerator() //----------------------------------------------------------------- ld = new ListDictionary(); // [] for empty dictionary // en = ld.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; }); // // [] for Filled dictionary // for (int i = 0; i < values.Length; i++) { ld.Add(keys[i], values[i]); } en = ld.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 < ld.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 (!ld.Contains(curr.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (!ld.Contains(en.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (!ld.Contains(de.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain Entry.Key from enumerator", i)); } if (String.Compare(ld[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(ld[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(ld[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 // if (ld.Count < 1) { for (int i = 0; i < values.Length; i++) { ld.Add(keys[i], values[i]); } } en = ld.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 = ld.Count; ld.Remove(keys[0]); if (ld.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(); }); // exception expected Assert.Throws<InvalidOperationException>(() => { en.Reset(); }); // // [] Modify dictionary after enumerated beyond the end // ld.Clear(); for (int i = 0; i < values.Length; i++) { ld.Add(keys[i], values[i]); } en = ld.GetEnumerator(); for (int i = 0; i < ld.Count; i++) { en.MoveNext(); } curr = (DictionaryEntry)en.Current; de = en.Entry; k = en.Key; v = en.Value; cnt = ld.Count; ld.Remove(keys[0]); if (ld.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 de2 = en.Entry; if (!de.Equals(de2)) { Assert.False(true, string.Format("Error, Entry returned different value after modification")); } // will return just removed item k2 = en.Key; if (!k.Equals(k2)) { Assert.False(true, string.Format("Error, Key returned different value after modification")); } // will return just removed item 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(); }); // exception expected Assert.Throws<InvalidOperationException>(() => { en.Reset(); }); ///////////////////////////////////// //// Code coverage // // [] Call IEnumerable.GetEnumerator() // en = ld.GetEnumerator(); IEnumerator en2 = ((IEnumerable)ld).GetEnumerator(); if (en2 == null) { Assert.False(true, string.Format("Error, enumerator was null")); } }
public void Test01() { IntlStrings intl; ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); cnt = ld.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after default ctor", ld.Count, 0)); } // [] Clear() on empty dictionary // ld.Clear(); cnt = ld.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", ld.Count, 0)); } cnt = ld.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = ld.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 = ld.Count; for (int i = 0; i < values.Length; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, values.Length)); } ld.Clear(); cnt = ld.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", ld.Count, 0)); } cnt = ld.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = ld.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 = values.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 = ld.Count; for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt + len)); } ld.Clear(); cnt = ld.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", ld.Count, 0)); } cnt = ld.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = ld.Values.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0)); } }
public void Test01() { ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); cnt = ld.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after default ctor", ld.Count, 0)); } // [] Clear() empty dictionary and get Count // ld.Clear(); cnt = ld.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", ld.Count, 0)); } cnt = ld.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = ld.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 - Count - Clear() - Count // cnt = ld.Count; for (int i = 0; i < values.Length; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, values.Length)); } ld.Clear(); cnt = ld.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", ld.Count, 0)); } cnt = ld.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = ld.Values.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0)); } }
public void Test01() { ListDictionary ld; // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); if (ld == null) { Assert.False(true, string.Format("Error, dictionary is null after default ctor")); } if (ld.Count != 0) { Assert.False(true, string.Format("Error, Count = {0} after default ctor", ld.Count)); } if (ld["key"] != null) { Assert.False(true, string.Format("Error, Item(some_key) returned non-null after default ctor")); } System.Collections.ICollection keys = ld.Keys; if (keys.Count != 0) { Assert.False(true, string.Format("Error, Keys contains {0} keys after default ctor", keys.Count)); } System.Collections.ICollection values = ld.Values; if (values.Count != 0) { Assert.False(true, string.Format("Error, Values contains {0} items after default ctor", values.Count)); } // // [] Add(string, string) // ld.Add("Name", "Value"); if (ld.Count != 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of 1", ld.Count)); } if (String.Compare(ld["Name"].ToString(), "Value") != 0) { Assert.False(true, string.Format("Error, Item() returned unexpected value")); } // // [] Clear() // ld.Clear(); if (ld.Count != 0) { Assert.False(true, string.Format("Error, Count returned {0} instead of 0 after Clear()", ld.Count)); } if (ld["Name"] != null) { Assert.False(true, string.Format("Error, Item() returned non-null value after Clear()")); } // // [] elements not overriding Equals() // ld.Clear(); Hashtable lbl = new Hashtable(); Hashtable lbl1 = new Hashtable(); ArrayList b = new ArrayList(); ArrayList b1 = new ArrayList(); ld.Add(lbl, b); ld.Add(lbl1, b1); if (ld.Count != 2) { Assert.False(true, string.Format("Error, Count returned {0} instead of 2", ld.Count)); } if (!ld.Contains(lbl)) { Assert.False(true, string.Format("Error, doesn't contain 1st special item")); } if (!ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, doesn't contain 2nd special item")); } if (ld.Values.Count != 2) { Assert.False(true, string.Format("Error, Values.Count returned {0} instead of 2", ld.Values.Count)); } ld.Remove(lbl1); if (ld.Count != 1) { Assert.False(true, string.Format("Error, failed to remove item")); } if (ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, failed to remove special item")); } }
public void Test01() { IntlStrings intl; ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] Contains() on empty dictionary // Assert.Throws<ArgumentNullException>(() => { ld.Contains(null); }); if (ld.Contains("some_string")) { Assert.False(true, string.Format("Error, empty dictionary contains some_object")); } if (ld.Contains(new Hashtable())) { Assert.False(true, string.Format("Error, empty dictionary contains some_object")); } // [] simple strings and Contains() // cnt = ld.Count; int len = values.Length; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, values.Length)); } // for (int i = 0; i < len; i++) { if (!ld.Contains(keys[i])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keys[i])); } } // // Intl strings // [] 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; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) caseInsensitive = true; } ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } for (int i = 0; i < len; i++) { // if (!ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, intlValues[i + len])); } } // // [] Case sensitivity // by default ListDictionary is case-sensitive // 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(); } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key if (!ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain added uppercase \"{1}\"", i, intlValues[i + len])); } // lowercase key if (!caseInsensitive && ld.Contains(intlValuesLower[i + len])) { Assert.False(true, string.Format("Error, contains lowercase \"{1}\" - should not", i, intlValuesLower[i + len])); } } // [] different_in_casing_only keys and Contains() // ld.Clear(); string[] ks = { "Key", "kEy", "keY" }; len = ks.Length; for (int i = 0; i < len; i++) { ld.Add(ks[i], "Value" + i); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } if (ld.Contains("Value0")) { Assert.False(true, string.Format("Error, returned true when should not")); } for (int i = 0; i < len; i++) { if (!ld.Contains(ks[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } // // [] Contains(null) - for filled dictionary // ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } Assert.Throws<ArgumentNullException>(() => { ld.Contains(null); }); // [] Contains() for case-insensitive comparer // ld = new ListDictionary(new InsensitiveComparer()); ld.Clear(); len = ks.Length; ld.Add(ks[0], "Value0"); for (int i = 1; i < len; i++) { Assert.Throws<ArgumentException>(() => { ld.Add(ks[i], "Value" + i); }); } if (ld.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 1)); } if (ld.Contains("Value0")) { Assert.False(true, string.Format("Error, returned true when should not")); } for (int i = 0; i < len; i++) { if (!ld.Contains(ks[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } if (!ld.Contains("KEY")) { Assert.False(true, string.Format("Error, returned false non-existing-cased key")); } // [] Contains() and objects_not_overriding_Equals // ld = new ListDictionary(); ld.Clear(); Hashtable lbl = new Hashtable(); Hashtable lbl1 = new Hashtable(); ArrayList b = new ArrayList(); ArrayList b1 = new ArrayList(); ld.Add(lbl, b); ld.Add(lbl1, b1); Assert.Throws<ArgumentException>(() => { ld.Add(lbl, "Hello"); }); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } if (!ld.Contains(lbl)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (ld.Contains(new Hashtable())) { Assert.False(true, string.Format("Error, returned true when false expected")); } // [] Contains and Special_Comparer for objects // ld = new ListDictionary(new SpecialComparer()); lbl["foo"] = "Hello"; lbl1["foo"] = "Hello"; ld.Add(lbl, b); Assert.Throws<ArgumentException>(() => { ld.Add(lbl1, b1); }); if (ld.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 1)); } if (!ld.Contains(lbl)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, returned false when true expected")); } lbl1["foo"] = "HELLO"; if (ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, returned true when false expected")); } // [] Contains() and special_structs_not_overriding_Equals ld = new ListDictionary(); SpecialStruct s = new SpecialStruct(); s.Num = 1; s.Wrd = "one"; SpecialStruct s1 = new SpecialStruct(); s.Num = 1; s.Wrd = "one"; ld.Add(s, "first"); ld.Add(s1, "second"); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } if (!ld.Contains(s)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!ld.Contains(s1)) { Assert.False(true, string.Format("Error, returned false when true expected")); } }
public void Performance() { // set the hashtable and SequencedHashMap to be the IDictionary hashtable; IDictionary sequenced; IDictionary list; int numOfRuns = 4; int numOfEntries = Int16.MaxValue; long hashStart; long[] hashPopulateTicks = new long[numOfRuns]; long[] hashItemTicks = new long[numOfRuns]; long seqStart; long[] seqPopulateTicks = new long[numOfRuns]; long[] seqItemTicks = new long[numOfRuns]; long listStart; long[] listPopulateTicks = new long[numOfRuns]; long[] listItemTicks = new long[numOfRuns]; for (int runIndex = 0; runIndex < numOfRuns; runIndex++) { object key; object value; hashtable = new Hashtable(); sequenced = new SequencedHashMap(); list = new ListDictionary(); hashStart = DateTime.Now.Ticks; for (int i = 0; i < numOfEntries; i++) { hashtable.Add("test" + i, new object()); } hashPopulateTicks[runIndex] = DateTime.Now.Ticks - hashStart; hashStart = DateTime.Now.Ticks; for (int i = 0; i < numOfEntries; i++) { key = "test" + i; value = hashtable[key]; } hashItemTicks[runIndex] = DateTime.Now.Ticks - hashStart; hashtable.Clear(); seqStart = DateTime.Now.Ticks; for (int i = 0; i < numOfEntries; i++) { sequenced.Add("test" + i, new object()); } seqPopulateTicks[runIndex] = DateTime.Now.Ticks - seqStart; seqStart = DateTime.Now.Ticks; for (int i = 0; i < numOfEntries; i++) { key = "test" + i; value = sequenced[key]; } seqItemTicks[runIndex] = DateTime.Now.Ticks - seqStart; sequenced.Clear(); listStart = DateTime.Now.Ticks; for (int i = 0; i < numOfEntries; i++) { list.Add("test" + i, new object()); } listPopulateTicks[runIndex] = DateTime.Now.Ticks - listStart; listStart = DateTime.Now.Ticks; for (int i = 0; i < numOfEntries; i++) { key = "test" + i; value = list[key]; } listItemTicks[runIndex] = DateTime.Now.Ticks - listStart; list.Clear(); } for (int runIndex = 0; runIndex < numOfRuns; runIndex++) { decimal seqPopulateOverhead = ((decimal) seqPopulateTicks[runIndex] / (decimal) hashPopulateTicks[runIndex]); decimal seqItemOverhead = ((decimal) seqItemTicks[runIndex] / (decimal) hashItemTicks[runIndex]); string errMessage = "SequenceHashMap vs Hashtable:"; errMessage += "\n POPULATE:"; errMessage += "\n\t seqPopulateTicks[" + runIndex + "] took " + seqPopulateTicks[runIndex] + " ticks."; errMessage += "\n\t hashPopulateTicks[" + runIndex + "] took " + hashPopulateTicks[runIndex] + " ticks."; errMessage += "\n\t for an overhead of " + seqPopulateOverhead.ToString(); errMessage += "\n ITEM:"; errMessage += "\n\t seqItemTicks[" + runIndex + "] took " + seqItemTicks[runIndex] + " ticks."; errMessage += "\n\t hashItemTicks[" + runIndex + "] took " + hashItemTicks[runIndex] + " ticks."; errMessage += "\n\t for an overhead of " + seqItemOverhead.ToString(); Console.Out.WriteLine(errMessage); decimal listPopulateOverhead = ((decimal) listPopulateTicks[runIndex] / (decimal) seqPopulateTicks[runIndex]); decimal listItemOverhead = ((decimal) listItemTicks[runIndex] / (decimal) seqItemTicks[runIndex]); errMessage = "ListDictionary vs SequenceHashMap:"; errMessage += "\n POPULATE:"; errMessage += "\n\t listPopulateTicks[" + runIndex + "] took " + listPopulateTicks[runIndex] + " ticks."; errMessage += "\n\t seqPopulateTicks[" + runIndex + "] took " + seqPopulateTicks[runIndex] + " ticks."; errMessage += "\n\t for an overhead of " + listPopulateOverhead.ToString(); errMessage += "\n ITEM:"; errMessage += "\n\t listItemTicks[" + runIndex + "] took " + listItemTicks[runIndex] + " ticks."; errMessage += "\n\t seqItemTicks[" + runIndex + "] took " + seqItemTicks[runIndex] + " ticks."; errMessage += "\n\t for an overhead of " + listItemOverhead.ToString(); Console.Out.WriteLine(errMessage); } }
public void Test01() { ListDictionary ld; int cnt; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; // [] ListDictionary.IsReadOnly should return false //----------------------------------------------------------------- ld = new ListDictionary(); // [] on empty dictionary // if (ld.IsReadOnly) { Assert.False(true, string.Format("Error, returned true for empty dictionary")); } // [] on filled dictionary // ld.Clear(); cnt = values.Length; for (int i = 0; i < cnt; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt)); } if (ld.IsReadOnly) { Assert.False(true, string.Format("Error, returned true for filled dictionary")); } }
public void Test01() { IntlStrings intl; ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aa", "tExt", " spAces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; Array arr; ICollection ks; // Keys collection int ind; // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] get Keys for empty dictionary // if (ld.Count > 0) ld.Clear(); if (ld.Keys.Count != 0) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ld.Keys.Count)); } // [] get Keys for filled dictionary // int len = values.Length; ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } ks = ld.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(Object), len); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key. Search result: {2}", i, keys[i], ind)); } } // // get Keys on dictionary with identical values // // [] get Keys for filled dictionary with identical value // ld.Clear(); string intlStr = intl.GetRandomString(MAX_LEN); ld.Add("keykey", intlStr); // 1st key for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } ld.Add("keyKey", intlStr); // 2nd key if (ld.Count != len + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len + 2)); } // get Keys // ks = ld.Keys; if (ks.Count != ld.Count) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(Object), len + 2); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, keys[i])); } } ind = Array.IndexOf(arr, "keykey"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain {0} key", "keykey")); } ind = Array.IndexOf(arr, "keyKey"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{0}\" key", "keyKey")); } // // Intl strings // // [] get Keys for dictionary filled with Intl strings // 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; } // // will use first half of array as values and second half as keys // ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } ks = ld.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(Object), len); ks.CopyTo(arr, 0); for (int i = 0; i < arr.Length; i++) { ind = Array.IndexOf(arr, intlValues[i + len]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, intlValues[i + len])); } } // // [] Change dictionary and verify Keys // ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } ks = ld.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } ld.Remove(keys[0]); if (ld.Count != len - 1) { Assert.False(true, string.Format("Error, didn't remove element")); } if (ks.Count != len - 1) { Assert.False(true, string.Format("Error, Keys were not updated after removal")); } arr = Array.CreateInstance(typeof(Object), ld.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0]); if (ind >= 0) { Assert.False(true, string.Format("Error, Keys still contains removed key " + ind)); } ld.Add(keys[0], "new item"); if (ld.Count != len) { Assert.False(true, string.Format("Error, didn't add element")); } if (ks.Count != len) { Assert.False(true, string.Format("Error, Keys were not updated after addition")); } arr = Array.CreateInstance(typeof(Object), ld.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain added key ")); } ICollection icol1; ListDictionary Hd = new ListDictionary(); for (int i = 0; i < 10; i++) Hd.Add("key_" + i, "val_" + i); icol1 = Hd.Keys; if (Hd.SyncRoot != icol1.SyncRoot) { Assert.False(true, string.Format("Error, SyncRoot is not the same for ListDictionary's SyncRoot and ICollection.SyncRoot")); } // [] Run IEnumerable tests Hd = new ListDictionary(); String[] expectedKeys = new String[10]; for (int i = 0; i < 10; i++) { Hd.Add("key_" + i, "val_" + i); expectedKeys[i] = "key_" + i; } TestSupport.Collections.IEnumerable_Test iEnumerableTest; iEnumerableTest = new TestSupport.Collections.IEnumerable_Test(Hd.Keys, expectedKeys); if (!iEnumerableTest.RunAllTests()) { Assert.False(true, string.Format("Err_98382apeuie System.Collections.IEnumerable tests FAILED")); } }
public void Test01() { IntlStrings intl; ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] Add simple strings // for (int i = 0; i < values.Length; i++) { cnt = ld.Count; ld.Add(keys[i], values[i]); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (String.Compare(ld[keys[i]].ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[keys[i]], values[i])); } } // // [] Add Intl strings // Intl strings // int len = values.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; } 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 values and second half as keys // for (int i = 0; i < len; i++) { cnt = ld.Count; ld.Add(intlValues[i + len], intlValues[i]); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (String.Compare(ld[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[intlValues[i + len]], intlValues[i])); } } // // [] Case sensitivity // Casing doesn't change ( keys 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(); } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { cnt = ld.Count; ld.Add(intlValues[i + len], intlValues[i]); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (ld[intlValues[i + len]] == null) { Assert.False(true, string.Format("Error, returned null", i)); } else { if (!ld[intlValues[i + len]].Equals(intlValues[i])) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[intlValues[i + len]], intlValues[i])); } } // verify that dictionary doesn't contains lowercase item // if (!caseInsensitive && ld[intlValuesLower[i + len]] != null) { Assert.False(true, string.Format("Error, returned non-null", i)); } } // // [] Add multiple values with the same key // Add multiple values with the same key - ArgumentException expected // ld.Clear(); len = values.Length; string k = "keykey"; ld.Add(k, "value"); Assert.Throws<ArgumentException>(() => { ld.Add(k, "newvalue"); }); // // [] Add(string, null) // Add null value // cnt = ld.Count; k = "kk"; ld.Add(k, null); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt + 1)); } // verify that dictionary contains null // if (ld[k] != null) { Assert.False(true, string.Format("Error, returned non-null on place of null")); } // // [] Add(null, string) // Add item with null key - ArgumentNullException expected // cnt = ld.Count; Assert.Throws<ArgumentNullException>(() => { ld.Add(null, "item"); }); // // [] Add duplicate values // ld.Clear(); for (int i = 0; i < values.Length; i++) { cnt = ld.Count; ld.Add(keys[i], "value"); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (!ld[keys[i]].Equals("value")) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[keys[i]], "value")); } } // verify Keys and Values if (ld.Keys.Count != values.Length) { Assert.False(true, string.Format("Error, Keys contains {0} instead of {1}", ld.Keys.Count, values.Length)); } if (ld.Values.Count != values.Length) { Assert.False(true, string.Format("Error, Values contains {0} instead of {1}", ld.Values.Count, values.Length)); } }