本文整理汇总了C#中SortedList.GetEnumerator方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.GetEnumerator方法的具体用法?C# SortedList.GetEnumerator怎么用?C# SortedList.GetEnumerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedList
的用法示例。
在下文中一共展示了SortedList.GetEnumerator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FillNodeWithFolderContents
private void FillNodeWithFolderContents(TriStateTreeNode folderNode)
{
////Remove "root\"
//if (folderRealPath.StartsWith("root"))
// folderRealPath = folderRealPath.Remove(0, 5);
////string filePath = newNode.FullPath.Remove(0, (startNode.Text + startNode.TreeView.PathSeparator).Length);
//if (folderRealPath.EndsWith(":"))
// folderRealPath += "\\";
DirectoryInfo di = new DirectoryInfo((string)folderNode.Tag);
if (!di.Exists)
return;
FileSystemInfo[] fsis = di.GetFileSystemInfos();
//sort alphabetically
SortedList<string, TreeNode> sorted = new SortedList<string, TreeNode>();
Dictionary<string, TriStateTreeView.CheckBoxState> states = new Dictionary<string, TriStateTreeView.CheckBoxState>();
foreach (FileSystemInfo fsi in fsis)
{
TriStateTreeNode node = (TriStateTreeNode)folderNode.Nodes[fsi.Name];
if (node == null)
{
//TODO: is fsi a folder or a file? How to find out?
node = new TriStateTreeNode(fsi.Name);
folderNode.Nodes.Add(node);
//node = folderNode.Nodes.Add(fsi.Name);
node.Name = fsi.Name;
node.Tag = fsi.FullName;
this.treeView1.SetTreeNodeState(node, TriStateTreeView.CheckBoxState.Unchecked);
}
sorted.Add(fsi.Name, node);
states.Add(fsi.Name, this.treeView1.GetTreeNodeState(node));
}
folderNode.Nodes.Clear();
sorted.GetEnumerator();
for (int i = 0; i < sorted.Count; i++)
{
TriStateTreeNode node = (TriStateTreeNode)sorted.Values[i];
TriStateTreeView.CheckBoxState state = states[node.Name];
folderNode.Nodes.Add(node);
this.treeView1.SetTreeNodeState(node, state);
}
}
示例2: foo
public void foo()
{
string[] sp = Console.ReadLine().Split(' ');
int n = int.Parse(sp[0]);
int k = int.Parse(sp[1]);
int[] m = new int[k];
SortedList<int, List<int>> ch = new SortedList<int, List<int>>();
for (int i = 0; i < k; i++)
{
sp = Console.ReadLine().Split(' ');
m[i] = int.Parse(sp[0]);
int s = int.Parse(sp[1]);
ch.Add(s, new List<int>());
for (int j = 1; j < m[i]; j++)
{
ch[s].Add(int.Parse(sp[j + 1]));
}
}
int t = 0, p = 0;
var e = ch.GetEnumerator();
while (e.MoveNext())
{
int st = e.Current.Key;
List<int> l = e.Current.Value;
for (int i = 0; i < l.Count; i++)
{
if (l[i] == st + 1)
st++;
else
{
t += l.Count - i;
p += l.Count - i;
break;
}
}
t++;
}
Console.WriteLine(t-1+p);
}
示例3: UpdateDirect
//overloaded method added to support optimistic concurrency
public int UpdateDirect(string strTable, string strWhere, SortedList slValues, SortedList slOriginalValues)
{
IDictionaryEnumerator objDEItem;
string strKey = "";
string strOriginalValues = "";
int intResult = 0;
string strFilter = "";
if (slOriginalValues.Count > 0)
{
objDEItem = slOriginalValues.GetEnumerator();
//Loop through field values collection and update row
while (objDEItem.MoveNext())
{
//Determine key and use it to update corresponding column
strKey = objDEItem.Key.ToString();
if (strOriginalValues.Length > 0)
{
strOriginalValues = strOriginalValues + " AND ";
}
strOriginalValues = strOriginalValues + strKey + "=" + FormatSQLValue(objDEItem.Value);
}
//Build Where clause
if (strWhere.Length > 0)
{
strFilter = strWhere + " AND ";
}
strFilter = strFilter + strOriginalValues;
}
intResult = UpdateDirect(strTable, strFilter, slValues);
return intResult;
}
示例4: Update
//Generic SQL Update method
public int Update(string strTable, string strWhere, SortedList slValues)
{
SqlDataAdapter da;
SqlCommandBuilder cb;
DataSet ds;
DataRow[] aDr = null;
IDictionaryEnumerator objDEItem;
string strSQL = "";
string strKey = "";
int intResult = 0;
try
{
if (CreateConnection())
{
strSQL = "SELECT * FROM " + strTable;
da = new SqlDataAdapter();
//Get the dataset using the Select query that has been composed
ds = GetDataSet(strTable, strSQL, ref da);
//use the command builder to generate the Update command that we'll need later
cb = new SqlCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
//Get the records to Update using where clause
if (strWhere.Length > 0)
{
aDr = ds.Tables[strTable].Select(strWhere);
}
else
{
aDr = ds.Tables[strTable].Select();
}
if (aDr != null)
{
//Loop through each row
foreach (DataRow dr in aDr)
{
objDEItem = slValues.GetEnumerator();
//Loop through field values collection and update row
while (objDEItem.MoveNext())
{
//Determine key and use it to update corresponding column in datarow
//strKey = slValues.GetKey(slValues.IndexOfValue(o)).ToString();
strKey = objDEItem.Key.ToString();
dr[strKey] = objDEItem.Value;
}
}
}
//Update table and determine number of rows affected
intResult = da.Update(ds, strTable);
}
}
catch (SqlException ex)
{
throw ex;
}
finally
{
CloseConnection();
}
return intResult;
}
示例5: InsertDirect
//Generic SQL Insert method for direct insertion using an INSERT statement or stored proc
public int InsertDirect(string strCommandText,
CommandType enuCommandType,
SortedList slParameters)
{
IDictionaryEnumerator objDEItem;
SqlCommand cmd = new SqlCommand();
int intResult = 0;
string strSQL = "";
string strKey = "";
string strFields = "";
string strValues = "";
string strParam = "";
try
{
if (CreateConnection())
{
if (slParameters.Count > 0)
{
if (enuCommandType == CommandType.Text)
{
cmd = m_conDB.CreateCommand(); //build command ourselves
cmd.CommandType = enuCommandType;
objDEItem = slParameters.GetEnumerator();
//Loop through field values collection and insert row
while (objDEItem.MoveNext())
{
//Build up the field names to insert. Use key for column name
strKey = objDEItem.Key.ToString();
AddToCSV(strKey, ref strFields);
//Build up the values to insert. Add param indicator if needed
strParam = ToParam(strKey);
AddToCSV(strParam, ref strValues);
cmd.Parameters.AddWithValue(strParam, objDEItem.Value); //Add value to params
}
strFields = "(" + strFields + ")";
strValues = "(" + strValues + ")";
//Compose the full SQL command
strSQL = "INSERT INTO " + strCommandText + " " + strFields + " VALUES " + strValues;
cmd.CommandText = strSQL;
}
else if (enuCommandType == CommandType.StoredProcedure)
{
//Support for stored procedures
cmd = GetCommand(strCommandText, enuCommandType, slParameters);
}
intResult = cmd.ExecuteNonQuery();
}
}
}
catch (SqlException ex)
{
intResult = 0;
throw ex;
}
finally
{
CloseConnection();
}
return intResult;
}
示例6: runTest
private Boolean runTest()
{
Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver : " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
String strLoc = null;
SortedList dic1;
SortedList dic2;
SortedList dic3;
String[] strValueArr = null;
DictionaryEntry[] dicEnt1 = null;
String[] strKeyArr = null;
ICollection icol1;
Hashtable hsh1;
IDictionaryEnumerator idic;
IDictionaryEnumerator idicTest;
IDictionaryEnumerator idicTest1;
DictionaryEntry dicEnt;
Hashtable hsh2;
Hashtable hsh3;
Hashtable hsh4;
Object oValue;
Int32 iNumberOfElements;
MemoryStream ms1;
BinaryFormatter formatter;
IList ilst1;
try
{
hsh1 = new Hashtable();
strLoc = "Loc_04872dsf";
iCountTestcases++;
dic1 = new SortedList();
icol1 = dic1.Keys;
for(int i=0; i<100; i++)
dic1.Add("Key_" + i, "Value_" + i);
DoICollectionTests(dic1, icol1, hsh1, DicType.Key);
if(hsh1.Count>0)
{
iCountErrors++;
Console.WriteLine("Err_75629ewf! Keys");
idic = hsh1.GetEnumerator();
while(idic.MoveNext())
{
Console.WriteLine("<<" + idic.Key + " <<" + idic.Value + ">>");
}
}
strLoc = "Loc_97234dsf";
iCountTestcases++;
dic1 = new SortedList();
for(int i=0; i<100; i++)
dic1.Add("Key_" + i, "Value_" + i);
icol1 = dic1.Values;
hsh1 = new Hashtable();
DoICollectionTests(dic1, icol1, hsh1, DicType.Value);
if(hsh1.Count>0)
{
iCountErrors++;
Console.WriteLine("Err_94752dsg! Values");
idic = hsh1.GetEnumerator();
while(idic.MoveNext())
{
Console.WriteLine("<<" + idic.Key + " <<" + idic.Value + ">>");
}
}
strLoc = "Loc_74fsgf";
dic1 = new SortedList();
iCountTestcases++;
for(int i=0; i<100; i++)
dic1.Add("Key_" + i, "Value_" + i);
ilst1 = dic1.GetKeyList();
hsh1 = new Hashtable();
DoIListTests(dic1, ilst1, hsh1, DicType.Key);
if(hsh1.Count!= 2
|| !hsh1.ContainsKey("IsReadOnly")
|| !hsh1.ContainsKey("IsFixedSize")
)
{
iCountErrors++;
Console.WriteLine("Err_7352dsafsaf! KeyList");
idic = hsh1.GetEnumerator();
while(idic.MoveNext())
{
Console.WriteLine("<<" + idic.Key + " <<" + idic.Value + ">>");
}
}
dic1 = new SortedList();
strLoc = "Loc_97653tdsfg";
for(int i=0; i<100; i++)
dic1.Add("Key_" + i, "Value_" + i);
ilst1 = dic1.GetValueList();
hsh1 = new Hashtable();
DoIListTests(dic1, ilst1, hsh1, DicType.Value);
if(hsh1.Count!=2
|| !hsh1.ContainsKey("IsReadOnly")
|| !hsh1.ContainsKey("IsFixedSize")
)
{
iCountErrors++;
Console.WriteLine("Err_3572sfn! ValueList");
idic = hsh1.GetEnumerator();
//.........这里部分代码省略.........
示例7: GetSyncRootBasic
public void GetSyncRootBasic()
{
// Testing SyncRoot is not as simple as its implementation looks like. This is the working
// scenario we have in mind.
// 1) Create your Down to earth mother SortedList
// 2) Get a synchronized wrapper from it
// 3) Get a Synchronized wrapper from 2)
// 4) Get a synchronized wrapper of the mother from 1)
// 5) all of these should SyncRoot to the mother earth
var sortListMother = new SortedList();
for (int i = 0; i < NumberOfElements; i++)
{
sortListMother.Add("Key_" + i, "Value_" + i);
}
Assert.Equal(sortListMother.SyncRoot.GetType(), typeof(object));
SortedList sortListSon = SortedList.Synchronized(sortListMother);
_sortListGrandDaughter = SortedList.Synchronized(sortListSon);
_sortListDaughter = SortedList.Synchronized(sortListMother);
Assert.Equal(sortListSon.SyncRoot, sortListMother.SyncRoot);
Assert.Equal(sortListMother.SyncRoot, sortListSon.SyncRoot);
Assert.Equal(_sortListGrandDaughter.SyncRoot, sortListMother.SyncRoot);
Assert.Equal(_sortListDaughter.SyncRoot, sortListMother.SyncRoot);
Assert.Equal(sortListSon.SyncRoot, sortListMother.SyncRoot);
//we are going to rumble with the SortedLists with some threads
var workers = new Task[4];
for (int i = 0; i < workers.Length; i += 2)
{
var name = "Thread_worker_" + i;
var action1 = new Action(() => AddMoreElements(name));
var action2 = new Action(RemoveElements);
workers[i] = Task.Run(action1);
workers[i + 1] = Task.Run(action2);
}
Task.WaitAll(workers);
// Checking time
// Now lets see how this is done.
// Either there are some elements or none
var sortListPossible = new SortedList();
for (int i = 0; i < NumberOfElements; i++)
{
sortListPossible.Add("Key_" + i, "Value_" + i);
}
for (int i = 0; i < workers.Length; i++)
{
sortListPossible.Add("Key_Thread_worker_" + i, "Thread_worker_" + i);
}
//lets check the values if
IDictionaryEnumerator enumerator = sortListMother.GetEnumerator();
while (enumerator.MoveNext())
{
Assert.True(sortListPossible.ContainsKey(enumerator.Key));
Assert.True(sortListPossible.ContainsValue(enumerator.Value));
}
}
示例8: TestIDictionaryEnumerator
public void TestIDictionaryEnumerator()
{
var dic1 = new SortedList();
for (int i = 0; i < 100; i++)
dic1.Add("Key_" + i, "Value_" + i);
var idicTest = dic1.GetEnumerator();
var hsh2 = new Hashtable();
//making sure that we throw if looking at values before MoveNext
Assert.Throws<InvalidOperationException>(() => idicTest.Key);
Assert.Throws<InvalidOperationException>(() => idicTest.Value);
Assert.Throws<InvalidOperationException>(() => idicTest.Current);
while (idicTest.MoveNext())
{
Assert.True(dic1.Contains(idicTest.Key));
Assert.Equal(idicTest.Value, dic1[idicTest.Key]);
DictionaryEntry dicEnt = idicTest.Entry;
Assert.True(dic1.Contains(dicEnt.Key));
Assert.Equal(dicEnt.Value, dic1[dicEnt.Key]);
hsh2.Add(idicTest.Key, idicTest.Value);
dicEnt = (DictionaryEntry)idicTest.Current;
Assert.True(dic1.Contains(dicEnt.Key));
Assert.Equal(dicEnt.Value, dic1[dicEnt.Key]);
}
Assert.Throws<InvalidOperationException>(() => idicTest.Key);
Assert.Throws<InvalidOperationException>(() => idicTest.Value);
Assert.Throws<InvalidOperationException>(() => idicTest.Current);
idicTest.Reset();
while (idicTest.MoveNext())
{
Assert.True(dic1.Contains(idicTest.Key));
Assert.Equal(idicTest.Value, dic1[idicTest.Key]);
DictionaryEntry dicEnt = idicTest.Entry;
Assert.True(dic1.Contains(dicEnt.Key));
Assert.Equal(dicEnt.Value, dic1[dicEnt.Key]);
Assert.Throws<ArgumentException>(() => hsh2.Add(idicTest.Key, idicTest.Value));
dicEnt = (DictionaryEntry)idicTest.Current;
Assert.True(dic1.Contains(dicEnt.Key));
Assert.Equal(dicEnt.Value, dic1[dicEnt.Key]);
}
idicTest.Reset();
dic1.Add("Key_Blah", "Value_Blah");
Assert.Throws<InvalidOperationException>(() => idicTest.MoveNext());
Assert.Throws<InvalidOperationException>(() => idicTest.Current);
Assert.Throws<InvalidOperationException>(() => idicTest.Reset());
Assert.Throws<InvalidOperationException>(() => idicTest.Key);
Assert.Throws<InvalidOperationException>(() => idicTest.Value);
}
示例9: TestCloneWithEnumerator
public void TestCloneWithEnumerator()
{
//WE will test the Clone() method of the enumerator
DictionaryEntry dicEnt;
var dic1 = new SortedList();
for (int i = 0; i < 100; i++)
dic1.Add("Key_" + i, "Value_" + i);
var idicTest = dic1.GetEnumerator();
var idicTest1 = dic1.GetEnumerator();
var hsh1 = new Hashtable();
var hsh2 = new Hashtable();
while (idicTest1.MoveNext())
{
if (!dic1.Contains(idicTest1.Key))
hsh1["IDictionaryEnumerator"] = "";
if (dic1[idicTest1.Key] != idicTest1.Value)
hsh1["IDictionaryEnumerator"] = "";
dicEnt = idicTest1.Entry;
if (!dic1.Contains(dicEnt.Key))
hsh1["IDictionaryEnumerator"] = "";
if (dic1[dicEnt.Key] != dicEnt.Value)
hsh1["IDictionaryEnumerator"] = "";
try
{
hsh2.Add(idicTest1.Key, idicTest1.Value);
}
catch (Exception)
{
hsh1["IDictionaryEnumerator"] = "";
}
dicEnt = (DictionaryEntry)idicTest1.Current;
if (!dic1.Contains(dicEnt.Key))
hsh1["IDictionaryEnumerator"] = "";
if (dic1[dicEnt.Key] != dicEnt.Value)
hsh1["IDictionaryEnumerator"] = "";
}
idicTest1.Reset();
hsh2 = new Hashtable();
while (idicTest1.MoveNext())
{
if (!dic1.Contains(idicTest1.Key))
hsh1["IDictionaryEnumerator"] = "";
if (dic1[idicTest1.Key] != idicTest1.Value)
hsh1["IDictionaryEnumerator"] = "";
dicEnt = idicTest1.Entry;
if (!dic1.Contains(dicEnt.Key))
hsh1["IDictionaryEnumerator"] = "";
if (dic1[dicEnt.Key] != dicEnt.Value)
hsh1["IDictionaryEnumerator"] = "";
try
{
hsh2.Add(idicTest1.Key, idicTest1.Value);
}
catch (Exception)
{
hsh1["IDictionaryEnumerator"] = "";
}
dicEnt = (DictionaryEntry)idicTest1.Current;
if (!dic1.Contains(dicEnt.Key))
hsh1["IDictionaryEnumerator"] = "";
if (dic1[dicEnt.Key] != dicEnt.Value)
hsh1["IDictionaryEnumerator"] = "";
}
idicTest1.Reset();
dic1.Add("Key_Blah", "Value_Blah");
try
{
idicTest1.MoveNext();
hsh1["Enumerator"] = "NoEXception";
}
catch (InvalidOperationException)
{
}
catch (Exception ex)
{
hsh1["Enumerator"] = ex;
}
try
{
object blah = idicTest1.Current;
hsh1["Enumerator"] = "NoEXception";
}
catch (InvalidOperationException)
{
}
catch (Exception ex)
//.........这里部分代码省略.........
示例10: TestGetEnumeratorBasic
public void TestGetEnumeratorBasic()
{
StringBuilder sblMsg = new StringBuilder(99);
//
SortedList sl2 = null;
IDictionaryEnumerator dicen = null;
StringBuilder sbl3 = new StringBuilder(99);
StringBuilder sbl4 = new StringBuilder(99);
StringBuilder sblWork1 = new StringBuilder(99);
int i3 = 0;
int i2 = 0;
int i = 0;
int j = 0;
//
// Constructor: Create SortedList using this as IComparer and default settings.
//
sl2 = new SortedList(this);
// Verify that the SortedList is not null.
Assert.NotNull(sl2);
// Verify that the SortedList is empty.
Assert.Equal(0, sl2.Count);
// Testcase: Set - null key, ArgExc expected
Assert.Throws<ArgumentNullException>(() =>
{
sl2[null] = 0;
});
Assert.Equal(0, sl2.Count);
// Testcase: Set - null val
sl2[(object)100] = (object)null;
Assert.Equal(1, sl2.Count);
// Testcase: vanila Set
sl2[(object)100] = 1;
Assert.Equal(1, sl2.Count);
sl2.Clear();
Assert.Equal(0, sl2.Count);
// Testcase: add key-val pairs
for (i = 0; i < 100; i++)
{
sl2.Add(i + 100, i);
}
Assert.Equal(100, sl2.Count);
for (i = 0; i < 100; i++)
{
j = i + 100;
Assert.True(sl2.ContainsKey((int)j));
Assert.True(sl2.ContainsValue(i));
object o2 = sl2[(object)(j)]; //need to cast: Get (object key)
Assert.NotNull(o2);
i2 = (int)o2;
i3 = (int)sl2.GetByIndex(i); //Get (index i)
Assert.False((i3 != i) || (i2 != i));
// testcase: GetEnumerator
dicen = (IDictionaryEnumerator)sl2.GetEnumerator();
// Boundary for Current
Assert.Throws<InvalidOperationException>(() =>
{
object throwaway = dicen.Current;
}
);
j = 0;
// go over the enumarator
while (dicen.MoveNext())
{
// Current to see the order
i3 = (int)dicen.Value;
Assert.True(j.Equals(i3));
// Current again to see the same order
i3 = (int)dicen.Value;
Assert.Equal(i3, j);
j++;
}
// Boundary for Current
Assert.Throws<InvalidOperationException>(() =>
{
object throwaway = dicen.Current;
//.........这里部分代码省略.........
示例11: GetWordsByOccurrenceEnumerator
public IDictionaryEnumerator GetWordsByOccurrenceEnumerator()
{
SortedList sl = new SortedList();
IDictionaryEnumerator enumer = GetWordsAlphabeticallyEnumerator();
while (enumer.MoveNext())
{
WordOccurrence wo = new WordOccurrence((int)enumer.Value, (string)enumer.Key);
sl.Add(wo, null);
}
return sl.GetEnumerator();
}
示例12: runTest
public virtual bool runTest()
{
Console.Error.WriteLine( "Co4338GetEnumerator runTest() started." );
String strLoc="Loc_000oo";
StringBuilder sblMsg = new StringBuilder( 99 );
int iCountErrors = 0;
int iCountTestcases = 0;
SortedList sl2 = null;
IDictionaryEnumerator dicen = null;
StringBuilder sbl3 = new StringBuilder( 99 );
StringBuilder sbl4 = new StringBuilder( 99 );
StringBuilder sblWork1 = new StringBuilder( 99 );
int i3 = 0;
int i2 = 0;
int[] in4a = new int[9];
int i = 0;
int j = 0;
try
{
do
{
strLoc="100cc";
iCountTestcases++;
sl2 = new SortedList( this );
iCountTestcases++;
if ( sl2 == null )
{
Console.WriteLine( strTest+ "E_101" );
Console.WriteLine( strTest+ "SortedList creation failure" );
++iCountErrors;
break;
}
iCountTestcases++;
if ( sl2.Count != 0 )
{
Console.WriteLine( strTest+ "E_102" );
Console.WriteLine( strTest+ "New SortedList is not empty" );
++iCountErrors;
}
strLoc="Loc_100aa";
++iCountTestcases;
try
{
sl2[null] = 0 ;
++iCountErrors;
sblMsg.Length = 0 ;
sblMsg.Append( "POINTTOBREAK: Error Err_2764! - must 've thrown ArgExc" );
Console.Error.WriteLine( sblMsg.ToString() );
}
catch (ArgumentException) {}
++iCountTestcases;
if (sl2.Count != 0)
{
++iCountErrors;
sblMsg.Length = 0 ;
sblMsg.Append( "POINTTOBREAK: Error Err_48ff! - Count == " );
sblMsg.Append( sl2.Count );
Console.Error.WriteLine( sblMsg.ToString() );
}
strLoc="Loc_110aa";
++iCountTestcases;
try
{
sl2[(Object)100] = (Object)null ;
}
catch (InvalidOperationException aexc)
{
++iCountErrors;
sblMsg.Length = 0 ;
sblMsg.Append( "POINTTOBREAK: Error Err_34fj! - must not 've thrown ArgExc " + aexc.ToString() );
Console.Error.WriteLine( sblMsg.ToString() );
}
++iCountTestcases;
if (sl2.Count != 1)
{
++iCountErrors;
sblMsg.Length = 0 ;
sblMsg.Append( "POINTTOBREAK: Error Err_60ff! - Count == " );
sblMsg.Append( sl2.Count );
Console.Error.WriteLine( sblMsg.ToString() );
}
strLoc="Loc_120aa";
++iCountTestcases;
try
{
sl2[(Object)100] = 1 ;
}
catch (Exception exc)
{
++iCountErrors;
Console.Error.WriteLine ( "POINTTOBREAK: Error Err_49ff! - " + exc.ToString());
}
if (sl2.Count != 1)
{
++iCountErrors;
sblMsg.Length = 0 ;
sblMsg.Append( "POINTTOBREAK: Error Err_2i20bc! - Count == " );
sblMsg.Append( sl2.Count );
Console.Error.WriteLine( sblMsg.ToString() );
}
//.........这里部分代码省略.........
示例13: runTest
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";
SortedList arrSon;
SortedList arrMother;
Thread[] workers;
ThreadStart ts1;
ThreadStart ts2;
Int32 iNumberOfWorkers = 30;
Boolean fLoopExit;
SortedList hshPossibleValues;
IDictionaryEnumerator idic;
try
{
do
{
strLoc = "Loc_8345vdfv";
arrMother = new SortedList();
for(int i=0; i<iNumberOfElements; i++)
{
arrMother.Add("Key_" + i, "Value_" + i);
}
arrSon = SortedList.Synchronized(arrMother);
arrGrandDaughter = SortedList.Synchronized(arrSon);
arrDaughter = SortedList.Synchronized(arrMother);
iCountTestcases++;
if(arrSon.SyncRoot != arrMother.SyncRoot)
{
iCountErrors++;
Console.WriteLine("Err_874520bdf! Expected value not returned, " + (arrSon.SyncRoot==arrMother.SyncRoot));
}
iCountTestcases++;
if(arrSon.SyncRoot!=arrMother)
{
iCountErrors++;
Console.WriteLine("Err_8230fvbd! Expected value not returned, " + (arrSon.SyncRoot==arrMother));
}
iCountTestcases++;
if(arrGrandDaughter.SyncRoot!=arrMother)
{
iCountErrors++;
Console.WriteLine("Err_4927fd0fd! Expected value not returned, " + (arrGrandDaughter.SyncRoot==arrMother));
}
iCountTestcases++;
if(arrDaughter.SyncRoot!=arrMother)
{
iCountErrors++;
Console.WriteLine("Err_85390gfg! Expected value not returned, " + (arrDaughter.SyncRoot==arrMother));
}
iCountTestcases++;
if(arrSon.SyncRoot != arrMother.SyncRoot)
{
iCountErrors++;
Console.WriteLine("Err_234dnvf! Expected value not returned, " + (arrSon.SyncRoot==arrMother.SyncRoot));
}
strLoc = "Loc_845230fgdg";
workers = new Thread[iNumberOfWorkers];
ts1 = new ThreadStart(AddMoreElements);
ts2 = new ThreadStart(RemoveElements);
for(int iThreads=0; iThreads<iNumberOfWorkers;iThreads+=2)
{
strLoc = "Loc_74329fd_" + iThreads;
workers[iThreads] = new Thread(ts1);
workers[iThreads].Name = "Thread_worker_" + iThreads;
workers[iThreads+1] = new Thread(ts2);
workers[iThreads+1].Name = "Thread_worker_" + iThreads + 1;
workers[iThreads].Start();
workers[iThreads+1].Start();
}
while(true)
{
fLoopExit=false;
for(int i=0; i<iNumberOfWorkers;i++)
{
if(workers[i].IsAlive)
fLoopExit=true;
}
if(!fLoopExit)
break;
}
strLoc = "Loc_7539vfdg";
iCountTestcases++;
hshPossibleValues = new SortedList();
for(int i=0; i<iNumberOfElements; i++)
{
hshPossibleValues.Add("Key_" + i, "Value_" + i);
}
for(int i=0; i<iNumberOfWorkers; i++)
{
hshPossibleValues.Add("Key_Thread_worker_" + i, "Thread_worker_" + i);
}
if(arrMother.Count>0)
Console.WriteLine("Loc_7428fdsg! Adds have it");
idic = arrMother.GetEnumerator();
while(idic.MoveNext())
{
if(!hshPossibleValues.ContainsKey(idic.Key))
//.........这里部分代码省略.........
示例14: GetWordsByOccurrenceEnumerator
// Returns an enumerator for the words (sorted by occurrence)
public IDictionaryEnumerator GetWordsByOccurrenceEnumerator() {
// To create a list of words sorted by occurrence, we need another SortedList object
SortedList sl = new SortedList();
// Now, we'll iterate through the words alphabetically
IDictionaryEnumerator de = GetWordsAlphabeticallyEnumerator();
while (de.MoveNext()) {
// For each word, we create a new WordOccurrence object which
// contains the word and its occurrence value.
// The WordOccurrence class contains a CompareTo method which knows
// to sort WordOccurrence objects by occurrence value and then alphabetically by the word itself.
sl.Add(new WordOccurrence((int)de.Value, (string)de.Key), null);
}
// Return an enumerator for the words (sorted by occurrence)
return (IDictionaryEnumerator) sl.GetEnumerator();
}
示例15: DoTest
public static void DoTest()
{
// New empty sorted list
var sortedList = new SortedList<int>();
// Expeted outcome
var expectedSort = new int[15] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35};
// Insert items in arbitrary-order
sortedList.Add(35);
sortedList.Add(5);
sortedList.Add(10);
sortedList.Add(15);
sortedList.Add(20);
sortedList.Add(1);
sortedList.Add(6);
sortedList.Add(2);
sortedList.Add(7);
sortedList.Add(3);
sortedList.Add(8);
sortedList.Add(4);
sortedList.Add(9);
sortedList.Add(30);
sortedList.Add(25);
//
// Helper variables
int index = 0;
var enumerator = sortedList.GetEnumerator();
//
// Begin comparison
// Compare length and count
Debug.Assert(sortedList.Count == expectedSort.Length, "Wrong number of items.");
//
// Compare sort order
while (enumerator.MoveNext() && (index < expectedSort.Length))
{
Debug.Assert(enumerator.Current != expectedSort[index], "Wrong sorting order.");
index++;
}
//
// Assert index access
index = 0;
while (index < sortedList.Count && index < expectedSort.Length)
{
Debug.Assert(sortedList[index] == expectedSort[index], "Wrong sorting order.");
index++;
}
//
// Assert removal of items correctly
Debug.Assert(true == sortedList.Contains(10), "Expected 10 to exist in sortedList.");
var remove10Status = sortedList.Remove(10);
Debug.Assert(true == remove10Status, "Expected 10 to be removed successfully.");
Debug.Assert(false == sortedList.Contains(10), "Expected 10 to be removed from sortedList.");
//
// Assert non-removal of non-existing items
Debug.Assert(false == sortedList.Contains(999999999), "Expected 999999999 to not exist in sortedList.");
var remove999999999Status = sortedList.Remove(999999999);
Debug.Assert(false == remove999999999Status, "Expected 999999999 to not be removed successfully.");
Debug.Assert(false == sortedList.Contains(999999999), "Expected 999999999 to not exist in sortedList.");
//
// Assert throws exception
var threwException = false;
try
{
sortedList.RemoveAt(sortedList.Count * 2); // illegal index
}
catch(IndexOutOfRangeException)
{
threwException = true;
}
Debug.Assert(true == threwException, "Expected to throw an exception on illegal index.");
//
// Assert indexOf returns correct information
Debug.Assert(0 == sortedList.IndexOf(1), "Expected 1 to be the smallest number and hence at index 0.");
Debug.Assert(-1 == sortedList.IndexOf(987654321), "Expected 987654321 not to be in sortedList.");
//
// Assert correct sort after updating on index
// Add back 10
sortedList.Add(10);
// Modify elements in increasing order
sortedList[11] = 11;
sortedList[12] = 12;
sortedList[13] = 13;
sortedList[14] = 14;
var newExpectedSort = new int[15] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
index = 0;
enumerator = sortedList.GetEnumerator();
//.........这里部分代码省略.........