public void Test01() { IntlStrings intl; StringCollection sc; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] StringCollection is constructed as expected //----------------------------------------------------------------- sc = new StringCollection(); // [] for empty collection // for (int i = 0; i < values.Length; i++) { if (sc.IndexOf(values[i]) != -1) { Assert.False(true, string.Format("Error, returned {1} for empty collection", i, sc.IndexOf(values[i]))); } } // // [] add simple strings and verify IndexOf() cnt = sc.Count; sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } for (int i = 0; i < values.Length; i++) { // verify that collection contains all added items // if (sc.IndexOf(values[i]) != i) { Assert.False(true, string.Format("Error, IndexOf returned {1} instead of {0}", i, sc.IndexOf(values[i]))); } } // // Intl strings // [] add Intl strings and verify IndexOf() // string[] intlValues = new string[values.Length]; // fill array with unique strings // for (int i = 0; i < values.Length; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } int len = values.Length; Boolean caseInsensitive = false; for (int i = 0; i < len; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) caseInsensitive = true; } cnt = sc.Count; sc.AddRange(intlValues); if (sc.Count != (cnt + intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, cnt + intlValues.Length)); } for (int i = 0; i < intlValues.Length; i++) { // verify that collection contains all added items // if (sc.IndexOf(intlValues[i]) != values.Length + i) { Assert.False(true, string.Format("Error, IndexOf returned {1} instead of {2}", i, sc.IndexOf(intlValues[i]), values.Length + i)); } } // // [] duplicate strings // sc.Clear(); string intlStr = intlValues[0]; sc.Add(intlStr); // index 0 sc.AddRange(values); sc.AddRange(intlValues); // second index values.Length + 1 cnt = values.Length + 1 + intlValues.Length; if (sc.Count != cnt) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", sc.Count, cnt)); } // verify Index of newly added item // if (sc.IndexOf(intlStr) != 0) { Assert.False(true, string.Format("Error, IndexOf returned {0} instead of {1}", sc.IndexOf(intlStr), 0)); } sc.Clear(); sc.AddRange(values); sc.AddRange(intlValues); // second index values.Length + 1 sc.Add(intlStr); // index values.Length + intlValues.Length cnt = values.Length + 1 + intlValues.Length; if (sc.Count != cnt) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", sc.Count, cnt)); } // verify Index of newly added item // if (sc.IndexOf(intlStr) != values.Length) { Assert.False(true, string.Format("Error, IndexOf returned {0} instead of {1}", sc.IndexOf(intlStr), values.Length)); } // // [] Case sensitivity: search should be case-sensitive // sc.Clear(); sc.Add(intlValues[0].ToUpper()); sc.AddRange(values); sc.Add(intlValues[0].ToLower()); cnt = values.Length + 2; if (sc.Count != cnt) { Assert.False(true, string.Format("Error, count is {1} instead of {2} ", sc.Count, cnt)); } // look for lowercase item - should be (values.Length + 1) index // intlStr = intlValues[0].ToLower(); if (!caseInsensitive && (sc.IndexOf(intlStr) != values.Length + 1)) { Assert.False(true, string.Format("Error, IndexOf() returned {0} instead of {1} ", sc.IndexOf(intlStr), values.Length + 1)); } // string half_upper+half_lower should not be found // if (intlStr.Length > 2) { int mid = intlStr.Length / 2; String strTemp = intlStr.Substring(0, mid).ToLower() + intlStr.Substring(mid, intlStr.Length - mid).ToUpper(); if (strTemp != intlStr.ToUpper() && strTemp != intlStr.ToLower() && !caseInsensitive) { intlStr = strTemp; if (sc.IndexOf(intlStr) != -1) { Assert.False(true, string.Format("Error, IndexOf() returned {0} instead of -1 ", sc.IndexOf(intlStr))); } } } }
public void Test01() { IntlStrings intl; StringCollection sc; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] StringCollection is constructed as expected //----------------------------------------------------------------- sc = new StringCollection(); // [] on empty collection // for (int i = 0; i < values.Length; i++) { if (sc.Contains(values[i])) { Assert.False(true, string.Format("Error, returned true for empty collection", i)); } } // [] add simple strings and verify Contains() // cnt = sc.Count; sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } for (int i = 0; i < values.Length; i++) { // verify that collection contains all added items // if (!sc.Contains(values[i])) { Assert.False(true, string.Format("Error, collection doesn't contain item \"{1}\"", i, values[i])); } } // // Intl strings // [] add Intl strings and verify Contains() // string[] intlValues = new string[values.Length]; // fill array with unique strings // for (int i = 0; i < values.Length; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } int len = values.Length; Boolean caseInsensitive = false; for (int i = 0; i < len; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) caseInsensitive = true; } cnt = sc.Count; sc.AddRange(intlValues); if (sc.Count != (cnt + intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, cnt + intlValues.Length)); } for (int i = 0; i < intlValues.Length; i++) { // verify that collection contains all added items // if (!sc.Contains(intlValues[i])) { Assert.False(true, string.Format("Error, collection doesn't contain item \"{1}\"", i, intlValues[i])); } } // // add very long string // [] add very long string and verify Contains() // cnt = sc.Count; string intlStr = intlValues[0]; while (intlStr.Length < 10000) intlStr += intlStr; sc.Add(intlStr); if (sc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", sc.Count, cnt + 1)); } // verify that collection contains newly added item // if (!sc.Contains(intlStr)) { Assert.False(true, string.Format("Error, collection doesn't contain new item")); } // // [] Case sensitivity: search should be case-sensitive // sc.Clear(); if (sc.Count != 0) { Assert.False(true, string.Format("Error, count is {1} instead of {2} after Clear()", sc.Count, 0)); } // add uppercase item // intlStr = intlValues[0].ToUpper(); sc.Add(intlStr); if (sc.Count != 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", sc.Count, 1)); } // verify that Contains returns true for newly added uppercase item // if (!sc.Contains(intlStr)) { Assert.False(true, string.Format("Error, Contains() returned false ")); } // verify that Contains returns false for lowercase version // intlStr = intlValues[0].ToLower(); if (!caseInsensitive && sc.Contains(intlStr)) { Assert.False(true, string.Format("Error, Contains() returned true for lowercase version")); } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); // [] add simple strings // for (int i = 0; i < values.Length; i++) { cnt = nvc.Count; nvc.Add(keys[i], values[i]); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, nvc.Count, cnt + 1)); } // verify that collection contains newly added item // if (Array.IndexOf(nvc.AllKeys, keys[i]) < 0) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } // access the item // if (String.Compare(nvc[keys[i]], values[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, nvc[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 = nvc.Count; nvc.Add(intlValues[i + len], intlValues[i]); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, nvc.Count, cnt + 1)); } // verify that collection contains newly added item // if (Array.IndexOf(nvc.AllKeys, intlValues[i + len]) < 0) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } // access the item // if (String.Compare(nvc[intlValues[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, nvc[intlValues[i + len]], intlValues[i])); } } // // [] Case sensitivity // Casing doesn't change ( keya are not converted to lower!) // string[] intlValuesLower = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToUpperInvariant(); } for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValues[i].ToLowerInvariant(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { cnt = nvc.Count; nvc.Add(intlValues[i + len], intlValues[i]); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, nvc.Count, cnt + 1)); } // verify that collection contains newly added uppercase item // if (Array.IndexOf(nvc.AllKeys, intlValues[i + len]) < 0) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } // access the item // if (String.Compare(nvc[intlValues[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, nvc[intlValues[i + len]], intlValues[i])); } // verify that collection doesn't contains lowercase item // if (!caseInsensitive && String.Compare(nvc[intlValuesLower[i + len]], intlValuesLower[i]) == 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" is lowercase after adding uppercase", i, nvc[intlValuesLower[i + len]])); } // key is not converted to lower if (!caseInsensitive && Array.IndexOf(nvc.AllKeys, intlValuesLower[i + len]) >= 0) { Assert.False(true, string.Format("Error, key was converted to lower", i)); } // but search among keys is case-insensitive if (String.Compare(nvc[intlValuesLower[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, could not find item using differently cased key", i)); } } // // [] Add multiple values with the same key // nvc.Clear(); len = values.Length; string k = "keykey"; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); } if (nvc.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of 1}", nvc.Count)); } if (nvc.AllKeys.Length != 1) { Assert.False(true, "Error, should contain only 1 key"); } // verify that collection contains newly added item // if (Array.IndexOf(nvc.AllKeys, k) < 0) { Assert.False(true, "Error, collection doesn't contain key of new item"); } // access the item // string[] vals = nvc.GetValues(k); if (vals.Length != len) { Assert.False(true, string.Format("Error, number of values at given key is {0} instead of {1}", vals.Length, len)); } for (int i = 0; i < len; i++) { if (Array.IndexOf(vals, "Value" + i) < 0) { Assert.False(true, string.Format("Error, doesn't contain {1}", i, "Value" + i)); } } // // [] Add null value // cnt = nvc.Count; k = "kk"; nvc.Add(k, null); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, cnt + 1)); } if (Array.IndexOf(nvc.AllKeys, k) < 0) { Assert.False(true, "Error, collection doesn't contain key of new item"); } // verify that collection contains null // if (nvc[k] != null) { Assert.False(true, "Error, returned non-null on place of null"); } // // [] Add item with null key // Add item with null key - NullReferenceException expected // cnt = nvc.Count; nvc.Add(null, "item"); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, cnt + 1)); } if (Array.IndexOf(nvc.AllKeys, null) < 0) { Assert.False(true, "Error, collection doesn't contain null key "); } // verify that collection contains null // if (nvc[null] != "item") { Assert.False(true, "Error, returned wrong value at null key"); } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; // 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[] ks; // Keys array // initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); // [] Remove() on empty collection // cnt = nvc.Count; nvc.Remove(null); if (nvc.Count != cnt) { Assert.False(true, "Error, changed collection after Remove(null)"); } cnt = nvc.Count; nvc.Remove("some_string"); if (nvc.Count != cnt) { Assert.False(true, "Error, changed collection after Remove(some_string)"); } // [] Remove() on collection filled with simple strings // cnt = nvc.Count; int len = values.Length; for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } // for (int i = 0; i < len; i++) { cnt = nvc.Count; nvc.Remove(keys[i]); if (nvc.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } ks = nvc.AllKeys; if (Array.IndexOf(ks, keys[i]) > -1) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // Intl strings // [] Remove() on collection filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } cnt = nvc.Count; for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); } if (nvc.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = nvc.Count; nvc.Remove(intlValues[i + len]); if (nvc.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } ks = nvc.AllKeys; if (Array.IndexOf(ks, intlValues[i + len]) > -1) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Case sensitivity // string[] intlValuesLower = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToUpperInvariant(); } for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValues[i].ToLowerInvariant(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key cnt = nvc.Count; nvc.Remove(intlValues[i + len]); if (nvc.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } ks = nvc.AllKeys; if (Array.IndexOf(ks, intlValues[i + len]) > -1) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // lowercase key cnt = nvc.Count; nvc.Remove(intlValuesLower[i + len]); if (nvc.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item using lowercase key", i)); } ks = nvc.AllKeys; if (Array.IndexOf(ks, intlValues[i + len]) > -1) { Assert.False(true, string.Format("Error, removed wrong item using lowercase key", i)); } } // [] Remove() on filled collection - with multiple items with the same key // nvc.Clear(); len = values.Length; string k = "keykey"; string k1 = "hm1"; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); nvc.Add(k1, "iTem" + i); } if (nvc.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, 2)); } nvc.Remove(k); if (nvc.Count != 1) { Assert.False(true, "Error, failed to remove item"); } ks = nvc.AllKeys; if (Array.IndexOf(ks, k) > -1) { Assert.False(true, "Error, removed wrong item"); } nvc.Remove(k1); if (nvc.Count != 0) { Assert.False(true, "Error, failed to remove item"); } ks = nvc.AllKeys; if (Array.IndexOf(ks, k1) > -1) { Assert.False(true, "Error, removed wrong item"); } // // [] Remove(null) - when there is an item with null-key // cnt = nvc.Count; nvc.Add(null, "nullValue"); if (nvc.Count != cnt + 1) { Assert.False(true, "Error, failed to add item with null-key"); } if (nvc[null] == null) { Assert.False(true, "Error, didn't add item with null-key"); } cnt = nvc.Count; nvc.Remove(null); if (nvc.Count != cnt - 1) { Assert.False(true, "Error, failed to remove item"); } if (nvc[null] != null) { Assert.False(true, "Error, didn't remove item with null-key"); } // // [] Remove(null) - when no item with null key // nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } cnt = nvc.Count; nvc.Remove(null); if (nvc.Count != cnt) { Assert.False(true, "Error, removed something "); } }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); // [] Contains on empty dictionary Assert.Throws<ArgumentNullException>(() => { hd.Contains(null); }); if (hd.Contains("some_string")) { Assert.False(true, string.Format("Error, empty dictionary contains some_object")); } if (hd.Contains(new Hashtable())) { Assert.False(true, string.Format("Error, empty dictionary contains some_object")); } // [] simple strings and Contains() // cnt = hd.Count; int len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } // for (int i = 0; i < len; i++) { if (!hd.Contains(keysShort[i])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysShort[i])); } } cnt = hd.Count; len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len + cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len + cnt)); } // verify new keys for (int i = 0; i < len; i++) { if (!hd.Contains(keysLong[i])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysLong[i])); } } // verify old keys for (int i = 0; i < valuesShort.Length; i++) { if (!hd.Contains(keysShort[i])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysShort[i])); } } // // [] Intl strings and Contains() // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { // if (!hd.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, intlValues[i + len])); } } // // [] Case sensitivity // by default HybridDictionary is case-sensitive // hd.Clear(); len = valuesLong.Length; // // will use first half of array as valuesShort and second half as keysShort // for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key if (!hd.Contains(keysLong[i])) { Assert.False(true, string.Format("Error, doesn't contain added uppercase \"{1}\"", i, keysLong[i])); } // lowercase key if (hd.Contains(keysLong[i].ToUpper())) { Assert.False(true, string.Format("Error, contains uppercase \"{1}\" - should not", i, keysLong[i].ToUpper())); } } // [] different_in_casing_only keys and Contains() // hd.Clear(); string[] ks = { "Key", "kEy", "keY" }; len = ks.Length; for (int i = 0; i < len; i++) { hd.Add(ks[i], "Value" + i); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } if (hd.Contains("Value0")) { Assert.False(true, string.Format("Error, returned true when should not")); } for (int i = 0; i < len; i++) { if (!hd.Contains(ks[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } cnt = hd.Count; len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len + cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len + cnt)); } // verify new keys for (int i = 0; i < len; i++) { if (!hd.Contains(keysLong[i])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, keysLong[i])); } } // verify old keys for (int i = 0; i < ks.Length; i++) { if (!hd.Contains(ks[i])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, ks[i])); } } // // [] Contains(null) - for filled dictionary // len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } Assert.Throws<ArgumentNullException>(() => { hd.Contains(null); }); // [] Contains() for case-insensitive comparer dictionary // hd = new HybridDictionary(true); hd.Clear(); len = ks.Length; hd.Add(ks[0], "Value0"); for (int i = 1; i < len; i++) { Assert.Throws<ArgumentException>(() => { hd.Add(ks[i], "Value" + i); }); } if (hd.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, 1)); } if (hd.Contains("Value0")) { Assert.False(true, string.Format("Error, returned true when should not")); } for (int i = 0; i < len; i++) { if (!hd.Contains(ks[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } if (!hd.Contains("KEY")) { Assert.False(true, string.Format("Error, returned false non-existing-cased key")); } // [] few not_overriding_Equals objects and Contains() // hd = new HybridDictionary(); hd.Clear(); Hashtable[] lbl = new Hashtable[2]; lbl[0] = new Hashtable(); lbl[1] = new Hashtable(); ArrayList[] b = new ArrayList[2]; b[0] = new ArrayList(); b[1] = new ArrayList(); hd.Add(lbl[0], b[0]); hd.Add(lbl[1], b[1]); Assert.Throws<ArgumentException>(() => { hd.Add(lbl[0], "Hello"); }); if (hd.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, 2)); } if (!hd.Contains(lbl[0])) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!hd.Contains(lbl[1])) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (hd.Contains(new Hashtable())) { Assert.False(true, string.Format("Error, returned true when false expected")); } // [] many not_overriding_Equals objects and Contains() // hd = new HybridDictionary(); hd.Clear(); int num = 40; lbl = new Hashtable[num]; b = new ArrayList[num]; for (int i = 0; i < num; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } Assert.Throws<ArgumentException>(() => { hd.Add(lbl[0], "Hello"); }); if (hd.Count != num) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, num)); } for (int i = 0; i < num; i++) { if (!hd.Contains(lbl[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } if (hd.Contains(new Hashtable())) { Assert.False(true, string.Format("Error, returned true when false expected")); } // [] few not_overriding_Equals structs and Contains() hd = new HybridDictionary(); SpecialStruct s = new SpecialStruct(); s.Num = 1; s.Wrd = "one"; SpecialStruct s1 = new SpecialStruct(); s1.Num = 1; s1.Wrd = "one"; hd.Add(s, "first"); hd.Add(s1, "second"); if (hd.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, 2)); } if (!hd.Contains(s)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!hd.Contains(s1)) { Assert.False(true, string.Format("Error, returned false when true expected")); } // [] many not_overriding_Equals structs and Contains() hd = new HybridDictionary(); SpecialStruct[] ss = new SpecialStruct[num]; for (int i = 0; i < num; i++) { ss[i] = new SpecialStruct(); ss[i].Num = i; ss[i].Wrd = "value" + i; hd.Add(ss[i], "item" + i); } if (hd.Count != num) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, num)); } for (int i = 0; i < num; i++) { if (!hd.Contains(ss[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } s = new SpecialStruct(); s.Num = 1; s.Wrd = "value1"; if (hd.Contains(s)) { Assert.False(true, string.Format("Error, returned true when false expected")); } }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); // [] Remove() on empty dictionary // cnt = hd.Count; try { hd.Remove(null); Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } cnt = hd.Count; hd.Remove("some_string"); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } cnt = hd.Count; hd.Remove(new Hashtable()); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } // // [] Remove() on short dictionary with simple strings // cnt = hd.Count; int len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysShort[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysShort[i])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } // remove second time hd.Remove(keysShort[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed when Remove() second time", i)); } } // [] Remove() on long dictionary with simple strings // hd.Clear(); cnt = hd.Count; len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysLong[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } // remove second time hd.Remove(keysLong[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed when Remove() second time", i)); } } // // [] Remove() on long dictionary with Intl strings // Intl strings // len = valuesLong.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; } cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; hd.Remove(intlValues[i + len]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // [] Remove() on short dictionary with Intl strings // len = valuesShort.Length; hd.Clear(); cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; hd.Remove(intlValues[i + len]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // Case sensitivity // [] Case sensitivity - hashtable // len = valuesLong.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToUpper())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = hd.Count; hd.Remove(keysLong[i].ToLower()); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, failed: removed item using lowercase key", i)); } } // // [] Case sensitivity - list len = valuesShort.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToUpper())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = hd.Count; hd.Remove(keysLong[i].ToLower()); if (hd.Count != cnt) { Assert.False(true, string.Format("Error, failed: removed item using lowercase key", i)); } } // // [] Remove() on case-insensitive HD - list // hd = new HybridDictionary(true); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToLower())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Remove() on case-insensitive HD - hashtable // hd = new HybridDictionary(true); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(keysLong[i].ToUpper()); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (hd.Contains(keysLong[i].ToLower())) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Remove(null) from filled HD - list // hd = new HybridDictionary(); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } try { hd.Remove(null); Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // // [] Remove(null) from filled HD - hashtable // hd = new HybridDictionary(); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } try { hd.Remove(null); Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // // [] Remove(special_object) from filled HD - list // hd = new HybridDictionary(); hd.Clear(); len = 2; ArrayList[] b = new ArrayList[len]; Hashtable[] lbl = new Hashtable[len]; for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(lbl[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove special object")); } if (hd.Contains(lbl[i])) { Assert.False(true, string.Format("Error, removed wrong special object")); } } // // [] Remove(special_object) from filled HD - hashtable // hd = new HybridDictionary(); hd.Clear(); len = 40; b = new ArrayList[len]; lbl = new Hashtable[len]; for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Remove(lbl[i]); if (hd.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove special object")); } if (hd.Contains(lbl[i])) { Assert.False(true, string.Format("Error, removed wrong special object")); } } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; string[] vls; // values collection // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // [] initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); // [] GetValues() on empty collection // if (nvc.GetValues(null) != null) { Assert.False(true, "Error, returned non-null"); } if (nvc.GetValues("some_string") != null) { Assert.False(true, "Error, returned non-null"); } // [] GetValues() on collection filled with simple strings // cnt = nvc.Count; int len = values.Length; for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } // for (int i = 0; i < len; i++) { vls = nvc.GetValues(keys[i]); if (vls.Length != 1) { Assert.False(true, string.Format("Error, returned number of strings {1} instead of 1", i, vls.Length)); } if (String.Compare(vls[0], values[i]) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, vls[0], values[i])); } } // // Intl strings // [] GetValues() on collection filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLowerInvariant() == intlValues[i].ToUpperInvariant()) caseInsensitive = true; } nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); } if (nvc.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, len)); } for (int i = 0; i < len; i++) { // vls = nvc.GetValues(intlValues[i + len]); if (vls.Length != 1) { Assert.False(true, string.Format("Error, returned number of strings {1} instead of 1", i, vls.Length)); } if (String.Compare(vls[0], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, vls[0], 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(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key vls = nvc.GetValues(intlValues[i + len]); if (vls.Length != 1) { Assert.False(true, string.Format("Error, returned number of strings {1} instead of 1", i, vls.Length)); } if (String.Compare(vls[0], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, vls[0], intlValues[i])); } // lowercase key vls = nvc.GetValues(intlValuesLower[i + len]); if (vls.Length != 1) { Assert.False(true, string.Format("Error, returned number of strings {1} instead of 1", i, vls.Length)); } if (String.Compare(vls[0], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, vls[0], intlValues[i])); } if (!caseInsensitive && String.Compare(vls[0], intlValuesLower[i]) == 0) { Assert.False(true, string.Format("Error, returned lowercase when added uppercase", i)); } } // [] GetValues() on filled collection - with multiple items with the same key // nvc.Clear(); len = values.Length; string k = "keykey"; string k1 = "hm1"; string exp = ""; string exp1 = ""; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); nvc.Add(k1, "iTem" + i); if (i < len - 1) { exp += "Value" + i + ","; exp1 += "iTem" + i + ","; } else { exp += "Value" + i; exp1 += "iTem" + i; } } if (nvc.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, 2)); } vls = nvc.GetValues(k); if (vls.Length != len) { Assert.False(true, string.Format("Error, returned number of strings {0} instead of {1}", vls.Length, len)); } for (int i = 0; i < len; i++) { if (String.Compare(vls[i], "Value" + i) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, vls[i], "Value" + i)); } } vls = nvc.GetValues(k1); if (vls.Length != len) { Assert.False(true, string.Format("Error, returned number of strings {0} instead of {1}", vls.Length, len)); } for (int i = 0; i < len; i++) { if (String.Compare(vls[i], "iTem" + i) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, vls[i], "iTem" + i)); } } // // [] GetValues(null) - when there is an item with null-key // cnt = nvc.Count; nvc.Add(null, "nullValue"); vls = nvc.GetValues(null); if (vls.Length != 1) { Assert.False(true, string.Format("Error, returned number of strings {0} instead of {1}", vls.Length, 1)); } if (String.Compare(vls[0], "nullValue") != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", vls[0], "nullValue")); } // // [] GetValues(null) - when no item with null key // nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } vls = nvc.GetValues(null); if (vls != null) { Assert.False(true, "Error, returned non-null "); } }
public void Test01() { IntlStrings intl; StringCollection sc; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; string[] destination; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] StringCollection is constructed as expected //----------------------------------------------------------------- sc = new StringCollection(); // [] Copy empty collection into empty array // destination = new string[values.Length]; for (int i = 0; i < values.Length; i++) { destination[i] = ""; } sc.CopyTo(destination, 0); if (destination.Length != values.Length) { Assert.False(true, string.Format("Error, altered array after copying empty collection")); } if (destination.Length == values.Length) { for (int i = 0; i < values.Length; i++) { if (String.Compare(destination[i], "") != 0) { Assert.False(true, string.Format("Error, item = \"{1}\" instead of \"{2}\" after copying empty collection", i, destination[i], "")); } } } // [] Copy empty collection into non-empty array // destination = values; sc.CopyTo(destination, 0); if (destination.Length != values.Length) { Assert.False(true, string.Format("Error, altered array after copying empty collection")); } if (destination.Length == values.Length) { for (int i = 0; i < values.Length; i++) { if (String.Compare(destination[i], values[i]) != 0) { Assert.False(true, string.Format("Error, altered item {0} after copying empty collection", i)); } } } // // [] add simple strings and CopyTo([], 0) cnt = sc.Count; sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } destination = new string[values.Length]; sc.CopyTo(destination, 0); for (int i = 0; i < values.Length; i++) { // verify that collection is copied correctly // if (String.Compare(sc[i], destination[i]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i], sc[i])); } } // [] add simple strings and CopyTo([], middle_index) // sc.Clear(); cnt = sc.Count; sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } destination = new string[values.Length * 2]; sc.CopyTo(destination, values.Length); for (int i = 0; i < values.Length; i++) { // verify that collection is copied correctly // if (String.Compare(sc[i], destination[i + values.Length]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i + values.Length], sc[i])); } } // // Intl strings // [] add intl strings and CopyTo([], 0) // string[] intlValues = new string[values.Length]; // fill array with unique strings // for (int i = 0; i < values.Length; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } sc.Clear(); sc.AddRange(intlValues); if (sc.Count != (intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } destination = new string[intlValues.Length]; sc.CopyTo(destination, 0); for (int i = 0; i < intlValues.Length; i++) { // verify that collection is copied correctly // if (String.Compare(sc[i], destination[i]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i], sc[i])); } } // // Intl strings // [] add intl strings and CopyTo([], middle_index) // sc.Clear(); sc.AddRange(intlValues); if (sc.Count != (intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } destination = new string[intlValues.Length * 2]; sc.CopyTo(destination, intlValues.Length); for (int i = 0; i < intlValues.Length; i++) { // verify that collection is copied correctly // if (String.Compare(sc[i], destination[i + intlValues.Length]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i + intlValues.Length], sc[i])); } } // // [] CopyTo(null, int) // sc.Clear(); sc.AddRange(values); if (sc.Count != (intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } destination = null; Assert.Throws<ArgumentNullException>(() => { sc.CopyTo(destination, 0); }); // // [] CopyTo(string[], -1) // if (sc.Count != values.Length) { sc.Clear(); sc.AddRange(values); if (sc.Count != (intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } } destination = new string[values.Length]; Assert.Throws<ArgumentOutOfRangeException>(() => { sc.CopyTo(destination, -1); }); // // [] CopyTo(string[], upperBound+1) // if (sc.Count != values.Length) { sc.Clear(); sc.AddRange(values); if (sc.Count != (intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } } destination = new string[values.Length]; Assert.Throws<ArgumentException>(() => { sc.CopyTo(destination, values.Length); }); // // [] CopyTo(string[], upperBound+2) // if (sc.Count != values.Length) { sc.Clear(); sc.AddRange(values); if (sc.Count != (intlValues.Length + 1)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } } destination = new string[values.Length]; Assert.Throws<ArgumentException>(() => { sc.CopyTo(destination, values.Length); }); // // [] CopyTo(string[], not_enough_space) // if (sc.Count != values.Length) { sc.Clear(); sc.AddRange(values); if (sc.Count != (intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } } destination = new string[values.Length]; Assert.Throws<ArgumentException>(() => { sc.CopyTo(destination, values.Length / 2); }); }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- // [] simple strings hd = new HybridDictionary(); for (int i = 0; i < valuesShort.Length; i++) { cnt = hd.Count; hd.Add(keysShort[i], valuesShort[i]); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (String.Compare(hd[keysShort[i]].ToString(), valuesShort[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysShort[i]], valuesShort[i])); } } // increase the number of items for (int i = 0; i < valuesLong.Length; i++) { cnt = hd.Count; hd.Add(keysLong[i], valuesLong[i]); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i]], valuesLong[i])); } } // // [] Intl strings // int len = valuesShort.Length; hd.Clear(); string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) caseInsensitive = true; } // // will use first half of array as valuesShort and second half as keysShort // for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Add(intlValues[i + len], intlValues[i]); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (String.Compare(hd[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[intlValues[i + len]], intlValues[i])); } } // increase the number of items for (int i = 0; i < valuesLong.Length; i++) { cnt = hd.Count; hd.Add(keysLong[i], intlValues[1]); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (String.Compare(hd[keysLong[i]].ToString(), intlValues[1]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i]], intlValues[1])); } } // // [] Case sensitivity // Casing doesn't change ( keysShort are not converted to lower!) // string[] intlValuesLower = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToUpper(); } for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValues[i].ToLower(); } hd.Clear(); // // will use first half of array as valuesShort and second half as keysShort // for (int i = 0; i < len; i++) { cnt = hd.Count; hd.Add(intlValues[i + len], intlValues[i]); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (hd[intlValues[i + len]] == null) { Assert.False(true, string.Format("Error, returned null", i)); } else { if (!hd[intlValues[i + len]].Equals(intlValues[i])) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[intlValues[i + len]], intlValues[i])); } } // verify that dictionary doesn't contains lowercase item // if (!caseInsensitive && hd[intlValuesLower[i + len]] != null) { Assert.False(true, string.Format("Error, returned non-null", i)); } } // // [] Add multiple valuesShort with the same key // Add multiple valuesShort with the same key - ArgumentException expected // hd.Clear(); len = valuesShort.Length; string k = "keykey"; hd.Add(k, "value"); Assert.Throws<ArgumentException>(() => { hd.Add(k, "newvalue"); }); // // [] Add null value // cnt = hd.Count; k = "kk"; hd.Add(k, null); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + 1)); } // verify that dictionary contains null // if (hd[k] != null) { Assert.False(true, string.Format("Error, returned non-null on place of null")); } // // [] Add item with null key // Add item with null key - ArgumentNullException expected // cnt = hd.Count; Assert.Throws<ArgumentNullException>(() => { hd.Add(null, "item"); }); // // [] Add duplicate values // hd.Clear(); for (int i = 0; i < valuesShort.Length; i++) { cnt = hd.Count; hd.Add(keysShort[i], "value"); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (!hd[keysShort[i]].Equals("value")) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysShort[i]], "value")); } } // verify Keys and Values if (hd.Keys.Count != valuesShort.Length) { Assert.False(true, string.Format("Error, Keys contains {0} instead of {1}", hd.Keys.Count, valuesShort.Length)); } if (hd.Values.Count != valuesShort.Length) { Assert.False(true, string.Format("Error, Values contains {0} instead of {1}", hd.Values.Count, valuesShort.Length)); } // // [] add many simple strings // hd = new HybridDictionary(); for (int i = 0; i < valuesLong.Length; i++) { cnt = hd.Count; hd.Add(keysLong[i], valuesLong[i]); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (String.Compare(hd[keysLong[i]].ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i]], valuesLong[i])); } } // increase the number of items for (int i = 0; i < valuesLong.Length; i++) { cnt = hd.Count; hd.Add(keysLong[i] + "_", valuesLong[i] + i); if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, hd.Count, cnt + 1)); } // access the item // if (String.Compare(hd[keysLong[i] + "_"].ToString(), valuesLong[i] + i) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, hd[keysLong[i] + "_"], valuesLong[i] + i)); } } }
public void Test01() { IntlStrings intl; ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] Remove() on empty dictionary // cnt = ld.Count; Assert.Throws<ArgumentNullException>(() => { ld.Remove(null); }); cnt = ld.Count; ld.Remove("some_string"); if (ld.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } cnt = ld.Count; ld.Remove(new Hashtable()); if (ld.Count != cnt) { Assert.False(true, string.Format("Error, changed dictionary after Remove(some_string)")); } // [] add simple strings and Remove() // cnt = ld.Count; int len = values.Length; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, values.Length)); } // for (int i = 0; i < len; i++) { cnt = ld.Count; ld.Remove(keys[i]); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } if (ld.Contains(keys[i])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // Intl strings // [] Add Intl strings and Remove() // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) caseInsensitive = true; } cnt = ld.Count; for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = ld.Count; ld.Remove(intlValues[i + len]); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } if (ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Case sensitivity // string[] intlValuesLower = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToUpper(); } for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValues[i].ToLower(); } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key cnt = ld.Count; ld.Remove(intlValues[i + len]); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, returned: failed to remove item", i)); } if (ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = ld.Count; ld.Remove(intlValuesLower[i + len]); if (!caseInsensitive && ld.Count != cnt) { Assert.False(true, string.Format("Error, failed: removed item using lowercase key", i)); } } // // [] Remove() on LD with case-insensitive comparer // ld = new ListDictionary(new InsensitiveComparer()); len = values.Length; ld.Clear(); string kk = "key"; for (int i = 0; i < len; i++) { ld.Add(kk + i, values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } for (int i = 0; i < len; i++) { cnt = ld.Count; ld.Remove(kk.ToUpper() + i); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove item", i)); } if (ld.Contains(kk + i)) { Assert.False(true, string.Format("Error, removed wrong item", i)); } } // // [] Remove(null) // ld = new ListDictionary(); cnt = ld.Count; if (ld.Count < len) { ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } } Assert.Throws<ArgumentNullException>(() => { ld.Remove(null); }); ld = new ListDictionary(); ld.Clear(); ArrayList b = new ArrayList(); ArrayList b1 = new ArrayList(); Hashtable lbl = new Hashtable(); Hashtable lbl1 = new Hashtable(); ld.Add(lbl, b); ld.Add(lbl1, b1); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } cnt = ld.Count; ld.Remove(lbl); if (ld.Count != cnt - 1) { Assert.False(true, string.Format("Error, failed to remove special object")); } if (ld.Contains(lbl)) { Assert.False(true, string.Format("Error, removed wrong special object")); } }
public void Test01() { 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 void Test01() { IntlStrings intl; ListDictionary ld; // simple string values string[] values = { "", " ", "a", "aa", "tExt", " spAces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; Array arr; ICollection ks; // Keys collection int ind; // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] get Keys for empty dictionary // if (ld.Count > 0) ld.Clear(); if (ld.Keys.Count != 0) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ld.Keys.Count)); } // [] get Keys for filled dictionary // int len = values.Length; ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } ks = ld.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(Object), len); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key. Search result: {2}", i, keys[i], ind)); } } // // get Keys on dictionary with identical values // // [] get Keys for filled dictionary with identical value // ld.Clear(); string intlStr = intl.GetRandomString(MAX_LEN); ld.Add("keykey", intlStr); // 1st key for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } ld.Add("keyKey", intlStr); // 2nd key if (ld.Count != len + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len + 2)); } // get Keys // ks = ld.Keys; if (ks.Count != ld.Count) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(Object), len + 2); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, keys[i])); } } ind = Array.IndexOf(arr, "keykey"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain {0} key", "keykey")); } ind = Array.IndexOf(arr, "keyKey"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{0}\" key", "keyKey")); } // // Intl strings // // [] get Keys for dictionary filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // // will use first half of array as values and second half as keys // ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } ks = ld.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(Object), len); ks.CopyTo(arr, 0); for (int i = 0; i < arr.Length; i++) { ind = Array.IndexOf(arr, intlValues[i + len]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, intlValues[i + len])); } } // // [] Change dictionary and verify Keys // ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } ks = ld.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } ld.Remove(keys[0]); if (ld.Count != len - 1) { Assert.False(true, string.Format("Error, didn't remove element")); } if (ks.Count != len - 1) { Assert.False(true, string.Format("Error, Keys were not updated after removal")); } arr = Array.CreateInstance(typeof(Object), ld.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0]); if (ind >= 0) { Assert.False(true, string.Format("Error, Keys still contains removed key " + ind)); } ld.Add(keys[0], "new item"); if (ld.Count != len) { Assert.False(true, string.Format("Error, didn't add element")); } if (ks.Count != len) { Assert.False(true, string.Format("Error, Keys were not updated after addition")); } arr = Array.CreateInstance(typeof(Object), ld.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0]); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain added key ")); } ICollection icol1; ListDictionary Hd = new ListDictionary(); for (int i = 0; i < 10; i++) Hd.Add("key_" + i, "val_" + i); icol1 = Hd.Keys; if (Hd.SyncRoot != icol1.SyncRoot) { Assert.False(true, string.Format("Error, SyncRoot is not the same for ListDictionary's SyncRoot and ICollection.SyncRoot")); } // [] Run IEnumerable tests Hd = new ListDictionary(); String[] expectedKeys = new String[10]; for (int i = 0; i < 10; i++) { Hd.Add("key_" + i, "val_" + i); expectedKeys[i] = "key_" + i; } TestSupport.Collections.IEnumerable_Test iEnumerableTest; iEnumerableTest = new TestSupport.Collections.IEnumerable_Test(Hd.Keys, expectedKeys); if (!iEnumerableTest.RunAllTests()) { Assert.False(true, string.Format("Err_98382apeuie System.Collections.IEnumerable tests FAILED")); } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; // 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[] ks; // keys array // initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); // [] AllKeys on empty collection // if (nvc.Count > 0) nvc.Clear(); ks = nvc.AllKeys; if (ks.Length != 0) { Assert.False(true, string.Format("Error, number of keys is {0} instead of 0", ks.Length)); } // [] AllKeys on collection filled with simple strings // for (int i = 0; i < values.Length; i++) { cnt = nvc.Count; nvc.Add(keys[i], values[i]); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, nvc.Count, cnt + 1)); } // verify that collection contains newly added item // if (nvc.AllKeys.Length != cnt + 1) { Assert.False(true, string.Format("Error, incorrect Keys array", i)); } if (Array.IndexOf(nvc.AllKeys, keys[i]) < 0) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } } // // Intl strings // [] AllKeys on collection filled with 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 = nvc.Count; nvc.Add(intlValues[i + len], intlValues[i]); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, nvc.Count, cnt + 1)); } // verify that collection contains newly added item // if (nvc.AllKeys.Length != cnt + 1) { Assert.False(true, string.Format("Error, wrong keys array", i)); } if (Array.IndexOf(nvc.AllKeys, intlValues[i + len]) < 0) { Assert.False(true, string.Format("Error, Array doesn't contain key of new item", i)); } } // // [] Case sensitivity // Casing doesn't change ( keys are not converted to lower!) // string[] intlValuesLower = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToUpperInvariant(); } for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValues[i].ToLowerInvariant(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { cnt = nvc.Count; // add uppercase items nvc.Add(intlValues[i + len], intlValues[i]); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, nvc.Count, cnt + 1)); } // verify that collection contains newly added uppercase item // if (nvc.AllKeys.Length != cnt + 1) { Assert.False(true, string.Format("Error, wrong keys array", i)); } if (Array.IndexOf(nvc.AllKeys, intlValues[i + len]) < 0) { Assert.False(true, string.Format("Error, collection doesn't contain key of new item", i)); } // key is not converted to lower if (!caseInsensitive && Array.IndexOf(nvc.AllKeys, intlValuesLower[i + len]) >= 0) { Assert.False(true, string.Format("Error, key was converted to lower", i)); } } // // [] AllKeys for multiple values with the same key // nvc.Clear(); len = values.Length; string k = "keykey"; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); if (nvc.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of 1", nvc.Count, i)); } if (nvc.AllKeys.Length != 1) { Assert.False(true, string.Format("Error, AllKeys contains {0} instead of 1", nvc.AllKeys.Length, i)); } if (Array.IndexOf(nvc.AllKeys, k) != 0) { Assert.False(true, string.Format("Error, wrong key", i)); } } // access the item // string[] vals = nvc.GetValues(k); if (vals.Length != len) { Assert.False(true, string.Format("Error, number of values at given key is {0} instead of {1}", vals.Length, 1)); } // // [] AllKeys when collection has null value // k = "kk"; nvc.Remove(k); // make sure there is no such item already cnt = nvc.Count; nvc.Add(k, null); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, cnt + 1)); } if (Array.IndexOf(nvc.AllKeys, k) < 0) { Assert.False(true, "Error, collection doesn't contain key of new item"); } // verify that collection contains null // if (nvc[k] != null) { Assert.False(true, "Error, returned non-null on place of null"); } // // [] Allkeys when item with null key is present // nvc.Remove(null); cnt = nvc.Count; nvc.Add(null, "item"); if (nvc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, cnt + 1)); } if (Array.IndexOf(nvc.AllKeys, null) < 0) { Assert.False(true, "Error, collection doesn't contain null key "); } // verify that collection contains null // if (nvc[null] != "item") { Assert.False(true, "Error, returned wrong value at null key"); } }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; // string [] destination; Array destination; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); // [] Copy empty dictionary into empty array // destination = Array.CreateInstance(typeof(Object), 0); Assert.Throws<ArgumentOutOfRangeException>(() => { hd.CopyTo(destination, -1); }); hd.CopyTo(destination, 0); // exception even when copying empty dictionary Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, 1); }); // [] Copy empty dictionary into filled array // destination = Array.CreateInstance(typeof(Object), valuesShort.Length); for (int i = 0; i < valuesShort.Length; i++) { destination.SetValue(valuesShort[i], i); } hd.CopyTo(destination, 0); if (destination.Length != valuesShort.Length) { Assert.False(true, string.Format("Error, altered array after copying empty dictionary")); } if (destination.Length == valuesShort.Length) { for (int i = 0; i < valuesShort.Length; i++) { if (String.Compare(destination.GetValue(i).ToString(), valuesShort[i]) != 0) { Assert.False(true, string.Format("Error, altered item {0} after copying empty dictionary", i)); } } } // [] few simple strings and CopyTo(Array, 0) // hd.Clear(); cnt = hd.Count; int len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } destination = Array.CreateInstance(typeof(Object), len); hd.CopyTo(destination, 0); // // for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // if (String.Compare(hd[keysShort[i]].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), hd[keysShort[i]])); } if (String.Compare(keysShort[i], ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), keysShort[i])); } } // [] few simple strings and CopyTo(Array, middle_index) // hd.Clear(); hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } destination = Array.CreateInstance(typeof(Object), len * 2); hd.CopyTo(destination, len); // // for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // if (String.Compare(hd[keysShort[i]].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, hd[keysShort[i]])); } // verify keysShort if (String.Compare(keysShort[i], ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, keysShort[i])); } } // [] many simple strings and CopyTo(Array, 0) // hd.Clear(); cnt = hd.Count; len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } destination = Array.CreateInstance(typeof(Object), len); hd.CopyTo(destination, 0); // // IDictionaryEnumerator en = hd.GetEnumerator(); en.MoveNext(); // items are copied in the same order they are enumerated for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value.ToString(), hd[k])); } if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key.ToString(), k.ToString())); } en.MoveNext(); } // [] many simple strings and CopyTo(Array, middle_index) // hd.Clear(); hd.Clear(); len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } destination = Array.CreateInstance(typeof(Object), len * 2); hd.CopyTo(destination, len); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, hd[k])); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, k)); } en.MoveNext(); } // // [] many Intl strings and CopyTo(Array, 0) // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) caseInsensitive = true; } hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } destination = Array.CreateInstance(typeof(Object), len); hd.CopyTo(destination, 0); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, hd[k])); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, k)); } en.MoveNext(); } // // [] many Intl strings and CopyTo(Array, middle_index) // destination = Array.CreateInstance(typeof(Object), len * 2); hd.CopyTo(destination, len); // // order of items is the same as they were in dictionary // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Value, hd[k])); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i + len)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len)).Key, k)); } en.MoveNext(); } // [] few Intl strings and CopyTo(Array, 0) // int len1 = valuesShort.Length; hd.Clear(); for (int i = 0; i < len1; i++) { hd.Add(intlValues[i + len1], intlValues[i]); } if (hd.Count != (len1)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len1)); } destination = Array.CreateInstance(typeof(Object), len1); hd.CopyTo(destination, 0); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len1; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, hd[k])); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, k)); } en.MoveNext(); } // // [] few Intl strings and CopyTo(Array, middle_index) // destination = Array.CreateInstance(typeof(Object), len1 * 2); hd.CopyTo(destination, len1); // // order of items is the same as they were in dictionary // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len1; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i + len1)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len1)).Value, hd[k])); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i + len1)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i + len1)).Key, k)); } en.MoveNext(); } // // [] Case sensitivity // string[] intlValuesUpper = new string[len * 2]; string[] intlValuesLower = new string[len * 2]; // fill array with unique upper-case strings // for (int i = 0; i < len * 2; i++) { string val = intlValues[i].ToUpper(); while (Array.IndexOf(intlValuesUpper, val) != -1) val = intl.GetRandomString(MAX_LEN).ToUpper(); intlValuesUpper[i] = val; } caseInsensitive = false; for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValuesUpper[i].ToLower(); if (intlValuesLower[i].Length != 0 && intlValuesLower[i] == intlValuesUpper[i]) caseInsensitive = true; } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(intlValuesUpper[i + len], intlValuesUpper[i]); // adding uppercase strings } destination = Array.CreateInstance(typeof(Object), len); hd.CopyTo(destination, 0); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Value, hd[k])); } if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) > -1) { Assert.False(true, string.Format("Error, copied lowercase string")); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, ((DictionaryEntry)destination.GetValue(i)).Key, k)); } if (!caseInsensitive && Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) > -1) { Assert.False(true, string.Format("Error, copied lowercase key")); } en.MoveNext(); } // ---------------------------------------------------------------- // [] Parameter validation for short HybridDictionary (list) // ---------------------------------------------------------------- hd = new HybridDictionary(); for (int i = 0; i < len1; i++) { hd.Add(keysShort[i], valuesShort[i]); } // // CopyTo(null, int) // destination = null; Assert.Throws<ArgumentNullException>(() => { hd.CopyTo(destination, 0); }); // // CopyTo(Array, -1) // destination = Array.CreateInstance(typeof(Object), 2); Assert.Throws<ArgumentOutOfRangeException>(() => { hd.CopyTo(destination, -1); }); // // CopyTo(Array, upperBound+1) // cnt = hd.Count; destination = Array.CreateInstance(typeof(Object), cnt); Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt); }); // // CopyTo(Array, upperBound+2) // Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt + 1); }); // // CopyTo(Array, not_enough_space) // Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt / 2); }); // // CopyTo(multidim_Array, 0) // Array dest = new String[cnt, cnt]; Assert.Throws<ArgumentException>(() => { hd.CopyTo(dest, 0); }); // // CopyTo(wrong_type, 0) // dest = Array.CreateInstance(typeof(ArrayList), cnt); Assert.Throws<System.InvalidCastException>(() => { hd.CopyTo(dest, 0); }); // // CopyTo(Array, upperBound+1) - copy empty dictionary - no exception // hd.Clear(); destination = Array.CreateInstance(typeof(Object), len); hd.CopyTo(destination, len); // ---------------------------------------------------------------- // [] Parameter validation for long HybridDictionary (hashtable) // ---------------------------------------------------------------- hd = new HybridDictionary(); len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } // // CopyTo(null, int) // destination = null; Assert.Throws<ArgumentNullException>(() => { hd.CopyTo(destination, 0); }); // // CopyTo(Array, -1) // destination = Array.CreateInstance(typeof(Object), 2); Assert.Throws<ArgumentOutOfRangeException>(() => { hd.CopyTo(destination, -1); }); // // CopyTo(Array, upperBound+1) // cnt = hd.Count; destination = Array.CreateInstance(typeof(Object), cnt); Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt); }); // // CopyTo(Array, upperBound+2) // Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt + 1); }); // // CopyTo(Array, not_enough_space) // Assert.Throws<ArgumentException>(() => { hd.CopyTo(destination, cnt / 2); }); // // CopyTo(multidim_Array, 0) // dest = Array.CreateInstance(typeof(string), cnt, cnt); Assert.Throws<ArgumentException>(() => { hd.CopyTo(dest, 0); }); // // CopyTo(wrong_type, 0) // dest = Array.CreateInstance(typeof(ArrayList), cnt); Assert.Throws<System.InvalidCastException>(() => { hd.CopyTo(dest, 0); }); // // [] CopyTo() for few not_overriding_Equals objects // hd.Clear(); int num = 2; Hashtable[] lbl = new Hashtable[num]; ArrayList[] b = new ArrayList[num]; for (int i = 0; i < num; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } destination = Array.CreateInstance(typeof(Object), num); hd.CopyTo(destination, 0); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < num; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (!hd[k].Equals(((DictionaryEntry)destination.GetValue(i)).Value)) { Assert.False(true, string.Format("Error, failed to copy {0}th entry", i)); } // verify keysShort if (!k.Equals(((DictionaryEntry)destination.GetValue(i)).Key)) { Assert.False(true, string.Format("Error, failed to copy {0} entry", i)); } en.MoveNext(); } // [] CopyTo() for many not_overriding_Equals objects // hd.Clear(); num = 40; lbl = new Hashtable[num]; b = new ArrayList[num]; for (int i = 0; i < num; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } destination = Array.CreateInstance(typeof(Object), num); hd.CopyTo(destination, 0); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < num; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (!hd[k].Equals(((DictionaryEntry)destination.GetValue(i)).Value)) { Assert.False(true, string.Format("Error, failed to copy {0}th entry", i)); } // verify keysShort if (!k.Equals(((DictionaryEntry)destination.GetValue(i)).Key)) { Assert.False(true, string.Format("Error, failed to copy {0} entry", i)); } en.MoveNext(); } // // [] CopyTo() - for short case-insensitive HybridDictionary // hd = new HybridDictionary(true); len = 3; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } destination = Array.CreateInstance(typeof(Object), len); hd.CopyTo(destination, 0); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) < 0) { Assert.False(true, string.Format("Error, failed to copy {0}th entry when case-insensitive", i)); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) < 0) { Assert.False(true, string.Format("Error, failed to copy {0} entry when case-insensitive", i)); } en.MoveNext(); } // [] CopyTo() - for long case-insensitive HybridDictionary // hd = new HybridDictionary(true); len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } destination = Array.CreateInstance(typeof(Object), len); hd.CopyTo(destination, 0); // // en = hd.GetEnumerator(); en.MoveNext(); for (int i = 0; i < len; i++) { // verify that dictionary is copied correctly // Object k = en.Key; if (String.Compare(hd[k].ToString(), ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) < 0) { Assert.False(true, string.Format("Error, failed to copy {0}th entry when case-insensitive", i)); } // verify keysShort if (String.Compare(k.ToString(), ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) < 0) { Assert.False(true, string.Format("Error, failed to copy {0} entry when case-insensitive", i)); } en.MoveNext(); } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; NameValueCollection nvc1; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); nvc1 = new NameValueCollection(); // [] Add(empty_coll) to empty collection // nvc.Clear(); nvc1.Clear(); nvc.Add(nvc1); if (nvc.Count != 0) { Assert.False(true, string.Format("Error, count is {0} instead of 0", nvc.Count)); } // [] Add(simple_strings_coll) to empty collection // nvc.Clear(); cnt = nvc.Count; int len = values.Length; for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } nvc1.Clear(); nvc1.Add(nvc); if (nvc1.Count != nvc.Count) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc1.Count, nvc.Count)); } // for (int i = 0; i < len; i++) { if (String.Compare(nvc1[keys[i]], values[i]) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc[keys[i]], values[i])); } } // [] Add(simple_strings_coll) to simple_string_collection // len = values.Length; if (nvc.Count < len) { nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } } nvc1.Clear(); for (int i = 0; i < len; i++) { nvc1.Add("k" + i, "v" + i); } cnt = nvc1.Count; nvc1.Add(nvc); if (nvc1.Count != cnt + nvc.Count) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc1.Count, cnt + nvc.Count)); } // for (int i = 0; i < len; i++) { if (String.Compare(nvc1[keys[i]], values[i]) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc[keys[i]], values[i])); } if (String.Compare(nvc1["k" + i], "v" + i) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc["k" + i], "v" + i)); } } // // Intl strings // [] Add(intl_strings_coll) to empty collection // 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; } // fill init collection with intl strings nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); } if (nvc.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, len)); } // add filled collection to tested empty collection nvc1.Clear(); nvc1.Add(nvc); for (int i = 0; i < len; i++) { // if (String.Compare(nvc1[intlValues[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc1[intlValues[i + len]], 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(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // add uppercase to tested collection nvc1.Clear(); nvc1.Add(nvc); // for (int i = 0; i < len; i++) { // uppercase key if (String.Compare(nvc1[intlValues[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc1[intlValues[i + len]], intlValues[i])); } // lowercase key if (String.Compare(nvc1[intlValuesLower[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc1[intlValuesLower[i + len]], intlValues[i])); } if (!caseInsensitive && (String.Compare(nvc1[intlValues[i + len]], intlValuesLower[i]) == 0)) { Assert.False(true, string.Format("Error, returned lowercase when added uppercase", i)); } } // // when adding values with existing keys, values should be added to existing values // [] Add(NVC) with already existing keys // nvc.Clear(); cnt = nvc.Count; len = values.Length; for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } nvc1.Clear(); for (int i = 0; i < len; i++) { nvc1.Add(keys[i], values[i]); } if (nvc1.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc1.Count, values.Length)); } cnt = nvc1.Count; nvc1.Add(nvc); // count should not change if (nvc1.Count != cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc1.Count, cnt)); } // values should be added to previously existed values for (int i = 0; i < len; i++) { if (String.Compare(nvc1[keys[i]], values[i] + "," + values[i]) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc[keys[i]], values[i] + "," + values[i])); } } // [] multiple items with same key // nvc.Clear(); len = values.Length; string k = "keykey"; string k1 = "hm1"; string exp = ""; string exp1 = ""; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); nvc.Add(k1, "iTem" + i); if (i < len - 1) { exp += "Value" + i + ","; exp1 += "iTem" + i + ","; } else { exp += "Value" + i; exp1 += "iTem" + i; } } if (nvc.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, 2)); } // add NVC nvc1.Clear(); nvc1.Add(nvc); if (String.Compare(nvc1[k], exp) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc1[k], exp)); } // Values array should contain len items if (nvc1.GetValues(k).Length != len) { Assert.False(true, string.Format("Error, number of values is {0} instead of {1}", nvc1.GetValues(k).Length, len)); } for (int i = 0; i < len; i++) { if (String.Compare(nvc1.GetValues(k)[i], "Value" + i) != 0) { Assert.False(true, string.Format("Error, returned {1} instead of {2}", i, nvc1.GetValues(k)[i], "Value" + i)); } } if (String.Compare(nvc1[k1], exp1) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc1[k1], exp1)); } // Values array should contain len items if (nvc1.GetValues(k1).Length != len) { Assert.False(true, string.Format("Error, number of values is {0} instead of {1}", nvc1.GetValues(k1).Length, len)); } for (int i = 0; i < len; i++) { if (String.Compare(nvc1.GetValues(k1)[i], "iTem" + i) != 0) { Assert.False(true, string.Format("Error, returned {1} instead of {2}", i, nvc1.GetValues(k1)[i], "iTem" + i)); } } // // [] Add(with_null_key) when there is item with null key // cnt = nvc.Count; nvc.Add(null, "nullValue"); nvc1[null] = "nullValue1"; nvc1.Add(nvc); if (String.Compare(nvc1[null], "nullValue1," + nvc[null]) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc1[null], "nullValue1," + nvc[null])); } // // [] Add(with_null_key) when there is no item with null key // nvc.Clear(); nvc1.Clear(); for (int i = 0; i < len; i++) { nvc1.Add(keys[i], values[i]); } nvc[null] = "newNullValue"; nvc1.Add(nvc); if (String.Compare(nvc1[null], "newNullValue") != 0) { Assert.False(true, "Error, returned unexpected value "); } // // [] Add(null_collection) // try { nvc1.Add(null); Assert.False(true, "Error, no exception"); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // // [] Add(empty_coll) to filled collection // nvc.Clear(); if (nvc1.Count < len) { nvc1.Clear(); for (int i = 0; i < len; i++) { nvc1.Add(keys[i], values[i]); } if (nvc1.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc1.Count, values.Length)); } } cnt = nvc1.Count; nvc1.Add(nvc); if (nvc1.Count != cnt) { Assert.False(true, string.Format("Error, count has changed: {0} instead of {1}", nvc1.Count, cnt)); } // verify that collection has not changed for (int i = 0; i < len; i++) { if (String.Compare(nvc1[keys[i]], values[i]) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc[keys[i]], values[i])); } } // // [] Add collection with null values // nvc.Clear(); nvc1.Clear(); for (int i = 0; i < len; i++) { nvc.Add(keys[i], null); } nvc1.Add(nvc); if (nvc1.Count != nvc.Count) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc1.Count, nvc.Count)); } // for (int i = 0; i < len; i++) { if (String.Compare(nvc1[keys[i]], null) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc[keys[i]], values[i])); } } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; string[] destination; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); // [] CopyTo() empty collection into empty array // destination = new string[] { }; try { nvc.CopyTo(destination, -1); Assert.False(true, "Error, no exception"); } catch (ArgumentOutOfRangeException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } try { nvc.CopyTo(destination, 0); } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } try { nvc.CopyTo(destination, 1); Assert.False(true, "Error, no exception"); } catch (ArgumentException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // [] CopyTo() empty collection into filled array // destination = new string[values.Length]; for (int i = 0; i < values.Length; i++) { destination[i] = values[i]; } nvc.CopyTo(destination, 0); if (destination.Length != values.Length) { Assert.False(true, "Error, altered array after copying empty collection"); } if (destination.Length == values.Length) { for (int i = 0; i < values.Length; i++) { if (String.Compare(destination[i], values[i]) != 0) { Assert.False(true, string.Format("Error, altered item {0} after copying empty collection", i)); } } } // // [] CopyTo(array, 0) collection with simple strings cnt = nvc.Count; int len = values.Length; for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } destination = new string[len]; nvc.CopyTo(destination, 0); // // order of items is the same as order it was in collection // for (int i = 0; i < len; i++) { // verify that collection is copied correctly // if (String.Compare(nvc[i], destination[i]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i], nvc[i])); } } // [] CopyTo(array, middle_index) collection with simple strings // nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } destination = new string[len * 2]; nvc.CopyTo(destination, len); // // order of items is the same as they wer in collection // for (int i = 0; i < len; i++) { // verify that collection is copied correctly // if (String.Compare(nvc[i], destination[i + len]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i + len], nvc[i])); } } // // Intl strings // [] CopyTo(array, 0) collection with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLowerInvariant() == intlValues[i].ToUpperInvariant()) caseInsensitive = true; } nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); } if (nvc.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, len)); } destination = new string[len]; nvc.CopyTo(destination, 0); // // order of items is the same as they wer in collection // for (int i = 0; i < len; i++) { // verify that collection is copied correctly // if (String.Compare(nvc[i], destination[i]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i], nvc[i])); } } // // Intl strings // [] CopyTo(array, middle_index) collection with Intl strings // destination = new string[len * 2]; nvc.CopyTo(destination, len); // // order of items is the same as they were in collection // for (int i = 0; i < len; i++) { // verify that collection is copied correctly // if (String.Compare(nvc[i], destination[i + len]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i + len], nvc[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(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } destination = new string[len]; nvc.CopyTo(destination, 0); // // order of items is the same as they were in collection // for (int i = 0; i < len; i++) { // verify that collection is copied correctly // if (String.Compare(nvc[i], destination[i]) != 0) { Assert.False(true, string.Format("Error, copied \"{1}\" instead of \"{2}\"", i, destination[i], nvc[i])); } if (!caseInsensitive && Array.IndexOf(intlValuesLower, destination[i]) != -1) { Assert.False(true, string.Format("Error, copied lowercase string")); } } // // [] CopyTo(null, int) // destination = null; Assert.Throws<ArgumentNullException>(() => { nvc.CopyTo(destination, 0); }); // // [] CopyTo(string[], -1) // cnt = nvc.Count; destination = new string[] { }; Assert.Throws<ArgumentOutOfRangeException>(() => { nvc.CopyTo(destination, -1); }); // // [] CopyTo(Array, upperBound+1) // if (nvc.Count < 1) { for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } } destination = new string[len]; Assert.Throws<ArgumentException>(() => { nvc.CopyTo(destination, len); }); // // [] CopyTo(Array, upperBound+2) // Assert.Throws<ArgumentException>(() => { nvc.CopyTo(destination, len + 1); }); // // [] CopyTo(Array, not_enough_space) // Assert.Throws<ArgumentException>(() => { nvc.CopyTo(destination, len / 2); }); // // [] CopyTo(multidim_Array, 0) // Array dest = new string[len, len]; Assert.Throws<ArgumentException>(() => { nvc.CopyTo(dest, 0); }); // [] CopyTo(array, 0) collection with multiple items with the same key // nvc.Clear(); len = values.Length; string k = "keykey"; string exp = ""; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); if (i < len - 1) exp += "Value" + i + ","; else exp += "Value" + i; } if (nvc.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, 1)); } destination = new string[1]; nvc.CopyTo(destination, 0); // verify that collection is copied correctly // if (String.Compare(nvc[0], destination[0]) != 0) { Assert.False(true, string.Format("Error, copied \"{0}\" instead of \"{1}\"", destination[0], nvc[0])); } if (String.Compare(exp, destination[0]) != 0) { Assert.False(true, string.Format("Error, copied string is not the same as expected: {0}", destination[0])); } // // [] CopyTo(wrong_type, 0) // dest = new DictionaryEntry[len]; Assert.Throws<InvalidCastException>(() => { nvc.CopyTo(dest, 0); }); }
public void Test01() { IntlStrings intl; StringDictionary sd; string ind; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] check for empty dictionary // for (int i = 0; i < values.Length; i++) { if (sd.ContainsKey(keys[i])) { Assert.False(true, string.Format("Error, returned true for empty dictionary", i)); } } // [] add simple strings and verify ContainsKey() // cnt = values.Length; for (int i = 0; i < cnt; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, cnt)); } for (int i = 0; i < cnt; i++) { // verify that collection contains all added items // if (!sd.ContainsValue(values[i])) { Assert.False(true, string.Format("Error, collection doesn't contain value \"{1}\"", i, values[i])); } if (!sd.ContainsKey(keys[i])) { Assert.False(true, string.Format("Error, collection doesn't contain key \"{1}\"", i, keys[i])); } } // // Intl strings // [] add Intl strings and verify ContainsKey() // 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])); } } // // add null string with non-null key // [] add null string with non-null key and verify ContainsKey() // cnt = sd.Count; string k = "keykey"; sd.Add(k, null); if (sd.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", sd.Count, cnt + 1)); } // verify that collection contains newly added item // if (!sd.ContainsKey(k)) { Assert.False(true, string.Format("Error, dictionary doesn't contain new key")); } // // [] Case sensitivity: search should be case-sensitive // sd.Clear(); if (sd.Count != 0) { Assert.False(true, string.Format("Error, count is {1} instead of {2} after Clear()", sd.Count, 0)); } 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]); // adding uppercase strings 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)); } } // // call ContainsKey with null - ArgumentNullException expected // [] ContainsKey (null) // Assert.Throws<ArgumentNullException>(() => { sd.ContainsKey(null); }); }
public void Test01() { IntlStrings intl; NameValueCollection nvc; // 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 itm; // item // [] initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); // [] set Item() on empty collection // nvc.Clear(); nvc[null] = "nullItem"; if (nvc.Count != 1) { Assert.False(true, "Error, failed to add item"); } if (nvc[null] == null) { Assert.False(true, "Error, returned null"); } else { if (String.Compare(nvc[null], "nullItem") != 0) { Assert.False(true, "Error, wrong value"); } } nvc.Clear(); nvc["some_string"] = "someItem"; if (nvc.Count != 1) { Assert.False(true, "Error, failed to add item"); } if (nvc["some_string"] == null) { Assert.False(true, "Error, returned null"); } else { if (String.Compare(nvc["some_string"], "someItem") != 0) { Assert.False(true, "Error, wrong value"); } } // [] set Item(string) on collection filled with simple strings // nvc.Clear(); cnt = nvc.Count; int len = values.Length; for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } // for (int i = 0; i < len; i++) { nvc[keys[i]] = "Item" + i; if (String.Compare(nvc[keys[i]], "Item" + i) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc[keys[i]], "Item" + i)); } } // // Intl strings // [] set Item(string) on collection filled with Intl strings // string[] intlValues = new string[len * 3]; // fill array with unique strings // for (int i = 0; i < len * 3; 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; } nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); } if (nvc.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, len)); } for (int i = 0; i < len; i++) { // nvc[intlValues[i + len]] = intlValues[i + len * 2]; if (String.Compare(nvc[intlValues[i + len]], intlValues[i + len * 2]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc[intlValues[i + len]], intlValues[i + len * 2])); } } // // [] 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(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key if (String.Compare(nvc[intlValues[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc[intlValues[i + len]], intlValues[i])); } // lowercase key if (String.Compare(nvc[intlValuesLower[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc[intlValuesLower[i + len]], intlValues[i])); } if (!caseInsensitive && String.Compare(nvc[intlValues[i + len]], intlValuesLower[i]) == 0) { Assert.False(true, string.Format("Error, returned lowercase when added uppercase", i)); } // set to lowercase value nvc[intlValues[i + len]] = intlValuesLower[i]; // uppercase key if (!caseInsensitive && String.Compare(nvc[intlValues[i + len]], intlValues[i]) == 0) { Assert.False(true, string.Format("Error, failed to set to uppercase value", i)); } // lowercase key if (!caseInsensitive && String.Compare(nvc[intlValuesLower[i + len]], intlValues[i]) == 0) { Assert.False(true, string.Format("Error, failed to set to lowercase value", i)); } if (String.Compare(nvc[intlValues[i + len]], intlValuesLower[i]) != 0) { Assert.False(true, string.Format("Error, returned uppercase when set to lowercase", i)); } } // [] set Item(string) on filled collection - multiple item swith the same key // nvc.Clear(); len = values.Length; string k = "keykey"; string k1 = "hm1"; string exp = ""; string exp1 = ""; string newVal = "nEw1,nEw2"; string newVal1 = "Hello,hello,hELLo"; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); nvc.Add(k1, "iTem" + i); if (i < len - 1) { exp += "Value" + i + ","; exp1 += "iTem" + i + ","; } else { exp += "Value" + i; exp1 += "iTem" + i; } } if (nvc.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, 2)); } if (String.Compare(nvc[k], exp) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[k], exp)); } nvc[k] = newVal; if (String.Compare(nvc[k], newVal) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[k], newVal)); } // Values array should contain 1 item if (nvc.GetValues(k).Length != 1) { Assert.False(true, string.Format("Error, number of values is {0} instead of 1", nvc.GetValues(k).Length)); } if (String.Compare(nvc[k1], exp1) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[k1], exp1)); } nvc[k1] = newVal1; if (String.Compare(nvc[k1], newVal1) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[k1], newVal1)); } // Values array should contain 1 item if (nvc.GetValues(k1).Length != 1) { Assert.False(true, string.Format("Error, number of values is {0} instead of 1", nvc.GetValues(k).Length)); } // // [] set Item(null) - when there is an item with null key // cnt = nvc.Count; nvc.Add(null, "nullValue"); if (String.Compare(nvc[null], "nullValue") != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[null], "nullValue")); } nvc[null] = "newnewValue"; if (String.Compare(nvc[null], "newnewValue") != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[null], "newnewValue")); } // // [] set Item(null) - when no item with null key // nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } nvc[null] = "newNullValue"; itm = nvc[null]; if (String.Compare(itm, "newNullValue") != 0) { Assert.False(true, "Error, returned unexpected value "); } }
public void Test01() { IntlStrings intl; ListDictionary ld; Object itm; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] ListDictionary is constructed as expected //----------------------------------------------------------------- ld = new ListDictionary(); // [] on empty dictionary // cnt = ld.Count; Assert.Throws<ArgumentNullException>(() => { itm = ld[null]; }); cnt = ld.Count; itm = ld["some_string"]; if (itm != null) { Assert.False(true, string.Format("Error, returned non=null")); } cnt = ld.Count; itm = ld[new Hashtable()]; if (itm != null) { Assert.False(true, string.Format("Error, returned non-null")); } // [] get Item() on dictionary filled with simple strings // cnt = ld.Count; int len = values.Length; for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, values.Length)); } // for (int i = 0; i < len; i++) { if (!ld.Contains(keys[i])) { Assert.False(true, string.Format("Error, doesn't contain key", i)); } if (String.Compare(ld[keys[i]].ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } // // Intl strings // [] get Item() on dictionary filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) caseInsensitive = true; } cnt = ld.Count; for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt + len)); } for (int i = 0; i < len; i++) { // if (!ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain key", i)); } if (String.Compare(ld[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } // // [] Case sensitivity // string[] intlValuesLower = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToUpper(); } for (int i = 0; i < len * 2; i++) { intlValuesLower[i] = intlValues[i].ToLower(); } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key if (!ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain key", i)); } if (String.Compare(ld[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } ld.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key cnt = ld.Count; if (!caseInsensitive && ld[intlValuesLower[i + len]] != null) { Assert.False(true, string.Format("Error, failed: returned non-null for lowercase key", i)); } } // // [] get Item() on filled dictionary with case-insensitive comparer ld = new ListDictionary(new InsensitiveComparer()); len = values.Length; ld.Clear(); string kk = "key"; for (int i = 0; i < len; i++) { ld.Add(kk + i, values[i]); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } for (int i = 0; i < len; i++) { if (ld[kk.ToUpper() + i] == null) { Assert.False(true, string.Format("Error, returned null for differently cased key", i)); } else { if (String.Compare(ld[kk.ToUpper() + i].ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong value", i)); } } } // // [] Item(null) for dilled dictionary // ld = new ListDictionary(); cnt = ld.Count; if (ld.Count < len) { ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } } Assert.Throws<ArgumentNullException>(() => { itm = ld[null]; }); // [] get Item(special_object) // ld = new ListDictionary(); ld.Clear(); ArrayList b = new ArrayList(); ArrayList b1 = new ArrayList(); Hashtable lbl = new Hashtable(); Hashtable lbl1 = new Hashtable(); ld.Add(lbl, b); ld.Add(lbl1, b1); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } if (!ld[lbl].Equals(b)) { Assert.False(true, string.Format("Error, failed to access special object")); } if (!ld[lbl1].Equals(b1)) { Assert.False(true, string.Format("Error, failed to access special object")); } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; // simple string values string[] values = { "", " ", "a", "aA", "text", " SPaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "oNe", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); cnt = nvc.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after default ctor", nvc.Count, 0)); } // [] Clear() on empty collection // nvc.Clear(); cnt = nvc.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", nvc.Count, 0)); } // [] Add simple strings and Clear() // cnt = nvc.Count; for (int i = 0; i < values.Length; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } nvc.Clear(); cnt = nvc.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", nvc.Count, 0)); } // // [] Add Intl strings and Clear() // int len = values.Length; string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // Add items // cnt = nvc.Count; for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); } if (nvc.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, cnt + len)); } nvc.Clear(); cnt = nvc.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", nvc.Count, 0)); } }
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; // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] Remove() from empty dictionary // if (sd.Count > 0) sd.Clear(); for (int i = 0; i < keys.Length; i++) { sd.Remove(keys[0]); } // [] Remove() from filled dictionary // int len = values.Length; sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Remove(keys[i]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove element with {0} key", i)); } // verify that indeed element with given key was removed if (sd.ContainsValue(values[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } if (sd.ContainsKey(keys[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } } // // [] Remove() on dictionary with identical values // sd.Clear(); string intlStr = intl.GetRandomString(MAX_LEN); sd.Add("keykey1", intlStr); // 1st duplicate for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } sd.Add("keykey2", intlStr); // 2nd duplicate if (sd.Count != len + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len + 2)); } // remove // sd.Remove("keykey2"); if (!sd.ContainsValue(intlStr)) { Assert.False(true, string.Format("Error, removed both duplicates")); } // second string should still be present if (sd.ContainsKey("keykey2")) { Assert.False(true, string.Format("Error, removed not given instance")); } if (!sd.ContainsKey("keykey1")) { Assert.False(true, string.Format("Error, removed wrong instance")); } // // Intl strings // [] Remove() from dictionary filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // // will use first half of array as values and second half as keys // sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } // remove for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Remove(intlValues[i + len]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove element with {0} key", i + len)); } // verify that indeed element with given key was removed if (sd.ContainsValue(intlValues[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } if (sd.ContainsKey(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong key", i)); } } // // [] Case sensitivity: keys are always lowercased // sd.Clear(); string[] intlValuesUpper = new string[len]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToLowerInvariant(); } // array of uppercase keys for (int i = 0; i < len; i++) { intlValuesUpper[i] = intlValues[i + len].ToUpperInvariant(); } sd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); // adding lowercase strings } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } // remove for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Remove(intlValuesUpper[i]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove element with {0} lower key", i + len)); } // verify that indeed element with given key was removed if (sd.ContainsValue(intlValues[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } if (sd.ContainsKey(intlValuesUpper[i])) { Assert.False(true, string.Format("Error, removed wrong key", i)); } } // // [] Invalid parameter // Assert.Throws<ArgumentNullException>(() => { sd.Remove(null); }); }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int cnt = 0; // Count Object itm; // Item // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); // [] get Item() on empty dictionary // cnt = hd.Count; Assert.Throws<ArgumentNullException>(() => { itm = hd[null]; }); cnt = hd.Count; itm = hd["some_string"]; if (itm != null) { Assert.False(true, string.Format("Error, returned non-null for Item(some_string)")); } cnt = hd.Count; itm = hd[new Hashtable()]; if (itm != null) { Assert.False(true, string.Format("Error, returned non-null for Item(some_object)")); } // [] get Item() on short dictionary with simple strings // cnt = hd.Count; int len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; itm = hd[keysShort[i]]; if (String.Compare(itm.ToString(), valuesShort[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong item", i)); } } // [] get Item() on long dictionary with simple strings // hd.Clear(); cnt = hd.Count; len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; itm = hd[keysLong[i]]; if (String.Compare(itm.ToString(), valuesLong[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong item", i)); } } // [] get Item() on long dictionary with Intl strings // Intl strings // len = valuesLong.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; } cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; itm = hd[intlValues[i + len]]; if (string.Compare(itm.ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong item", i)); } } // [] get Item() on short dictionary with Intl strings // len = valuesShort.Length; hd.Clear(); cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; itm = hd[intlValues[i + len]]; if (String.Compare(itm.ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned wrong item", i)); } } // // [] Case sensitivity - hashtable // len = valuesLong.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0) { Assert.False(true, string.Format("Error, returned wrong item", i)); } if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) == 0) { Assert.False(true, string.Format("Error, returned lowercase when added uppercase", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key itm = hd[keysLong[i].ToLower()]; if (itm != null) { Assert.False(true, string.Format("Error, returned non-null for lowercase key", i)); } } // // [] Case sensitivity - list // len = valuesShort.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0) { Assert.False(true, string.Format("Error, returned wrong item", i)); } if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) == 0) { Assert.False(true, string.Format("Error, returned lowercase when added uppercase", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default for (int i = 0; i < len; i++) { // lowercase key itm = hd[keysLong[i].ToLower()]; if (itm != null) { Assert.False(true, string.Format("Error, returned non-null for lowercase key", i)); } } // // [] get Item() in case-insensitive HD - list // hd = new HybridDictionary(true); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0) { Assert.False(true, string.Format("Error, returned wrong item for uppercase key", i)); } itm = hd[keysLong[i].ToLower()]; if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0) { Assert.False(true, string.Format("Error, returned wrong item - for lowercase key", i)); } } // [] get Item() in case-insensitive HD - hashtable // hd = new HybridDictionary(true); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0) { Assert.False(true, string.Format("Error, returned wrong item for uppercase key", i)); } itm = hd[keysLong[i].ToLower()]; if (String.Compare(itm.ToString(), valuesLong[i].ToLower()) != 0) { Assert.False(true, string.Format("Error, returned wrong item for lowercase key", i)); } } // // [] get Item(null) on filled HD - list // hd = new HybridDictionary(); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } Assert.Throws<ArgumentNullException>(() => { itm = hd[null]; }); // [] get Item(null) on filled HD - hashtable // hd = new HybridDictionary(); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } Assert.Throws<ArgumentNullException>(() => { itm = hd[null]; }); // // [] get Item(cpecial_object) on filled HD - list // hd = new HybridDictionary(); hd.Clear(); len = 2; ArrayList[] b = new ArrayList[len]; Hashtable[] lbl = new Hashtable[len]; for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { itm = hd[lbl[i]]; if (!itm.Equals(b[i])) { Assert.False(true, string.Format("Error, returned wrong special object")); } } // [] get Item(cpecial_object) on filled HD - hashtable // hd = new HybridDictionary(); hd.Clear(); len = 40; b = new ArrayList[len]; lbl = new Hashtable[len]; for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { itm = hd[lbl[i]]; if (!itm.Equals(b[i])) { Assert.False(true, string.Format("Error, returned wrong special object")); } } }
public void Test01() { IntlStrings intl; NameValueCollection nvc; // 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 itm; // item // initialize IntStrings intl = new IntlStrings(); // [] NameValueCollection is constructed as expected //----------------------------------------------------------------- nvc = new NameValueCollection(); // [] get Item() on empty collection // Assert.Throws<ArgumentOutOfRangeException>(() => { itm = nvc[-1]; }); Assert.Throws<ArgumentOutOfRangeException>(() => { itm = nvc[0]; }); // [] get Item() on collection filled with simple strings // cnt = nvc.Count; int len = values.Length; for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } if (nvc.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, values.Length)); } for (int i = 0; i < len; i++) { if (String.Compare(nvc[i], values[i]) != 0) { Assert.False(true, string.Format("Error, returned: \"{1}\", expected \"{2}\"", i, nvc[i], values[i])); } } // // Intl strings // [] get Item() on collection filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLowerInvariant() == intlValues[i].ToUpperInvariant()) caseInsensitive = true; } nvc.Clear(); for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); } if (nvc.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, len)); } for (int i = 0; i < len; i++) { // if (String.Compare(nvc[i], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc[i], 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(); } nvc.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { nvc.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } // for (int i = 0; i < len; i++) { // if (String.Compare(nvc[i], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned \"{1}\" instead of \"{2}\"", i, nvc[i], intlValues[i])); } if (!caseInsensitive && String.Compare(nvc[i], intlValuesLower[i]) == 0) { Assert.False(true, string.Format("Error, returned lowercase when added uppercase", i)); } } // [] get Item() on filled collection - multiple items with the same key // nvc.Clear(); len = values.Length; string k = "keykey"; string k1 = "hm1"; string exp = ""; string exp1 = ""; for (int i = 0; i < len; i++) { nvc.Add(k, "Value" + i); nvc.Add(k1, "iTem" + i); if (i < len - 1) { exp += "Value" + i + ","; exp1 += "iTem" + i + ","; } else { exp += "Value" + i; exp1 += "iTem" + i; } } if (nvc.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", nvc.Count, 2)); } if (String.Compare(nvc[0], exp) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[0], exp)); } if (String.Compare(nvc[1], exp1) != 0) { Assert.False(true, string.Format("Error, returned \"{0}\" instead of \"{1}\"", nvc[1], exp1)); } // // [] Item(-1) // cnt = nvc.Count; Assert.Throws<ArgumentOutOfRangeException>(() => { itm = nvc[-1]; }); // // [] Item(count) // if (nvc.Count < 1) { for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } } cnt = nvc.Count; Assert.Throws<ArgumentOutOfRangeException>(() => { itm = nvc[cnt]; }); // // [] Item(count+1) // if (nvc.Count < 1) { for (int i = 0; i < len; i++) { nvc.Add(keys[i], values[i]); } } cnt = nvc.Count; Assert.Throws<ArgumentOutOfRangeException>(() => { itm = nvc[cnt + 1]; }); // // [] Item(null) // Item(null) - calls other overloaded version of Get - Get(string) - no exception // itm = nvc[null]; if (itm != null) { Assert.False(true, "Error, returned non-null "); } }
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" }; string itm; // returned Item // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] get Item() on empty dictionary // if (sd.Count > 0) sd.Clear(); for (int i = 0; i < keys.Length; i++) { itm = sd[keys[i]]; if (itm != null) { Assert.False(true, string.Format("Error, returned non-null for {0} key", i)); } } // [] get Item() on filled dictionary // int len = values.Length; sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } for (int i = 0; i < len; i++) { if (String.Compare(sd[keys[i]], values[i]) != 0) { Assert.False(true, string.Format("Error, returned {1} instead of {2}", i, sd[keys[i]], values[i])); } } // // [] get Item on dictionary with identical values // sd.Clear(); string intlStr = intl.GetRandomString(MAX_LEN); sd.Add("keykey1", intlStr); // 1st duplicate for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } sd.Add("keykey2", intlStr); // 2nd duplicate if (sd.Count != len + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len + 2)); } // get Item // if (String.Compare(sd["keykey1"], intlStr) != 0) { Assert.False(true, string.Format("Error, returned {1} instead of {2}", sd["keykey1"], intlStr)); } if (String.Compare(sd["keykey2"], intlStr) != 0) { Assert.False(true, string.Format("Error, returned {1} instead of {2}", sd["keykey2"], intlStr)); } // // Intl strings // [] get Item() on dictionary filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // // will use first half of array as values and second half as keys // sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } // get item for (int i = 0; i < len; i++) { if (String.Compare(sd[intlValues[i + len]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned {1} instead of {2}", i, sd[intlValues[i + len]], intlValues[i])); } } // // [] Case sensitivity: keys are always lowercased // sd.Clear(); string[] intlValuesUpper = new string[len]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToLowerInvariant(); } // array of uppercase keys for (int i = 0; i < len; i++) { intlValuesUpper[i] = intlValues[i + len].ToUpperInvariant(); } sd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); // adding lowercase strings } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } // get Item for (int i = 0; i < len; i++) { if (String.Compare(sd[intlValuesUpper[i]], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned {1} instead of {2}", i, sd[intlValuesUpper[i]], intlValues[i])); } } // // [] Invalid parameter // Assert.Throws<ArgumentNullException>(() => { itm = sd[null]; }); }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int cnt = 0; // Count Object itm; // Item // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); // [] set Item() on empty dictionary // cnt = hd.Count; try { hd[null] = valuesShort[0]; Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } cnt = hd.Count; hd["some_string"] = valuesShort[0]; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add Item(some_string)")); } itm = hd["some_string"]; if (String.Compare(itm.ToString(), valuesShort[0]) != 0) { Assert.False(true, string.Format("Error, added wrong Item(some_string)")); } cnt = hd.Count; Hashtable l = new Hashtable(); ArrayList bb = new ArrayList(); hd[l] = bb; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add Item(some_object)")); } itm = hd[l]; if (!itm.Equals(bb)) { Assert.False(true, string.Format("Error, returned wrong Item(some_object)")); } // [] set Item() on short dictionary with simple strings // hd.Clear(); cnt = hd.Count; int len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; hd[keysShort[i]] = valuesLong[0]; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added item instead of setting", i)); } itm = hd[keysShort[i]]; if (String.Compare(itm.ToString(), valuesLong[0]) != 0) { Assert.False(true, string.Format("Error, failed to set item", i)); } } // should add non-existing item cnt = hd.Count; hd[keysLong[0]] = valuesLong[0]; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, didn't add non-existing item")); } itm = hd[keysLong[0]]; if (String.Compare(itm.ToString(), valuesLong[0]) != 0) { Assert.False(true, string.Format("Error, failed to set item")); } // [] set Item() on long dictionary with simple strings // hd.Clear(); cnt = hd.Count; len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } // for (int i = 0; i < len; i++) { cnt = hd.Count; hd[keysLong[i]] = valuesShort[0]; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added item instead of setting", i)); } itm = hd[keysLong[i]]; if (String.Compare(itm.ToString(), valuesShort[0]) != 0) { Assert.False(true, string.Format("Error, failed to set item", i)); } } // should add non-existing item cnt = hd.Count; hd[keysShort[0]] = valuesShort[0]; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, didn't add non-existing item")); } itm = hd[keysShort[0]]; if (String.Compare(itm.ToString(), valuesShort[0]) != 0) { Assert.False(true, string.Format("Error, failed to set item")); } // // [] set Item() on long dictionary with Intl strings // Intl strings // len = valuesLong.Length; string[] intlValues = new string[len * 2 + 1]; // fill array with unique strings // for (int i = 0; i < len * 2 + 1; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } string toSet = intlValues[len * 2]; // string to set cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; hd[intlValues[i + len]] = toSet; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added item instead of setting", i)); } itm = hd[intlValues[i + len]]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, failed to set item", i)); } } // [] set Item() on short dictionary with Intl strings // len = valuesShort.Length; hd.Clear(); cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } for (int i = 0; i < len; i++) { // cnt = hd.Count; hd[intlValues[i + len]] = toSet; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added item instead of setting", i)); } itm = hd[intlValues[i + len]]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, returned wrong item", i)); } } // // Case sensitivity // [] Case sensitivity - hashtable // len = valuesLong.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key hd[keysLong[i].ToUpper()] = toSet; itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, failed to set", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default - should add lowercase key for (int i = 0; i < len; i++) { // lowercase key cnt = hd.Count; hd[keysLong[i].ToLower()] = toSet; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add when setting", i)); } itm = hd[keysLong[i].ToLower()]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, failed to set item", i)); } itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0) { Assert.False(true, string.Format("Error, failed to preserve item", i)); } } // // [] Case sensitivity - list len = valuesShort.Length; hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // for (int i = 0; i < len; i++) { // uppercase key hd[keysLong[i].ToUpper()] = toSet; itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, failed to set", i)); } } hd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToUpper(), valuesLong[i].ToUpper()); // adding uppercase strings } // LD is case-sensitive by default - should add lowercase key for (int i = 0; i < len; i++) { // lowercase key cnt = hd.Count; hd[keysLong[i].ToLower()] = toSet; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add when setting", i)); } itm = hd[keysLong[i].ToLower()]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, failed to set item", i)); } itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), valuesLong[i].ToUpper()) != 0) { Assert.False(true, string.Format("Error, failed to preserve item", i)); } } // [] set Item() on case-insensitive HD - list // hd = new HybridDictionary(true); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { hd[keysLong[i].ToUpper()] = toSet; itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, failed to set via uppercase key", i)); } hd[keysLong[i].ToLower()] = valuesLong[0]; itm = hd[keysLong[i].ToLower()]; if (String.Compare(itm.ToString(), valuesLong[0]) != 0) { Assert.False(true, string.Format("Error, failed to set via lowercase key", i)); } } // [] set Item() on case-insensitive HD - hashtable // hd = new HybridDictionary(true); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i].ToLower(), valuesLong[i].ToLower()); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { hd[keysLong[i].ToUpper()] = toSet; itm = hd[keysLong[i].ToUpper()]; if (String.Compare(itm.ToString(), toSet) != 0) { Assert.False(true, string.Format("Error, failed to set via uppercase key", i)); } hd[keysLong[i].ToLower()] = valuesLong[0]; itm = hd[keysLong[i].ToLower()]; if (String.Compare(itm.ToString(), valuesLong[0]) != 0) { Assert.False(true, string.Format("Error, failed to set via lowercase key", i)); } } // // [] set Item(null) on filled HD - list // hd = new HybridDictionary(); len = valuesShort.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } try { hd[null] = toSet; Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // [] set Item(null) on filled HD - hashtable // hd = new HybridDictionary(); len = valuesLong.Length; hd.Clear(); for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } try { hd[null] = toSet; Assert.False(true, string.Format("Error, no exception")); } catch (ArgumentNullException) { } catch (Exception e) { Assert.False(true, string.Format("Error, unexpected exception: {0}", e.ToString())); } // [] set Item(special_object) on filled HD - list // hd = new HybridDictionary(); hd.Clear(); len = 2; ArrayList[] b = new ArrayList[len]; Hashtable[] lbl = new Hashtable[len]; SortedList cb = new SortedList(); for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd[lbl[i]] = cb; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added special object instead of setting")); } itm = hd[lbl[i]]; if (!itm.Equals(cb)) { Assert.False(true, string.Format("Error, failed to set special object")); } } cnt = hd.Count; hd[cb] = lbl[0]; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add non-existing special object")); } itm = hd[cb]; if (!itm.Equals(lbl[0])) { Assert.False(true, string.Format("Error, failed to set special object")); } // [] set Item(special_object) on filled HD - hashtable // hd = new HybridDictionary(); hd.Clear(); len = 40; b = new ArrayList[len]; lbl = new Hashtable[len]; for (int i = 0; i < len; i++) { lbl[i] = new Hashtable(); b[i] = new ArrayList(); hd.Add(lbl[i], b[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } for (int i = 0; i < len; i++) { cnt = hd.Count; hd[lbl[i]] = cb; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added special object instead of setting")); } itm = hd[lbl[i]]; if (!itm.Equals(cb)) { Assert.False(true, string.Format("Error, failed to set special object")); } } cnt = hd.Count; hd[cb] = lbl[0]; if (hd.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add non-existing special object")); } itm = hd[cb]; if (!itm.Equals(lbl[0])) { Assert.False(true, string.Format("Error, failed to set special object")); } // [] set Item() to null on filled HD - list // hd = new HybridDictionary(); hd.Clear(); len = valuesShort.Length; for (int i = 0; i < len; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } cnt = hd.Count; hd[keysShort[0]] = null; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added entry instead of setting")); } itm = hd[keysShort[0]]; if (itm != null) { Assert.False(true, string.Format("Error, failed to set to null")); } // [] set Item() to null on filled HD - hashtable // hd.Clear(); len = valuesLong.Length; for (int i = 0; i < len; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, len)); } cnt = hd.Count; hd[keysLong[0]] = null; if (hd.Count != cnt) { Assert.False(true, string.Format("Error, added entry instead of setting")); } itm = hd[keysLong[0]]; if (itm != null) { Assert.False(true, string.Format("Error, failed to set to null")); } }
public void Test01() { IntlStrings intl; StringCollection sc; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] StringCollection is constructed as expected //----------------------------------------------------------------- sc = new StringCollection(); // [] AddRange() of simple strings // cnt = sc.Count; sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } for (int i = 0; i < values.Length; i++) { // verify that collection contains all added items // if (!sc.Contains(values[i])) { Assert.False(true, string.Format("Error, collection doesn't contain new item", i)); } } // // Intl strings // [] AddRange() of Intl strings // string[] intlValues = new string[values.Length]; // fill array with unique strings // for (int i = 0; i < values.Length; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // AddRange // cnt = sc.Count; sc.AddRange(intlValues); if (sc.Count != (cnt + intlValues.Length)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, cnt + intlValues.Length)); } for (int i = 0; i < intlValues.Length; i++) { // verify that collection contains all newly added items // if (!sc.Contains(intlValues[i])) { Assert.False(true, string.Format("Error, collection doesn't contain new item", i)); } } // [] AddRange() - empty range // cnt = sc.Count; string[] empty = { }; sc.AddRange(empty); if (sc.Count != cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, cnt)); } // [] AddRange() - null // cnt = sc.Count; Assert.Throws<ArgumentNullException>(() => { sc.AddRange((string[])null); }); if (sc.Count != cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, cnt)); } }
public void Test01() { IntlStrings intl; StringCollection sc; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // initialize IntStrings intl = new IntlStrings(); // [] StringCollection is constructed as expected //----------------------------------------------------------------- sc = new StringCollection(); // [] Insert into empty collection // for (int i = 0; i < values.Length; i++) { if (sc.Count > 0) sc.Clear(); sc.Insert(0, values[i]); if (sc.Count != 1) { Assert.False(true, string.Format("Error, Count {1} instead of 1", i, sc.Count)); } if (!sc.Contains(values[i])) { Assert.False(true, string.Format("Error, doesn't contain just inserted item", i)); } } // // [] Insert into filled collection sc.Clear(); sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } // string to insert string val = intl.GetRandomString(MAX_LEN); sc.Insert(0, val); if (sc.Count != values.Length + 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", sc.Count, values.Length + 1)); } if (sc.IndexOf(val) != 0) { Assert.False(true, string.Format("Error, IndexOf returned {0} instead of {1}", sc.IndexOf(val), 0)); } // check that all init items were moved for (int i = 0; i < values.Length; i++) { if (sc.IndexOf(values[i]) != i + 1) { Assert.False(true, string.Format("Error, IndexOf returned {1} instead of {2}", i, sc.IndexOf(values[i]), i + 1)); } } sc.Clear(); sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } sc.Insert(values.Length, val); if (sc.Count != values.Length + 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", sc.Count, values.Length + 1)); } if (sc.IndexOf(val) != values.Length) { Assert.False(true, string.Format("Error, IndexOf returned {0} instead of {1}", sc.IndexOf(val), values.Length)); } // check that all init items were moved for (int i = 0; i < values.Length; i++) { if (sc.IndexOf(values[i]) != i) { Assert.False(true, string.Format("Error, IndexOf returned {1} instead of {2}", i, sc.IndexOf(values[i]), i)); } } sc.Clear(); sc.AddRange(values); if (sc.Count != values.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length)); } sc.Insert(values.Length / 2, val); if (sc.Count != values.Length + 1) { Assert.False(true, string.Format("Error, Count returned {0} instead of {1}", sc.Count, values.Length + 1)); } if (sc.IndexOf(val) != values.Length / 2) { Assert.False(true, string.Format("Error, IndexOf returned {0} instead of {1}", sc.IndexOf(val), values.Length / 2)); } // check that all init items were moved for (int i = 0; i < values.Length; i++) { int expected = i; if (i >= values.Length / 2) expected = i + 1; if (sc.IndexOf(values[i]) != expected) { Assert.False(true, string.Format("Error, IndexOf returned {1} instead of {2}", i, sc.IndexOf(values[i]), expected)); } } // // [] Invalid parameter // Assert.Throws<ArgumentOutOfRangeException>(() => { sc.Insert(-1, val); }); Assert.Throws<ArgumentOutOfRangeException>(() => { sc.Insert(sc.Count + 1, val); }); Assert.Throws<ArgumentOutOfRangeException>(() => { sc.Insert(sc.Count + 2, val); }); }
public void Test01() { IntlStrings intl; HybridDictionary hd; const int BIG_LENGTH = 100; // simple string values string[] valuesShort = { "", " ", "$%^#", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keysShort = { Int32.MaxValue.ToString(), " ", System.DateTime.Today.ToString(), "", "$%^#" }; string[] valuesLong = new string[BIG_LENGTH]; string[] keysLong = new string[BIG_LENGTH]; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); for (int i = 0; i < BIG_LENGTH; i++) { valuesLong[i] = "Item" + i; keysLong[i] = "keY" + i; } // [] HybridDictionary is constructed as expected //----------------------------------------------------------------- hd = new HybridDictionary(); cnt = hd.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after default ctor", hd.Count, 0)); } // [] Clear() on empty dictionary // hd.Clear(); cnt = hd.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0)); } cnt = hd.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = hd.Values.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0)); } // [] Add simple strings and Clear() // cnt = hd.Count; for (int i = 0; i < valuesShort.Length; i++) { hd.Add(keysShort[i], valuesShort[i]); } if (hd.Count != valuesShort.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesShort.Length)); } hd.Clear(); cnt = hd.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0)); } cnt = hd.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = hd.Values.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0)); } // // [] Add Intl strings and Clear() // int len = valuesShort.Length; string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // Add items // cnt = hd.Count; for (int i = 0; i < len; i++) { hd.Add(intlValues[i + len], intlValues[i]); } if (hd.Count != (cnt + len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, cnt + len)); } hd.Clear(); cnt = hd.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0)); } cnt = hd.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = hd.Values.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0)); } // // [] Add many simple strings and Clear() // hd.Clear(); for (int i = 0; i < valuesLong.Length; i++) { hd.Add(keysLong[i], valuesLong[i]); } if (hd.Count != valuesLong.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", hd.Count, valuesLong.Length)); } hd.Clear(); cnt = hd.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, count is {0} instead of {1} after Clear()", hd.Count, 0)); } cnt = hd.Keys.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Keys.Count is {0} instead of {1} after Clear()", cnt, 0)); } cnt = hd.Values.Count; if (cnt != 0) { Assert.False(true, string.Format("Error, Values.Count is {0} instead of {1} after Clear()", cnt, 0)); } // Clear should clear underlying collection's items hd = new HybridDictionary(); for (int i = 0; i < 100; i++) hd.Add("key_" + i, "val_" + i); ICollection icol1 = hd.Keys; ICollection icol2 = hd.Values; hd.Clear(); if (icol1.Count != 0) { Assert.False(true, string.Format("Error, icol1.Count wrong, expected {0} got {1}", 0, icol1.Count)); } if (icol2.Count != 0) { Assert.False(true, string.Format("Error, icol2.Count wrong, expected {0} got {1}", 0, icol2.Count)); } }
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" }; Array arr; ICollection ks; // Keys collection int ind; // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] get Keys on empty dictionary // if (sd.Count > 0) sd.Clear(); if (sd.Keys.Count != 0) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", sd.Keys.Count)); } // // [] get Keys on filled dictionary // int len = values.Length; sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } ks = sd.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(string), len); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key. Search result: {2}", i, keys[i], ind)); } } // // [] get Keys on dictionary with identical values // sd.Clear(); string intlStr = intl.GetRandomString(MAX_LEN); sd.Add("keykey1", intlStr); // 1st duplicate for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } sd.Add("keykey2", intlStr); // 2nd duplicate if (sd.Count != len + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len + 2)); } // get Keys // ks = sd.Keys; if (ks.Count != sd.Count) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(string), len + 2); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, keys[i])); } } ind = Array.IndexOf(arr, "keykey1"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain {0} key", "keykey1")); } ind = Array.IndexOf(arr, "keykey2"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{0}\" key", "keykey2")); } // // Intl strings // [] get Keys on dictionary filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // // will use first half of array as values and second half as keys // sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } ks = sd.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(string), len); ks.CopyTo(arr, 0); for (int i = 0; i < arr.Length; i++) { ind = Array.IndexOf(arr, intlValues[i + len].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, intlValues[i + len])); } } // // Case sensitivity: keys are always lowercased - not doing it // [] Change dictionary and check Keys // sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } ks = sd.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } sd.Remove(keys[0]); if (sd.Count != len - 1) { Assert.False(true, string.Format("Error, didn't remove element")); } if (ks.Count != len - 1) { Assert.False(true, string.Format("Error, Keys were not updated after removal")); } arr = Array.CreateInstance(typeof(string), sd.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0].ToLowerInvariant()); if (ind >= 0) { Assert.False(true, string.Format("Error, Keys still contains removed key " + ind)); } sd.Add(keys[0], "new item"); if (sd.Count != len) { Assert.False(true, string.Format("Error, didn't add element")); } if (ks.Count != len) { Assert.False(true, string.Format("Error, Keys were not updated after addition")); } arr = Array.CreateInstance(typeof(string), sd.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain added key ")); } }
public void Test01() { IntlStrings intl; StringCollection sc; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; int cnt = 0; // Count int ind = 0; // Index // initialize IntStrings intl = new IntlStrings(); // [] StringCollection is constructed as expected //----------------------------------------------------------------- sc = new StringCollection(); // [] Add() simple strings // for (int i = 0; i < values.Length; i++) { cnt = sc.Count; sc.Add(values[i]); if (sc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, sc.Count, cnt + 1)); } // verify that collection contains newly added item // if (!sc.Contains(values[i])) { Assert.False(true, string.Format("Error, collection doesn't contain new item", i)); } // verify that item was added at the end // ind = sc.IndexOf(values[i]); if (ind != sc.Count - 1) { Assert.False(true, string.Format("Error, returned index {1} instead of {2}", i, ind, sc.Count - 1)); } // access the item // if (ind != -1) { if (String.Compare(sc[ind], values[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, sc[ind], values[i])); } } } // // Intl strings // [] Add() Intl strings // string[] intlValues = new string[values.Length]; // fill array with unique strings // for (int i = 0; i < values.Length; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } for (int i = 0; i < intlValues.Length; i++) { cnt = sc.Count; sc.Add(intlValues[i]); if (sc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, sc.Count, cnt + 1)); } // verify that collection contains newly added item // if (!sc.Contains(intlValues[i])) { Assert.False(true, string.Format("Error, collection doesn't contain new item", i)); } // verify that item was added at the end // ind = sc.IndexOf(intlValues[i]); if (ind != sc.Count - 1) { Assert.False(true, string.Format("Error, returned index {1} instead of {2}", i, ind, sc.Count - 1)); } // access the item // if (ind != -1) { if (String.Compare(sc[ind], intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, sc[ind], intlValues[i])); } } } // // add very long string // [] Add() very long string // cnt = sc.Count; string intlStr = intlValues[0]; while (intlStr.Length < 10000) intlStr += intlStr; sc.Add(intlStr); if (sc.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", sc.Count, cnt + 1)); } // verify that collection contains newly added item // if (!sc.Contains(intlStr)) { Assert.False(true, string.Format("Error, collection doesn't contain new item")); } // verify that item was added at the end // ind = sc.IndexOf(intlStr); if (ind != sc.Count - 1) { Assert.False(true, string.Format("Error, returned index {1} instead of {2}", ind, sc.Count - 1)); } // access the item // if (ind != -1) { if (String.Compare(sc[ind], intlStr) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", sc[ind], intlStr)); } } }