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"); }
private static void AddressChangedCallback(object stateObject, bool signaled) { lock (s_callerArray) { s_isPending = false; if (s_isListening) { s_isListening = false; DictionaryEntry[] array = new DictionaryEntry[s_callerArray.Count]; s_callerArray.CopyTo(array, 0); StartHelper(null, false, (StartIPOptions)stateObject); for (int i = 0; i < array.Length; i++) { NetworkAddressChangedEventHandler key = (NetworkAddressChangedEventHandler)array[i].Key; ExecutionContext context = (ExecutionContext)array[i].Value; if (context == null) { key(null, EventArgs.Empty); } else { ExecutionContext.Run(context.CreateCopy(), s_runHandlerCallback, key); } } } } }
private static void ChangedAddress(object sender, EventArgs eventArgs) { lock (syncObject){ bool isAvailableNow = SystemNetworkInterface.InternalGetIsNetworkAvailable(); if (isAvailableNow != isAvailable) { isAvailable = isAvailableNow; DictionaryEntry[] callerArray = new DictionaryEntry[s_availabilityCallerArray.Count]; s_availabilityCallerArray.CopyTo(callerArray, 0); for (int i = 0; i < callerArray.Length; i++) { NetworkAvailabilityChangedEventHandler handler = (NetworkAvailabilityChangedEventHandler)callerArray[i].Key; ExecutionContext context = (ExecutionContext)callerArray[i].Value; if (context == null) { handler(null, new NetworkAvailabilityEventArgs(isAvailable)); } else { ExecutionContext.Run(context.CreateCopy(), s_RunHandlerCallback, handler); } } } } }
public static void Main() { // Creates and initializes a new ListDictionary. ListDictionary myCol = new ListDictionary(); myCol.Add("Braeburn Apples", "1.49"); myCol.Add("Fuji Apples", "1.29"); myCol.Add("Gala Apples", "1.49"); myCol.Add("Golden Delicious Apples", "1.29"); myCol.Add("Granny Smith Apples", "0.89"); myCol.Add("Red Delicious Apples", "0.99"); // Displays the values in the ListDictionary in three different ways. Console.WriteLine("Initial contents of the ListDictionary:"); PrintKeysAndValues(myCol); // Copies the ListDictionary to an array with DictionaryEntry elements. DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; myCol.CopyTo(myArr, 0); // Displays the values in the array. Console.WriteLine("Displays the elements in the array:"); Console.WriteLine(" KEY VALUE"); for (int i = 0; i < myArr.Length; i++) { Console.WriteLine(" {0,-25} {1}", myArr[i].Key, myArr[i].Value); } Console.WriteLine(); }
public static void Main() { // Creates and initializes a new ListDictionary. ListDictionary myCol = new ListDictionary(); myCol.Add("Braeburn Apples", "1.49"); myCol.Add("Fuji Apples", "1.29"); myCol.Add("Gala Apples", "1.49"); myCol.Add("Golden Delicious Apples", "1.29"); myCol.Add("Granny Smith Apples", "0.89"); myCol.Add("Red Delicious Apples", "0.99"); // Display the contents of the collection using foreach. This is the preferred method. Console.WriteLine("Displays the elements using foreach:"); PrintKeysAndValues1(myCol); // Display the contents of the collection using the enumerator. Console.WriteLine("Displays the elements using the IDictionaryEnumerator:"); PrintKeysAndValues2(myCol); // Display the contents of the collection using the Keys, Values, Count, and Item properties. Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); PrintKeysAndValues3(myCol); // Copies the ListDictionary to an array with DictionaryEntry elements. DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; myCol.CopyTo(myArr, 0); // Displays the values in the array. Console.WriteLine("Displays the elements in the array:"); Console.WriteLine(" KEY VALUE"); for (int i = 0; i < myArr.Length; i++) { Console.WriteLine(" {0,-25} {1}", myArr[i].Key, myArr[i].Value); } Console.WriteLine(); // Searches for a key. if (myCol.Contains("Kiwis")) { Console.WriteLine("The collection contains the key \"Kiwis\"."); } else { Console.WriteLine("The collection does not contain the key \"Kiwis\"."); } Console.WriteLine(); // Deletes a key. myCol.Remove("Plums"); Console.WriteLine("The collection contains the following elements after removing \"Plums\":"); PrintKeysAndValues1(myCol); // Clears the entire collection. myCol.Clear(); Console.WriteLine("The collection contains the following elements after it is cleared:"); PrintKeysAndValues1(myCol); }
static void Main() { ListDictionary listDictionary = new ListDictionary(); listDictionary.Add(1, "Asia"); listDictionary.Add(2, "Australia"); listDictionary.Add(3, "Africa"); listDictionary.Add(4, "America"); listDictionary.Add(5, "Antartica"); Console.WriteLine("Printing the elements"); foreach (DictionaryEntry entry in listDictionary) { Console.WriteLine("{0} = {1}", entry.Key, entry.Value); } bool ld = listDictionary.Contains(8); Console.WriteLine("Contains the key 8 in the list or not:{0}", ld); Console.WriteLine("Printing the element list using GetEnumerator method-----"); IDictionaryEnumerator ide = listDictionary.GetEnumerator(); DictionaryEntry de; while (ide.MoveNext()) { de = (DictionaryEntry)ide.Current; Console.WriteLine("{0} = {1}", de.Key, de.Value); } DictionaryEntry[] arr = new DictionaryEntry[listDictionary.Count]; listDictionary.CopyTo(arr, 0); Console.WriteLine("Printing the Copied Array"); foreach (var arr1 in arr) { Console.WriteLine("{0}-->{1}", arr1.Key, arr1.Value); } listDictionary.Remove(4); Console.WriteLine("List Printing after removing the element from the index 4"); foreach (DictionaryEntry item2 in listDictionary) { Console.WriteLine("{0} --> {1}", item2.Key, item2.Value); } }
static void Main() { ListDictionary listDictionary = new ListDictionary(); listDictionary.Add(1, "Aron"); listDictionary.Add(2, "Smith"); listDictionary.Add(3, "Ray"); listDictionary.Add(4, "Todd"); listDictionary.Add(5, "James"); Console.WriteLine("List dictionary items"); foreach (DictionaryEntry de in listDictionary) { Console.WriteLine("{0} = {1}", de.Key, de.Value); } bool bb = listDictionary.Contains(7); Console.WriteLine("Contains the key 7 in the list or not:{0}", bb); Console.WriteLine("Printing the element list using GetEnumerator method-----"); IDictionaryEnumerator ide = listDictionary.GetEnumerator(); DictionaryEntry dde; while (ide.MoveNext()) { dde = (DictionaryEntry)ide.Current; Console.WriteLine("{0} = {1}", dde.Key, dde.Value); } DictionaryEntry[] arr = new DictionaryEntry[listDictionary.Count]; listDictionary.CopyTo(arr, 0); Console.WriteLine("Printing the Copied Array"); foreach (var arr1 in arr) { Console.WriteLine("{0}-->{1}", arr1.Key, arr1.Value); } listDictionary.Remove(4); Console.WriteLine("List Printing after removing the element from the index 4"); foreach (DictionaryEntry item2 in listDictionary) { Console.WriteLine("{0} --> {1}", item2.Key, item2.Value); } }
/// <summary> /// /// </summary> /// <param name="entries"></param> /// <returns></returns> public DictionaryEntry[] ConvertToDE(DictionaryEntry[] entries) { //用ListDictionary主要是為了稍後可以直接CopyTo轉DictionaryEntry[] ListDictionary list = new ListDictionary(); foreach (DictionaryEntry entry in entries) { list.Add(entry.Key, entry.Value); } DictionaryEntry[] result = new DictionaryEntry[list.Count]; list.CopyTo(result, 0); return(result); }
public static void CopyToTest(ListDictionary ld, KeyValuePair <string, string>[] data) { DictionaryEntry[] translated = data.Select(kv => new DictionaryEntry(kv.Key, kv.Value)).ToArray(); DictionaryEntry[] full = new DictionaryEntry[data.Length]; ld.CopyTo(full, 0); Assert.Equal(translated, full); DictionaryEntry[] large = new DictionaryEntry[data.Length * 2]; ld.CopyTo(large, data.Length / 2); for (int i = 0; i < large.Length; i++) { if (i < data.Length / 2 || i >= data.Length + data.Length / 2) { Assert.Equal(new DictionaryEntry(), large[i]); Assert.Null(large[i].Key); Assert.Null(large[i].Value); } else { Assert.Equal(translated[i - data.Length / 2], large[i]); } } }
//callback fired when an address change occurs private static void AddressChangedCallback(object stateObject, bool signaled) { lock (s_callerArray) { //the listener was cancelled, which would only happen if we aren't listening //for more events. s_isPending = false; if (!s_isListening) { return; } s_isListening = false; // Need to copy the array so the callback can call start and stop DictionaryEntry[] callerArray = new DictionaryEntry[s_callerArray.Count]; s_callerArray.CopyTo(callerArray, 0); try { //wait for the next address change StartHelper(null, false, (StartIPOptions)stateObject); } catch (NetworkInformationException nie) { if (Logging.On) { Logging.Exception(Logging.Web, "AddressChangeListener", "AddressChangedCallback", nie); } } for (int i = 0; i < callerArray.Length; i++) { NetworkAddressChangedEventHandler handler = (NetworkAddressChangedEventHandler)callerArray[i].Key; ExecutionContext context = (ExecutionContext)callerArray[i].Value; if (context == null) { handler(null, EventArgs.Empty); } else { ExecutionContext.Run(context.CreateCopy(), s_runHandlerCallback, handler); } } } }
public static void CopyTo_ArgumentInvalidTest(ListDictionary ld, KeyValuePair <string, string>[] data) { Assert.Throws <ArgumentNullException>("array", () => ld.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException>("index", () => ld.CopyTo(data, -1)); if (data.Length > 0) { Assert.Throws <ArgumentException>(() => ld.CopyTo(new KeyValuePair <string, string> [1, data.Length], 0)); Assert.Throws <ArgumentException>(() => ld.CopyTo(new KeyValuePair <string, string> [0], data.Length - 1)); Assert.Throws <ArgumentException>(() => ld.CopyTo(new KeyValuePair <string, string> [data.Length - 1], 0)); Assert.Throws <InvalidCastException>(() => ld.CopyTo(new int[data.Length], 0)); } }
private static void ChangedAddress(object sender, EventArgs eventArgs) { DictionaryEntry[] callerArray = null; lock (s_globalLock) { bool isAvailableNow = SystemNetworkInterface.InternalGetIsNetworkAvailable(); if (isAvailableNow != isAvailable) { isAvailable = isAvailableNow; callerArray = new DictionaryEntry[s_availabilityCallerArray.Count]; s_availabilityCallerArray.CopyTo(callerArray, 0); } } // Release the s_globalLock before calling into user callback. if (callerArray != null) { // Synchronization dedicated just to the invocation of the callback, // for back compat reason. lock (s_protectCallbackLock) { Debug.Assert(!Monitor.IsEntered(s_globalLock), "Should not invoke user callback while holding globalLock"); foreach (var entry in callerArray) { NetworkAvailabilityChangedEventHandler handler = (NetworkAvailabilityChangedEventHandler)entry.Key; ExecutionContext context = (ExecutionContext)entry.Value; if (context == null) { handler(null, new NetworkAvailabilityEventArgs(isAvailable)); } else { ExecutionContext.Run(context.CreateCopy(), s_RunHandlerCallback, handler); } } } } }
public static void CopyTo_ArgumentInvalidTest(ListDictionary ld, KeyValuePair <string, string>[] data) { Assert.Throws <ArgumentNullException>("array", () => ld.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException>("index", () => ld.CopyTo(data, -1)); if (data.Length > 0) { Assert.Throws <ArgumentException>(() => ld.CopyTo(new KeyValuePair <string, string> [0], data.Length - 1)); Assert.Throws <ArgumentException>(() => ld.CopyTo(new KeyValuePair <string, string> [data.Length - 1], 0)); Assert.Throws <InvalidCastException>(() => ld.CopyTo(new int[data.Length], 0)); } // Multidimensional array Assert.Throws <ArgumentException>(() => ld.CopyTo(new KeyValuePair <string, string> [1, data.Length], 0)); // Invalid lower bound Array arr = Array.CreateInstance(typeof(object), new int[] { 2 }, new int[] { 2 }); Assert.Throws <ArgumentException>(() => ld.CopyTo(arr, 0)); }
public void CopyTo1() { ListDictionary ld = new ListDictionary(); ld.CopyTo(null, 0); }
public virtual bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; IntlStrings intl; String strLoc = "Loc_000oo"; ListDictionary ld; string [] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; string [] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; Array destination; int cnt = 0; try { intl = new IntlStrings(); Console.WriteLine("--- create dictionary ---"); strLoc = "Loc_001oo"; iCountTestcases++; ld = new ListDictionary(); Console.WriteLine("1. Copy empty dictionary into empty array"); iCountTestcases++; destination = Array.CreateInstance(typeof(Object), 0); Console.WriteLine(" destination Length = " + destination.Length); Console.WriteLine(" - CopyTo(arr, -1)"); try { ld.CopyTo(destination, -1); iCountErrors++; Console.WriteLine("Err_0001a, no exception"); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_001b, unexpected exception: {0}", e.ToString()); } iCountTestcases++; Console.WriteLine(" - CopyTo(arr, 0)"); try { ld.CopyTo(destination, 0); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_001c, unexpected exception: {0}", e.ToString()); } iCountTestcases++; Console.WriteLine(" - CopyTo(arr, 1)"); try { ld.CopyTo(destination, 1); } catch (ArgumentException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_001d, unexpected exception: {0}", e.ToString()); } Console.WriteLine("2. Copy empty dictionary into non-empty array"); iCountTestcases++; destination = Array.CreateInstance(typeof(Object), values.Length); for (int i = 0; i < values.Length; i++) { destination.SetValue(values[i], i); } ld.CopyTo(destination, 0); if (destination.Length != values.Length) { iCountErrors++; Console.WriteLine("Err_0002a, altered array after copying empty dictionary"); } if (destination.Length == values.Length) { for (int i = 0; i < values.Length; i++) { iCountTestcases++; if (String.Compare(destination.GetValue(i).ToString(), values[i], false) != 0) { iCountErrors++; Console.WriteLine("Err_0002_{0}b, altered item {0} after copying empty dictionary", i); } } } Console.WriteLine("3. add simple strings and CopyTo(Array, 0)"); strLoc = "Loc_003oo"; iCountTestcases++; cnt = ld.Count; int len = values.Length; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { iCountErrors++; Console.WriteLine("Err_0003a, count is {0} instead of {1}", ld.Count, values.Length); } destination = Array.CreateInstance(typeof(Object), len); ld.CopyTo(destination, 0); for (int i = 0; i < len; i++) { iCountTestcases++; if (String.Compare(ld[keys[i]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), ld[keys[i]]); } iCountTestcases++; if (String.Compare(keys[i], ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), keys[i]); } } Console.WriteLine("4. add simple strings and CopyTo(Array, {0})", values.Length); ld.Clear(); strLoc = "Loc_004oo"; iCountTestcases++; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { iCountErrors++; Console.WriteLine("Err_0004a, count is {0} instead of {1}", ld.Count, values.Length); } destination = Array.CreateInstance(typeof(Object), len * 2); ld.CopyTo(destination, len); for (int i = 0; i < len; i++) { iCountTestcases++; if (String.Compare(ld[keys[i]].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, ld[keys[i]]); } iCountTestcases++; if (String.Compare(keys[i], ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, keys[i]); } } Console.WriteLine("5. add intl strings and CopyTo(Array, 0)"); strLoc = "Loc_005oo"; iCountTestcases++; string [] intlValues = new string [len * 2]; for (int i = 0; i < len * 2; i++) { string val = intl.GetString(MAX_LEN, true, true, true); while (Array.IndexOf(intlValues, val) != -1) { val = intl.GetString(MAX_LEN, true, true, true); } 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; } } iCountTestcases++; ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (len)) { iCountErrors++; Console.WriteLine("Err_0005a, count is {0} instead of {1}", ld.Count, len); } destination = Array.CreateInstance(typeof(Object), len); ld.CopyTo(destination, 0); for (int i = 0; i < len; i++) { iCountTestcases++; if (String.Compare(ld[intlValues[i + len]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, ld[intlValues[i + len]]); } iCountTestcases++; if (String.Compare(intlValues[i + len], ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, intlValues[i + len]); } } Console.WriteLine("6. add intl strings and CopyTo(Array, {0})", len); strLoc = "Loc_006oo"; destination = Array.CreateInstance(typeof(Object), len * 2); ld.CopyTo(destination, len); for (int i = 0; i < len; i++) { iCountTestcases++; if (String.Compare(ld[intlValues[i + len]].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0006_{0}a, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, ld[intlValues[i + len]]); } iCountTestcases++; if (String.Compare(intlValues[i + len], ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0006_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, intlValues[i + len]); } } Console.WriteLine("7. case sensitivity"); strLoc = "Loc_007oo"; string [] intlValuesLower = new string [len * 2]; 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(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } destination = Array.CreateInstance(typeof(Object), len); ld.CopyTo(destination, 0); for (int i = 0; i < len; i++) { iCountTestcases++; if (String.Compare(ld[intlValues[i + len]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0006_{0}a, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, ld[intlValues[i + len]]); } iCountTestcases++; if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) > -1) { iCountErrors++; Console.WriteLine("Err_0006_{0}b, copied lowercase string"); } iCountTestcases++; if (String.Compare(intlValues[i + len], ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0006_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, intlValues[i + len]); } iCountTestcases++; if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) > -1) { iCountErrors++; Console.WriteLine("Err_0006_{0}d, copied lowercase key"); } } Console.WriteLine("8. CopyTo(null, int)"); strLoc = "Loc_008oo"; iCountTestcases++; destination = null; try { ld.CopyTo(destination, 0); iCountErrors++; Console.WriteLine("Err_0008a: no exception "); } catch (ArgumentNullException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0008b, unexpected exception: {0}", e.ToString()); } Console.WriteLine("9. CopyTo(Array, -1)"); strLoc = "Loc_009oo"; iCountTestcases++; cnt = ld.Count; destination = Array.CreateInstance(typeof(Object), 2); try { ld.CopyTo(destination, -1); iCountErrors++; Console.WriteLine("Err_0009b: no exception "); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0009c, unexpected exception: {0}", e.ToString()); } Console.WriteLine("10. CopyTo(Array, upperBound+1)"); strLoc = "Loc_0010oo"; iCountTestcases++; if (ld.Count < 1) { ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } } destination = Array.CreateInstance(typeof(Object), len); try { ld.CopyTo(destination, len); iCountErrors++; Console.WriteLine("Err_0010b: no exception "); } catch (ArgumentException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0010c, unexpected exception: {0}", e.ToString()); } Console.WriteLine("11. CopyTo(Array, upperBound+2)"); strLoc = "Loc_010oo"; iCountTestcases++; try { ld.CopyTo(destination, len + 1); iCountErrors++; Console.WriteLine("Err_0011b: no exception "); } catch (ArgumentException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0011c, unexpected exception: {0}", e.ToString()); } Console.WriteLine("12. CopyTo(Array, not_enough_space)"); strLoc = "Loc_012oo"; iCountTestcases++; try { ld.CopyTo(destination, len / 2); iCountErrors++; Console.WriteLine("Err_0012a: no exception "); } catch (ArgumentException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0012b, unexpected exception: {0}", e.ToString()); } Console.WriteLine("13. CopyTo(multidim_Array, 0)"); strLoc = "Loc_013oo"; iCountTestcases++; Array dest = Array.CreateInstance(typeof(string), len, len); try { ld.CopyTo(dest, 0); iCountErrors++; Console.WriteLine("Err_0013a: no exception "); } catch (ArgumentException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0013b, unexpected exception: {0}", e.ToString()); } Console.WriteLine("15. copy empty LD: CopyTo(Array, upperBound+1)"); strLoc = "Loc_0015oo"; iCountTestcases++; ld.Clear(); destination = Array.CreateInstance(typeof(Object), len); try { ld.CopyTo(destination, len); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0015a, unexpected exception: {0}", e.ToString()); } } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine(s_strTFAbbrev + " : Error Err_general! strLoc==" + strLoc + ", exc_general==\n" + exc_general.ToString()); } if (iCountErrors == 0) { Console.WriteLine("Pass. " + s_strTFPath + " " + s_strTFName + " ,iCountTestcases==" + iCountTestcases); return(true); } else { Console.WriteLine("Fail! " + s_strTFPath + " " + s_strTFName + " ,iCountErrors==" + iCountErrors + " , BugNums?: " + s_strActiveBugNums); return(false); } }
public static void CopyTo_ArgumentInvalidTest(ListDictionary ld, KeyValuePair<string, string>[] data) { Assert.Throws<ArgumentNullException>("array", () => ld.CopyTo(null, 0)); Assert.Throws<ArgumentOutOfRangeException>("index", () => ld.CopyTo(data, -1)); if (data.Length > 0) { Assert.Throws<ArgumentException>(() => ld.CopyTo(new KeyValuePair<string, string>[1, data.Length], 0)); Assert.Throws<ArgumentException>(() => ld.CopyTo(new KeyValuePair<string, string>[0], data.Length - 1)); Assert.Throws<ArgumentException>(() => ld.CopyTo(new KeyValuePair<string, string>[data.Length - 1], 0)); Assert.Throws<InvalidCastException>(() => ld.CopyTo(new int[data.Length], 0)); } }
public void CopyTo2() { ListDictionary ld = new ListDictionary(); ld.CopyTo(new int[1], -1); }
public static void CopyToTest(ListDictionary ld, KeyValuePair<string, string>[] data) { DictionaryEntry[] translated = data.Select(kv => new DictionaryEntry(kv.Key, kv.Value)).ToArray(); DictionaryEntry[] full = new DictionaryEntry[data.Length]; ld.CopyTo(full, 0); Assert.Equal(translated, full); DictionaryEntry[] large = new DictionaryEntry[data.Length * 2]; ld.CopyTo(large, data.Length / 2); for (int i = 0; i < large.Length; i++) { if (i < data.Length / 2 || i >= data.Length + data.Length / 2) { Assert.Equal(new DictionaryEntry(), large[i]); Assert.Null(large[i].Key); Assert.Null(large[i].Value); } else { Assert.Equal(translated[i - data.Length / 2], large[i]); } } }
public virtual bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; IntlStrings intl; String strLoc = "Loc_000oo"; ListDictionary ld; string [] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; string [] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; Array destination; int cnt = 0; try { intl = new IntlStrings(); Console.WriteLine("--- create dictionary ---"); strLoc = "Loc_001oo"; iCountTestcases++; ld = new ListDictionary(); Console.WriteLine("1. Copy empty dictionary into empty array"); iCountTestcases++; destination = Array.CreateInstance(typeof(Object), 0); Console.WriteLine(" destination Length = " + destination.Length); Console.WriteLine(" - CopyTo(arr, -1)"); try { ld.CopyTo(destination, -1); iCountErrors++; Console.WriteLine("Err_0001a, no exception"); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_001b, unexpected exception: {0}", e.ToString()); } iCountTestcases++; Console.WriteLine(" - CopyTo(arr, 0)"); try { ld.CopyTo(destination, 0); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_001c, unexpected exception: {0}", e.ToString()); } iCountTestcases++; Console.WriteLine(" - CopyTo(arr, 1)"); try { ld.CopyTo(destination, 1); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_001d, unexpected exception: {0}", e.ToString()); } Console.WriteLine("2. Copy empty dictionary into non-empty array"); iCountTestcases++; destination = Array.CreateInstance(typeof(Object), values.Length); for (int i = 0; i < values.Length; i++) { destination.SetValue(values[i], i); } ld.CopyTo(destination, 0); if( destination.Length != values.Length) { iCountErrors++; Console.WriteLine("Err_0002a, altered array after copying empty dictionary"); } if (destination.Length == values.Length) { for (int i = 0; i < values.Length; i++) { iCountTestcases++; if (String.Compare(destination.GetValue(i).ToString(), values[i], false) != 0) { iCountErrors++; Console.WriteLine("Err_0002_{0}b, altered item {0} after copying empty dictionary", i); } } } Console.WriteLine("3. add simple strings and CopyTo(Array, 0)"); strLoc = "Loc_003oo"; iCountTestcases++; cnt = ld.Count; int len = values.Length; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { iCountErrors++; Console.WriteLine("Err_0003a, count is {0} instead of {1}", ld.Count, values.Length); } destination = Array.CreateInstance(typeof(Object), len); ld.CopyTo(destination, 0); for (int i = 0; i < len; i++) { iCountTestcases++; if ( String.Compare(ld[keys[i]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), ld[keys[i]]); } iCountTestcases++; if ( String.Compare(keys[i], ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), keys[i]); } } Console.WriteLine("4. add simple strings and CopyTo(Array, {0})", values.Length); ld.Clear(); strLoc = "Loc_004oo"; iCountTestcases++; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { iCountErrors++; Console.WriteLine("Err_0004a, count is {0} instead of {1}", ld.Count, values.Length); } destination = Array.CreateInstance(typeof(Object), len*2); ld.CopyTo(destination, len); for (int i = 0; i < len; i++) { iCountTestcases++; if ( String.Compare(ld[keys[i]].ToString(), ((DictionaryEntry)destination.GetValue(i+len)).Value.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i+len)).Value, ld[keys[i]]); } iCountTestcases++; if ( String.Compare(keys[i], ((DictionaryEntry)destination.GetValue(i+len)).Key.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i+len)).Key, keys[i]); } } Console.WriteLine("5. add intl strings and CopyTo(Array, 0)"); strLoc = "Loc_005oo"; iCountTestcases++; string [] intlValues = new string [len * 2]; for (int i = 0; i < len*2; i++) { string val = intl.GetString(MAX_LEN, true, true, true); while (Array.IndexOf(intlValues, val) != -1 ) val = intl.GetString(MAX_LEN, true, true, true); 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; } iCountTestcases++; ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i+len], intlValues[i]); } if ( ld.Count != (len) ) { iCountErrors++; Console.WriteLine("Err_0005a, count is {0} instead of {1}", ld.Count, len); } destination = Array.CreateInstance(typeof(Object), len); ld.CopyTo(destination, 0); for (int i = 0; i < len; i++) { iCountTestcases++; if ( String.Compare(ld[intlValues[i+len]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, ld[intlValues[i+len]]); } iCountTestcases++; if ( String.Compare(intlValues[i+len], ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, intlValues[i+len]); } } Console.WriteLine("6. add intl strings and CopyTo(Array, {0})", len); strLoc = "Loc_006oo"; destination = Array.CreateInstance(typeof(Object), len*2); ld.CopyTo(destination, len); for (int i = 0; i < len; i++) { iCountTestcases++; if ( String.Compare(ld[intlValues[i+len]].ToString(), ((DictionaryEntry)destination.GetValue(i+len)).Value.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0006_{0}a, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i+len)).Value, ld[intlValues[i+len]]); } iCountTestcases++; if ( String.Compare(intlValues[i+len], ((DictionaryEntry)destination.GetValue(i+len)).Key.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0006_{0}b, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i+len)).Key, intlValues[i+len]); } } Console.WriteLine("7. case sensitivity"); strLoc = "Loc_007oo"; string [] intlValuesLower = new string [len * 2]; 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(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i+len], intlValues[i]); } destination = Array.CreateInstance(typeof(Object), len); ld.CopyTo(destination, 0); for (int i = 0; i < len; i++) { iCountTestcases++; if ( String.Compare(ld[intlValues[i+len]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0006_{0}a, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, ld[intlValues[i+len]]); } iCountTestcases++; if ( !caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) > -1 ) { iCountErrors++; Console.WriteLine("Err_0006_{0}b, copied lowercase string"); } iCountTestcases++; if ( String.Compare(intlValues[i+len], ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0006_{0}c, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, intlValues[i+len]); } iCountTestcases++; if ( !caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) > -1 ) { iCountErrors++; Console.WriteLine("Err_0006_{0}d, copied lowercase key"); } } Console.WriteLine("8. CopyTo(null, int)"); strLoc = "Loc_008oo"; iCountTestcases++; destination = null; try { ld.CopyTo(destination, 0); iCountErrors++; Console.WriteLine("Err_0008a: no exception "); } catch (ArgumentNullException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0008b, unexpected exception: {0}", e.ToString()); } Console.WriteLine("9. CopyTo(Array, -1)"); strLoc = "Loc_009oo"; iCountTestcases++; cnt = ld.Count; destination = Array.CreateInstance(typeof(Object), 2); try { ld.CopyTo(destination, -1); iCountErrors++; Console.WriteLine("Err_0009b: no exception "); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0009c, unexpected exception: {0}", e.ToString()); } Console.WriteLine("10. CopyTo(Array, upperBound+1)"); strLoc = "Loc_0010oo"; iCountTestcases++; if (ld.Count < 1) { ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } } destination = Array.CreateInstance(typeof(Object), len); try { ld.CopyTo(destination, len); iCountErrors++; Console.WriteLine("Err_0010b: no exception "); } catch (IndexOutOfRangeException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0010c, unexpected exception: {0}", e.ToString()); } Console.WriteLine("11. CopyTo(Array, upperBound+2)"); strLoc = "Loc_010oo"; iCountTestcases++; try { ld.CopyTo(destination, len+1); iCountErrors++; Console.WriteLine("Err_0011b: no exception "); } catch (IndexOutOfRangeException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0011c, unexpected exception: {0}", e.ToString()); } Console.WriteLine("12. CopyTo(Array, not_enough_space)"); strLoc = "Loc_012oo"; iCountTestcases++; try { ld.CopyTo(destination, len / 2); iCountErrors++; Console.WriteLine("Err_0012a: no exception "); } catch (IndexOutOfRangeException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0012b, unexpected exception: {0}", e.ToString()); } Console.WriteLine("13. CopyTo(multidim_Array, 0)"); strLoc = "Loc_013oo"; iCountTestcases++; Array dest = Array.CreateInstance(typeof(string), len, len); try { ld.CopyTo(dest, 0); iCountErrors++; Console.WriteLine("Err_0013a: no exception "); } catch (ArgumentException ex) { Console.WriteLine(" Expected exception: {0}", ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0013b, unexpected exception: {0}", e.ToString()); } Console.WriteLine("15. copy empty LD: CopyTo(Array, upperBound+1)"); strLoc = "Loc_0015oo"; iCountTestcases++; ld.Clear(); destination = Array.CreateInstance(typeof(Object), len); try { ld.CopyTo(destination, len); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0015a, unexpected exception: {0}", e.ToString()); } } catch (Exception exc_general ) { ++iCountErrors; Console.WriteLine (s_strTFAbbrev + " : Error Err_general! strLoc=="+ strLoc +", exc_general==\n"+exc_general.ToString()); } if ( iCountErrors == 0 ) { Console.WriteLine( "Pass. "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases); return true; } else { Console.WriteLine("Fail! "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums ); return false; } }
//callback fired when an address change occurs private static void AddressChangedCallback(object stateObject, bool signaled) { DictionaryEntry[] callerArray = null; lock (s_globalLock) { //the listener was cancelled, which would only happen if we aren't listening //for more events. s_isPending = false; if (!s_isListening) { return; } s_isListening = false; if (s_callerArray.Count > 0) { // Need to copy the array so the callback can call start and stop callerArray = new DictionaryEntry[s_callerArray.Count]; s_callerArray.CopyTo(callerArray, 0); } try { //wait for the next address change StartHelper(null, false, (StartIPOptions)stateObject); } catch (NetworkInformationException nie) { if (Logging.On) { Logging.Exception(Logging.Web, "AddressChangeListener", "AddressChangedCallback", nie); } } } // Release the s_globalLock before calling into user callback. if (callerArray != null) { // Synchronization dedicated just to the invocation of the callback, // for back compat reason. lock (s_protectCallbackLock) { Debug.Assert(!Monitor.IsEntered(s_globalLock), "Should not invoke user callback while holding globalLock"); foreach (var entry in callerArray) { NetworkAddressChangedEventHandler handler = (NetworkAddressChangedEventHandler)entry.Key; ExecutionContext context = (ExecutionContext)entry.Value; if (context == null) { handler(null, EventArgs.Empty); } else { ExecutionContext.Run(context.CreateCopy(), s_runHandlerCallback, handler); } } } } }
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" }; // string [] destination; Array destination; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] CopyTo empty dictionary into empty array // destination = new Object[0]; Assert.Throws <ArgumentOutOfRangeException>(() => { ld.CopyTo(destination, -1); }); ld.CopyTo(destination, 0); // exception even when copying empty dictionary Assert.Throws <ArgumentException>(() => { ld.CopyTo(destination, 1); }); // [] CopyTo empty dictionary into filled array // destination = new Object[values.Length]; for (int i = 0; i < values.Length; i++) { destination.SetValue(values[i], i); } ld.CopyTo(destination, 0); if (destination.Length != values.Length) { Assert.False(true, string.Format("Error, altered array after copying empty dictionary")); } if (destination.Length == values.Length) { for (int i = 0; i < values.Length; i++) { if (String.Compare(destination.GetValue(i).ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, altered item {0} after copying empty dictionary", i)); } } } // // [] CopyTo(Array, 0) for 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)); } destination = new Object[len]; ld.CopyTo(destination, 0); // // for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // if (String.Compare(ld[keys[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(), ld[keys[i]])); } if (String.Compare(keys[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(), keys[i])); } } // // [] CopyTo(Array, middle_index) for dictionary filled with simple strings // 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, values.Length)); } destination = new Object[len * 2]; ld.CopyTo(destination, len); // // for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // if (String.Compare(ld[keys[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, ld[keys[i]])); } // verify keys if (String.Compare(keys[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, keys[i])); } } // // Intl strings // // [] CopyTo(Array, 0) 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; } 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)); } destination = new Object[len]; ld.CopyTo(destination, 0); // // for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // if (String.Compare(ld[intlValues[i + len]].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, ld[intlValues[i + len]])); } // verify keys if (String.Compare(intlValues[i + len], ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, intlValues[i + len])); } } // // Intl strings // // [] CopyTo(Array, middle_index) for dictionary filled with Intl strings // destination = new Object[len * 2]; ld.CopyTo(destination, len); // // order of items is the same as they were in dictionary // for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // if (String.Compare(ld[intlValues[i + len]].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, ld[intlValues[i + len]])); } // verify keys if (String.Compare(intlValues[i + len], ((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, intlValues[i + len])); } } // // [] 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 } destination = new Object[len]; ld.CopyTo(destination, 0); // // for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // if (String.Compare(ld[intlValues[i + len]].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, ld[intlValues[i + len]])); } if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) > -1) { Assert.False(true, string.Format("Error, copied lowercase string")); } // verify keys if (String.Compare(intlValues[i + len], ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, intlValues[i + len])); } if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) > -1) { Assert.False(true, string.Format("Error, copied lowercase key")); } } // // [] CopyTo(null, int) // destination = null; Assert.Throws <ArgumentNullException>(() => { ld.CopyTo(destination, 0); }); // // [] CopyTo(Array, -1) // cnt = ld.Count; destination = new Object[2]; Assert.Throws <ArgumentOutOfRangeException>(() => { ld.CopyTo(destination, -1); }); // // [] CopyTo(Array, upperBound+1) // if (ld.Count < 1) { ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } } destination = new Object[len]; Assert.Throws <ArgumentException>(() => { ld.CopyTo(destination, len); }); // // [] CopyTo(Array, upperBound+2) // Assert.Throws <ArgumentException>(() => { ld.CopyTo(destination, len + 1); }); // // [] CopyTo(Array, not_enough_space) // Assert.Throws <ArgumentException>(() => { ld.CopyTo(destination, len / 2); }); // // [] CopyTo(multidim_Array, 0) // Array dest = new string[len, len]; Assert.Throws <ArgumentException>(() => { ld.CopyTo(dest, 0); }); // // [] CopyTo(wrong_type, 0) // dest = new ArrayList[len]; Assert.Throws <InvalidCastException>(() => { ld.CopyTo(dest, 0); }); // // [] CopyTo(Array, upperBound+1) - copy empty dictionary - no exception // ld.Clear(); destination = new Object[len]; ld.CopyTo(destination, len); }
static void Main(string[] args) { Console.WriteLine("============StringCollection==========="); String[] myArr = new String[] { "red", "orange", "yellow", "green", "blue", "indigo", "violet" }; StringCollection myCol = new StringCollection(); myCol.Add("Apple"); myCol.Add("Orange"); myCol.Add("Mango"); myCol.AddRange(myArr); foreach (var item2 in myCol) { Console.WriteLine(item2); } Console.WriteLine("count of string collection: " + myCol.Count); Console.WriteLine("CopyTo:====="); string[] str1 = new string[myCol.Count]; myCol.CopyTo(str1, 0); foreach (var item1 in str1) { Console.WriteLine(item1); } Console.WriteLine("contains method"); Console.WriteLine(myCol.Contains("Mango")); Console.WriteLine("Insert: dear"); myCol.Insert(5, "dear"); StringEnumerator myEnumerator = myCol.GetEnumerator(); while (myEnumerator.MoveNext()) { Console.WriteLine("{0}", myEnumerator.Current); } Console.WriteLine(); Console.WriteLine("============StringDictionary==========="); StringDictionary myCol1 = new StringDictionary(); myCol1.Add("red", "rojo"); myCol1.Add("green", "verde"); myCol1.Add("blue", "azul"); Console.WriteLine("contains key red :{0}", myCol1.ContainsKey("red")); Console.WriteLine("containe value 'azul': {0}", myCol1.ContainsValue("azul")); Console.WriteLine("remove key------"); myCol1.Remove("red"); IEnumerator myEnumerator1 = myCol1.GetEnumerator(); foreach (DictionaryEntry de in myCol1) { Console.WriteLine("{0} {1}", de.Key, de.Value); } Console.WriteLine(); Console.WriteLine("============ListDictionary==========="); ListDictionary myCol2 = new ListDictionary(); myCol2.Add("Braeburn Apples", "1.49"); myCol2.Add("Fuji Apples", "1.29"); myCol2.Add("Gala Apples", "1.49"); myCol2.Add("Golden Delicious Apples", "1.29"); myCol2.Add("Granny Smith Apples", "0.89"); myCol2.Add("Red Delicious Apples", "0.99"); Console.WriteLine("CopyTo==============="); DictionaryEntry[] myArr1 = new DictionaryEntry[myCol2.Count]; myCol2.CopyTo(myArr1, 0); foreach (var arr1 in myArr1) { Console.WriteLine("{0}-->{1}", arr1.Key, arr1.Value); } Console.WriteLine("Printing the element list using GetEnumerator method-----"); IEnumerator myEnumerator2 = myCol2.GetEnumerator(); foreach (DictionaryEntry de in myCol2) { Console.WriteLine("{0} {1}", de.Key, de.Value); } Console.WriteLine(); Console.WriteLine("============HybridDictionary==========="); HybridDictionary myCol3 = new HybridDictionary(); myCol3.Add("Braeburn Apples", "1.49"); myCol3.Add("Fuji Apples", "1.29"); myCol3.Add("Gala Apples", "1.49"); myCol3.Add("Golden Delicious Apples", "1.29"); myCol3.Add("Granny Smith Apples", "0.89"); myCol3.Add("Red Delicious Apples", "0.99"); myCol3.Add("Plantain Bananas", "1.49"); myCol3.Add("Yellow Bananas", "0.79"); foreach (DictionaryEntry de in myCol3) { Console.WriteLine(" {0,-25} {1}", de.Key, de.Value); } Console.WriteLine(); Console.WriteLine("Number of elements in myDict are :{0} ", myCol3.Count); Console.WriteLine("After Remove======="); myCol3.Remove("Gala Apples"); foreach (DictionaryEntry de in myCol3) { Console.WriteLine(" {0,-25} {1}", de.Key, de.Value); } }