📄 ordereddictionarytests.cs
字号:
Assert.Fail("should only enumerate two items");
}
++i;
}
dict1.Remove(null);
Assert.IsFalse(dict1.ContainsKey(null));
}
/// <summary>
/// Check that enumerators are enumerating the correct keys and values.
/// </summary>
/// <param name="inorder">An IEnumerable enumerating in order</param>
/// <param name="reversed">An IEnumerable enumerating reversed</param>
/// <param name="keys">Expected keys in order</param>
/// <param name="values">Expected values in order</param>
void CheckEnumeration<TKey,TValue> (IEnumerable<KeyValuePair<TKey,TValue>> inorder, IEnumerable<KeyValuePair<TKey,TValue>> reversed,
TKey[] keys, TValue[] values)
{
int i = 0;
foreach (KeyValuePair<TKey,TValue> pair in inorder)
{
Assert.AreEqual(keys[i], pair.Key);
Assert.AreEqual(values[i], pair.Value);
++i;
}
Assert.AreEqual(i, keys.Length);
i = 0;
foreach (KeyValuePair<TKey,TValue> pair in reversed)
{
Assert.AreEqual(keys[keys.Length - i - 1], pair.Key);
Assert.AreEqual(values[values.Length - i - 1], pair.Value);
++i;
}
Assert.AreEqual(i, keys.Length);
}
[Test]
public void Enumerate()
{
OrderedDictionary<string,int> dict1 = new OrderedDictionary<string,int>();
dict1["foo"] = 23;
dict1["a"] = 11;
dict1["b"] = 119;
dict1[""] = 981;
dict1["r4"] = 9;
dict1["b"] = 7;
dict1["hello"] = 198;
dict1["q"] = 199;
dict1["ww"] = -8;
dict1["ww"] = -9;
dict1["p"] = 1234;
CheckEnumeration(dict1, dict1.Reversed(),
new string[] {"", "a", "b", "foo", "hello", "p", "q", "r4", "ww" },
new int[] {981, 11, 7, 23, 198, 1234, 199, 9, -9});
}
/// <summary>
/// Test that cloning works.
/// </summary>
[Test]
public void Clone()
{
OrderedDictionary<string,int> dict1 = new OrderedDictionary<string,int>();
OrderedDictionary<string,int> dict2, dict3;
dict1["foo"] = 23;
dict1["a"] = 11;
dict1["b"] = 119;
dict1[""] = 981;
dict1["r4"] = 9;
dict1["b"] = 7;
dict1["hello"] = 198;
dict1["q"] = 199;
dict1["ww"] = -8;
dict1["ww"] = -9;
dict1["p"] = 1234;
dict2 = dict1.Clone();
dict3 = (OrderedDictionary<string, int>)((ICloneable)dict1).Clone();
Assert.IsFalse(dict2 == dict1);
// Modify dict1, make sure dict2 doesn't change.
dict1.Remove("a");
dict1.Remove("b");
dict1.Remove("");
dict1["qqq"] = 1;
CheckEnumeration(dict2, dict2.Reversed(), new string[] { "", "a", "b", "foo", "hello", "p", "q", "r4", "ww" }, new int[] { 981, 11, 7, 23, 198, 1234, 199, 9, -9 });
Assert.AreEqual(981, dict2[""]);
CheckEnumeration(dict3, dict3.Reversed(), new string[] { "", "a", "b", "foo", "hello", "p", "q", "r4", "ww" }, new int[] { 981, 11, 7, 23, 198, 1234, 199, 9, -9 });
Assert.AreEqual(981, dict3[""]);
OrderedDictionary<string, int> dict4 = new OrderedDictionary<string, int>();
OrderedDictionary<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);
}
// Simple class for testing cloning.
public class MyInt : ICloneable
{
public int value;
public MyInt(int value)
{
this.value = value;
}
public object Clone()
{
return new MyInt(value);
}
public override bool Equals(object obj)
{
return (obj is MyInt && ((MyInt)obj).value == value);
}
public override int GetHashCode()
{
return value.GetHashCode();
}
public override string ToString()
{
return value.ToString();
}
}
void CompareClones<K, V>(OrderedDictionary<K, V> d1, OrderedDictionary<K, V> d2)
{
IEnumerator<KeyValuePair<K, V>> e1 = d1.GetEnumerator();
IEnumerator<KeyValuePair<K, V>> e2 = d2.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()
{
OrderedDictionary<int, MyInt> dict1 = new OrderedDictionary<int, MyInt>();
dict1[4] = new MyInt(143);
dict1[7] = new MyInt(2);
dict1[11] = new MyInt(9);
dict1[18] = null;
dict1[3] = new MyInt(14);
dict1[1] = new MyInt(111);
OrderedDictionary<int, MyInt> dict2 = dict1.CloneContents();
CompareClones(dict1, dict2);
OrderedDictionary<MyInt, int> dict3 = new OrderedDictionary<MyInt, int>(
delegate(MyInt v1, MyInt v2) { return v2.value.CompareTo(v1.value);});
dict3[new MyInt(7)] = 144;
dict3[new MyInt(16)] = 13;
dict3[new MyInt(-6)] = -14;
dict3[new MyInt(0)] = 31415;
dict3[new MyInt(1111)] = 0;
OrderedDictionary<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);
};
OrderedDictionary<UtilTests.CloneableStruct,UtilTests.CloneableStruct> dict5 = new OrderedDictionary<UtilTests.CloneableStruct, UtilTests.CloneableStruct>(comparison);
dict5[new UtilTests.CloneableStruct(7)] = new UtilTests.CloneableStruct(144);
dict5[new UtilTests.CloneableStruct(16)] = new UtilTests.CloneableStruct(13);
dict5[new UtilTests.CloneableStruct(-6)] = new UtilTests.CloneableStruct(-14);
dict5[new UtilTests.CloneableStruct(0)] = new UtilTests.CloneableStruct(31415);
dict5[new UtilTests.CloneableStruct(1111)] = new UtilTests.CloneableStruct(0);
OrderedDictionary<UtilTests.CloneableStruct,UtilTests.CloneableStruct> dict6 = dict5.CloneContents();
IEnumerator<KeyValuePair<UtilTests.CloneableStruct, UtilTests.CloneableStruct>> e1 = dict5.GetEnumerator();
IEnumerator<KeyValuePair<UtilTests.CloneableStruct, UtilTests.CloneableStruct>> e2 = dict6.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 { }
[Test, ExpectedException(typeof(InvalidOperationException))]
public void CantCloneContents()
{
OrderedDictionary<int, NotCloneable> dict1 = new OrderedDictionary<int,NotCloneable>();
dict1[4] = new NotCloneable();
dict1[5] = new NotCloneable();
OrderedDictionary<int, NotCloneable> dict2 = dict1.CloneContents();
}
// Check that a View has the correct keys and values in it.
private void CheckView<TKey, TValue>(OrderedDictionary<TKey,TValue>.View view, TKey[] keys, TValue[] values, TKey nonKey)
{
CheckEnumeration(view, view.Reversed(), keys, values);
// Check Count.
Assert.AreEqual(keys.Length, view.Count);
Assert.AreEqual(values.Length, view.Count);
// Check CopyTo
KeyValuePair<TKey, TValue>[] pairArray = new KeyValuePair<TKey, TValue>[view.Count];
view.CopyTo(pairArray, 0);
for (int i = 0; i < view.Count; ++i) {
Assert.AreEqual(keys[i], pairArray[i].Key);
Assert.AreEqual(values[i], pairArray[i].Value);
}
InterfaceTests.TestDictionary<TKey, TValue>((IDictionary)view, keys, values, nonKey, true);
InterfaceTests.TestDictionaryGeneric<TKey, TValue>((IDictionary<TKey, TValue>)view, keys, values, nonKey, true, null, null);
}
[Test]
public void Range()
{
OrderedDictionary<string, int> dict1 = new OrderedDictionary<string, int>();
dict1["foo"] = 23;
dict1["a"] = 11;
dict1["b"] = 119;
dict1[""] = 981;
dict1["r4"] = 9;
dict1["b"] = 7;
dict1["hello"] = 198;
dict1["q"] = 199;
dict1["ww"] = -8;
dict1["ww"] = -9;
dict1["p"] = 1234;
CheckView(dict1.Range("a", true, "z", false),
new string[] { "a", "b", "foo", "hello", "p", "q", "r4", "ww" },
new int[] { 11, 7, 23, 198, 1234, 199, 9, -9 },
"");
CheckView(dict1.Range("b", true, "q", false),
new string[] { "b", "foo", "hello", "p" },
new int[] { 7, 23, 198, 1234},
"q");
CheckView(dict1.Range("b", false, "q", false),
new string[] { "foo", "hello", "p" },
new int[] { 23, 198, 1234 },
"q");
CheckView(dict1.Range("b", true, "q", true),
new string[] { "b", "foo", "hello", "p", "q" },
new int[] { 7, 23, 198, 1234, 199 },
"ww");
CheckView(dict1.Range("b", false, "q", true),
new string[] { "foo", "hello", "p", "q" },
new int[] { 23, 198, 1234, 199 },
"b");
CheckView(dict1.Range("", true, "z", false),
new string[] { "", "a", "b", "foo", "hello", "p", "q", "r4", "ww" },
new int[] { 981, 11, 7, 23, 198, 1234, 199, 9, -9 },
"FOO");
CheckView(dict1.Range("", false, "z", true),
new string[] { "a", "b", "foo", "hello", "p", "q", "r4", "ww" },
new int[] { 11, 7, 23, 198, 1234, 199, 9, -9 },
"FOO");
CheckView(dict1.Range("f", true, "i", false),
new string[] { "foo", "hello" },
new int[] { 23, 198 },
"b");
CheckView(dict1.Range("b", true, "b", false),
new string[] { }, new int[] { },
"b");
CheckView(dict1.Range("p", true, "q1", false),
new string[] { "p", "q" }, new int[] { 1234, 199 },
"q1");
CheckView(dict1.Range("p", true, "q", false),
new string[] { "p" }, new int[] { 1234},
"q");
CheckView(dict1.Range("p", false, "q", true),
new string[] { "q" }, new int[] { 199 },
"p");
CheckView(dict1.Range("p", false, "q", false),
new string[] { }, new int[] { },
"q");
CheckView(dict1.Range("p1", true, "q1", false),
new string[] { "q" }, new int[] { 199 },
"p");
CheckView(dict1.Range("p1", true, "q", false),
new string[] { }, new int[] { },
"q");
CheckView(dict1.Range("p1", false, "q", true),
new string[] { "q" }, new int[] { 199 },
"p");
CheckView(dict1.Range("z", true, "f", false),
new string[0], new int[0],
"p");
CheckView(dict1.Range("g", true, "h", false),
new string[0], new int[0],
"h");
CheckView(dict1.Range("a", true, "z", false).Reversed(),
new string[] { "ww", "r4", "q", "p", "hello", "foo", "b", "a" },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -