public void Add() { int count = 10; StringDictionary stringDictionary = new StringDictionary(); for (int i = 0; i < count; i++) { string key = "Key_" + i; string value = "Value_" + i; stringDictionary.Add(key, value); Assert.Equal(i + 1, stringDictionary.Count); Assert.True(stringDictionary.ContainsKey(key)); Assert.True(stringDictionary.ContainsValue(value)); Assert.Equal(value, stringDictionary[key]); } Assert.False(stringDictionary.ContainsValue(null)); stringDictionary.Add("nullkey", null); Assert.Equal(count + 1, stringDictionary.Count); Assert.True(stringDictionary.ContainsKey("nullkey")); Assert.True(stringDictionary.ContainsValue(null)); Assert.Null(stringDictionary["nullkey"]); }
void collection_StringDictionary() { ClearConsole(); StringDictionary dict = new StringDictionary(); dict.Add("cat", "feline"); dict.Add("dog", "canine"); Debug.Log(dict["cat"]); Debug.Log(dict["test"] == null); Debug.Log(dict.ContainsKey("cat")); Debug.Log(dict.ContainsKey("puppet")); Debug.Log(dict.ContainsValue("feline")); Debug.Log(dict.ContainsValue("perls")); Debug.Log(dict.IsSynchronized); foreach (DictionaryEntry entry in dict) { Debug.Log(entry.Key + " " + entry.Value); } foreach (string key in dict.Keys) { Debug.Log(key); } foreach (string value in dict.Values) { Debug.Log(value); } dict.Remove("cat"); Debug.Log(dict.Count); dict.Clear(); Debug.Log(dict.Count); }
public void Add() { int count = 10; StringDictionary stringDictionary = new StringDictionary(); for (int i = 0; i < count; i++) { string key = "Key_" + i; string value = "Value_" + i; stringDictionary.Add(key, value); Assert.Equal(i + 1, stringDictionary.Count); Assert.True(stringDictionary.ContainsKey(key)); Assert.True(stringDictionary.ContainsValue(value)); Assert.Equal(value, stringDictionary[key]); } Assert.False(stringDictionary.ContainsValue(null)); stringDictionary.Add("nullkey", null); Assert.Equal(count + 1, stringDictionary.Count); Assert.True(stringDictionary.ContainsKey("nullkey")); Assert.True(stringDictionary.ContainsValue(null)); Assert.Null(stringDictionary["nullkey"]); }
public void ContainsValue_IsCaseSensitive() { StringDictionary stringDictionary = new StringDictionary(); stringDictionary.Add("key", "value"); Assert.False(stringDictionary.ContainsValue("VALUE")); Assert.False(stringDictionary.ContainsValue("vaLue")); Assert.True(stringDictionary.ContainsValue("value")); }
public void ContainsValue_IsCaseSensitive() { StringDictionary stringDictionary = new StringDictionary(); stringDictionary.Add("key", "value"); Assert.False(stringDictionary.ContainsValue("VALUE")); Assert.False(stringDictionary.ContainsValue("vaLue")); Assert.True(stringDictionary.ContainsValue("value")); }
static void Main() { StringDictionary strdict = new StringDictionary(); strdict.Add("One", "first"); strdict.Add("Two", "second"); strdict.Add("Three", "third"); strdict.Add("Four", "fourth"); strdict.Add("Five", "fifth"); foreach (DictionaryEntry v in strdict) { Console.WriteLine(v.Key + ":" + v.Value); } Console.WriteLine("contains key One :{0}", strdict.ContainsKey("One")); Console.WriteLine("containe value 'second': {0}", strdict.ContainsValue("second")); Console.WriteLine("remove key------"); strdict.Remove("three"); foreach (DictionaryEntry v1 in strdict) { Console.WriteLine(v1.Key + ":" + v1.Value); } Console.WriteLine("***copyto***"); DictionaryEntry[] de = new DictionaryEntry[strdict.Count]; strdict.CopyTo(de, 0); foreach (DictionaryEntry v3 in de) { Console.WriteLine(v3.Key + ":" + v3.Value); } }
public void ContainsValue_DuplicateValues(string value) { StringDictionary stringDictionary = new StringDictionary(); stringDictionary.Add("key1", value); stringDictionary.Add("key2", value); Assert.True(stringDictionary.ContainsValue(value)); }
/////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// private List <FieldMap> _LoadFieldsMap(XmlNode nodeFieldsMap, ImportType type) { StringDictionary mapTitle2Name = PropertyHelpers.GetTitle2NameMap(type); List <FieldMap> fieldsMap = new List <FieldMap>(); foreach (XmlNode node in nodeFieldsMap.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; // skip comments and other non element nodes } if (node.Name.Equals(NODE_NAME_FIELDSMAP, StringComparison.OrdinalIgnoreCase)) { FieldMap fieldMap = new FieldMap(node.Attributes[ATTRIBUTE_NAME_DSTFIELD].Value, node.Attributes[ATTRIBUTE_NAME_SRCFIELD].Value); if (mapTitle2Name.ContainsValue(fieldMap.ObjectFieldName)) { fieldsMap.Add(fieldMap); } } } return(fieldsMap); }
public void Remove(int count) { StringDictionary stringDictionary = Helpers.CreateStringDictionary(count); stringDictionary.Remove("non-existent-key"); Assert.Equal(count, stringDictionary.Count); for (int i = 0; i < count; i++) { string key = "Key_" + i; if (i == 0) { stringDictionary.Remove(key.ToUpperInvariant()); } else if (i == 1) { stringDictionary.Remove(key.ToLowerInvariant()); } else { stringDictionary.Remove(key); } Assert.False(stringDictionary.ContainsKey(key)); Assert.False(stringDictionary.ContainsValue("Value_" + i)); Assert.Equal(count - i - 1, stringDictionary.Count); } }
/// <summary> /// Binds the country dropdown list with countries retrieved /// from the .NET Framework. /// </summary> public void BindCountries() { StringDictionary dic = new StringDictionary(); List<string> col = new List<string>(); foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { RegionInfo ri = new RegionInfo(ci.Name); if (!dic.ContainsKey(ri.EnglishName)) dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant()); if (!col.Contains(ri.EnglishName)) col.Add(ri.EnglishName); } // Add custom cultures if (!dic.ContainsValue("bd")) { dic.Add("Bangladesh", "bd"); col.Add("Bangladesh"); } col.Sort(); ddlCountry.Items.Add(new ListItem("[Not specified]", "")); foreach (string key in col) { ddlCountry.Items.Add(new ListItem(key, dic[key])); } SetDefaultCountry(); }
public void Remove_DuplicateValues() { StringDictionary stringDictionary = new StringDictionary(); stringDictionary.Add("key1", "value"); stringDictionary.Add("key2", "value"); stringDictionary.Remove("key1"); Assert.Equal(1, stringDictionary.Count); Assert.False(stringDictionary.ContainsKey("key1")); Assert.True(stringDictionary.ContainsValue("value")); stringDictionary.Remove("key2"); Assert.Equal(0, stringDictionary.Count); Assert.False(stringDictionary.ContainsKey("key2")); Assert.False(stringDictionary.ContainsValue("value")); }
public static void Main() { // Creates and initializes a new StringDictionary. StringDictionary myCol = new StringDictionary(); myCol.Add("red", "rojo"); myCol.Add("green", "verde"); myCol.Add("blue", "azul"); // Displays the values in the StringDictionary. Console.WriteLine("Initial contents of the StringDictionary:"); PrintKeysAndValues(myCol); // Searches for a key. if (myCol.ContainsKey("red")) { Console.WriteLine("The collection contains the key \"red\"."); } else { Console.WriteLine("The collection does not contain the key \"red\"."); } Console.WriteLine(); // Searches for a value. if (myCol.ContainsValue("amarillo")) { Console.WriteLine("The collection contains the value \"amarillo\"."); } else { Console.WriteLine("The collection does not contain the value \"amarillo\"."); } Console.WriteLine(); }
public void GetEnumerator(int count) { StringDictionary stringDictionary = Helpers.CreateStringDictionary(count); IEnumerator enumerator = stringDictionary.GetEnumerator(); Assert.NotSame(stringDictionary.GetEnumerator(), stringDictionary.GetEnumerator()); for (int i = 0; i < 2; i++) { int counter = 0; while (enumerator.MoveNext()) { DictionaryEntry current = (DictionaryEntry)enumerator.Current; string key = (string)current.Key; string value = (string)current.Value; Assert.True(stringDictionary.ContainsKey(key)); Assert.True(stringDictionary.ContainsValue(value)); counter++; } Assert.Equal(stringDictionary.Count, counter); enumerator.Reset(); } }
public void Values_CopyTo(int count, int index) { StringDictionary stringDictionary = Helpers.CreateStringDictionary(count); ICollection values = stringDictionary.Values; string[] array = new string[count + index + 5]; values.CopyTo(array, index); IEnumerator enumerator = stringDictionary.GetEnumerator(); for (int i = 0; i < index; i++) { Assert.Null(array[i]); } for (int i = index; i < index + count; i++) { enumerator.MoveNext(); DictionaryEntry entry = (DictionaryEntry)enumerator.Current; string value = array[i]; Assert.Equal(entry.Value, value); Assert.True(stringDictionary.ContainsValue(value)); } for (int i = index + count; i < array.Length; i++) { Assert.Null(array[i]); } }
public static void Main() { // Creates and initializes a new StringDictionary. StringDictionary myCol = new StringDictionary(); myCol.Add("red", "rojo"); myCol.Add("green", "verde"); myCol.Add("blue", "azul"); Console.WriteLine("Count: {0}", myCol.Count); // 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 IEnumerator:"); 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 StringDictionary 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,-10} {1}", myArr[i].Key, myArr[i].Value); } Console.WriteLine(); // Searches for a value. if (myCol.ContainsValue("amarillo")) { Console.WriteLine("The collection contains the value \"amarillo\"."); } else { Console.WriteLine("The collection does not contain the value \"amarillo\"."); } Console.WriteLine(); // Searches for a key and deletes it. if (myCol.ContainsKey("green")) { myCol.Remove("green"); } Console.WriteLine("The collection contains the following elements after removing \"green\":"); PrintKeysAndValues1(myCol); // Clears the entire collection. myCol.Clear(); Console.WriteLine("The collection contains the following elements after it is cleared:"); PrintKeysAndValues1(myCol); }
public void Remove_DuplicateValues() { StringDictionary stringDictionary = new StringDictionary(); stringDictionary.Add("key1", "value"); stringDictionary.Add("key2", "value"); stringDictionary.Remove("key1"); Assert.Equal(1, stringDictionary.Count); Assert.False(stringDictionary.ContainsKey("key1")); Assert.True(stringDictionary.ContainsValue("value")); stringDictionary.Remove("key2"); Assert.Equal(0, stringDictionary.Count); Assert.False(stringDictionary.ContainsKey("key2")); Assert.False(stringDictionary.ContainsValue("value")); }
/// <summary> /// Binds the country dropdown list with countries retrieved /// from the .NET Framework. /// </summary> public void BindCountries() { var dic = new StringDictionary(); var col = new List <string>(); foreach ( var ri in CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(ci => new RegionInfo(ci.Name))) { if (!dic.ContainsKey(ri.EnglishName)) { dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant()); } if (!col.Contains(ri.EnglishName)) { col.Add(ri.EnglishName); } } // Add custom cultures if (!dic.ContainsValue("bd")) { dic.Add("Bangladesh", "bd"); col.Add("Bangladesh"); } if (!dic.ContainsValue("bm")) { dic.Add("Bermuda", "bm"); col.Add("Bermuda"); } col.Sort(); this.ddlCountry.Items.Add(new ListItem("[Not specified]", string.Empty)); foreach (var key in col) { this.ddlCountry.Items.Add(new ListItem(key, dic[key])); } if (this.ddlCountry.SelectedIndex == 0) { this.ddlCountry.SelectedValue = ResolveRegion().TwoLetterISORegionName.ToLowerInvariant(); this.SetFlagImageUrl(); } }
public void ContainsValue_DuplicateValues(string value) { StringDictionary stringDictionary = new StringDictionary(); stringDictionary.Add("key1", value); stringDictionary.Add("key2", value); Assert.True(stringDictionary.ContainsValue(value)); }
public void This_Empty() { StringDictionary sd = new StringDictionary(); sd[String.Empty] = null; Assert.IsNull(sd[String.Empty], "this[String.Empty]"); Assert.AreEqual(1, sd.Count, "Count-1"); Assert.IsTrue(sd.ContainsKey(String.Empty), "ContainsKey"); Assert.IsTrue(sd.ContainsValue(null), "ContainsValue"); }
static void Main() { StringDictionary strdic = new StringDictionary(); strdic.Add("one", "EmpId=101"); strdic.Add("two", "EmpId=102"); strdic.Add("three", "EmpId=103"); strdic.Add("four", "EmpId=104"); strdic.Add("five", "EmpId=105"); Console.WriteLine("Printing the Key Vakue pair"); foreach (DictionaryEntry item in strdic) { Console.WriteLine("{0} : {1}", item.Key, item.Value); } Console.WriteLine("Checking weather the key six is present in the String dictionary or not"); bool ck = strdic.ContainsKey("six"); Console.WriteLine("Contains or not:{0}", ck); Console.WriteLine("Checking weather the value Empid=104 exist in the String Dictionary or not"); bool cv = strdic.ContainsValue("EmpId=104"); Console.WriteLine("Value present or not:{0}", cv); DictionaryEntry[] str1 = new DictionaryEntry[strdic.Count]; strdic.CopyTo(str1, 0); Console.WriteLine("Printing the Copied array"); foreach (var ar in str1) { Console.WriteLine("{0}:{1}", ar.Key, ar.Value); } Console.WriteLine("Printing the elements of the String Dictionary using GetEnumerator method"); IEnumerator ie = strdic.GetEnumerator(); DictionaryEntry de; while (ie.MoveNext()) { de = (DictionaryEntry)ie.Current; Console.WriteLine("{0}:{1}", de.Key, de.Value); } strdic.Remove("five"); Console.WriteLine("Printing the String Dictionary after removing the key-----"); foreach (DictionaryEntry item1 in strdic) { Console.WriteLine("{0} : {1}", item1.Key, item1.Value); } //strdic.Clear(); Console.Read(); }
public static string GetUniqueName(StringDictionary list, string name) { // duplicate check int counter = 2; while (list.ContainsValue(name)) { name = _endNumberRegex.Replace(name, ""); name += counter.ToString(); counter++; } return(name); }
public void Empty() { StringDictionary sd = new StringDictionary(); Assert.AreEqual(0, sd.Count, "Count"); Assert.IsFalse(sd.IsSynchronized, "IsSynchronized"); Assert.AreEqual(0, sd.Keys.Count, "Keys"); Assert.AreEqual(0, sd.Values.Count, "Values"); Assert.IsNotNull(sd.SyncRoot, "SyncRoot"); Assert.IsFalse(sd.ContainsKey("a"), "ContainsKey"); Assert.IsFalse(sd.ContainsValue("1"), "ContainsValue"); sd.CopyTo(new DictionaryEntry[0], 0); Assert.IsNotNull(sd.GetEnumerator(), "GetEnumerator"); sd.Remove("a"); // doesn't exists sd.Clear(); }
public void SomeElements() { StringDictionary sd = new StringDictionary(); for (int i = 0; i < 10; i++) { sd.Add(i.ToString(), (i * 10).ToString()); } Assert.AreEqual("10", sd["1"], "this[1]"); Assert.AreEqual(10, sd.Count, "Count-10"); Assert.AreEqual(10, sd.Keys.Count, "Keys"); Assert.AreEqual(10, sd.Values.Count, "Values"); Assert.IsTrue(sd.ContainsKey("2"), "ContainsKey"); Assert.IsTrue(sd.ContainsValue("20"), "ContainsValue"); DictionaryEntry[] array = new DictionaryEntry[10]; sd.CopyTo(array, 0); sd.Remove("1"); Assert.AreEqual(9, sd.Count, "Count-9"); sd.Clear(); Assert.AreEqual(0, sd.Count, "Count-0"); }
static void Main() { StringDictionary sd = new StringDictionary(); sd.Add("cat", "feline"); sd.Add("dog", "canine"); Console.WriteLine(sd["cat"]); Console.WriteLine(sd["test"] == null); Console.WriteLine(sd.ContainsKey("puppet")); Console.WriteLine(sd.ContainsValue("feline")); Console.WriteLine(sd.IsSynchronized); Console.WriteLine("elements in the stringdictionary are:"); foreach (DictionaryEntry entry in sd) { Console.WriteLine("{0} = {1}", entry.Key, entry.Value); } foreach (string key in sd.Keys) { Console.WriteLine(key); } foreach (string value in sd.Values) { Console.WriteLine(value); } sd.Remove("cat"); Console.WriteLine("the no.of elements in the stringdictionary :"); Console.WriteLine(sd.Count); sd.Clear(); Console.WriteLine("after clear() no.of elements in the stringdictionary :"); Console.WriteLine(sd.Count); Console.ReadLine(); }
public void Values(int count) { StringDictionary stringDictionary = Helpers.CreateStringDictionary(count); ICollection values = stringDictionary.Values; Assert.Equal(count, values.Count); stringDictionary.Add("duplicatevaluekey1", "value"); stringDictionary.Add("duplicatevaluekey2", "value"); Assert.Equal(count + 2, values.Count); IEnumerator enumerator = stringDictionary.GetEnumerator(); foreach (string value in values) { enumerator.MoveNext(); DictionaryEntry entry = (DictionaryEntry)enumerator.Current; Assert.Equal(value, entry.Value); Assert.True(stringDictionary.ContainsValue(value)); } }
/// <summary> /// Binds the country dropdown list with countries retrieved /// from the .NET Framework. /// </summary> public void BindCountries() { StringDictionary dic = new StringDictionary(); List<string> col = new List<string>(); foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { RegionInfo ri = new RegionInfo(ci.Name); if (!dic.ContainsKey(ri.EnglishName)) dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant()); if (!col.Contains(ri.EnglishName)) col.Add(ri.EnglishName); } // Add custom cultures if (!dic.ContainsValue("bd")) { dic.Add("Bangladesh", "bd"); col.Add("Bangladesh"); } col.Sort(); ddlCountry.Items.Add(new ListItem("[Not specified]", "")); foreach (string key in col) { ddlCountry.Items.Add(new ListItem(key, dic[key])); } if (ddlCountry.SelectedIndex == 0 && Request.UserLanguages != null && Request.UserLanguages[0].Length == 5) { ddlCountry.SelectedValue = Request.UserLanguages[0].Substring(3); SetFlagImageUrl(); } }
/// <summary> /// Binds the country dropdown list with countries retrieved /// from the .NET Framework. /// </summary> public void BindCountries() { StringDictionary dic = new StringDictionary(); List <string> col = new List <string>(); foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { RegionInfo ri = new RegionInfo(ci.Name); if (!dic.ContainsKey(ri.EnglishName)) { dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant()); } if (!col.Contains(ri.EnglishName)) { col.Add(ri.EnglishName); } } // Add custom cultures if (!dic.ContainsValue("bd")) { dic.Add("Bangladesh", "bd"); col.Add("Bangladesh"); } col.Sort(); ddlCountry.Items.Add(new ListItem("[Not specified]", "")); foreach (string key in col) { ddlCountry.Items.Add(new ListItem(key, dic[key])); } SetDefaultCountry(); }
static void Main(string[] args) { StringDictionary strd = new StringDictionary(); strd.Add("1", "one"); strd.Add("2", "two"); strd.Add("3", "three"); strd.Add("4", "four"); strd.Add("5", "five"); Console.WriteLine("elements in the dictionary....."); foreach (DictionaryEntry i in strd) { Console.WriteLine("{0}={1}", i.Key, i.Value); } Console.WriteLine("contains key in the dictionary or not......."); var j = strd.ContainsKey("6"); Console.WriteLine(j); Console.WriteLine("contains value in the dictionary or not......."); var k = strd.ContainsValue("six"); Console.WriteLine(k); Console.WriteLine("retriving all keys from the dictionary..."); foreach (DictionaryEntry d in strd) { Console.WriteLine(d.Key); } Console.WriteLine("retriving all values from the dictionary..."); foreach (DictionaryEntry d in strd) { Console.WriteLine(d.Value); } Console.WriteLine("number of elements present in the dictionary....."); Console.WriteLine(strd.Count); Console.Read(); }
public void Test01() { IntlStrings intl; StringDictionary sd; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count string ind; // key // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] Add() simple strings // for (int i = 0; i < values.Length; i++) { cnt = sd.Count; sd.Add(keys[i], values[i]); if (sd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, sd.Count, cnt + 1)); } // verify that collection contains newly added item // if (!sd.ContainsValue(values[i])) { Assert.False(true, string.Format("Error, collection doesn't contain value of new item", i)); } if (!sd.ContainsKey(keys[i])) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } // access the item // if (String.Compare(sd[keys[i]], values[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, sd[keys[i]], values[i])); } } // // Intl strings // [] Add() Intl strings // int len = values.Length; string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) { val = intl.GetRandomString(MAX_LEN); } intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLowerInvariant() == intlValues[i].ToUpperInvariant()) { caseInsensitive = true; } } // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Add(intlValues[i + len], intlValues[i]); if (sd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, sd.Count, cnt + 1)); } // verify that collection contains newly added item // if (!sd.ContainsValue(intlValues[i])) { Assert.False(true, string.Format("Error, collection doesn't contain value of new item", i)); } if (!sd.ContainsKey(intlValues[i + len])) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } // access the item // ind = intlValues[i + len]; if (String.Compare(sd[ind], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, sd[ind], intlValues[i])); } } // // [] Case sensitivity // string[] intlValuesLower = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToUpperInvariant(); } for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValues[i].ToLowerInvariant(); } sd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Add(intlValues[i + len], intlValues[i]); if (sd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, sd.Count, cnt + 1)); } // verify that collection contains newly added uppercase item // if (!sd.ContainsValue(intlValues[i])) { Assert.False(true, string.Format("Error, collection doesn't contain value of new item", i)); } if (!sd.ContainsKey(intlValues[i + len])) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } // verify that collection doesn't contains lowercase item // if (!caseInsensitive && sd.ContainsValue(intlValuesLower[i])) { Assert.False(true, string.Format("Error, collection contains lowercase value of new item", i)); } // key is case insensitive if (!sd.ContainsKey(intlValuesLower[i + len])) { Assert.False(true, string.Format("Error, collection doesn't contain lowercase key of new item", i)); } } // // [] Add (string, null) // cnt = sd.Count; sd.Add("keykey", null); if (sd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, cnt + 1)); } // verify that collection contains null // if (!sd.ContainsValue(null)) { Assert.False(true, string.Format("Error, collection doesn't contain null")); } // access item-null // if (sd["keykey"] != null) { Assert.False(true, string.Format("Error, returned non-null on place of null")); } // // Add item with null key - ArgumentNullException expected // [] Add (null, string) // Assert.Throws <ArgumentNullException>(() => { sd.Add(null, "item"); }); // // Add duplicate key item - ArgumentException expected // [] Add duplicate key item // // generate key string k = intl.GetRandomString(MAX_LEN); if (!sd.ContainsKey(k)) { sd.Add(k, "newItem"); } if (!sd.ContainsKey(k)) { Assert.False(true, string.Format("Error,failed to add item")); } else { Assert.Throws <ArgumentException>(() => { sd.Add(k, "itemitemitem"); }); } }
public virtual 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"; StringDictionary sd; IEnumerator en; DictionaryEntry curr; 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" }; try { Console.WriteLine("--- create dictionary ---"); strLoc = "Loc_001oo"; iCountTestcases++; sd = new StringDictionary(); Console.WriteLine("1. Enumerator for empty dictionary"); Console.WriteLine(" - get type"); iCountTestcases++; en = sd.GetEnumerator(); string type = en.GetType().ToString(); if ( type.IndexOf("Enumerator", 0) == 0 ) { iCountErrors++; Console.WriteLine("Err_0001a, type is not Enumerator"); } Console.WriteLine(" - MoveNext"); iCountTestcases++; bool res = en.MoveNext(); if ( res ) { iCountErrors++; Console.WriteLine("Err_0001b, MoveNext returned true"); } Console.WriteLine(" - Current"); iCountTestcases++; try { curr = (DictionaryEntry)en.Current; iCountErrors++; Console.WriteLine("Err_0001c, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0001d, unexpected exception: {0}", e.ToString()); } Console.WriteLine("2. GetEnumerator for filled collection"); strLoc = "Loc_002oo"; iCountTestcases++; for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } Console.WriteLine(" - get type"); iCountTestcases++; en = sd.GetEnumerator(); type = en.GetType().ToString(); if ( type.IndexOf("Enumerator", 0) == 0 ) { iCountErrors++; Console.WriteLine("Err_0002a, type is not Enumerator"); } Console.WriteLine(" - MoveNext and Current within collection"); for (int i = 0; i < sd.Count; i++) { iCountTestcases++; res = en.MoveNext(); if ( !res ) { iCountErrors++; Console.WriteLine("Err_0002b_{0}, MoveNext returned false", i); } iCountTestcases++; curr = (DictionaryEntry)en.Current; if (! sd.ContainsValue(curr.Value.ToString()) ) { iCountErrors++; Console.WriteLine("Err_0002c_{0}, Current dictionary doesn't contain value from enumerator", i); } iCountTestcases++; if (! sd.ContainsKey(curr.Key.ToString()) ) { iCountErrors++; Console.WriteLine("Err_0002d_{0}, Current dictionary doesn't contain key from enumerator", i); } iCountTestcases++; if ( String.Compare(sd[curr.Key.ToString()], curr.Value.ToString(), false) != 0 ) { iCountErrors++; Console.WriteLine("Err_0002e_{0}, Value for current Key is different in dictionary", i); } iCountTestcases++; DictionaryEntry curr1 = (DictionaryEntry)en.Current; if (! curr.Equals(curr1) ) { iCountErrors++; Console.WriteLine("Err_0002f_{0}, second call of Current returned different result", i); } } res = en.MoveNext(); Console.WriteLine(" - MoveNext outside of the collection"); iCountTestcases++; res = en.MoveNext(); if ( res ) { iCountErrors++; Console.WriteLine("Err_0002g, MoveNext returned true"); } Console.WriteLine(" - Current outside of the collection"); iCountTestcases++; try { curr = (DictionaryEntry)en.Current; iCountErrors++; Console.WriteLine("Err_0002h, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0002i, unexpected exception: {0}", e.ToString()); } Console.WriteLine(" - Reset"); iCountTestcases++; en.Reset(); Console.WriteLine(" - get Current after Reset"); iCountTestcases++; try { curr = (DictionaryEntry)en.Current; iCountErrors++; Console.WriteLine("Err_0002j, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0002k, unexpected exception: {0}", e.ToString()); } Console.WriteLine("3. Enumerator and modified dictionary"); strLoc = "Loc_003oo"; iCountTestcases++; if (sd.Count < 1) { for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } } iCountTestcases++; en = sd.GetEnumerator(); Console.WriteLine(" - MoveNext"); res = en.MoveNext(); if (!res) { iCountErrors++; Console.WriteLine("Err_0003a, MoveNext returned false"); } Console.WriteLine(" - modify collection"); curr = (DictionaryEntry)en.Current; int cnt = sd.Count; iCountTestcases++; sd.Remove(keys[0]); if ( sd.Count != cnt - 1 ) { iCountErrors++; Console.WriteLine("Err_0003b, didn't remove item with 0th key"); } Console.WriteLine(" - get Current"); iCountTestcases++; DictionaryEntry curr2 = (DictionaryEntry)en.Current; if (! curr.Equals(curr2) ) { iCountErrors++; Console.WriteLine("Err_0003c, current returned different value after midification"); } Console.WriteLine(" - call MoveNext"); iCountTestcases++; try { res = en.MoveNext(); iCountErrors++; Console.WriteLine("Err_0003d, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0003e, unexpected exception: {0}", e.ToString()); } Console.WriteLine("4. Modify dictionary after enumerated beyond the end"); strLoc = "Loc_004oo"; iCountTestcases++; sd.Clear(); for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } iCountTestcases++; en = sd.GetEnumerator(); for (int i = 0; i < sd.Count; i ++) { en.MoveNext(); } Console.WriteLine(" - get Current at the end of the dictionary"); curr = (DictionaryEntry)en.Current; Console.WriteLine(" - modify collection"); curr = (DictionaryEntry)en.Current; cnt = sd.Count; iCountTestcases++; sd.Remove(keys[0]); if ( sd.Count != cnt - 1 ) { iCountErrors++; Console.WriteLine("Err_0004b, didn't remove item with 0th key"); } Console.WriteLine(" - get Current after modifying"); iCountTestcases++; curr2 = (DictionaryEntry)en.Current; if (! curr.Equals(curr2) ) { iCountErrors++; Console.WriteLine("Err_0004c, current returned different value after midification"); } Console.WriteLine(" - call MoveNext after modifying"); iCountTestcases++; try { res = en.MoveNext(); iCountErrors++; Console.WriteLine("Err_0004d, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0004e, 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 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"; StringDictionary sd; 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" }; int cnt = 0; string ind; try { intl = new IntlStrings(); Console.WriteLine("--- create collection ---"); strLoc = "Loc_001oo"; iCountTestcases++; sd = new StringDictionary(); Console.WriteLine("1. add simple strings"); for (int i = 0; i < values.Length; i++) { iCountTestcases++; cnt = sd.Count; sd.Add(keys[i], values[i]); if (sd.Count != cnt+1) { iCountErrors++; Console.WriteLine("Err_0001_{0}a, count is {1} instead of {2}", i, sd.Count, cnt+1); } iCountTestcases++; if (!sd.ContainsValue(values[i])) { iCountErrors++; Console.WriteLine("Err_0001_{0}b, collection doesn't contain value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(keys[i])) { iCountErrors++; Console.WriteLine("Err_0001_{0}c, collection doesn't contain key of new item", i); } iCountTestcases++; if (String.Compare(sd[keys[i]], values[i], false) != 0) { iCountErrors++; Console.WriteLine("Err_0001_{0}d, returned item \"{1}\" instead of \"{2}\"", i, sd[keys[i]], values[i]); } } Console.WriteLine("2. add intl strings"); int len = values.Length; 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; } Console.WriteLine(" initial number of items: " + sd.Count); strLoc = "Loc_002oo"; for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Add(intlValues[i+len], intlValues[i]); if (sd.Count != cnt+1) { iCountErrors++; Console.WriteLine("Err_0002_{0}a, count is {1} instead of {2}", i, sd.Count, cnt+1); } iCountTestcases++; if (!sd.ContainsValue(intlValues[i])) { iCountErrors++; Console.WriteLine("Err_0002_{0}b, collection doesn't contain value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(intlValues[i+len])) { iCountErrors++; Console.WriteLine("Err_0002_{0}c, collection doesn't contain key of new item", i); } ind = intlValues[i+len]; iCountTestcases++; if (String.Compare(sd[ind], intlValues[i], false) != 0) { iCountErrors++; Console.WriteLine("Err_0002_{0}d, returned item \"{1}\" instead of \"{2}\"", i, sd[ind], intlValues[i]); } } Console.WriteLine("3. Case sensitivity"); 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(); } sd.Clear(); Console.WriteLine(" initial number of items: " + sd.Count); strLoc = "Loc_003oo"; for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Add(intlValues[i+len], intlValues[i]); if (sd.Count != cnt+1) { iCountErrors++; Console.WriteLine("Err_0003_{0}a, count is {1} instead of {2}", i, sd.Count, cnt+1); } iCountTestcases++; if (!sd.ContainsValue(intlValues[i])) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, collection doesn't contain value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(intlValues[i+len])) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, collection doesn't contain key of new item", i); } iCountTestcases++; if (!caseInsensitive && sd.ContainsValue(intlValuesLower[i])) { iCountErrors++; Console.WriteLine("Err_0003_{0}d, collection contains lowercase value of new item", i); } iCountTestcases++; if ( !sd.ContainsKey(intlValuesLower[i+len])) { iCountErrors++; Console.WriteLine("Err_0003_{0}e, collection doesn't contain lowercase key of new item", i); } } Console.WriteLine("4. Add (string, null) "); Console.WriteLine(" initial number of items: " + sd.Count); strLoc = "Loc_004oo"; iCountTestcases++; cnt = sd.Count; sd.Add("keykey", null); iCountTestcases++; if (sd.Count != cnt+1) { iCountErrors++; Console.WriteLine("Err_0004a, count is {0} instead of {1}", sd.Count, cnt+1); } iCountTestcases++; if (!sd.ContainsValue(null)) { iCountErrors++; Console.WriteLine("Err_0004b, collection doesn't contain null"); } iCountTestcases++; if (sd["keykey"] != null) { iCountErrors++; Console.WriteLine("Err_0004c, returned non-null on place of null"); } Console.WriteLine("5. Add (null, string) "); Console.WriteLine(" initial number of items: " + sd.Count); strLoc = "Loc_005oo"; iCountTestcases++; try { sd.Add(null, "item"); iCountErrors++; Console.WriteLine("Err_0005a, ArgumentNullException Expected"); } // catch (System.ArgumentNullException e) { Console.WriteLine("expected exception: {0}", e.ToString()); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0005b, unexpected exception: " + e.ToString()); } Console.WriteLine("6. Add (key, value) with duplicate key "); Console.WriteLine(" initial number of items: " + sd.Count); strLoc = "Loc_006oo"; iCountTestcases++; string k = intl.GetString(MAX_LEN, true, true, true); if (! sd.ContainsKey(k)) { sd.Add(k, "newItem"); } if (! sd.ContainsKey(k)) { iCountErrors++; Console.WriteLine("Err_0005a,failed to add item"); } else { try { sd.Add(k, "itemitemitem"); iCountErrors++; Console.WriteLine("Err_0005b, no exception"); } catch (ArgumentException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0005c, unexpected exception: " + 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 void Test01() { StringDictionary sd; IEnumerator en; DictionaryEntry curr; // Enumerator.Current value // simple string values string[] values = { "a", "aa", "", " ", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; // [] StringDictionary GetEnumerator() //----------------------------------------------------------------- sd = new StringDictionary(); // [] Enumerator for empty dictionary // en = sd.GetEnumerator(); string type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } // // MoveNext should return false // bool res = en.MoveNext(); if (res) { Assert.False(true, string.Format("Error, MoveNext returned true")); } // // Attempt to get Current should result in exception // Assert.Throws <InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // Filled collection // [] Enumerator for filled dictionary // for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } en = sd.GetEnumerator(); type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } // // MoveNext should return true // for (int i = 0; i < sd.Count; i++) { res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false", i)); } curr = (DictionaryEntry)en.Current; // //enumerator enumerates in different than added order // so we'll check Contains // if (!sd.ContainsValue(curr.Value.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain value from enumerator", i)); } if (!sd.ContainsKey(curr.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (String.Compare(sd[curr.Key.ToString()], curr.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, Value for current Key is different in dictionary", i)); } // while we didn't MoveNext, Current should return the same value DictionaryEntry curr1 = (DictionaryEntry)en.Current; if (!curr.Equals(curr1)) { Assert.False(true, string.Format("Error, second call of Current returned different result", i)); } } // next MoveNext should bring us outside of the collection // res = en.MoveNext(); res = en.MoveNext(); if (res) { Assert.False(true, string.Format("Error, MoveNext returned true")); } // // Attempt to get Current should result in exception // Assert.Throws <InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); en.Reset(); // // Attempt to get Current should result in exception // Assert.Throws <InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // [] Modify dictionary when enumerating // if (sd.Count < 1) { for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } } en = sd.GetEnumerator(); res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false")); } curr = (DictionaryEntry)en.Current; int cnt = sd.Count; sd.Remove(keys[0]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item DictionaryEntry curr2 = (DictionaryEntry)en.Current; if (!curr.Equals(curr2)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // exception expected Assert.Throws <InvalidOperationException>(() => { res = en.MoveNext(); }); // // [] Modify dictionary when enumerated beyond the end // sd.Clear(); for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } en = sd.GetEnumerator(); for (int i = 0; i < sd.Count; i++) { en.MoveNext(); } curr = (DictionaryEntry)en.Current; curr = (DictionaryEntry)en.Current; cnt = sd.Count; sd.Remove(keys[0]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item curr2 = (DictionaryEntry)en.Current; if (!curr.Equals(curr2)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // exception expected Assert.Throws <InvalidOperationException>(() => { res = en.MoveNext(); }); }
public void TestEnvironmentVariablesPropertyUnix() { ProcessStartInfo psi = new ProcessStartInfo(); // Creating a detached ProcessStartInfo will pre-populate the environment // with current environmental variables. StringDictionary environmentVariables = psi.EnvironmentVariables; Assert.NotEqual(0, environmentVariables.Count); int CountItems = environmentVariables.Count; environmentVariables.Add("NewKey", "NewValue"); environmentVariables.Add("NewKey2", "NewValue2"); Assert.Equal(CountItems + 2, environmentVariables.Count); environmentVariables.Remove("NewKey"); Assert.Equal(CountItems + 1, environmentVariables.Count); //Exception not thrown with invalid key Assert.Throws <ArgumentException>(() => { environmentVariables.Add("NewKey2", "NewValue2"); }); Assert.False(environmentVariables.ContainsKey("NewKey")); environmentVariables.Add("newkey2", "newvalue2"); Assert.True(environmentVariables.ContainsKey("newkey2")); Assert.Equal("newvalue2", environmentVariables["newkey2"]); Assert.Equal("NewValue2", environmentVariables["NewKey2"]); environmentVariables.Clear(); Assert.Equal(0, environmentVariables.Count); environmentVariables.Add("NewKey", "newvalue"); environmentVariables.Add("newkey2", "NewValue2"); Assert.False(environmentVariables.ContainsKey("newkey")); Assert.False(environmentVariables.ContainsValue("NewValue")); string result = null; int index = 0; foreach (string e1 in environmentVariables.Values) { index++; result += e1; } Assert.Equal(2, index); Assert.Equal("newvalueNewValue2", result); result = null; index = 0; foreach (string e1 in environmentVariables.Keys) { index++; result += e1; } Assert.Equal("NewKeynewkey2", result); Assert.Equal(2, index); result = null; index = 0; foreach (DictionaryEntry e1 in environmentVariables) { index++; result += e1.Key; } Assert.Equal("NewKeynewkey2", result); Assert.Equal(2, index); //Key not found Assert.Throws <KeyNotFoundException>(() => { string stringout = environmentVariables["NewKey99"]; }); //Exception not thrown with invalid key Assert.Throws <ArgumentNullException>(() => { string stringout = environmentVariables[null]; }); //Exception not thrown with invalid key Assert.Throws <ArgumentNullException>(() => environmentVariables.Add(null, "NewValue2")); Assert.Throws <ArgumentException>(() => environmentVariables.Add("newkey2", "NewValue2")); //Use DictionaryEntry Enumerator var x = environmentVariables.GetEnumerator() as IEnumerator; x.MoveNext(); var y1 = (DictionaryEntry)x.Current; Assert.Equal("NewKey newvalue", y1.Key + " " + y1.Value); x.MoveNext(); y1 = (DictionaryEntry)x.Current; Assert.Equal("newkey2 NewValue2", y1.Key + " " + y1.Value); environmentVariables.Add("newkey3", "newvalue3"); KeyValuePair <string, string>[] kvpa = new KeyValuePair <string, string> [10]; environmentVariables.CopyTo(kvpa, 0); Assert.Equal("NewKey", kvpa[0].Key); Assert.Equal("newkey3", kvpa[2].Key); Assert.Equal("newvalue3", kvpa[2].Value); string[] kvp = new string[10]; Assert.Throws <ArgumentException>(() => { environmentVariables.CopyTo(kvp, 6); }); environmentVariables.CopyTo(kvpa, 6); Assert.Equal("NewKey", kvpa[6].Key); Assert.Equal("newvalue", kvpa[6].Value); Assert.Throws <ArgumentOutOfRangeException>(() => { environmentVariables.CopyTo(kvpa, -1); }); Assert.Throws <ArgumentException>(() => { environmentVariables.CopyTo(kvpa, 9); }); Assert.Throws <ArgumentNullException>(() => { KeyValuePair <string, string>[] kvpanull = null; environmentVariables.CopyTo(kvpanull, 0); }); }
static void Main(string[] args) { StringDictionary Sd = new StringDictionary(); Sd.Add("1", "Lohitha"); Sd.Add("2", "ASmin"); Sd.Add("3", "Anitha"); Sd.Add("4", "Chamu"); Console.WriteLine("**************Displaying********************"); foreach (DictionaryEntry de in Sd) { Console.WriteLine(" {0} : {1}", de.Key, de.Value); } Console.WriteLine("**********ContainsValue***************"); Console.WriteLine(Sd.ContainsValue("Anitha")); Console.WriteLine("***********ContainKey**************"); Console.WriteLine(Sd.ContainsKey("1")); // DictionaryEntry[] myARr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() }; // DictionaryEntry[] STrArr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() }; Dictionary <string, string>[] STrArr = new Dictionary <string, string> [20]; //Sd.CopyTo(STrArr, 1); //for(int i=0;i<STrArr.Length;i++) //{ // Console.WriteLine("{0}:{1}",STrArr[i].Keys,STrArr[i].Values); // } DictionaryEntry[] myArr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() }; Sd.CopyTo(myArr, 0); Console.WriteLine("*************CopyTo***************"); // Displaying key and value pairs in Array myArr for (int i = 0; i < myArr.Length; i++) { Console.WriteLine(myArr[i].Key + " " + myArr[i].Value); } Console.WriteLine(Sd.Equals(Sd)); Console.WriteLine("*********GEtEnumerator************"); IEnumerator Ie = Sd.GetEnumerator(); DictionaryEntry ds; while (Ie.MoveNext()) { ds = (DictionaryEntry)Ie.Current; Console.WriteLine("{0}:{1}", ds.Key, ds.Value); } HybridDictionary Hd = new HybridDictionary(); Console.WriteLine("HyBRID DIcTIOnARY-----------------------------------------------------------------------------------------------------"); Hd.Add(1, "Amma"); Hd.Add(2, "Nanna"); Hd.Add(3, "Chelli"); foreach (DictionaryEntry de1 in Hd) { Console.WriteLine("{0}:{1}", de1.Key, de1.Value); } Console.WriteLine("ContainsKEyWord"); Console.WriteLine(Hd.Contains(1)); Console.WriteLine("Equals"); Console.WriteLine(Hd.Equals(Sd)); Console.WriteLine("GETs Hash Code"); Console.WriteLine(Hd.GetHashCode()); Console.WriteLine("REturns String the Represent dictionary TOSTRING"); Console.WriteLine(Hd.ToString()); ListDictionary Ld = new ListDictionary(); Ld.Add(1, "one"); Ld.Add(2, "Two"); Ld.Add(3, "Three"); foreach (DictionaryEntry de2 in Ld) { Console.WriteLine("{0}:{1}", de2.Key, de2.Value); } Ld.Remove(2); Console.WriteLine("AFter remove() Method"); foreach (DictionaryEntry de2 in Ld) { Console.WriteLine("{0}:{1}", de2.Key, de2.Value); } Console.WriteLine("Contains"); Console.WriteLine(Ld.Contains(1)); Ld.Clear(); Console.WriteLine("Clear"); foreach (DictionaryEntry de2 in Ld) { Console.WriteLine("{0}:{1}", de2.Key, de2.Value); } }
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"; StringDictionary sd; string ind; 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" }; int cnt = 0; try { intl = new IntlStrings(); Console.WriteLine("--- create dictionary ---"); strLoc = "Loc_001oo"; iCountTestcases++; sd = new StringDictionary(); Console.WriteLine("1. Check for empty dictionary"); for (int i = 0; i < values.Length; i++) { iCountTestcases++; if (sd.ContainsKey(keys[i])) { iCountErrors++; Console.WriteLine("Err_0001_{0}, returned true for empty dictionary", i); } } Console.WriteLine("2. add simple strings and verify ContainsKey()"); strLoc = "Loc_002oo"; iCountTestcases++; cnt = values.Length; for (int i = 0; i < cnt; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != cnt) { iCountErrors++; Console.WriteLine("Err_0002a, count is {0} instead of {1}", sd.Count, cnt); } for (int i = 0; i < cnt; i++) { iCountTestcases++; if (!sd.ContainsValue(values[i])) { iCountErrors++; Console.WriteLine("Err_0002_{0}b, collection doesn't contain value \"{1}\"", i, values[i]); } iCountTestcases++; if (!sd.ContainsKey(keys[i])) { iCountErrors++; Console.WriteLine("Err_0002_{0}c, collection doesn't contain key \"{1}\"", i, keys[i]); } } Console.WriteLine("3. add intl strings and verify ContainsKey()"); strLoc = "Loc_003oo"; int len = values.Length; 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; } } for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Add(intlValues[i + len], intlValues[i]); if (sd.Count != cnt + 1) { iCountErrors++; Console.WriteLine("Err_0003_{0}a, count is {1} instead of {2}", i, sd.Count, cnt + 1); } iCountTestcases++; if (!sd.ContainsValue(intlValues[i])) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, collection doesn't contain value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(intlValues[i + len])) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, collection doesn't contain key of new item", i); } ind = intlValues[i + len]; iCountTestcases++; if (String.Compare(sd[ind], intlValues[i], false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}d, returned item \"{1}\" instead of \"{2}\"", i, sd[ind], intlValues[i]); } } Console.WriteLine("4. add null string with non-null key and verify ContainsKey()"); strLoc = "Loc_004oo"; iCountTestcases++; cnt = sd.Count; string k = "keykey"; sd.Add(k, null); if (sd.Count != cnt + 1) { iCountErrors++; Console.WriteLine("Err_0004a, count is {1} instead of {2}", sd.Count, cnt + 1); } iCountTestcases++; if (!sd.ContainsKey(k)) { iCountErrors++; Console.WriteLine("Err_0004b, dictionary doesn't contain new key"); } Console.WriteLine("5. Case sensitivity"); strLoc = "Loc_005oo"; iCountTestcases++; sd.Clear(); if (sd.Count != 0) { iCountErrors++; Console.WriteLine("Err_0005, count is {1} instead of {2} after Clear()", sd.Count, 0); } 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(); } sd.Clear(); Console.WriteLine(" initial number of items: " + sd.Count); for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Add(intlValues[i + len], intlValues[i]); if (sd.Count != cnt + 1) { iCountErrors++; Console.WriteLine("Err_0005_{0}a, count is {1} instead of {2}", i, sd.Count, cnt + 1); } iCountTestcases++; if (!sd.ContainsValue(intlValues[i])) { iCountErrors++; Console.WriteLine("Err_0005_{0}b, collection doesn't contain value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(intlValues[i + len])) { iCountErrors++; Console.WriteLine("Err_0005_{0}c, collection doesn't contain key of new item", i); } iCountTestcases++; if (!caseInsensitive && sd.ContainsValue(intlValuesLower[i])) { iCountErrors++; Console.WriteLine("Err_0005_{0}d, collection contains lowercase value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(intlValuesLower[i + len])) { iCountErrors++; Console.WriteLine("Err_0005_{0}e, collection doesn't contain lowercase key of new item", i); } } Console.WriteLine("6. ContainsKey (null) "); Console.WriteLine(" initial number of items: " + sd.Count); strLoc = "Loc_006oo"; iCountTestcases++; try { sd.ContainsKey(null); iCountErrors++; Console.WriteLine("Err_0006a, no exception"); } catch (NullReferenceException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0006b, unexpected exception: " + 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 virtual 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"; StringDictionary sd; try { Console.WriteLine("--- default ctor ---"); strLoc = "Loc_001oo"; iCountTestcases++; sd = new StringDictionary(); Console.WriteLine("1. compare to null"); iCountTestcases++; if (sd == null) { iCountErrors++; Console.WriteLine("Err_0001, collection is null after default ctor"); } Console.WriteLine("2. check Count"); iCountTestcases++; if (sd.Count != 0) { iCountErrors++; Console.WriteLine("Err_0002, Count = {0} after default ctor", sd.Count); } Console.WriteLine("3. check ContainsValue()"); iCountTestcases++; if (sd.ContainsValue("string")) { iCountErrors++; Console.WriteLine("Err_0003, ContainsValue() returned true after default ctor"); } Console.WriteLine("4. check ContainsKey()"); iCountTestcases++; if (sd.ContainsKey("string")) { iCountErrors++; Console.WriteLine("Err_0004, ContainsKey() returned true after default ctor"); } Console.WriteLine("5. check ToString()"); iCountTestcases++; string temp = sd.ToString(); Console.WriteLine(" ToString(): " + temp); if (temp.IndexOf("StringDictionary") == -1) { iCountErrors++; Console.WriteLine("Err_0005, ToString() doesn't contain \"StringDictionary\""); } Console.WriteLine("6. check returned Type"); iCountTestcases++; temp = sd.GetType().ToString().Trim(); Console.WriteLine(" GetType(): " + temp); if (temp.IndexOf("StringDictionary") == -1) { iCountErrors++; Console.WriteLine("Err_0006: returned type doesn't contain \"StringDictionary\""); } Console.WriteLine("7. compare returned Type of two Dictionaries"); iCountTestcases++; string temp1 = (new StringDictionary()).GetType().ToString().Trim(); if (String.Compare(temp, temp1) != 0) { iCountErrors++; Console.WriteLine("Err_0007: returned types of two collections differ"); } Console.WriteLine("8. check IsSynchronized"); iCountTestcases++; Console.WriteLine(" IsSynchronized: " + sd.IsSynchronized); if (sd.IsSynchronized) { iCountErrors++; Console.WriteLine("Err_0008: IsSynchronized returned {0}", sd.IsSynchronized); } Console.WriteLine("9. add item and verify"); iCountTestcases++; sd.Add("key", "value"); iCountTestcases++; if (sd.Count != 1) { iCountErrors++; Console.WriteLine("Err_0009a: Count returned {0}", sd.Count); } iCountTestcases++; if ( !sd.ContainsKey("key") ) { iCountErrors++; Console.WriteLine("Err_0009b: ContainsKey() returned false"); } iCountTestcases++; if ( !sd.ContainsValue("value") ) { iCountErrors++; Console.WriteLine("Err_0009c: ContainsValue() returned false"); } } 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 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"; StringDictionary sd; 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" }; int cnt = 0; try { intl = new IntlStrings(); Console.WriteLine("--- create dictionary ---"); strLoc = "Loc_001oo"; iCountTestcases++; sd = new StringDictionary(); Console.WriteLine("1. Remove() from empty dictionary"); iCountTestcases++; if (sd.Count > 0) sd.Clear(); for (int i = 0; i < keys.Length; i++) { sd.Remove(keys[0]); } Console.WriteLine("2. Remove() on filled dictionary"); strLoc = "Loc_002oo"; int len = values.Length; iCountTestcases++; sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { iCountErrors++; Console.WriteLine("Err_0002a, count is {0} instead of {1}", sd.Count, len); } for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Remove(keys[i]); if (sd.Count != cnt - 1) { iCountErrors++; Console.WriteLine("Err_0002b_{0}, didn't remove element with {0} key", i); } iCountTestcases++; if ( sd.ContainsValue(values[i]) ) { iCountErrors++; Console.WriteLine("Err_0002c_{0}, removed wrong value", i); } iCountTestcases++; if ( sd.ContainsKey(keys[i]) ) { iCountErrors++; Console.WriteLine("Err_0002d_{0}, removed wrong value", i); } } Console.WriteLine("3. Remove() on dictionary with duplicate values "); strLoc = "Loc_003oo"; iCountTestcases++; sd.Clear(); string intlStr = intl.GetString(MAX_LEN, true, true, true); sd.Add("keykey1", intlStr); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } sd.Add("keykey2", intlStr); if (sd.Count != len+2) { iCountErrors++; Console.WriteLine("Err_0003a, count is {0} instead of {1}", sd.Count, len+2); } iCountTestcases++; sd.Remove("keykey2"); if (!sd.ContainsValue(intlStr)) { iCountErrors++; Console.WriteLine("Err_0003b, removed both duplicates"); } if ( sd.ContainsKey("keykey2") ) { iCountErrors++; Console.WriteLine("Err_0003c, removed not given instance"); } if (! sd.ContainsKey("keykey1") ) { iCountErrors++; Console.WriteLine("Err_0003d, removed wrong instance"); } Console.WriteLine("4. Remove() from dictionary with intl strings"); strLoc = "Loc_004oo"; 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; } sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(intlValues[i+len], intlValues[i]); } if (sd.Count != len) { iCountErrors++; Console.WriteLine("Err_0004a, count is {0} instead of {1}", sd.Count, len); } for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Remove(intlValues[i+len]); if (sd.Count != cnt - 1) { iCountErrors++; Console.WriteLine("Err_0004b_{0}, didn't remove element with {0} key", i+len); } iCountTestcases++; if ( sd.ContainsValue(intlValues[i]) ) { iCountErrors++; Console.WriteLine("Err_0004c_{0}, removed wrong value", i); } iCountTestcases++; if ( sd.ContainsKey(intlValues[i+len]) ) { iCountErrors++; Console.WriteLine("Err_0004d_{0}, removed wrong key", i); } } Console.WriteLine("5. Case sensitivity"); strLoc = "Loc_005oo"; iCountTestcases++; sd.Clear(); string [] intlValuesUpper = new string [len]; for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToLower(); } for (int i = 0; i < len; i++) { intlValuesUpper[i] = intlValues[i+len].ToUpper(); } sd.Clear(); Console.WriteLine(" ... add Lowercased ..."); for (int i = 0; i < len; i++) { sd.Add(intlValues[i+len], intlValues[i]); } if (sd.Count != len) { iCountErrors++; Console.WriteLine("Err_0005a, count is {0} instead of {1}", sd.Count, len); } Console.WriteLine(" ... remove Uppercased ..."); for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Remove(intlValuesUpper[i]); if (sd.Count != cnt - 1) { iCountErrors++; Console.WriteLine("Err_0005b_{0}, didn't remove element with {0} lower key", i+len); } iCountTestcases++; if ( sd.ContainsValue(intlValues[i]) ) { iCountErrors++; Console.WriteLine("Err_0005c_{0}, removed wrong value", i); } iCountTestcases++; if ( sd.ContainsKey(intlValuesUpper[i]) ) { iCountErrors++; Console.WriteLine("Err_0005d_{0}, removed wrong key", i); } } Console.WriteLine("6. Remove(null)"); strLoc = "Loc_006oo"; iCountTestcases++; try { sd.Remove(null); iCountErrors++; Console.WriteLine("Err_0006a, no exception"); } catch (NullReferenceException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0006b, 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 virtual 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"; StringDictionary sd; IEnumerator en; DictionaryEntry curr; 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" }; try { Console.WriteLine("--- create dictionary ---"); strLoc = "Loc_001oo"; iCountTestcases++; sd = new StringDictionary(); Console.WriteLine("1. Enumerator for empty dictionary"); Console.WriteLine(" - get type"); iCountTestcases++; en = sd.GetEnumerator(); string type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { iCountErrors++; Console.WriteLine("Err_0001a, type is not Enumerator"); } Console.WriteLine(" - MoveNext"); iCountTestcases++; bool res = en.MoveNext(); if (res) { iCountErrors++; Console.WriteLine("Err_0001b, MoveNext returned true"); } Console.WriteLine(" - Current"); iCountTestcases++; try { curr = (DictionaryEntry)en.Current; iCountErrors++; Console.WriteLine("Err_0001c, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0001d, unexpected exception: {0}", e.ToString()); } Console.WriteLine("2. GetEnumerator for filled collection"); strLoc = "Loc_002oo"; iCountTestcases++; for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } Console.WriteLine(" - get type"); iCountTestcases++; en = sd.GetEnumerator(); type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { iCountErrors++; Console.WriteLine("Err_0002a, type is not Enumerator"); } Console.WriteLine(" - MoveNext and Current within collection"); for (int i = 0; i < sd.Count; i++) { iCountTestcases++; res = en.MoveNext(); if (!res) { iCountErrors++; Console.WriteLine("Err_0002b_{0}, MoveNext returned false", i); } iCountTestcases++; curr = (DictionaryEntry)en.Current; if (!sd.ContainsValue(curr.Value.ToString())) { iCountErrors++; Console.WriteLine("Err_0002c_{0}, Current dictionary doesn't contain value from enumerator", i); } iCountTestcases++; if (!sd.ContainsKey(curr.Key.ToString())) { iCountErrors++; Console.WriteLine("Err_0002d_{0}, Current dictionary doesn't contain key from enumerator", i); } iCountTestcases++; if (String.Compare(sd[curr.Key.ToString()], curr.Value.ToString(), false) != 0) { iCountErrors++; Console.WriteLine("Err_0002e_{0}, Value for current Key is different in dictionary", i); } iCountTestcases++; DictionaryEntry curr1 = (DictionaryEntry)en.Current; if (!curr.Equals(curr1)) { iCountErrors++; Console.WriteLine("Err_0002f_{0}, second call of Current returned different result", i); } } res = en.MoveNext(); Console.WriteLine(" - MoveNext outside of the collection"); iCountTestcases++; res = en.MoveNext(); if (res) { iCountErrors++; Console.WriteLine("Err_0002g, MoveNext returned true"); } Console.WriteLine(" - Current outside of the collection"); iCountTestcases++; try { curr = (DictionaryEntry)en.Current; iCountErrors++; Console.WriteLine("Err_0002h, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0002i, unexpected exception: {0}", e.ToString()); } Console.WriteLine(" - Reset"); iCountTestcases++; en.Reset(); Console.WriteLine(" - get Current after Reset"); iCountTestcases++; try { curr = (DictionaryEntry)en.Current; iCountErrors++; Console.WriteLine("Err_0002j, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0002k, unexpected exception: {0}", e.ToString()); } Console.WriteLine("3. Enumerator and modified dictionary"); strLoc = "Loc_003oo"; iCountTestcases++; if (sd.Count < 1) { for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } } iCountTestcases++; en = sd.GetEnumerator(); Console.WriteLine(" - MoveNext"); res = en.MoveNext(); if (!res) { iCountErrors++; Console.WriteLine("Err_0003a, MoveNext returned false"); } Console.WriteLine(" - modify collection"); curr = (DictionaryEntry)en.Current; int cnt = sd.Count; iCountTestcases++; sd.Remove(keys[0]); if (sd.Count != cnt - 1) { iCountErrors++; Console.WriteLine("Err_0003b, didn't remove item with 0th key"); } Console.WriteLine(" - get Current"); iCountTestcases++; DictionaryEntry curr2 = (DictionaryEntry)en.Current; if (!curr.Equals(curr2)) { iCountErrors++; Console.WriteLine("Err_0003c, current returned different value after midification"); } Console.WriteLine(" - call MoveNext"); iCountTestcases++; try { res = en.MoveNext(); iCountErrors++; Console.WriteLine("Err_0003d, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0003e, unexpected exception: {0}", e.ToString()); } Console.WriteLine("4. Modify dictionary after enumerated beyond the end"); strLoc = "Loc_004oo"; iCountTestcases++; sd.Clear(); for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } iCountTestcases++; en = sd.GetEnumerator(); for (int i = 0; i < sd.Count; i++) { en.MoveNext(); } Console.WriteLine(" - get Current at the end of the dictionary"); curr = (DictionaryEntry)en.Current; Console.WriteLine(" - modify collection"); curr = (DictionaryEntry)en.Current; cnt = sd.Count; iCountTestcases++; sd.Remove(keys[0]); if (sd.Count != cnt - 1) { iCountErrors++; Console.WriteLine("Err_0004b, didn't remove item with 0th key"); } Console.WriteLine(" - get Current after modifying"); iCountTestcases++; curr2 = (DictionaryEntry)en.Current; if (!curr.Equals(curr2)) { iCountErrors++; Console.WriteLine("Err_0004c, current returned different value after midification"); } Console.WriteLine(" - call MoveNext after modifying"); iCountTestcases++; try { res = en.MoveNext(); iCountErrors++; Console.WriteLine("Err_0004d, no exception"); } catch (InvalidOperationException ex) { Console.WriteLine(" expected exception: " + ex.Message); } catch (Exception e) { iCountErrors++; Console.WriteLine("Err_0004e, 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 string GetUniqueName(StringDictionary list, string name) { // duplicate check int counter = 2; while(list.ContainsValue(name)) { name = _endNumberRegex.Replace(name, ""); name += counter.ToString(); counter++; } return name; }
private void DocumentType(XmlNode typeNode, ElementDocType docType, NAntXsltUtilities utilities) { if (typeNode == null) { throw new ArgumentNullException("typeNode"); } if (docType == ElementDocType.None || docType == ElementDocType.FunctionSet) { // we don't need to document this type return; } string classID = typeNode.Attributes["id"].Value; if (!classID.Substring(2).StartsWith(NamespaceFilter)) { // we don't need to types in this namespace return; } string filename = utilities.GetFileNameForType(typeNode); if (filename == null) { // we should never get here, but just in case ... return; } if (_writtenFiles.ContainsValue(classID)) { return; } else { _writtenFiles.Add(filename, classID); } // create arguments for nant task page transform (valid args are class-id, refType, imagePath, relPathAdjust) XsltArgumentList arguments = CreateXsltArgumentList(); arguments.AddParam("class-id", String.Empty, classID); string refTypeString; switch (docType) { case ElementDocType.DataTypeElement: refTypeString = "Type"; break; case ElementDocType.Element: refTypeString = "Element"; break; case ElementDocType.Task: refTypeString = "Task"; break; case ElementDocType.Enum: refTypeString = "Enum"; break; case ElementDocType.Filter: refTypeString = "Filter"; break; default: refTypeString = "Other?"; break; } arguments.AddParam("refType", string.Empty, refTypeString); // add extension object to Xslt arguments arguments.AddExtensionObject("urn:NAntUtil", utilities); // Process all sub-elements and generate docs for them. :) // Just look for properties with attributes to narrow down the foreach loop. // (This is a restriction of NAnt.Core.Attributes.BuildElementAttribute) foreach (XmlNode propertyNode in typeNode.SelectNodes("property[attribute]")) { //get the xml element string elementName = utilities.GetElementNameForProperty(propertyNode); if (elementName != null) { // try to get attribute info if it is an array/collection. // strip the array brakets "[]" to get the type string elementType = "T:" + propertyNode.Attributes["type"].Value.Replace("[]", ""); // check whether property is an element array XmlNode nestedElementNode = propertyNode.SelectSingleNode("attribute[@name='" + typeof(BuildElementArrayAttribute).FullName + "']"); if (nestedElementNode == null) { // check whether property is an element collection nestedElementNode = propertyNode.SelectSingleNode("attribute[@name='" + typeof(BuildElementCollectionAttribute).FullName + "']"); } // if property is either array or collection type element if (nestedElementNode != null) { // select the item type in the collection XmlAttribute elementTypeAttribute = _xmlDocumentation.SelectSingleNode("//class[@id='" + elementType + "']/method[@name='Add']/parameter/@type") as XmlAttribute; if (elementTypeAttribute != null) { // get type of collection elements elementType = "T:" + elementTypeAttribute.Value; } // if it contains a ElementType attribute then it is an array or collection // if it is a collection, then we care about the child type. XmlNode explicitElementType = propertyNode.SelectSingleNode("attribute/property[@ElementType]"); if (explicitElementType != null) { // ndoc is inconsistent about how classes are named. elementType = explicitElementType.Attributes["value"].Value.Replace("+", "."); } } XmlNode elementTypeNode = utilities.GetTypeNodeByID(elementType); if (elementTypeNode != null) { ElementDocType elementDocType = utilities.GetElementDocType(elementTypeNode); if (elementDocType != ElementDocType.None) { DocumentType(elementTypeNode, elementDocType, utilities); } } } } // create the page TransformAndWriteResult(_xsltTypeDoc, arguments, filename); }
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"; StringDictionary sd; string ind; 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" }; int cnt = 0; try { intl = new IntlStrings(); Console.WriteLine("--- create collection ---"); strLoc = "Loc_001oo"; iCountTestcases++; sd = new StringDictionary(); Console.WriteLine("1. Check for empty dictionary"); for (int i = 0; i < values.Length; i++) { iCountTestcases++; if (sd.ContainsValue(values[i])) { iCountErrors++; Console.WriteLine("Err_0001_{0}, returned true for empty dictionary", i); } } Console.WriteLine("2. add simple strings and verify ContainsValue()"); strLoc = "Loc_002oo"; iCountTestcases++; cnt = values.Length; for (int i = 0; i < cnt; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != cnt) { iCountErrors++; Console.WriteLine("Err_0002a, count is {0} instead of {1}", sd.Count, cnt); } for (int i = 0; i < cnt; i++) { iCountTestcases++; if (!sd.ContainsValue(values[i])) { iCountErrors++; Console.WriteLine("Err_0002_{0}b, collection doesn't contain value \"{1}\"", i, values[i]); } iCountTestcases++; if (!sd.ContainsKey(keys[i])) { iCountErrors++; Console.WriteLine("Err_0002_{0}c, collection doesn't contain key \"{1}\"", i, keys[i]); } } Console.WriteLine("3. add intl strings and verify ContainsValue()"); strLoc = "Loc_003oo"; int len = values.Length; 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; } for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Add(intlValues[i+len], intlValues[i]); if (sd.Count != cnt+1) { iCountErrors++; Console.WriteLine("Err_0003_{0}a, count is {1} instead of {2}", i, sd.Count, cnt+1); } iCountTestcases++; if (!sd.ContainsValue(intlValues[i])) { iCountErrors++; Console.WriteLine("Err_0003_{0}b, collection doesn't contain value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(intlValues[i+len])) { iCountErrors++; Console.WriteLine("Err_0003_{0}c, collection doesn't contain key of new item", i); } ind = intlValues[i+len]; iCountTestcases++; if (String.Compare(sd[ind], intlValues[i], false) != 0) { iCountErrors++; Console.WriteLine("Err_0003_{0}d, returned item \"{1}\" instead of \"{2}\"", i, sd[ind], intlValues[i]); } } Console.WriteLine("4. add null string with non-null key and verify ContainsValue()"); strLoc = "Loc_004oo"; iCountTestcases++; cnt = sd.Count; string k = "keykey"; sd.Add(k, null); if (sd.Count != cnt+1) { iCountErrors++; Console.WriteLine("Err_0004a, count is {1} instead of {2}", sd.Count, cnt+1); } iCountTestcases++; if (!sd.ContainsValue(null)) { iCountErrors++; Console.WriteLine("Err_0004b, dictionary doesn't contain value null"); } Console.WriteLine("5. Case sensitivity"); strLoc = "Loc_005oo"; iCountTestcases++; sd.Clear(); if (sd.Count != 0) { iCountErrors++; Console.WriteLine("Err_0005, count is {1} instead of {2} after Clear()", sd.Count, 0); } 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(); } sd.Clear(); for (int i = 0; i < len; i++) { iCountTestcases++; cnt = sd.Count; sd.Add(intlValues[i+len], intlValues[i]); if (sd.Count != cnt+1) { iCountErrors++; Console.WriteLine("Err_0005_{0}a, count is {1} instead of {2}", i, sd.Count, cnt+1); } iCountTestcases++; if (!sd.ContainsValue(intlValues[i])) { iCountErrors++; Console.WriteLine("Err_0005_{0}b, collection doesn't contain value of new item", i); } iCountTestcases++; if (!sd.ContainsKey(intlValues[i+len])) { iCountErrors++; Console.WriteLine("Err_0005_{0}c, collection doesn't contain key of new item", i); } iCountTestcases++; if (!caseInsensitive && sd.ContainsValue(intlValuesLower[i])) { iCountErrors++; Console.WriteLine("Err_0005_{0}d, collection contains lowercase value of new item", i); } iCountTestcases++; if ( !sd.ContainsKey(intlValuesLower[i+len])) { iCountErrors++; Console.WriteLine("Err_0005_{0}e, collection doesn't contain lowercase key of new item", i); } } } 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 void ContainsValue_NoSuchValue_ReturnsFalse(int count) { StringDictionary stringDictionary = Helpers.CreateStringDictionary(count); Assert.False(stringDictionary.ContainsValue("value")); }