📄 multidictionarytests.cs
字号:
dict.Add(7, "foo");
ICollection<string> vals = dict.Values;
string[] expected = {
"bar", "baz", "BAZ", "FOO", "foo", "FOo", "bAZ", "Foo", "foo", "Gizzle", "hello"};
InterfaceTests.TestReadonlyCollectionGeneric<string>(vals, expected, false, null);
Assert.IsTrue(vals.Contains("gizzle"));
Assert.IsTrue(vals.Contains("FOO"));
Assert.IsTrue(vals.Contains("fOO"));
Assert.IsTrue(vals.Contains("hello"));
Assert.IsTrue(vals.Contains("bar"));
Assert.IsTrue(vals.Contains("BAR"));
Assert.IsFalse(vals.Contains("qatar"));
}
[Test]
public void KeyValuesCollection1()
{
MultiDictionary<string, string> dict = new MultiDictionary<string, string>(false, StringComparer.InvariantCultureIgnoreCase, StringComparer.InvariantCultureIgnoreCase);
dict.Add("7A", "Gizzle");
dict.Add("4a", "foo");
dict.Add("6A", "Foo");
dict.Add("3a", "FOO");
dict.Add("3A", "baz");
dict.Add("3a", "bar");
dict.Add("4a", "FOo");
dict.Add("3A", "BAZ");
dict.Add("5a", "bAZ");
dict.Add("7a", "hello");
dict.Add("7A", "foo");
ICollection<KeyValuePair<string, string>> pairs = dict.KeyValuePairs;
string[] expectedKeys = {
"3a", "3a", "3a", "4a", "5a", "6A", "7A", "7A", "7A"};
string[] expectedVals = {
"bar", "BAZ", "FOO", "FOo", "bAZ", "Foo", "foo", "Gizzle", "hello"};
KeyValuePair<string, string>[] expectedPairs = new KeyValuePair<string, string>[expectedKeys.Length];
for (int i = 0; i < expectedKeys.Length; ++i)
expectedPairs[i] = new KeyValuePair<string, string>(expectedKeys[i], expectedVals[i]);
InterfaceTests.TestReadonlyCollectionGeneric<KeyValuePair<string, string>>(pairs, expectedPairs, false, null);
Assert.IsTrue(pairs.Contains(new KeyValuePair<string, string>("3a", "baz")));
Assert.IsTrue(pairs.Contains(new KeyValuePair<string, string>("3A", "baz")));
Assert.IsTrue(pairs.Contains(new KeyValuePair<string, string>("6a", "foo")));
Assert.IsFalse(pairs.Contains(new KeyValuePair<string, string>("7A", "bar")));
}
[Test]
public void KeyValuesCollection2()
{
MultiDictionary<string, string> dict = new MultiDictionary<string, string>(true, StringComparer.InvariantCultureIgnoreCase, StringComparer.InvariantCultureIgnoreCase);
dict.Add("7A", "Gizzle");
dict.Add("4A", "foo");
dict.Add("6A", "Foo");
dict.Add("3a", "FOO");
dict.Add("3A", "baz");
dict.Add("3a", "bar");
dict.Add("4a", "FOo");
dict.Add("3a", "BAZ");
dict.Add("5a", "bAZ");
dict.Add("7a", "hello");
dict.Add("7A", "foo");
ICollection<KeyValuePair<string, string>> pairs = dict.KeyValuePairs;
string[] expectedKeys = {
"3a", "3a", "3a", "3a", "4A", "4A", "5a", "6A", "7A", "7A", "7A"};
string[] expectedVals = {
"bar", "baz", "BAZ", "FOO", "foo", "FOo", "bAZ", "Foo", "foo", "Gizzle", "hello"};
KeyValuePair<string, string>[] expectedPairs = new KeyValuePair<string, string>[expectedKeys.Length];
for (int i = 0; i < expectedKeys.Length; ++i)
expectedPairs[i] = new KeyValuePair<string, string>(expectedKeys[i], expectedVals[i]);
InterfaceTests.TestReadonlyCollectionGeneric<KeyValuePair<string, string>>(pairs, expectedPairs, false, null);
Assert.IsTrue(pairs.Contains(new KeyValuePair<string, string>("3a", "baz")));
Assert.IsTrue(pairs.Contains(new KeyValuePair<string, string>("3A", "baz")));
Assert.IsTrue(pairs.Contains(new KeyValuePair<string, string>("6a", "foo")));
Assert.IsFalse(pairs.Contains(new KeyValuePair<string, string>("7A", "bar")));
}
[Test]
public void Indexer()
{
MultiDictionary<string, string> dict1 = new MultiDictionary<string, string>(true, StringComparer.InvariantCultureIgnoreCase, StringComparer.InvariantCultureIgnoreCase);
dict1.Add("foo", "BAR");
dict1.Add(null, "hello");
dict1.Add("Hello", "sailor");
dict1.Add(null, "hi");
dict1.Add("foo", "bar");
dict1.Add("HELLO", null);
dict1.Add("foo", "a");
dict1.Add("Foo", "A");
dict1.Add("trail", "mix");
InterfaceTests.TestEnumerableElementsAnyOrder(dict1[null], new string[] { "hello", "hi" });
InterfaceTests.TestEnumerableElementsAnyOrder(dict1["hELLo"], new string[] { null, "sailor" });
InterfaceTests.TestEnumerableElementsAnyOrder(dict1["foo"], new string[] { "a", "A", "BAR", "bar" });
InterfaceTests.TestEnumerableElementsAnyOrder(dict1["trail"], new string[] { "mix" });
InterfaceTests.TestEnumerableElementsAnyOrder(dict1["nothing"], new string[] { });
}
[Test]
public void GetValueCount()
{
MultiDictionary<string, string> dict1 = new MultiDictionary<string, string>(true, StringComparer.InvariantCultureIgnoreCase, StringComparer.InvariantCultureIgnoreCase);
dict1.Add("foo", "BAR");
dict1.Add(null, "hello");
dict1.Add("Hello", "sailor");
dict1.Add(null, "hi");
dict1.Add("foo", "bar");
dict1.Add("HELLO", null);
dict1.Add("foo", "a");
dict1.Add("Foo", "A");
dict1.Add("hello", null);
dict1.Add("trail", "mix");
Assert.AreEqual(2, dict1[null].Count);
Assert.AreEqual(3, dict1["hELLo"].Count);
Assert.AreEqual(4, dict1["foo"].Count);
Assert.AreEqual(1, dict1["trail"].Count);
Assert.AreEqual(0, dict1["nothing"].Count);
dict1 = new MultiDictionary<string, string>(false, StringComparer.InvariantCultureIgnoreCase, StringComparer.InvariantCultureIgnoreCase);
dict1.Add("foo", "BAR");
dict1.Add(null, "hello");
dict1.Add("Hello", "sailor");
dict1.Add(null, "hi");
dict1.Add("foo", "bar");
dict1.Add("HELLO", null);
dict1.Add("foo", "a");
dict1.Add("Foo", "A");
dict1.Add("hello", null);
dict1.Add("trail", "mix");
Assert.AreEqual(2, dict1[null].Count);
Assert.AreEqual(2, dict1["hELLo"].Count);
Assert.AreEqual(2, dict1["foo"].Count);
Assert.AreEqual(1, dict1["trail"].Count);
Assert.AreEqual(0, dict1["nothing"].Count);
}
[Test]
public void IMultiDictionaryInterface()
{
MultiDictionary<string, string> dict1 = new MultiDictionary<string, string>(true);
dict1.Add("foo", "bar");
dict1.Add(null, "hello");
dict1.Add("hello", "sailor");
dict1.Add(null, "hi");
dict1.Add("foo", "bar");
dict1.Add("hello", null);
dict1.Add("foo", "a");
dict1.Add("foo", "a");
dict1.Add("hello", null);
dict1.Add("trail", "mix");
CheckMultiDictionaryContents<string, string>(dict1,
new string[] { null, "foo", "hello", "trail" },
new string[][] { new string[] { "hello", "hi" }, new string[] { "a", "a", "bar", "bar" }, new string[] { null, null, "sailor" }, new string[] { "mix" } },
"zippy", "pinhead", null, null);
dict1 = new MultiDictionary<string, string>(false);
dict1.Add("foo", "bar");
dict1.Add(null, "hello");
dict1.Add("hello", "sailor");
dict1.Add(null, "hi");
dict1.Add("foo", "bar");
dict1.Add("hello", null);
dict1.Add("foo", "a");
dict1.Add("foo", "a");
dict1.Add("hello", null);
dict1.Add("trail", "mix");
CheckMultiDictionaryContents<string, string>(dict1,
new string[] { null, "foo", "hello", "trail" },
new string[][] { new string[] { "hello", "hi" }, new string[] { "a", "bar" }, new string[] { null, "sailor" }, new string[] { "mix" } },
"zippy", "pinhead", null, null);
}
[Test]
public void CustomComparer()
{
IEqualityComparer<string> firstLetterComparer = new FirstLetterComparer();
MultiDictionary<string, string> dict1 = new MultiDictionary<string, string>(false, firstLetterComparer);
dict1.Add("hello", "AAA");
dict1.Add("hi", "aaa");
dict1.Add("qubert", "hello");
dict1.Add("queztel", "hello");
dict1.Add("alpha", "omega");
dict1.Add("alzabar", "oz");
InterfaceTests.TestEnumerableElementsAnyOrder(dict1.KeyValuePairs, new KeyValuePair<string, string>[] {
new KeyValuePair<string,string>("qubert", "hello"),
new KeyValuePair<string,string>("hello", "aaa"),
new KeyValuePair<string,string>("hello", "AAA"),
new KeyValuePair<string,string>("alpha", "omega"),
new KeyValuePair<string,string>("alpha", "oz")});
InterfaceTests.TestEnumerableElementsAnyOrder(dict1.Keys, new string[] { "qubert", "hello", "alpha" });
MultiDictionary<string, string> dict2 = new MultiDictionary<string, string>(false, StringComparer.InvariantCultureIgnoreCase, firstLetterComparer);
dict2.Add("qubert", "dinosaur");
dict2.Add("Hello", "AAA");
dict2.Add("Hi", "aaa");
dict2.Add("qubert", "hello");
dict2.Add("queztel", "hello");
dict2.Add("alpha", "omega");
dict2.Add("Alpha", "oz");
dict2.Add("qubert", "hippy");
InterfaceTests.TestEnumerableElementsAnyOrder(dict2.KeyValuePairs, new KeyValuePair<string, string>[] {
new KeyValuePair<string,string>("alpha", "oz"),
new KeyValuePair<string,string>("Hello", "AAA"),
new KeyValuePair<string,string>("Hi", "aaa"),
new KeyValuePair<string,string>("qubert", "hippy"),
new KeyValuePair<string,string>("qubert", "dinosaur"),
new KeyValuePair<string,string>("queztel", "hello")});
}
[Test]
public void NotComparable1()
{
// This should work -- all types are comparable in a hash way via object.Equals and object.GetHashCode.
MultiDictionary<OrderedDictionaryTests.UncomparableClass1, string> dict1 = new MultiDictionary<OrderedDictionaryTests.UncomparableClass1, string>(false);
}
[Test]
public void NotComparable2()
{
// This should work -- all types are comparable in a hash way via object.Equals and object.GetHashCode.
MultiDictionary<string, OrderedDictionaryTests.UncomparableClass2> dict2 = new MultiDictionary<string, OrderedDictionaryTests.UncomparableClass2>(true);
}
class FirstLetterComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
if (x == null)
return y == null;
else if (x.Length == 0)
return (y != null && y.Length == 0);
else {
if (y == null || y.Length == 0)
return false;
else
return x[0] == y[0];
}
}
public int GetHashCode(string obj)
{
if (obj == null)
return 0x12383;
else if (obj.Length == 0)
return 17;
else
return obj[0].GetHashCode();
}
}
[Test]
public void Clone()
{
IEqualityComparer<string> firstLetterComparer = new FirstLetterComparer();
MultiDictionary<string, string> dict1 = new MultiDictionary<string, string>(false, StringComparer.InvariantCultureIgnoreCase, firstLetterComparer);
dict1.Add("qubert", "dinosaur");
dict1.Add("Hello", "AAA");
dict1.Add("Hi", "aaa");
dict1.Add("Qubert", "hello");
dict1.Add("queztel", "hello");
dict1.Add("Alpha", "omega");
dict1.Add("alpha", "oz");
dict1.Add("qubert", "hippy");
MultiDictionary<string, string> dict2 = dict1.Clone();
Assert.IsTrue(dict1 != dict2);
dict2.Add("qubert", "hoover");
dict2.Remove("queztel");
dict2.Add("hello", "banana");
InterfaceTests.TestEnumerableElementsAnyOrder(dict1.KeyValuePairs, new KeyValuePair<string, string>[] {
new KeyValuePair<string,string>("Alpha", "oz"),
new KeyValuePair<string,string>("Hello", "AAA"),
new KeyValuePair<string,string>("Hi", "aaa"),
new KeyValuePair<string,string>("qubert", "hippy"),
new KeyValuePair<string,string>("qubert", "dinosaur"),
new KeyValuePair<string,string>("queztel", "hello")});
InterfaceTests.TestEnumerableElementsAnyOrder(dict2.KeyValuePairs, new KeyValuePair<string, string>[] {
new KeyValuePair<string,string>("Alpha", "oz"),
new KeyValuePair<string,string>("Hello", "banana"),
new KeyValuePair<string,string>("Hello", "AAA"),
new KeyValuePair<string,string>("Hi", "aaa"),
new KeyValuePair<string,string>("qubert", "hoover"),
new KeyValuePair<string,string>("qubert", "dinosaur")});
dict2 = ((MultiDictionary<string, string>)((ICloneable)dict1).Clone());
Assert.IsTrue(dict1 != dict2);
dict2.Add("qubert", "hoover");
dict2.Remove("queztel");
dict2.Add("hello", "banana");
InterfaceTests.TestEnumerableElementsAnyOrder(dict2.KeyValuePairs, new KeyValuePair<string, string>[] {
new KeyValuePair<string,string>("Alpha", "oz"),
new KeyValuePair<string,string>("Hello", "banana"),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -