public static IEnumerable <ReflectPropertyDescriptorInfo> Find() { ReflectionPermission perm = new ReflectionPermission(PermissionState.Unrestricted); if (perm.IsUnrestricted()) { Type reflectType = typeof(PropertyDescriptor).Module.GetType("System.ComponentModel.ReflectTypeDescriptionProvider"); FieldInfo propertyCacheFieldInfo = reflectType.GetField("_propertyCache", BindingFlags.Static | BindingFlags.NonPublic); Hashtable propertyCache = (Hashtable)propertyCacheFieldInfo.GetValue((object)null); if (propertyCache != null) { DictionaryEntry[] entries = new DictionaryEntry[propertyCache.Count]; propertyCache.CopyTo((Array)entries, 0); FieldInfo valueChangedHandlersFieldInfo = typeof(PropertyDescriptor).GetField("valueChangedHandlers", BindingFlags.Instance | BindingFlags.NonPublic); foreach (DictionaryEntry dictionaryEntry in entries) { PropertyDescriptor[] propertyDescriptors = (PropertyDescriptor[])dictionaryEntry.Value; if (propertyDescriptors != null) { foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors) { Hashtable valueChangedHandlers = (Hashtable)valueChangedHandlersFieldInfo.GetValue((object)propertyDescriptor); if (valueChangedHandlers != null && valueChangedHandlers.Count != 0) { yield return(new ReflectPropertyDescriptorInfo(dictionaryEntry.Key.ToString(), propertyDescriptor.Name, valueChangedHandlers.Count)); } } } } } } }
/// <summary> /// Copies the objects to an Array, starting at a the specified index. /// </summary> /// <param name="array">The one-dimensional Array that is the destination for the objects.</param> /// <param name="index">The zero-based index in the Array at which copying begins.</param> public virtual void CopyTo(Array array, int index) { if (_dictionary != null) { _dictionary.CopyTo(array, index); } }
Vector2 getAdjacentEmptyCoord(Vector2 arg) { Hashtable coords = new Hashtable(); if (!map.Contains(arg + Vector2.up)) { coords.Add(0, arg + Vector2.up); } if (!map.Contains(arg + Vector2.right)) { coords.Add(1, arg + Vector2.right); } if (!map.Contains(arg + Vector2.down)) { coords.Add(2, arg + Vector2.down); } if (!map.Contains(arg + Vector2.left)) { coords.Add(3, arg + Vector2.left); } if (coords.Count <= 0) { //return (Vector2) null; } DictionaryEntry[] coord = new DictionaryEntry[coords.Count]; coords.CopyTo(coord, 0); //string s = ""; // foreach(DictionaryEntry en in coord) { // s += en.Key.ToString() + " " + en.Value.ToString(); // } // print(s); return((Vector2)coord[Random.Range(0, coords.Count)].Value); }
/// <summary> /// /// </summary> /// <param name="array"></param> /// <param name="index"></param> public void CopyTo( Array array, int index ) { m_htFwd.CopyTo(array, index); }
public void TestCopyTo2() { SetDefaultData(); Object[] entries = new Object[ht.Count]; ht.CopyTo(entries, 0); Assert.IsTrue(entries[0] is DictionaryEntry, "Not an entry."); }
static void Main(string[] args) { Utility.showTitle("Demonstrate Collections.Hashtable"); Hashtable S = new Hashtable(); S.Add("first", 1.5); S.Add("second", 2.0); S["third"] = 2.5; S["fourth"] = 3.0; S["fifth"] = 3.5; Utility.show(S); S["fourth"] = 3.75; Utility.show(S); Console.Write( "\n value of key {0} is {1}", "\"third\"", S["third"] ); Console.WriteLine(); DictionaryEntry[] values = new DictionaryEntry[S.Count]; S.CopyTo(values, 0); Utility.show(values); Console.Write("\n"); }
// test CopyTo on a empty HashTable public void TestHashTableCopyToEmpty () { Hashtable hashTable = new Hashtable (); AssertEquals ("count", 0, hashTable.Count); object[] array = new object [hashTable.Count]; hashTable.CopyTo (array, 0); }
///<summary> ///Copies the elements of the <see cref="T:System.Collections.ICollection"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index. ///</summary> ///<param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing. </param> ///<param name="index">The zero-based index in array at which copying begins. </param> ///<exception cref="T:System.ArgumentNullException">array is null. </exception> ///<exception cref="T:System.ArgumentException">The type of the source <see cref="T:System.Collections.ICollection"></see> cannot be cast automatically to the type of the destination array. </exception> ///<exception cref="T:System.ArgumentOutOfRangeException">index is less than zero. </exception> ///<exception cref="T:System.ArgumentException">array is multidimensional.-or- index is equal to or greater than the length of array.-or- The number of elements in the source <see cref="T:System.Collections.ICollection"></see> is greater than the available space from index to the end of the destination array. </exception><filterpriority>2</filterpriority> public void CopyTo(Array array, int index) { lock (SyncRoot) { _table.CopyTo(array, index); } }
public object ToArray(Type type) { Array list = Array.CreateInstance(type, table.Count); table.CopyTo(list, 0); return(list); }
protected void Session_End(Object sender, EventArgs e) { //--Michael Hashtable sessionIDs = (Hashtable)this.Application["SessionIDs"]; sessionIDs = Hashtable.Synchronized(sessionIDs); //For multithread safety using following code DictionaryEntry[] userIDToSessionID = new DictionaryEntry[sessionIDs.Count]; sessionIDs.CopyTo(userIDToSessionID, 0); foreach (DictionaryEntry de in userIDToSessionID) { if ((string)de.Value == this.Session.SessionID) { sessionIDs.Remove(de.Key); break; } } if (this.Session["Token"] != null && this.Context != null) { Token token = (Token)this.Session["Token"]; DealingConsoleServer.SaveLog2(token, this.Context.Request.UserHostAddress.ToString(), "Logout", "Logout", Guid.Empty, ""); this.StateServer.Logout((Token)this.Session["Token"]); } // Hashtable sessions = (Hashtable)this.Context.Application["Sessions"]; // this.Context.Application.Lock(); // sessions.Remove(this.Context.User); // this.Context.Application.UnLock(); }
/// <summary> /// /// </summary> /// <param name="array"></param> /// <param name="index"></param> public void CopyValuesTo( Array array, int index ) { m_htBkwd.CopyTo(array, index); }
// test CopyTo on a empty HashTable public void TestHashTableCopyToEmpty() { Hashtable hashTable = new Hashtable(); AssertEquals("count", 0, hashTable.Count); object[] array = new object [hashTable.Count]; hashTable.CopyTo(array, 0); }
public void CopyTo_Empty() { Hashtable ht = new Hashtable(); Assert.AreEqual(0, ht.Count, "Count"); object[] array = new object [ht.Count]; ht.CopyTo(array, 0); }
public void CopyTo_Empty() { Hashtable ht = new Hashtable(); AssertEquals("Count", 0, ht.Count); object[] array = new object[ht.Count]; ht.CopyTo(array, 0); }
static void Test1() { Hashtable h = new Hashtable(); h['a'] = 1; h['b'] = 2; object[] o = new object[2]; h.CopyTo(o, 0); }
internal DictionaryEntry [] LocalsAtDepth(int depth) { object [] hashes = new object [locals.Count]; locals.CopyTo(hashes, 0); Hashtable hash = (Hashtable)hashes [locals.Count - depth - 1]; DictionaryEntry [] _locals = new DictionaryEntry [hash.Count]; hash.CopyTo(_locals, 0); return(_locals); }
static int CopyTo(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 3); Hashtable obj = LuaScriptMgr.GetNetObject <Hashtable>(L, 1); Array arg0 = LuaScriptMgr.GetNetObject <Array>(L, 2); int arg1 = (int)LuaScriptMgr.GetNumber(L, 3); obj.CopyTo(arg0, arg1); return(0); }
public static List <object> GetReversedHashtableKeys(Hashtable theTable) { object[] aReverseArray = new object[theTable.Count]; theTable.CopyTo(aReverseArray, 0); List <object> aReverseList = new List <object> (aReverseArray); aReverseList.Reverse(); return(aReverseList); }
public void CopyTo() { object[] c = new object[15]; _theHashtable.CopyTo(c, 2); Assert.AreEqual(15, c.Length); Assert.IsNull(c[0]); Assert.IsNull(c[1]); Assert.IsNotNull(c[2]); Assert.IsNotNull(c[14]); }
public void CopyTo(Array array, int index) { if (_hashtable != null) { _hashtable.CopyTo(array, index); } else { List.CopyTo(array, index); } }
// Implement the ICollection interface. public void CopyTo(Array array, int index) { if (hash != null) { hash.CopyTo(array, index); } else { list.CopyTo(array, index); } }
public void CopyTo(Array array, int arrayIndex) { _lock.EnterWriteLock(); try { currentList.CopyTo(array, arrayIndex); } finally { _lock.ExitWriteLock(); } }
/// <summary>Implements the <see cref="T:System.Runtime.Serialization.ISerializable" /> interface and returns the data needed to serialize the <see cref="T:System.Collections.Specialized.OrderedDictionary" /> collection.</summary> /// <param name="info">A <see cref="T:System.Runtime.Serialization.SerializationInfo" /> object containing the information required to serialize the <see cref="T:System.Collections.Specialized.OrderedDictionary" /> collection.</param> /// <param name="context">A <see cref="T:System.Runtime.Serialization.StreamingContext" /> object containing the source and destination of the serialized stream associated with the <see cref="T:System.Collections.Specialized.OrderedDictionary" />.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="info" /> is null.</exception> public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } info.AddValue("KeyComparer", comparer, typeof(IEqualityComparer)); info.AddValue("ReadOnly", readOnly); info.AddValue("InitialCapacity", initialCapacity); object[] array = new object[hash.Count]; hash.CopyTo(array, 0); info.AddValue("ArrayList", array); }
void readGuideStep(System.Security.SecurityElement se, GuideGroup guideGroup) { foreach (System.Security.SecurityElement child in se.Children) { Hashtable hash = child.Attributes; DictionaryEntry[] array = new DictionaryEntry[hash.Count]; hash.CopyTo(array, 0); GuideStep guideStep = new GuideStep(); for (int i = 0; i < array.Length; i++) { DictionaryEntry attr = array[i]; string strKey = (string)attr.Key; string strValue = (string)attr.Value; if (strKey == ("id")) { guideStep._id = uint.Parse(strValue); } else if (strKey == "step_name") { guideStep._guideShowName = strValue; } else if (strKey == "step_para") { guideStep._step_para = strValue; } else if (strKey == "end_trigger_type") { guideStep._end_trigger_type = strValue; } else if (strKey == "end_trigger_para") { guideStep._end_trigger_para = strValue; } else if (strKey == "begin_reset") { guideStep._begin_reset = int.Parse(strValue); } else if (strKey == "end_reset") { guideStep._end_reset = int.Parse(strValue); } else if (strKey == "guide_arrow_pos") { guideStep._guideArrowPos = strValue; } } guideGroup._guideSteps.Add(guideStep._id, guideStep); } }
public void Create() { Hashtable H = new Hashtable(); H.Add(1, "hello"); H.Add(2, "hi"); H.Add(3, "hmm"); H.Add(4, "ahh"); Console.WriteLine($"this is count of H {H.Count}"); object[] o1 = new object[H.Count]; H.CopyTo(o1, 0); Console.WriteLine(H.ContainsKey(1)); Console.WriteLine(H.ContainsValue("hi")); Console.WriteLine(H.Contains(3)); }
static void Test2() { try { Hashtable h = new Hashtable(); h["blue"] = 1; h["green"] = 2; h["red"] = 3; Char[] o = new Char[3]; h.CopyTo(o, 0); } catch (InvalidCastException) { Console.WriteLine("ok"); return; } Console.WriteLine("invalid cast error not thrown"); }
public static Hashtable applyKeepWords(Hashtable centroidValues, int keepWords) { DictionaryEntry[] centValuesArr = new DictionaryEntry[centroidValues.Count]; centroidValues.CopyTo(centValuesArr, 0); Array.Sort(centValuesArr, new DictionaryEntryValueComparer()); Array.Reverse(centValuesArr); Hashtable finalCentroidValues = new Hashtable(); for (int i = 0; i < keepWords && i < centValuesArr.Length; i++) { DictionaryEntry entry = centValuesArr[i]; finalCentroidValues.Add(entry.Key, entry.Value); } return(finalCentroidValues); }
public void TestCtorIntSingle() { // variables used for tests Hashtable hash = null; // [] should get ArgumentException if trying to have large num of entries Assert.Throws <ArgumentException>(() => { hash = new Hashtable(int.MaxValue, .1f); } ); // []should not get any exceptions for valid values - we also check that the HT works here hash = new Hashtable(100, .1f); int iNumberOfElements = 100; for (int i = 0; i < iNumberOfElements; i++) { hash.Add("Key_" + i, "Value_" + i); } //Count Assert.Equal(hash.Count, iNumberOfElements); DictionaryEntry[] strValueArr = new DictionaryEntry[hash.Count]; hash.CopyTo(strValueArr, 0); Hashtable hsh3 = new Hashtable(); for (int i = 0; i < iNumberOfElements; i++) { Assert.True(hash.Contains("Key_" + i), "Error, Expected value not returned, " + hash.Contains("Key_" + i)); Assert.True(hash.ContainsKey("Key_" + i), "Error, Expected value not returned, " + hash.ContainsKey("Key_" + i)); Assert.True(hash.ContainsValue("Value_" + i), "Error, Expected value not returned, " + hash.ContainsValue("Value_" + i)); //we still need a way to make sure that there are all these unique values here -see below code for that Assert.True(hash.ContainsValue(((DictionaryEntry)strValueArr[i]).Value), "Error, Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value); hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null); } }
static void Main() { Hashtable hsTbl = new Hashtable(); hsTbl.Add(1, "Suhas"); hsTbl.Add(2, "Madhuri"); hsTbl.Add(3, "Om"); DictionaryEntry[] entries = new DictionaryEntry[hsTbl.Count]; hsTbl.CopyTo(entries, 0); hsTbl.Clear(); foreach (DictionaryEntry de in entries) { hsTbl.Add(de.Value, de.Key); } // check it worked foreach (DictionaryEntry de in hsTbl) { Console.WriteLine("{0} : {1}", de.Key, de.Value); } }
public void CopyTo(Array array, int index) { hashtable.CopyTo(array, index); }
void ICollection.CopyTo(Array array, int index) { _innerHash.CopyTo(array, index); }
public void TestSynchronizedBasic() { Hashtable hsh1; string strValue; Task[] workers; Action ts1; int iNumberOfWorkers = 3; DictionaryEntry[] strValueArr; string[] strKeyArr; Hashtable hsh3; Hashtable hsh4; IDictionaryEnumerator idic; object oValue; //[]Vanila - Syncronized returns a wrapped HT. We must make sure that all the methods //are accounted for here for the wrapper hsh1 = new Hashtable(); for (int i = 0; i < _iNumberOfElements; i++) { hsh1.Add("Key_" + i, "Value_" + i); } _hsh2 = Hashtable.Synchronized(hsh1); //Count Assert.Equal(_hsh2.Count, hsh1.Count); //get/set item for (int i = 0; i < _iNumberOfElements; i++) { Assert.True(((string)_hsh2["Key_" + i]).Equals("Value_" + i)); } Assert.Throws<ArgumentNullException>(() => { oValue = _hsh2[null]; }); _hsh2.Clear(); for (int i = 0; i < _iNumberOfElements; i++) { _hsh2["Key_" + i] = "Value_" + i; } strValueArr = new DictionaryEntry[_hsh2.Count]; _hsh2.CopyTo(strValueArr, 0); //ContainsXXX hsh3 = new Hashtable(); for (int i = 0; i < _iNumberOfElements; i++) { Assert.True(_hsh2.Contains("Key_" + i)); Assert.True(_hsh2.ContainsKey("Key_" + i)); Assert.True(_hsh2.ContainsValue("Value_" + i)); //we still need a way to make sure that there are all these unique values here -see below code for that Assert.True(hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value)); hsh3.Add(strValueArr[i], null); } hsh4 = (Hashtable)_hsh2.Clone(); Assert.Equal(hsh4.Count, hsh1.Count); strValueArr = new DictionaryEntry[hsh4.Count]; hsh4.CopyTo(strValueArr, 0); //ContainsXXX hsh3 = new Hashtable(); for (int i = 0; i < _iNumberOfElements; i++) { Assert.True(hsh4.Contains("Key_" + i)); Assert.True(hsh4.ContainsKey("Key_" + i)); Assert.True(hsh4.ContainsValue("Value_" + i)); //we still need a way to make sure that there are all these unique values here -see below code for that Assert.True(hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value)); hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null); } Assert.False(hsh4.IsReadOnly); Assert.True(hsh4.IsSynchronized); //Phew, back to other methods idic = _hsh2.GetEnumerator(); hsh3 = new Hashtable(); hsh4 = new Hashtable(); while (idic.MoveNext()) { Assert.True(_hsh2.ContainsKey(idic.Key)); Assert.True(_hsh2.ContainsValue(idic.Value)); hsh3.Add(idic.Key, null); hsh4.Add(idic.Value, null); } hsh4 = (Hashtable)_hsh2.Clone(); strValueArr = new DictionaryEntry[hsh4.Count]; hsh4.CopyTo(strValueArr, 0); hsh3 = new Hashtable(); for (int i = 0; i < _iNumberOfElements; i++) { Assert.True(hsh4.Contains("Key_" + i)); Assert.True(hsh4.ContainsKey("Key_" + i)); Assert.True(hsh4.ContainsValue("Value_" + i)); //we still need a way to make sure that there are all these unique values here -see below code for that Assert.True(hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value)); hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null); } Assert.False(hsh4.IsReadOnly); Assert.True(hsh4.IsSynchronized); //Properties Assert.False(_hsh2.IsReadOnly); Assert.True(_hsh2.IsSynchronized); Assert.Equal(_hsh2.SyncRoot, hsh1.SyncRoot); //we will test the Keys & Values string[] strValueArr11 = new string[hsh1.Count]; strKeyArr = new string[hsh1.Count]; _hsh2.Keys.CopyTo(strKeyArr, 0); _hsh2.Values.CopyTo(strValueArr11, 0); hsh3 = new Hashtable(); hsh4 = new Hashtable(); for (int i = 0; i < _iNumberOfElements; i++) { Assert.True(_hsh2.ContainsKey(strKeyArr[i])); Assert.True(_hsh2.ContainsValue(strValueArr11[i])); hsh3.Add(strKeyArr[i], null); hsh4.Add(strValueArr11[i], null); } //now we test the modifying methods _hsh2.Remove("Key_1"); Assert.False(_hsh2.ContainsKey("Key_1")); Assert.False(_hsh2.ContainsValue("Value_1")); _hsh2.Add("Key_1", "Value_1"); Assert.True(_hsh2.ContainsKey("Key_1")); Assert.True(_hsh2.ContainsValue("Value_1")); _hsh2["Key_1"] = "Value_Modified_1"; Assert.True(_hsh2.ContainsKey("Key_1")); Assert.False(_hsh2.ContainsValue("Value_1")); /////////////////////////// Assert.True(_hsh2.ContainsValue("Value_Modified_1")); hsh3 = Hashtable.Synchronized(_hsh2); //we are not going through all of the above again:) we will just make sure that this syncrnized and that //values are there Assert.Equal(hsh3.Count, hsh1.Count); Assert.True(hsh3.IsSynchronized); _hsh2.Clear(); Assert.Equal(_hsh2.Count, 0); //[] Synchronized returns a HT that is thread safe // We will try to test this by getting a number of threads to write some items // to a synchronized IList hsh1 = new Hashtable(); _hsh2 = Hashtable.Synchronized(hsh1); workers = new Task[iNumberOfWorkers]; for (int i = 0; i < workers.Length; i++) { var name = "Thread worker " + i; ts1 = new Action(() => AddElements(name)); workers[i] = Task.Run(ts1); } Task.WaitAll(workers); //checking time Assert.Equal(_hsh2.Count, _iNumberOfElements * iNumberOfWorkers); for (int i = 0; i < iNumberOfWorkers; i++) { for (int j = 0; j < _iNumberOfElements; j++) { strValue = "Thread worker " + i + "_" + j; Assert.True(_hsh2.Contains(strValue)); } } //I dont think that we can make an assumption on the order of these items though //now we are going to remove all of these workers = new Task[iNumberOfWorkers]; for (int i = 0; i < workers.Length; i++) { var name = "Thread worker " + i; ts1 = new Action(() => RemoveElements(name)); workers[i] = Task.Run(ts1); } Task.WaitAll(workers); Assert.Equal(_hsh2.Count, 0); Assert.False(hsh1.IsSynchronized); Assert.True(_hsh2.IsSynchronized); //[] Tyr calling Synchronized with null Assert.Throws<ArgumentNullException>(() => { Hashtable.Synchronized(null); } ); }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver : " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; Hashtable hsh1; String strValue; Thread[] workers; ThreadStart ts1; Int32 iNumberOfWorkers = 15; Boolean fLoopExit; DictionaryEntry[] strValueArr; String[] strKeyArr; Hashtable hsh3; Hashtable hsh4; IDictionaryEnumerator idic; MemoryStream ms1; Boolean fPass; Object oValue; try { do { hsh1 = new Hashtable(); for(int i=0; i<iNumberOfElements; i++) { hsh1.Add("Key_" + i, "Value_" + i); } hsh2 = Hashtable.Synchronized(hsh1); fPass = true; iCountTestcases++; if(hsh2.Count != hsh1.Count) { iCountErrors++; Console.WriteLine("Err_742dsf! Expected value not returned, " + hsh2.Count); } for(int i=0; i<iNumberOfElements; i++) { if(!((String)hsh2["Key_" + i]).Equals("Value_" + i)) { Console.WriteLine(hsh2["Key_" + i]); fPass = false; } } try { oValue = hsh2[null]; fPass = false; } catch(ArgumentNullException) { } catch(Exception) { fPass = false; } hsh2.Clear(); for(int i=0; i<iNumberOfElements; i++) { hsh2["Key_" + i] = "Value_" + i; } if(!fPass) { iCountErrors++; Console.WriteLine("Err_752dsgf! Oh man! This is busted!!!!"); } strValueArr = new DictionaryEntry[hsh2.Count]; hsh2.CopyTo(strValueArr, 0); hsh3 = new Hashtable(); for(int i=0; i<iNumberOfElements; i++) { if(!hsh2.Contains("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742ds8f! Expected value not returned, " + hsh2.Contains("Key_" + i)); } if(!hsh2.ContainsKey("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742389dsaf! Expected value not returned, " + hsh2.ContainsKey("Key_" + i)); } if(!hsh2.ContainsValue("Value_" + i)) { iCountErrors++; Console.WriteLine("Err_563fgd! Expected value not returned, " + hsh2.ContainsValue("Value_" + i)); } if(!hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value)) { iCountErrors++; Console.WriteLine("Err_87429dsfd! Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value); } try { hsh3.Add(strValueArr[i], null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_74298dsd! Exception thrown for " + strValueArr[i]); } } hsh4 = (Hashtable)hsh2.Clone(); if(hsh4.Count != hsh1.Count) { iCountErrors++; Console.WriteLine("Err_342342! Expected value not returned, " + hsh4.Count); } strValueArr = new DictionaryEntry[hsh4.Count]; hsh4.CopyTo(strValueArr, 0); hsh3 = new Hashtable(); for(int i=0; i<iNumberOfElements; i++) { if(!hsh4.Contains("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742ds8f! Expected value not returned, " + hsh4.Contains("Key_" + i)); } if(!hsh4.ContainsKey("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742389dsaf! Expected value not returned, " + hsh4.ContainsKey("Key_" + i)); } if(!hsh4.ContainsValue("Value_" + i)) { iCountErrors++; Console.WriteLine("Err_6-4142dsf! Expected value not returned, " + hsh4.ContainsValue("Value_" + i)); } if(!hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value)) { iCountErrors++; Console.WriteLine("Err_87429dsfd! Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value); } try { hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_74298dsd! Exception thrown for " + ((DictionaryEntry)strValueArr[i]).Value); } } if(hsh4.IsReadOnly) { iCountErrors++; Console.WriteLine("Err_723sadf! Expected value not returned, " + hsh4.IsReadOnly); } if(!hsh4.IsSynchronized) { iCountErrors++; Console.WriteLine("Err_723sadf! Expected value not returned, " + hsh4.IsSynchronized); } idic = hsh2.GetEnumerator(); hsh3 = new Hashtable(); hsh4 = new Hashtable(); while(idic.MoveNext()) { if(!hsh2.ContainsKey(idic.Key)) { iCountErrors++; Console.WriteLine("Err_4532sfds! Expected value not returned"); } if(!hsh2.ContainsValue(idic.Value)) { iCountErrors++; Console.WriteLine("Err_682wm! Expected value not returned"); } try { hsh3.Add(idic.Key, null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_5243sfd! Exception thrown for " + idic.Key); } try { hsh4.Add(idic.Value, null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_25sfs! Exception thrown for " + idic.Value); } } BinaryFormatter formatter = new BinaryFormatter(); ms1 = new MemoryStream(); formatter.Serialize(ms1, hsh2); ms1.Position = 0; hsh4 = (Hashtable)formatter.Deserialize(ms1); if(hsh4.Count != hsh1.Count) { iCountErrors++; Console.WriteLine("Err_072xsf! Expected value not returned, " + hsh4.Count); } strValueArr = new DictionaryEntry[hsh4.Count]; hsh4.CopyTo(strValueArr, 0); hsh3 = new Hashtable(); for(int i=0; i<iNumberOfElements; i++) { if(!hsh4.Contains("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742ds8f! Expected value not returned, " + hsh4.Contains("Key_" + i)); } if(!hsh4.ContainsKey("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742389dsaf! Expected value not returned, " + hsh4.ContainsKey("Key_" + i)); } if(!hsh4.ContainsValue("Value_" + i)) { iCountErrors++; Console.WriteLine("Err_0672esfs! Expected value not returned, " + hsh4.ContainsValue("Value_" + i)); } if(!hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value)) { iCountErrors++; Console.WriteLine("Err_87429dsfd! Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value); } try { hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_74298dsd! Exception thrown for " + strValueArr[i]); } } if(hsh4.IsReadOnly) { iCountErrors++; Console.WriteLine("Err_723sadf! Expected value not returned, " + hsh4.IsReadOnly); } if(!hsh4.IsSynchronized) { iCountErrors++; Console.WriteLine("Err_723sadf! Expected value not returned, " + hsh4.IsSynchronized); } if(hsh2.IsReadOnly) { iCountErrors++; Console.WriteLine("Err_723sadf! Expected value not returned, " + hsh2.IsReadOnly); } if(!hsh2.IsSynchronized) { iCountErrors++; Console.WriteLine("Err_723sadf! Expected value not returned, " + hsh2.IsSynchronized); } if(hsh2.SyncRoot != hsh1) { iCountErrors++; Console.WriteLine("Err_7428dsafd! Expected value not returned, "); } String[] strValueArr11 = new String[hsh1.Count]; strKeyArr = new String[hsh1.Count]; hsh2.Keys.CopyTo(strKeyArr, 0); hsh2.Values.CopyTo(strValueArr11, 0); hsh3 = new Hashtable(); hsh4 = new Hashtable(); for(int i=0; i<iNumberOfElements; i++) { if(!hsh2.ContainsKey(strKeyArr[i])) { iCountErrors++; Console.WriteLine("Err_4532sfds! Expected value not returned"); } if(!hsh2.ContainsValue(strValueArr11[i])) { iCountErrors++; Console.WriteLine("Err_074dsd! Expected value not returned, " + strValueArr11[i]); } try { hsh3.Add(strKeyArr[i], null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_5243sfd! Exception thrown for " + idic.Key); } try { hsh4.Add(strValueArr11[i], null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_25sfs! Exception thrown for " + idic.Value); } } hsh2.Remove("Key_1"); if(hsh2.ContainsKey("Key_1")) { iCountErrors++; Console.WriteLine("Err_423ewd! Expected value not returned, "); } if(hsh2.ContainsValue("Value_1")) { iCountErrors++; Console.WriteLine("Err_64q213d! Expected value not returned, "); } hsh2.Add("Key_1", "Value_1"); if(!hsh2.ContainsKey("Key_1")) { iCountErrors++; Console.WriteLine("Err_423ewd! Expected value not returned, "); } if(!hsh2.ContainsValue("Value_1")) { iCountErrors++; Console.WriteLine("Err_74523esf! Expected value not returned, "); } hsh2["Key_1"] = "Value_Modified_1"; if(!hsh2.ContainsKey("Key_1")) { iCountErrors++; Console.WriteLine("Err_423ewd! Expected value not returned, "); } if(hsh2.ContainsValue("Value_1")) { iCountErrors++; Console.WriteLine("Err_74523esf! Expected value not returned, "); } if(!hsh2.ContainsValue("Value_Modified_1")) { iCountErrors++; Console.WriteLine("Err_342fs! Expected value not returned, "); } hsh3 = Hashtable.Synchronized(hsh2); if(hsh3.Count != hsh1.Count) { iCountErrors++; Console.WriteLine("Err_742dsf! Expected value not returned, " + hsh3.Count); } if(!hsh3.IsSynchronized) { iCountErrors++; Console.WriteLine("Err_723sadf! Expected value not returned, " + hsh3.IsSynchronized); } hsh2.Clear(); if(hsh2.Count != 0) { iCountErrors++; Console.WriteLine("Err_742dsf! Expected value not returned, " + hsh2.Count); } strLoc = "Loc_8345vdfv"; hsh1 = new Hashtable(); hsh2 = Hashtable.Synchronized(hsh1); workers = new Thread[iNumberOfWorkers]; ts1 = new ThreadStart(AddElements); for(int i=0; i<workers.Length; i++) { workers[i] = new Thread(ts1); workers[i].Name = "Thread worker " + i; workers[i].Start(); } while(true) { fLoopExit=false; for(int i=0; i<iNumberOfWorkers;i++) { if(workers[i].IsAlive) fLoopExit=true; } if(!fLoopExit) break; } iCountTestcases++; if(hsh2.Count != iNumberOfElements*iNumberOfWorkers) { iCountErrors++; Console.WriteLine("Err_75630fvbdf! Expected value not returned, " + hsh2.Count); } iCountTestcases++; for(int i=0; i<iNumberOfWorkers; i++) { for(int j=0; j<iNumberOfElements; j++) { strValue = "Thread worker " + i + "_" + j; if(!hsh2.Contains(strValue)) { iCountErrors++; Console.WriteLine("Err_452dvdf_" + i + "_" + j + "! Expected value not returned, " + strValue); } } } workers = new Thread[iNumberOfWorkers]; ts1 = new ThreadStart(RemoveElements); for(int i=0; i<workers.Length; i++) { workers[i] = new Thread(ts1); workers[i].Name = "Thread worker " + i; workers[i].Start(); } while(true) { fLoopExit=false; for(int i=0; i<iNumberOfWorkers;i++) { if(workers[i].IsAlive) fLoopExit=true; } if(!fLoopExit) break; } iCountTestcases++; if(hsh2.Count != 0) { iCountErrors++; Console.WriteLine("Err_6720fvdg! Expected value not returned, " + hsh2.Count); } iCountTestcases++; if(hsh1.IsSynchronized) { iCountErrors++; Console.WriteLine("Err_4820fdf! Expected value not returned, " + hsh1.IsSynchronized); } iCountTestcases++; if(!hsh2.IsSynchronized) { iCountErrors++; Console.WriteLine("Err_4820fdf! Expected value not returned, " + hsh2.IsSynchronized); } } while (false); } catch (Exception exc_general ) { ++iCountErrors; Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy! 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 Boolean runTest() { Console.Error.WriteLine( s_strTFPath + " " + s_strTFName + " , for " + s_strComponentBeingTested + " ,Source ver " + s_strDtTmVer ); String strLoc = "Loc_000ooo"; int iCountTestcases = 0; int iCountErrors = 0; Hashtable hash = null; Object [] objArr = null; Object [] objArr2 = null; Object [][] objArrMDim = null; Object [,] objArrRMDim = null; Object [] keys = new Object [] { new Object(), "Hello" , "my array" , new DateTime(), new JulianCalendar(), typeof( System.Environment ), 5 }; Object [] values = new Object[] { "SomeString" , new Object(), new int [] { 1, 2, 3, 4, 5 }, new Hashtable(), new Exception(), new Co1657CopyTo_ai(), null }; if ( verbose ) Console.WriteLine( "test normal conditions, array is large enough to hold all elements" ); try { strLoc = "Err_0001a"; ++iCountTestcases; hash = new Hashtable(); for ( int i = 0; i < values.Length; i++ ) { hash.Add( keys[i], values[i] ); } objArr = new Object [values.Length + 2]; objArr[0] = "startstring" ; objArr[values.Length+1] = "endstring" ; hash.Values.CopyTo( (Array)objArr, 1 ); if ( ! "startstring".Equals( objArr[0] ) ) { ++iCountErrors; Console.WriteLine( "Location: Err_0001b" ); Console.WriteLine( "StartSentinal was overwritten by something" ); } else if ( ! "endstring".Equals( objArr[values.Length+1] ) ) { ++iCountErrors; Console.WriteLine( "Location: Err_0001c" ); Console.WriteLine( "EndSentinal was overwritten by something" ); } objArr2 = new Object[ values.Length ]; Array.Copy( objArr, 1, objArr2, 0, values.Length ); objArr = objArr2; if ( ! CompareArrays( objArr, values ) ) { ++iCountErrors; Console.WriteLine( "Location: Err_0001d" ); Console.WriteLine( "values do not match values which were inserted" ); } try { ++iCountTestcases; hash = new Hashtable(); objArr = new Object[0]; hash.CopyTo( objArr, Int32.MaxValue ); ++iCountErrors; Console.WriteLine( "Err_015a, Expected ArgumentException but no exception thrown" ); } catch ( ArgumentException ) { } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Err_015b, Expected ArgumentException but exception thrown= " + ex.ToString() ); } try { ++iCountTestcases; hash = new Hashtable(); objArr = new Object[0]; hash.CopyTo( objArr, Int32.MinValue ); ++iCountErrors; Console.WriteLine( "Err_015a, Expected ArgumentException but no exception thrown" ); } catch ( ArgumentException ) { } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Err_015b, Expected ArgumentException but exception thrown= " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "copy should throw because of outofrange" ); Random random = new Random(); for(int iii=0; iii<20; iii++) { try { ++iCountTestcases; hash = new Hashtable(); objArr = new Object[0]; hash.CopyTo( objArr, random.Next(-1000, 0)); ++iCountErrors; Console.WriteLine( "Err_015a, Expected ArgumentException but no exception thrown" ); } catch ( ArgumentException ) { } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Err_015b, Expected ArgumentException but exception thrown= " + ex.ToString() ); } } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when array is to small to hold all values hashtable has more values then array can hold" ); try { strLoc = "Err_0002a"; ++iCountTestcases; hash = new Hashtable(); for ( int i = 0; i < values.Length; i++ ) { hash.Add( keys[i], values[i] ); } objArr = new Object [values.Length - 1]; try { hash.Values.CopyTo( (Array) objArr, 0 ); ++iCountErrors; Console.WriteLine( strLoc + " method to CopyTo should throw an exception when array is smaller then num of elements in hashtable, no exception was thrown here" ); } catch ( ArgumentException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() + " and expected ArgumentException" ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when array is size 0" ); try { strLoc = "Err_0003a"; ++iCountTestcases; hash = new Hashtable(); for ( int i = 0; i < values.Length; i++ ) { hash.Add( keys[i], values[i] ); } objArr = new Object [0]; try { hash.Values.CopyTo( (Array) objArr, 0 ); ++iCountErrors; Console.WriteLine( strLoc + " method to CopyTo should throw an exception when array is size 0, no exception was thrown here" ); } catch ( ArgumentException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() + " and expected ArgumentException" ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when array is null" ); try { strLoc = "Err_0004a"; ++iCountTestcases; hash = new Hashtable(); for ( int i = 0; i < values.Length; i++ ) { hash.Add( keys[i], values[i] ); } try { hash.Values.CopyTo( null, 0 ); ++iCountErrors; Console.WriteLine( strLoc + " method to CopyTo should throw an exception when array is null, no exception was thrown here" ); } catch ( ArgumentNullException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() + " and expected ArgumentNullException" ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when hashtable has no elements in it" ); try { strLoc = "Err_0005a"; ++iCountTestcases; hash = new Hashtable(); try { strLoc = "Err_0005c"; objArr = new Object[100]; hash.Values.CopyTo( objArr, 0 ); strLoc = "Err_0005d"; objArr = new Object[100]; hash.Values.CopyTo( objArr, 99 ); } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when hashtable has no elements in it and index is out of range" ); try { strLoc = "Err_0006a"; ++iCountTestcases; hash = new Hashtable(); try { strLoc = "Err_0006b"; objArr = new Object[100]; hash.Values.CopyTo( objArr, 100 ); } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when hashtable has no elements in it trying to copy to array of size 0" ); try { strLoc = "Err_0007a"; ++iCountTestcases; hash = new Hashtable(); try { strLoc = "Err_0007b"; objArr = new Object[0]; hash.Values.CopyTo( objArr, 0 ); } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " expected ArgumentOutOfRangeException but got exception, we got "+ ex.ToString() ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when hashtable has no elements in it and index is out of range (negative)" ); strLoc = "Err_0007aa"; try { ++iCountTestcases; hash = new Hashtable(); try { strLoc = "Err_0007bb"; objArr = new Object[0]; hash.Values.CopyTo( objArr, -1 ); ++iCountErrors; Console.WriteLine( strLoc + " expected ArgumentOutOfRange exception but no exception was thrown" ); } catch ( ArgumentOutOfRangeException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when array is multi dimensional and array is large enough" ); try { strLoc = "Err_0008a"; ++iCountTestcases; hash = new Hashtable(); for ( int i = 0; i < 100; i++ ) { hash.Add( i.ToString(), i.ToString() ); } try { strLoc = "Err_0008b1"; objArrMDim = new Object[100][]; for ( int i = 0; i < 100; i++ ) { objArrMDim[i] = new Object[i+1]; } hash.Values.CopyTo( objArrMDim, 0 ); ++iCountErrors; Console.WriteLine( strLoc + " expected ArgumentRange exception but no exception was thrown" ); } catch ( InvalidCastException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } try { strLoc = "Err_0008b"; objArrRMDim = new Object[100,100]; hash.Values.CopyTo( objArrRMDim, 0 ); ++iCountErrors; Console.WriteLine( strLoc + " expected ArgumentRange exception but no exception was thrown" ); } catch ( ArgumentException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test when array is multi dimensional and array is small" ); try { strLoc = "Err_0009a"; ++iCountTestcases; hash = new Hashtable(); for ( int i = 0; i < 100; i++ ) { hash.Add( i.ToString(), i.ToString() ); } try { strLoc = "Err_0009b"; objArrMDim = new Object[99][]; for ( int i = 0; i < 99; i++ ) { objArrMDim[i] = new Object[i+1]; } hash.Values.CopyTo( objArrMDim, 0 ); ++iCountErrors; Console.WriteLine( strLoc + " expected ArgumentException but no exception was thrown" ); } catch ( ArgumentException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " unexpected exception, we got "+ ex.ToString() ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( verbose ) Console.WriteLine( "test to see if CopyTo throws correct exception" ); try { strLoc = "Err_00010a"; ++iCountTestcases; hash = new Hashtable(); try { String [] str = new String[100]; hash.Values.CopyTo( str, 101 ); ++iCountErrors; Console.WriteLine( strLoc + " expected ArgumentException but no exception was thrown" ); } catch ( ArgumentException ) { } catch ( Exception ex ) { ++iCountErrors; Console.WriteLine( strLoc + " expected ArgumentException exception, we got "+ ex.ToString() ); } } catch (Exception ex) { ++iCountErrors; Console.WriteLine( "Location: " + strLoc ); Console.WriteLine( "Unexpected exception was thrown ex: " + ex.ToString() ); } if ( iCountErrors == 0 ) { Console.WriteLine("paSs. " + s_strTFPath + s_strTFName + " iCountTestcases==" + iCountTestcases.ToString ()); return true; } else { Console.WriteLine("Related Bugs: " + s_strActiveBugNums ); Console.WriteLine("FAiL! " + s_strTFPath + s_strTFName + " iCountErrors==" + iCountErrors.ToString ()); return false; } }
public void TestCtorIntSingle() { // variables used for tests Hashtable hash = null; // [] should get ArgumentException if trying to have large num of entries Assert.Throws<ArgumentException>(() => { hash = new Hashtable(int.MaxValue, .1f); } ); // []should not get any exceptions for valid values - we also check that the HT works here hash = new Hashtable(100, .1f); int iNumberOfElements = 100; for (int i = 0; i < iNumberOfElements; i++) { hash.Add("Key_" + i, "Value_" + i); } //Count Assert.Equal(hash.Count, iNumberOfElements); DictionaryEntry[] strValueArr = new DictionaryEntry[hash.Count]; hash.CopyTo(strValueArr, 0); Hashtable hsh3 = new Hashtable(); for (int i = 0; i < iNumberOfElements; i++) { Assert.True(hash.Contains("Key_" + i), "Error, Expected value not returned, " + hash.Contains("Key_" + i)); Assert.True(hash.ContainsKey("Key_" + i), "Error, Expected value not returned, " + hash.ContainsKey("Key_" + i)); Assert.True(hash.ContainsValue("Value_" + i), "Error, Expected value not returned, " + hash.ContainsValue("Value_" + i)); //we still need a way to make sure that there are all these unique values here -see below code for that Assert.True(hash.ContainsValue(((DictionaryEntry)strValueArr[i]).Value), "Error, Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value); hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null); } }
public void TestCopyToBasic() { Hashtable hash = null; // the hashtable object which will be used in the tests object[] objArr = null; // the object array corresponding to arr object[] objArr2 = null; // helper object array object[,] objArrRMDim = null; // multi dimensional object array // these are the keys and values which will be added to the hashtable in the tests object[] keys = new object[] { new object(), "Hello" , "my array" , new DateTime(), new SortedList(), typeof( System.Environment ), 5 }; object[] values = new object[] { "Somestring" , new object(), new int [] { 1, 2, 3, 4, 5 }, new Hashtable(), new Exception(), new Hashtable_CopyToTests(), null }; //[]test normal conditions, array is large enough to hold all elements // make new hashtable hash = new Hashtable(); // put in values and keys for (int i = 0; i < values.Length; i++) { hash.Add(keys[i], values[i]); } // now try getting out the values using CopyTo method objArr = new object[values.Length + 2]; // put a sentinal in first position, and make sure it is not overriden objArr[0] = "startstring"; // put a sentinal in last position, and make sure it is not overriden objArr[values.Length + 1] = "endstring"; hash.Values.CopyTo((Array)objArr, 1); // make sure sentinal character is still there Assert.Equal("startstring", objArr[0]); Assert.Equal("endstring", objArr[values.Length + 1]); // check to make sure arr is filled up with the correct elements objArr2 = new object[values.Length]; Array.Copy(objArr, 1, objArr2, 0, values.Length); objArr = objArr2; Assert.True(CompareArrays(objArr, values)); //[] This is the same test as before but now we are going to used Hashtable.CopyTo instead of Hasthabe.Values.CopyTo // now try getting out the values using CopyTo method objArr = new object[values.Length + 2]; // put a sentinal in first position, and make sure it is not overriden objArr[0] = "startstring"; // put a sentinal in last position, and make sure it is not overriden objArr[values.Length + 1] = "endstring"; hash.CopyTo((Array)objArr, 1); // make sure sentinal character is still there Assert.Equal("startstring", objArr[0]); Assert.Equal("endstring", objArr[values.Length + 1]); // check to make sure arr is filled up with the correct elements BitArray bitArray = new BitArray(values.Length); for (int i = 0; i < values.Length; i++) { DictionaryEntry entry = (DictionaryEntry)objArr[i + 1]; int valueIndex = Array.IndexOf(values, entry.Value); int keyIndex = Array.IndexOf(keys, entry.Key); Assert.NotEqual(-1, valueIndex); Assert.NotEqual(-1, keyIndex); Assert.Equal(valueIndex, keyIndex); bitArray[i] = true; } for (int i = 0; i < ((ICollection)bitArray).Count; i++) { Assert.True(bitArray[i]); } //[] Parameter validation //[] Null array Assert.Throws<ArgumentNullException>(() => { hash = new Hashtable(); objArr = new object[0]; hash.CopyTo(null, 0); } ); //[] Multidimentional array Assert.Throws<ArgumentException>(() => { hash = new Hashtable(); objArrRMDim = new object[16, 16]; hash.CopyTo(objArrRMDim, 0); } ); //[] Array not large enough Assert.Throws<ArgumentException>(() => { hash = new Hashtable(); for (int i = 0; i < 256; i++) { hash.Add(i.ToString(), i); } objArr = new object[hash.Count + 8]; hash.CopyTo(objArr, 9); } ); Assert.Throws<ArgumentException>(() => { hash = new Hashtable(); objArr = new object[0]; hash.CopyTo(objArr, Int32.MaxValue); } ); Assert.Throws<ArgumentOutOfRangeException>(() => { hash = new Hashtable(); objArr = new object[0]; hash.CopyTo(objArr, Int32.MinValue); } ); //[]copy should throw because of outofrange Random random = new Random(-55); for (int iii = 0; iii < 20; iii++) { Assert.Throws<ArgumentOutOfRangeException>(() => { hash = new Hashtable(); objArr = new object[0]; hash.CopyTo(objArr, random.Next(-1000, 0)); } ); } //[]test when array is to small to hold all values hashtable has more values then array can hold hash = new Hashtable(); // put in values and keys for (int i = 0; i < values.Length; i++) { hash.Add(keys[i], values[i]); } // now try getting out the values using CopyTo method into a small array objArr = new object[values.Length - 1]; Assert.Throws<ArgumentException>(() => { hash.Values.CopyTo((Array)objArr, 0); } ); //[]test when array is size 0 // now try getting out the values using CopyTo method into a 0 sized array objArr = new object[0]; Assert.Throws<ArgumentException>(() => { hash.Values.CopyTo((Array)objArr, 0); } ); //[]test when array is null Assert.Throws<ArgumentNullException>(() => { hash.Values.CopyTo(null, 0); } ); }
public Boolean runTest() { Console.WriteLine( s_strTFPath +" "+ s_strTFName +" ,for "+ s_strClassMethod +" ,Source ver "+ s_strDtTmVer ); String strLoc = "Loc_000oo"; Hashtable hash = null; int iCountErrors = 0; int iCountTestcases = 0; strLoc = "Loc_001oo"; try { ++iCountTestcases; hash = new Hashtable( Int32.MaxValue, .1f, null, null ); ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_0001! hashtable should have thrown ArgumentException here" ); } catch( ArgumentException ) { } catch( Exception ex ) { ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_1001! we expected ArgumentException but got exception " + ex.ToString() ); } try { ++iCountTestcases; hash = new Hashtable( 100, .1f, null, null ); } catch( Exception ex ) { ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_1002! we expected no exception but got exception " + ex.ToString() ); } Int32 iNumberOfElements = 100; for(int i=0; i<iNumberOfElements; i++) { hash.Add("Key_" + i, "Value_" + i); } iCountTestcases++; if(hash.Count != iNumberOfElements) { iCountErrors++; Console.WriteLine("Err_742dsf! Expected value not returned, " + hash.Count); } DictionaryEntry[] strValueArr = new DictionaryEntry[hash.Count]; hash.CopyTo(strValueArr, 0); Hashtable hsh3 = new Hashtable(); for(int i=0; i<iNumberOfElements; i++) { if(!hash.Contains("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742ds8f! Expected value not returned, " + hash.Contains("Key_" + i)); } if(!hash.ContainsKey("Key_" + i)) { iCountErrors++; Console.WriteLine("Err_742389dsaf! Expected value not returned, " + hash.ContainsKey("Key_" + i)); } if(!hash.ContainsValue("Value_" + i)) { iCountErrors++; Console.WriteLine("Err_563fgd! Expected value not returned, " + hash.ContainsValue("Value_" + i)); } if(!hash.ContainsValue(((DictionaryEntry)strValueArr[i]).Value)) { iCountErrors++; Console.WriteLine("Err_87429dsfd! Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value); } try { hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null); } catch(Exception) { iCountErrors++; Console.WriteLine("Err_74298dsd! Exception thrown for " + ((DictionaryEntry)strValueArr[i]).Value); } } try { ++iCountTestcases; hash = new Hashtable( 5, .01f, null, null ); ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_0003! hashtable should have thrown ArgumentOutOfRangeException here" ); } catch( ArgumentOutOfRangeException ) { } catch( Exception ex ) { ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_1003! we expected ArgumentOutOfRangeException but got exception " + ex.ToString() ); } try { ++iCountTestcases; hash = new Hashtable( 5, 100.1f, null, null ); ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_0004! hashtable should have thrown ArgumentOutOfRangeException here" ); } catch( ArgumentOutOfRangeException ) { } catch( Exception ex ) { ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_1004! we expected ArgumentOutOfRangeException but got exception " + ex.ToString() ); } try { ++iCountTestcases; hash = new Hashtable( Int32.MaxValue, 100.1f, null, null ); ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_0004! hashtable should have thrown ArgumentOutOfRangeException here" ); } catch( ArgumentOutOfRangeException ) { } catch( Exception ex ) { ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_1004! we expected ArgumentOutOfRangeException but got exception " + ex.ToString() ); } try { ++iCountTestcases; hash = new Hashtable( Int32.MinValue, 10.1f, null, null ); ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_0005! hashtable should have thrown ArgumentOutOfRangeException here" ); } catch( ArgumentOutOfRangeException ) { } catch( Exception ex ) { ++iCountErrors; Console.WriteLine( s_strTFAbbrev +" Error_1005! we expected ArgumentOutOfRangeException but got exception strLoc= " + strLoc + ex.ToString() ); } if ( iCountErrors == 0 ) { Console.WriteLine( "paSs. "+ s_strTFPath +" "+ s_strTFName +" ,iCountTestcases="+ iCountTestcases.ToString() ); return true; } else { Console.WriteLine( "ACTIVE BUGS: " + s_strActiveBugNums ); Console.WriteLine( "FAiL! "+ s_strTFPath +" "+ s_strTFName +" ,iCountErrors="+ iCountErrors.ToString() +" ,BugNums?: "+ s_strActiveBugNums ); return false; } }