📄 orderedmultidictionarytests.cs
字号:
"zippy", "pinhead", null, null);
}
[Test]
public void CustomComparison()
{
Comparison<string> reverseFirstLetter = delegate(string x, string y) {
if (x[0] < y[0])
return 1;
else if (x[0] > y[0])
return -1;
else
return 0;
};
OrderedMultiDictionary<string,string> dict1 = new OrderedMultiDictionary<string,string>(false, reverseFirstLetter);
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.TestEnumerableElements(dict1.KeyValuePairs, new KeyValuePair<string,string>[] {
new KeyValuePair<string,string>("queztel", "hello"),
new KeyValuePair<string,string>("hi", "aaa"),
new KeyValuePair<string,string>("hello", "AAA"),
new KeyValuePair<string,string>("alpha", "omega"),
new KeyValuePair<string,string>("alzabar", "oz")});
InterfaceTests.TestEnumerableElements(dict1.Keys, new string[] { "queztel", "hi", "alpha" });
OrderedMultiDictionary<string, string> dict2 = new OrderedMultiDictionary<string, string>(false, StringComparer.InvariantCultureIgnoreCase.Compare, reverseFirstLetter);
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.TestEnumerableElements(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, ExpectedException(typeof(InvalidOperationException))]
public void NotComparable1()
{
OrderedMultiDictionary<OrderedDictionaryTests.UncomparableClass1, string> dict1 = new OrderedMultiDictionary<OrderedDictionaryTests.UncomparableClass1, string>(false);
}
[Test, ExpectedException(typeof(InvalidOperationException))]
public void NotComparable2()
{
OrderedMultiDictionary<string, OrderedDictionaryTests.UncomparableClass2> dict2 = new OrderedMultiDictionary<string, OrderedDictionaryTests.UncomparableClass2>(true);
}
[Test]
public void Clone()
{
Comparison<string> reverseFirstLetter = delegate(string x, string y) {
if (x[0] < y[0])
return 1;
else if (x[0] > y[0])
return -1;
else
return 0;
};
OrderedMultiDictionary<string, string> dict1 = new OrderedMultiDictionary<string, string>(false, StringComparer.InvariantCultureIgnoreCase.Compare, reverseFirstLetter);
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");
OrderedMultiDictionary<string, string> dict2 = dict1.Clone();
Assert.IsTrue(dict1 != dict2);
dict2.Add("qubert", "hoover");
dict2.Remove("queztel");
dict2.Add("hello", "banana");
InterfaceTests.TestEnumerableElements(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.TestEnumerableElements(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 = ((OrderedMultiDictionary<string, string>)((ICloneable)dict1).Clone());
Assert.IsTrue(dict1 != dict2);
dict2.Add("qubert", "hoover");
dict2.Remove("queztel");
dict2.Add("hello", "banana");
InterfaceTests.TestEnumerableElements(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")});
OrderedMultiDictionary<string, int> dict4 = new OrderedMultiDictionary<string, int>(true);
OrderedMultiDictionary<string, int> dict5;
dict5 = dict4.Clone();
Assert.IsFalse(dict4 == dict5);
Assert.IsTrue(dict4.Count == 0 && dict5.Count == 0);
dict4.Add("hello", 1);
Assert.IsTrue(dict4.Count == 1 && dict5.Count == 0);
dict5.Add("hi", 7);
dict4.Clear();
Assert.IsTrue(dict4.Count == 0 && dict5.Count == 1);
}
void CompareClones<K, V>(OrderedMultiDictionary<K, V> d1, OrderedMultiDictionary<K, V> d2)
{
IEnumerator<KeyValuePair<K, V>> e1 = d1.KeyValuePairs.GetEnumerator();
IEnumerator<KeyValuePair<K, V>> e2 = d2.KeyValuePairs.GetEnumerator();
// Check that the dictionaries are equal, but not reference equals (e.g., have been cloned).
while (e1.MoveNext()) {
e2.MoveNext();
if (e1.Current.Key == null)
Assert.IsNull(e2.Current.Key);
else {
Assert.IsTrue(e1.Current.Key.Equals(e2.Current.Key));
Assert.IsFalse(object.ReferenceEquals(e1.Current.Key, e2.Current.Key));
}
if (e1.Current.Value == null)
Assert.IsNull(e2.Current.Value);
else {
Assert.IsTrue(e1.Current.Value.Equals(e2.Current.Value));
Assert.IsFalse(object.ReferenceEquals(e1.Current.Value, e2.Current.Value));
}
}
}
[Test]
public void CloneContents()
{
Comparison<MyInt> myIntComparison =
delegate(MyInt v1, MyInt v2) {
if (v1 == null)
return (v2 == null) ? 0 : -1;
else if (v2 == null)
return 1;
else
return v2.value.CompareTo(v1.value);
};
OrderedMultiDictionary<int, MyInt> dict1 = new OrderedMultiDictionary<int, MyInt>(true,
delegate(int v1, int v2) { return - v2.CompareTo(v1); },
myIntComparison);
dict1.Add(4, new MyInt(143));
dict1.Add(7, new MyInt(2));
dict1.Add(11, new MyInt(9));
dict1.Add(7, new MyInt(119));
dict1.Add(18, null);
dict1.Add(4, new MyInt(16));
dict1.Add(7, null);
dict1.Add(7, new MyInt(119));
OrderedMultiDictionary<int, MyInt> dict2 = dict1.CloneContents();
CompareClones(dict1, dict2);
OrderedMultiDictionary<MyInt, int> dict3 = new OrderedMultiDictionary<MyInt, int>(false, myIntComparison);
dict3.Add(new MyInt(4), 143);
dict3.Add(new MyInt(7), 2);
dict3.Add(new MyInt(11), 9);
dict3.Add(new MyInt(7), 119);
dict3.Add(new MyInt(18), 0);
dict3.Add(new MyInt(4), 16);
dict3.Add(null, 11);
dict3.Add(new MyInt(7), 119);
OrderedMultiDictionary<MyInt, int> dict4 = dict3.CloneContents();
CompareClones(dict3, dict4);
Comparison<UtilTests.CloneableStruct> comparison = delegate(UtilTests.CloneableStruct s1, UtilTests.CloneableStruct s2) {
return s1.value.CompareTo(s2.value);
};
OrderedMultiDictionary<UtilTests.CloneableStruct, UtilTests.CloneableStruct> dict5 = new OrderedMultiDictionary<UtilTests.CloneableStruct, UtilTests.CloneableStruct>(true, comparison, comparison);
dict5.Add(new UtilTests.CloneableStruct(7) , new UtilTests.CloneableStruct(-14));
dict5.Add(new UtilTests.CloneableStruct(16) , new UtilTests.CloneableStruct(13));
dict5.Add(new UtilTests.CloneableStruct(7) , new UtilTests.CloneableStruct(-14));
dict5.Add(new UtilTests.CloneableStruct(7) , new UtilTests.CloneableStruct(31415));
dict5.Add(new UtilTests.CloneableStruct(1111) , new UtilTests.CloneableStruct(0));
OrderedMultiDictionary<UtilTests.CloneableStruct, UtilTests.CloneableStruct> dict6 = dict5.CloneContents();
IEnumerator<KeyValuePair<UtilTests.CloneableStruct, UtilTests.CloneableStruct>> e1 = dict5.KeyValuePairs.GetEnumerator();
IEnumerator<KeyValuePair<UtilTests.CloneableStruct, UtilTests.CloneableStruct>> e2 = dict6.KeyValuePairs.GetEnumerator();
Assert.IsTrue(dict5.Count == dict6.Count);
// Check that the dictionaries are equal, but not identical (e.g., have been cloned).
while (e1.MoveNext()) {
e2.MoveNext();
Assert.IsTrue(e1.Current.Key.Equals(e2.Current.Key));
Assert.IsFalse(e1.Current.Key.Identical(e2.Current.Key));
Assert.IsTrue(e1.Current.Value.Equals(e2.Current.Value));
Assert.IsFalse(e1.Current.Value.Identical(e2.Current.Value));
}
}
class NotCloneable: IComparable<NotCloneable>
{
public int CompareTo(NotCloneable other)
{
return 0;
}
public bool Equals(NotCloneable other)
{
return true;
}
}
[Test, ExpectedException(typeof(InvalidOperationException))]
public void CantCloneContents()
{
OrderedMultiDictionary<int, NotCloneable> dict1 = new OrderedMultiDictionary<int, NotCloneable>(true);
dict1[4] = new NotCloneable[] { new NotCloneable() };
dict1[5] = new NotCloneable[] { new NotCloneable(), new NotCloneable() };
OrderedMultiDictionary<int, NotCloneable> dict2 = dict1.CloneContents();
}
[Test]
public void FailFastEnumerator()
{
OrderedMultiDictionary<string, int> dict1 = new OrderedMultiDictionary<string, int>(true);
dict1.Add("foo", 12);
dict1.Add("foo", 15);
dict1.Add("foo", 3);
dict1.Add("foo", 12);
dict1.Add("bar", 1);
dict1.Add("bar", 17);
int iter = 0;
try {
foreach (KeyValuePair<string, int> pair in dict1.KeyValuePairs) {
if (pair.Key == "foo")
dict1.Replace("bar", 19);
++iter;
}
}
catch (Exception e) {
Assert.IsTrue(e is InvalidOperationException);
Assert.AreEqual(3, iter);
}
iter = 0;
try {
foreach (string key in dict1.Keys) {
if (key == "foo")
dict1.Add("grump", 117);
++iter;
}
}
catch (Exception e) {
Assert.IsTrue(e is InvalidOperationException);
Assert.AreEqual(2, iter);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -