// Dump the contents of a TagDictionary. static void TestTagsDump(TagDictionary d1) { Console.WriteLine("Count: " + d1.Count); Console.WriteLine("Existing contents."); foreach (string s in d1.Keys) Console.WriteLine(s + ": " + d1[s]); Console.WriteLine("Existing contents as IDictionary."); IDictionary d = d1; foreach (string s in d.Keys) Console.WriteLine(s + ": " + d[s]); }
// Test various operations on the dictionary. static bool TestTags(PowerPoint.Tags tags) { bool passed = true; TagDictionary d1 = new TagDictionary(tags, true, "|Bob_"); TestTagsDump(d1); Console.WriteLine("Test clear..."); d1.Add("test", ""); TestTagsDump(d1); d1.Clear(); if (d1.Count == 0) Console.WriteLine("Passed."); else { passed = false; Console.WriteLine("Failed."); } TestTagsDump(d1); Console.WriteLine("Testing access to missing element."); try { string s = d1["test"]; if (s == "") Console.WriteLine("Passed."); else { passed = false; Console.WriteLine("Failed."); } } catch (ArgumentException) { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing adding and accessing elements..."); // Alternate entries are keys. Must be at least two pairs. string[] strings = new string[] { "hello", "Yes!", "FOO BAR!", "BaZ", "no", "D'oh!", "Farnsworth", "Zeke" }; bool passedStrings = true; for (int i = 0; i < strings.Length; i += 2) d1.Add(strings[i], strings[i+1]); TestTagsDump(d1); for (int i = strings.Length - 2; i >= 0; i -= 2) if (d1[strings[i]] != strings[i+1]) passedStrings = false; if (passedStrings) Console.WriteLine("Passed."); else { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing removal..."); d1.Remove(strings[2]); if (d1.Contains(strings[2])) { passed = false; Console.WriteLine("Failed."); } else Console.WriteLine("Passed."); Console.WriteLine("Testing overwrite..."); d1[strings[0]] = strings[3]; if (d1[strings[0]] == strings[3]) Console.WriteLine("Passed."); else { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing null key..."); try { d1.Add(null, "five"); passed = false; Console.WriteLine("Failed."); } catch (ArgumentNullException) { Console.WriteLine("Passed."); } Console.WriteLine("Testing null value..."); try { d1.Add("five", null); passed = false; Console.WriteLine("Failed."); } catch (ArgumentNullException) { Console.WriteLine("Passed."); } Console.WriteLine("Testing empty value..."); try { d1.Add("five", ""); if (d1["five"] == "") { Console.WriteLine("Passed."); } else { passed = false; Console.WriteLine("Failed."); } } catch (ArgumentOutOfRangeException) { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing coexisting dictionaries separation..."); TagDictionary d2 = new TagDictionary(tags, true, ""); TagDictionary d3 = new TagDictionary(tags, true, "sam"); d1["test"] = "one"; d2["test"] = "two"; d3["test"] = "three"; if (d1["test"] != "one" || d2["test"] != "two" || d3["test"] != "three") { Console.WriteLine("Failed."); passed = false; } else Console.WriteLine("Passed."); Console.WriteLine("Testing coexisting dictionaries clearing..."); d1.Clear(); if (d1.Contains("test") || d2["test"] != "two" || d3["test"] != "three") { Console.WriteLine("Failed."); passed = false; } else Console.WriteLine("Passed."); Console.WriteLine("d1:"); TestTagsDump(d1); Console.WriteLine("d2:"); TestTagsDump(d2); Console.WriteLine("d3:"); TestTagsDump(d3); return passed; }