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; 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; 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; 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}\" insteead 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; 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; 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[] 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; 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; 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; 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(); // [] set Item() on empty dictionary // cnt = ld.Count; Assert.Throws <ArgumentNullException>(() => { ld[null] = "item"; }); cnt = ld.Count; ld["some_string"] = "item"; if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add item")); } if (String.Compare(ld["some_string"].ToString(), "item") != 0) { Assert.False(true, string.Format("Error, failed to set item")); } cnt = ld.Count; Hashtable lbl = new Hashtable(); ArrayList b = new ArrayList(); ld[lbl] = b; if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add item")); } if (!ld[lbl].Equals(b)) { Assert.False(true, string.Format("Error, failed to set object-item")); } // [] set 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 != 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(keys[i])) { Assert.False(true, string.Format("Error, doesn't contain key", i)); } ld[keys[i]] = "newValue" + i; if (String.Compare(ld[keys[i]].ToString(), "newValue" + i) != 0) { Assert.False(true, string.Format("Error, failed to set value", i)); } ld[keys[i]] = b; if (!ld[keys[i]].Equals(b)) { Assert.False(true, string.Format("Error, failed to set object-value", i)); } } // // Intl strings // [] set Item() on dictionary filled with Intl strings // 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; } 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)); } ld[intlValues[i + len]] = intlValues[len * 2]; if (String.Compare(ld[intlValues[i + len]].ToString(), intlValues[len * 2]) != 0) { Assert.False(true, string.Format("Error, failed to set value", i)); } ld[intlValues[i + len]] = b; if (!ld[intlValues[i + len]].Equals(b)) { Assert.False(true, string.Format("Error, failed to set object-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)); } ld[intlValues[i + len]] = b; if (!ld[intlValues[i + len]].Equals(b)) { Assert.False(true, string.Format("Error, failed to set via uppercase key", 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 - new entries should be added for (int i = 0; i < len; i++) { // lowercase key cnt = ld.Count; ld[intlValuesLower[i + len]] = "item"; if (ld[intlValuesLower[i + len]] == null) { Assert.False(true, string.Format("Error, failed: returned non-null for lowercase key", i)); } if (!caseInsensitive && String.Compare(ld[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, failed: changed value via lowercase key", i)); } // lowercase itemshould be added to the dictionary if (String.Compare(ld[intlValuesLower[i + len]].ToString(), "item") != 0) { Assert.False(true, string.Format("Error, failed: didn't add when set via lowercase key", i)); } } // // [] set 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 { ld[kk.ToUpper() + i] = "Item" + i; if (String.Compare(ld[kk.ToUpper() + i].ToString(), "Item" + i) != 0) { Assert.False(true, string.Format("Error, failed to set value", i)); } ld[kk.ToUpper() + i] = b; if (!ld[kk.ToUpper() + i].Equals(b)) { Assert.False(true, string.Format("Error, failed to set object-value", i)); } } } // // [] set Item(null) on filled LD // 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[null] = "item"; }); // [] set Item(special_object) // ld = new ListDictionary(); ld.Clear(); b = new ArrayList(); ArrayList b1 = new ArrayList(); 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)); } ld[lbl] = b1; if (!ld[lbl].Equals(b1)) { Assert.False(true, string.Format("Error, failed to set special object")); } ld[lbl1] = b; if (!ld[lbl1].Equals(b)) { Assert.False(true, string.Format("Error, failed to set special object")); } // // [] set to null // ld = new ListDictionary(); cnt = ld.Count; ld["null_key"] = null; if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add entry")); } if (ld["null_key"] != null) { Assert.False(true, string.Format("Error, failed to add entry with null value")); } cnt = ld.Count; ld.Add("key", "value"); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, failed to add entry")); } cnt = ld.Count; ld["key"] = null; if (ld["key"] != null) { Assert.False(true, string.Format("Error, failed to set entry to 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[] 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, incorrects 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; 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(); // [] Contains() on empty dictionary // Assert.Throws <ArgumentNullException>(() => { ld.Contains(null); }); if (ld.Contains("some_string")) { Assert.False(true, string.Format("Error, empty dictionary contains some_object")); } if (ld.Contains(new Hashtable())) { Assert.False(true, string.Format("Error, empty dictionary contains some_object")); } // [] simple strings and Contains() // 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 \"{1}\"", i, keys[i])); } } // // Intl strings // [] 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; } Boolean caseInsensitive = false; for (int i = 0; i < len * 2; i++) { if (intlValues[i].Length != 0 && intlValues[i].ToLower() == intlValues[i].ToUpper()) { caseInsensitive = true; } } ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(intlValues[i + len], intlValues[i]); } if (ld.Count != (len)) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } for (int i = 0; i < len; i++) { // if (!ld.Contains(intlValues[i + len])) { Assert.False(true, string.Format("Error, doesn't contain \"{1}\"", i, intlValues[i + len])); } } // // [] Case sensitivity // by default ListDictionary is case-sensitive // 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 added uppercase \"{1}\"", i, intlValues[i + len])); } // lowercase key if (!caseInsensitive && ld.Contains(intlValuesLower[i + len])) { Assert.False(true, string.Format("Error, contains lowercase \"{1}\" - should not", i, intlValuesLower[i + len])); } } // [] different_in_casing_only keys and Contains() // ld.Clear(); string[] ks = { "Key", "kEy", "keY" }; len = ks.Length; for (int i = 0; i < len; i++) { ld.Add(ks[i], "Value" + i); } if (ld.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, len)); } if (ld.Contains("Value0")) { Assert.False(true, string.Format("Error, returned true when should not")); } for (int i = 0; i < len; i++) { if (!ld.Contains(ks[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } // // [] Contains(null) - for filled dictionary // ld.Clear(); for (int i = 0; i < len; i++) { ld.Add(keys[i], values[i]); } Assert.Throws <ArgumentNullException>(() => { ld.Contains(null); }); // [] Contains() for case-insensitive comparer // ld = new ListDictionary(new InsensitiveComparer()); ld.Clear(); len = ks.Length; ld.Add(ks[0], "Value0"); for (int i = 1; i < len; i++) { Assert.Throws <ArgumentException>(() => { ld.Add(ks[i], "Value" + i); }); } if (ld.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 1)); } if (ld.Contains("Value0")) { Assert.False(true, string.Format("Error, returned true when should not")); } for (int i = 0; i < len; i++) { if (!ld.Contains(ks[i])) { Assert.False(true, string.Format("Error, returned false when true expected", i)); } } if (!ld.Contains("KEY")) { Assert.False(true, string.Format("Error, returned false non-existing-cased key")); } // [] Contains() and objects_not_overriding_Equals // ld = new ListDictionary(); ld.Clear(); Hashtable lbl = new Hashtable(); Hashtable lbl1 = new Hashtable(); ArrayList b = new ArrayList(); ArrayList b1 = new ArrayList(); ld.Add(lbl, b); ld.Add(lbl1, b1); Assert.Throws <ArgumentException>(() => { ld.Add(lbl, "Hello"); }); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } if (!ld.Contains(lbl)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (ld.Contains(new Hashtable())) { Assert.False(true, string.Format("Error, returned true when false expected")); } // [] Contains and Special_Comparer for objects // ld = new ListDictionary(new SpecialComparer()); lbl["foo"] = "Hello"; lbl1["foo"] = "Hello"; ld.Add(lbl, b); Assert.Throws <ArgumentException>(() => { ld.Add(lbl1, b1); }); if (ld.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 1)); } if (!ld.Contains(lbl)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, returned false when true expected")); } lbl1["foo"] = "HELLO"; if (ld.Contains(lbl1)) { Assert.False(true, string.Format("Error, returned true when false expected")); } // [] Contains() and special_structs_not_overriding_Equals ld = new ListDictionary(); SpecialStruct s = new SpecialStruct(); s.Num = 1; s.Wrd = "one"; SpecialStruct s1 = new SpecialStruct(); s.Num = 1; s.Wrd = "one"; ld.Add(s, "first"); ld.Add(s1, "second"); if (ld.Count != 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, 2)); } if (!ld.Contains(s)) { Assert.False(true, string.Format("Error, returned false when true expected")); } if (!ld.Contains(s1)) { Assert.False(true, string.Format("Error, returned false when true expected")); } }
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 destination; int cnt = 0; // Count // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] Copy empty dictionary into empty array // destination = Array.CreateInstance(typeof(Object), sd.Count); Assert.Throws <ArgumentOutOfRangeException>(() => { sd.CopyTo(destination, -1); }); sd.CopyTo(destination, 0); Assert.Throws <ArgumentException>(() => { sd.CopyTo(destination, 1); }); // [] Copy empty dictionary into non-empty array // destination = Array.CreateInstance(typeof(Object), values.Length); for (int i = 0; i < values.Length; i++) { destination.SetValue(values[i], i); } sd.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.GetValue(i).ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, altered item {0} after copying empty collection", i)); } } } // [] add simple strings and CopyTo(Array, 0) // cnt = sd.Count; int len = values.Length; 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, values.Length)); } destination = Array.CreateInstance(typeof(Object), len); sd.CopyTo(destination, 0); IEnumerator en = sd.GetEnumerator(); // // order of items is the same as order of enumerator // for (int i = 0; i < len; i++) { en.MoveNext(); // verify that collection is copied correctly // DictionaryEntry curr = (DictionaryEntry)en.Current; if (String.Compare(curr.Value.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, curr.Value)); } if (String.Compare(curr.Key.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, curr.Key)); } } // // [] add simple strings and CopyTo(Array, middle_index) 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, values.Length)); } destination = Array.CreateInstance(typeof(Object), len * 2); sd.CopyTo(destination, len); en = sd.GetEnumerator(); // // order of items is the same as order of enumerator // for (int i = 0; i < len; i++) { en.MoveNext(); // verify that collection is copied correctly // DictionaryEntry curr = (DictionaryEntry)en.Current; if (String.Compare(curr.Value.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, curr.Value)); } if (String.Compare(curr.Key.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, curr.Key)); } } // // Intl strings // [] add 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].ToLowerInvariant() == intlValues[i].ToUpperInvariant()) { caseInsensitive = true; } } 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)); } destination = Array.CreateInstance(typeof(Object), len); sd.CopyTo(destination, 0); en = sd.GetEnumerator(); // // order of items is the same as order of enumerator // for (int i = 0; i < len; i++) { en.MoveNext(); // verify that collection is copied correctly // DictionaryEntry curr = (DictionaryEntry)en.Current; if (String.Compare(curr.Value.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, curr.Value)); } if (String.Compare(curr.Key.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, curr.Key)); } } // // Intl strings // [] add intl strings and CopyTo(Array, middle_index) // destination = Array.CreateInstance(typeof(Object), len * 2); sd.CopyTo(destination, len); en = sd.GetEnumerator(); // // order of items is the same as order of enumerator // for (int i = 0; i < len; i++) { en.MoveNext(); // verify that collection is copied correctly // DictionaryEntry curr = (DictionaryEntry)en.Current; if (String.Compare(curr.Value.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, curr.Value)); } if (String.Compare(curr.Key.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, curr.Key)); } } // // [] 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++) { sd.Add(intlValues[i + len], intlValues[i]); // adding uppercase strings } destination = Array.CreateInstance(typeof(Object), len); sd.CopyTo(destination, 0); en = sd.GetEnumerator(); // // order of items is the same as order of enumerator // for (int i = 0; i < len; i++) { en.MoveNext(); // verify that collection is copied correctly // DictionaryEntry curr = (DictionaryEntry)en.Current; if (String.Compare(curr.Value.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, curr.Value)); } if (!caseInsensitive && (Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Value.ToString()) != -1)) { Assert.False(true, string.Format("Error, copied lowercase string")); } if (String.Compare(curr.Key.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, curr.Key)); } if (Array.IndexOf(intlValuesLower, ((DictionaryEntry)destination.GetValue(i)).Key.ToString()) == -1) { Assert.False(true, string.Format("Error, copied uppercase key")); } } // // [] CopyTo(null, int) // destination = null; Assert.Throws <ArgumentNullException>(() => { sd.CopyTo(destination, 0); }); // // [] CopyTo(string[], -1) // cnt = sd.Count; destination = Array.CreateInstance(typeof(Object), cnt); Assert.Throws <ArgumentOutOfRangeException>(() => { sd.CopyTo(destination, -1); }); // // [] CopyTo(Array, upperBound+1) // if (sd.Count < 1) { for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } } destination = Array.CreateInstance(typeof(Object), len); Assert.Throws <ArgumentException>(() => { sd.CopyTo(destination, len); }); // // [] CopyTo(Array, upperBound+2) // Assert.Throws <ArgumentException>(() => { sd.CopyTo(destination, len + 1); }); // // [] CopyTo(Array, not_enough_space) // Assert.Throws <ArgumentException>(() => { sd.CopyTo(destination, len / 2); }); // // [] CopyTo(multidim_Array, 0) // destination = new object[len, len]; Assert.Throws <ArgumentException>(() => { sd.CopyTo(destination, 0); }); }
public void Test01() { IntlStrings intl; HybridDictionary hd; // simple string values string[] valuesShort = Test_EdgeCases(); // keys for simple string values string[] keysShort = Test_EdgeCases(); string[] valuesLong = TestValues(100); string[] keysLong = TestKeys(100); int cnt = 0; // Count Object itm; // Item // initialize IntStrings intl = new IntlStrings(); // [] 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(special_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(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++) { itm = hd[lbl[i]]; if (!itm.Equals(b[i])) { Assert.False(true, string.Format("Error, returned wrong special object")); } } }
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; 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(); // [] Set() - new simple strings // for (int i = 0; i < values.Length; i++) { cnt = nvc.Count; nvc.Set(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 // [] Set() - new 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.Set(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 ( 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.Set(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)); } } // // [] Set multiple values with the same key // nvc.Clear(); len = values.Length; string k = "keykey"; for (int i = 0; i < len; i++) { nvc.Set(k, "Value" + i); // should replace previous value if (nvc.Count != 1) { Assert.False(true, string.Format("Error, count is {0} instead of 1", nvc.Count, i)); } if (String.Compare(nvc[k], "Value" + i) != 0) { Assert.False(true, string.Format("Error, didn't replace value", i)); } } 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 != 1) { Assert.False(true, string.Format("Error, number of values at given key is {0} instead of {1}", vals.Length, 1)); } if (Array.IndexOf(vals, "Value" + (len - 1).ToString()) < 0) { Assert.False(true, string.Format("Error, value is not {0}", "Value" + (len - 1))); } // // [] Set(string, null) // k = "kk"; nvc.Remove(k); // make sure there is no such item already cnt = nvc.Count; nvc.Set(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"); } nvc.Remove(k); // make sure there is no such item already nvc.Add(k, "kItem"); cnt = nvc.Count; nvc.Set(k, null); if (nvc.Count != cnt) { Assert.False(true, string.Format("Error, count has changed: {0} instead of {1}", nvc.Count, cnt)); } if (Array.IndexOf(nvc.AllKeys, k) < 0) { Assert.False(true, "Error, collection doesn't contain key of new item"); } // verify that item at k-key was replaced with null // if (nvc[k] != null) { Assert.False(true, "Error, non-null was not replaced with null"); } // // Set item with null key - no NullReferenceException expected // [] Set(null, string) // nvc.Remove(null); cnt = nvc.Count; nvc.Set(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"); } // replace item with null key cnt = nvc.Count; nvc.Set(null, "newItem"); if (nvc.Count != cnt) { Assert.False(true, string.Format("Error, count has changed: {0} instead of {1}", nvc.Count, cnt)); } if (Array.IndexOf(nvc.AllKeys, null) < 0) { Assert.False(true, "Error, collection doesn't contain null key "); } // verify that item with null key was replaced // if (nvc[null] != "newItem") { Assert.False(true, "Error, didn't replace 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]; 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; 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; 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" }; 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; 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(); // [] Add simple strings // for (int i = 0; i < values.Length; i++) { cnt = ld.Count; ld.Add(keys[i], values[i]); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (String.Compare(ld[keys[i]].ToString(), values[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[keys[i]], values[i])); } } // // [] Add Intl strings // 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].ToLower() == intlValues[i].ToUpper()) { caseInsensitive = true; } } // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { cnt = ld.Count; ld.Add(intlValues[i + len], intlValues[i]); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (String.Compare(ld[intlValues[i + len]].ToString(), intlValues[i]) != 0) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[intlValues[i + len]], intlValues[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].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++) { cnt = ld.Count; ld.Add(intlValues[i + len], intlValues[i]); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (ld[intlValues[i + len]] == null) { Assert.False(true, string.Format("Error, returned null", i)); } else { if (!ld[intlValues[i + len]].Equals(intlValues[i])) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[intlValues[i + len]], intlValues[i])); } } // verify that dictionary doesn't contains lowercase item // if (!caseInsensitive && ld[intlValuesLower[i + len]] != null) { Assert.False(true, string.Format("Error, returned non-null", i)); } } // // [] Add multiple values with the same key // Add multiple values with the same key - ArgumentException expected // ld.Clear(); len = values.Length; string k = "keykey"; ld.Add(k, "value"); Assert.Throws <ArgumentException>(() => { ld.Add(k, "newvalue"); }); // // [] Add(string, null) // Add null value // cnt = ld.Count; k = "kk"; ld.Add(k, null); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", ld.Count, cnt + 1)); } // verify that dictionary contains null // if (ld[k] != null) { Assert.False(true, string.Format("Error, returned non-null on place of null")); } // // [] Add(null, string) // Add item with null key - ArgumentNullException expected // cnt = ld.Count; Assert.Throws <ArgumentNullException>(() => { ld.Add(null, "item"); }); // // [] Add duplicate values // ld.Clear(); for (int i = 0; i < values.Length; i++) { cnt = ld.Count; ld.Add(keys[i], "value"); if (ld.Count != cnt + 1) { Assert.False(true, string.Format("Error, count is {1} instead of {2}", i, ld.Count, cnt + 1)); } // access the item // if (!ld[keys[i]].Equals("value")) { Assert.False(true, string.Format("Error, returned item \"{1}\" instead of \"{2}\"", i, ld[keys[i]], "value")); } } // verify Keys and Values if (ld.Keys.Count != values.Length) { Assert.False(true, string.Format("Error, Keys contains {0} instead of {1}", ld.Keys.Count, values.Length)); } if (ld.Values.Count != values.Length) { Assert.False(true, string.Format("Error, Values contains {0} instead of {1}", ld.Values.Count, values.Length)); } }
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; 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; StringCollection sc; string itm; // returned value of Item // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; intl = new IntlStrings(); // [] StringCollection Item is returned as expected //----------------------------------------------------------------- sc = new StringCollection(); // exception expected // all indexes should be invalid for empty collection // // [] Invalid parameter - set Item() on empty collection // itm = intl.GetRandomString(MAX_LEN); Assert.Throws <ArgumentOutOfRangeException>(() => { sc[-1] = itm; }); Assert.Throws <ArgumentOutOfRangeException>(() => { sc[0] = itm; }); Assert.Throws <ArgumentOutOfRangeException>(() => { sc[0] = null; }); // [] set Item() on collection filled with simple strings // sc.Clear(); sc.AddRange(values); int cnt = values.Length; if (sc.Count != cnt) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, cnt)); } for (int i = 0; i < cnt; i++) { sc[i] = values[cnt - i - 1]; if (String.Compare(sc[i], values[cnt - i - 1]) != 0) { Assert.False(true, string.Format("Error, value is {1} instead of {2}", i, sc[i], values[cnt - i - 1])); } } // // Intl strings // [] set Item() on collection filled with 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; } 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; } } sc.Clear(); cnt = intlValues.Length; sc.AddRange(intlValues); if (sc.Count != intlValues.Length) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, intlValues.Length)); } for (int i = cnt; i < cnt; i++) { sc[i] = intlValues[cnt - i - 1]; if (String.Compare(sc[i], intlValues[cnt - i - 1]) != 0) { Assert.False(true, string.Format("Error, actual item is {1} instead of {2}", i, sc[i], intlValues[cnt - i - 1])); } } // // [] Case sensitivity // string intlStrUpper = intlValues[0]; intlStrUpper = intlStrUpper.ToUpper(); string intlStrLower = intlStrUpper.ToLower(); sc.Clear(); sc.AddRange(values); sc.Add(intlStrUpper); if (sc.Count != values.Length + 1) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sc.Count, values.Length + 1)); } // verify that Item returns intl string in a case sensitive way // // set simple to intl lower // sc[0] = intlStrLower; if (!caseInsensitive && (String.Compare(sc[0], intlStrUpper) == 0)) { Assert.False(true, string.Format("Error, set to uppercase when should have to lower")); } if (String.Compare(sc[0], intlStrLower) != 0) { Assert.False(true, string.Format("Error, didn't set to lower")); } // // set intlUpper to intl lower // sc[sc.Count - 1] = intlStrLower; if (!caseInsensitive && (String.Compare(sc[sc.Count - 1], intlStrUpper) == 0)) { Assert.False(true, string.Format("Error, didn't set from uppercase to lowercase ")); } if (String.Compare(sc[sc.Count - 1], intlStrLower) != 0) { Assert.False(true, string.Format("Error, didn't set to lower")); } // // set to null - it's legal - return value will be null // [] set Item() to null // if (sc.Count < 1) { sc.AddRange(values); } // // set middle item to null // int ind = sc.Count / 2; sc[ind] = null; if (sc[ind] != null) { Assert.False(true, string.Format("Error, failed to set to null")); } if (!sc.Contains(null)) { Assert.False(true, string.Format("Error, Contains() didn't return true for null")); } if (sc.IndexOf(null) != ind) { Assert.False(true, string.Format("Error, IndexOf() returned {0} instead of {1}", sc.IndexOf(null), ind)); } // // [] Invalid parameter - for filled collection // sc.Clear(); sc.AddRange(intlValues); Assert.Throws <ArgumentOutOfRangeException>(() => { sc[-1] = intlStrUpper; }); Assert.Throws <ArgumentOutOfRangeException>(() => { sc[sc.Count] = intlStrUpper; }); Assert.Throws <ArgumentOutOfRangeException>(() => { sc[sc.Count + 1] = intlStrUpper; }); Assert.Throws <ArgumentOutOfRangeException>(() => { sc[sc.Count] = null; }); }