📄 orderedmultidictionarytests.cs
字号:
iter = 0;
try {
foreach (int value in dict1["foo"]) {
if (value == 12)
dict1.Remove("grump", 117);
++iter;
}
}
catch (Exception e) {
Assert.IsTrue(e is InvalidOperationException);
Assert.AreEqual(2, iter);
}
iter = 0;
try {
foreach (string key in dict1.Keys) {
if (key == "foo")
dict1.Clear();
++iter;
}
}
catch (Exception e) {
Assert.IsTrue(e is InvalidOperationException);
Assert.AreEqual(2, iter);
}
}
[Test]
public void KeyComparerProperty()
{
IComparer<int> comparer1 = new GOddEvenComparer();
OrderedMultiDictionary<int, string> dict1 = new OrderedMultiDictionary<int, string>(false, comparer1);
Assert.AreSame(comparer1, dict1.KeyComparer);
OrderedMultiDictionary<decimal, string> dict2 = new OrderedMultiDictionary<decimal, string>(true);
Assert.AreSame(Comparer<decimal>.Default, dict2.KeyComparer);
OrderedMultiDictionary<string, string> dict3 = new OrderedMultiDictionary<string, string>(true, StringComparer.OrdinalIgnoreCase, StringComparer.CurrentCulture);
Assert.AreSame(StringComparer.OrdinalIgnoreCase, dict3.KeyComparer);
Comparison<int> comparison1 = ComparersTests.CompareOddEven;
OrderedMultiDictionary<int, string> dict4 = new OrderedMultiDictionary<int, string>(true, comparison1);
OrderedMultiDictionary<int, string> dict5 = new OrderedMultiDictionary<int, string>(false, comparison1, delegate(string x, string y) { return - x.CompareTo(y); });
Assert.AreEqual(dict4.KeyComparer, dict5.KeyComparer);
Assert.IsFalse(dict4.KeyComparer == dict5.KeyComparer);
Assert.IsFalse(object.Equals(dict4.KeyComparer, dict1.KeyComparer));
Assert.IsFalse(object.Equals(dict4.KeyComparer, Comparer<int>.Default));
Assert.IsTrue(dict4.KeyComparer.Compare(7, 6) < 0);
Assert.AreSame(dict1.KeyComparer, dict1.Clone().KeyComparer);
Assert.AreSame(dict2.KeyComparer, dict2.Clone().KeyComparer);
Assert.AreSame(dict3.KeyComparer, dict3.Clone().KeyComparer);
Assert.AreSame(dict4.KeyComparer, dict4.Clone().KeyComparer);
Assert.AreSame(dict5.KeyComparer, dict5.Clone().KeyComparer);
}
[Test]
public void ValueComparerProperty()
{
IComparer<int> comparer1 = new GOddEvenComparer();
OrderedMultiDictionary<string, int> dict1 = new OrderedMultiDictionary<string, int>(true, StringComparer.InvariantCulture, comparer1);
Assert.AreSame(comparer1, dict1.ValueComparer);
OrderedMultiDictionary<string, decimal> dict2 = new OrderedMultiDictionary<string, decimal>(false);
Assert.AreSame(Comparer<decimal>.Default, dict2.ValueComparer);
OrderedMultiDictionary<string, string> dict3 = new OrderedMultiDictionary<string, string>(true, StringComparer.InvariantCulture, StringComparer.OrdinalIgnoreCase);
Assert.AreSame(StringComparer.OrdinalIgnoreCase, dict3.ValueComparer);
Comparison<int> comparison1 = ComparersTests.CompareOddEven;
OrderedMultiDictionary<string, int> dict4 = new OrderedMultiDictionary<string, int>(true, delegate(string x, string y) { return x.CompareTo(y); }, comparison1);
OrderedMultiDictionary<string, int> dict5 = new OrderedMultiDictionary<string, int>(false, delegate(string x, string y) { return - x.CompareTo(y); }, comparison1);
Assert.AreEqual(dict4.ValueComparer, dict5.ValueComparer);
Assert.IsFalse(dict4.ValueComparer == dict5.ValueComparer);
Assert.IsFalse(object.Equals(dict4.ValueComparer, dict1.ValueComparer));
Assert.IsFalse(object.Equals(dict4.ValueComparer, Comparer<int>.Default));
Assert.IsTrue(dict4.ValueComparer.Compare(7, 6) < 0);
Assert.AreSame(dict1.ValueComparer, dict1.Clone().ValueComparer);
Assert.AreSame(dict2.ValueComparer, dict2.Clone().ValueComparer);
Assert.AreSame(dict3.ValueComparer, dict3.Clone().ValueComparer);
Assert.AreSame(dict4.ValueComparer, dict4.Clone().ValueComparer);
Assert.AreSame(dict5.ValueComparer, dict5.Clone().ValueComparer);
}
// Check the contents of a Multi-Dictionary non-destructively. Keys and Values must be in order.
internal static void CheckView<TKey, TValue>(OrderedMultiDictionary<TKey, TValue>.View dict, TKey[] keys, TValue[][] values, TKey nonKey, TValue nonValue, bool cantAddNonKey, BinaryPredicate<TKey> keyEquals, BinaryPredicate<TValue> valueEquals)
{
int iKey, iValue;
ICollection<TValue> getValues;
if (keyEquals == null)
keyEquals = delegate(TKey x, TKey y) { return object.Equals(x, y); };
if (valueEquals == null)
valueEquals = delegate(TValue x, TValue y) { return object.Equals(x, y); };
// Check Count.
Assert.AreEqual(keys.Length, dict.Count);
// Check indexer, ContainsKey, Contains, TryGetValue for each key.
for (iKey = 0; iKey < keys.Length; ++iKey) {
Assert.IsTrue(dict.ContainsKey(keys[iKey]));
Assert.IsTrue(dict.Contains(new KeyValuePair<TKey, ICollection<TValue>>(keys[iKey], values[iKey])));
bool b = ((IDictionary<TKey, ICollection<TValue>>)dict).TryGetValue(keys[iKey], out getValues);
Assert.IsTrue(b);
iValue = 0;
foreach (TValue val in getValues) {
Assert.IsTrue(valueEquals(values[iKey][iValue], val));
++iValue;
}
iValue = 0;
foreach (TValue val in values[iKey]) {
Assert.IsTrue(dict.Contains(keys[iKey], val));
++iValue;
}
iValue = 0;
foreach (TValue val in dict[keys[iKey]]) {
Assert.IsTrue(valueEquals(values[iKey][iValue], val));
++iValue;
}
Assert.IsTrue(iValue == values[iKey].Length);
}
// Check Keys collection.
iKey = 0;
foreach (TKey key in dict.Keys) {
Assert.IsTrue(keyEquals(keys[iKey], key));
++iKey;
}
Assert.IsTrue(iKey == keys.Length);
InterfaceTests.TestReadonlyCollectionGeneric<TKey>(dict.Keys, keys, true, null);
// Check Values collection
iKey = 0; iValue = 0;
int valueCount = 0;
foreach (TValue val in dict.Values) {
Assert.IsTrue(valueEquals(values[iKey][iValue], val));
++iValue;
if (iValue == values[iKey].Length) {
iValue = 0;
++iKey;
}
++valueCount;
}
Assert.IsTrue(iKey == keys.Length);
int a = 0;
TValue[] vals = new TValue[valueCount];
for (iKey = 0; iKey < keys.Length; ++iKey) {
for (iValue = 0; iValue < values[iKey].Length; ++iValue) {
vals[a++] = values[iKey][iValue];
}
}
InterfaceTests.TestReadonlyCollectionGeneric<TValue>(dict.Values, vals, true, null);
// Check KeyValuePairs collection.
iKey = 0; iValue = 0;
valueCount = 0;
foreach (KeyValuePair<TKey, TValue> pair in dict.KeyValuePairs) {
Assert.IsTrue(keyEquals(keys[iKey], pair.Key));
Assert.IsTrue(valueEquals(values[iKey][iValue], pair.Value));
++iValue;
if (iValue == values[iKey].Length) {
iValue = 0;
++iKey;
}
++valueCount;
}
Assert.IsTrue(iKey == keys.Length);
a = 0;
KeyValuePair<TKey, TValue>[] pairs = new KeyValuePair<TKey, TValue>[valueCount];
for (iKey = 0; iKey < keys.Length; ++iKey) {
for (iValue = 0; iValue < values[iKey].Length; ++iValue) {
pairs[a++] = new KeyValuePair<TKey, TValue>(keys[iKey], values[iKey][iValue]);
}
}
InterfaceTests.TestReadonlyCollectionGeneric<KeyValuePair<TKey, TValue>>(dict.KeyValuePairs, pairs, true, null);
// Tests Contains, ContainsKey, TryGetValue for wrong values.
Assert.IsFalse(dict.ContainsKey(nonKey));
Assert.IsFalse(((IDictionary<TKey, ICollection<TValue>>)dict).TryGetValue(nonKey, out getValues));
Assert.AreEqual(0, dict[nonKey].Count);
Assert.IsFalse(dict.Remove(nonKey));
for (iKey = 0; iKey < keys.Length; ++iKey) {
Assert.IsFalse(dict.Contains(keys[iKey], nonValue));
Assert.IsFalse(dict.Contains(new KeyValuePair<TKey, ICollection<TValue>>(keys[iKey], new TValue[1] { nonValue })));
}
if (cantAddNonKey) {
// Make sure Add throws exception
try {
dict[nonKey] = new TValue[1] { nonValue };
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
try {
dict.Add(nonKey, nonValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
}
// Test IDictionary<TKey,IEnumerable<TValue>> implementation
InterfaceTests.TestReadWriteMultiDictionaryGeneric<TKey, TValue>(dict, keys, values, nonKey, nonValue, true, "OrderedMultiDictionary", null, null);
}
[Test]
public void RangeFrom()
{
OrderedMultiDictionary<string, double> dict1 = new OrderedMultiDictionary<string, double>(false);
dict1.Add("foo", 3.5);
dict1.Add("foo", -1.2);
dict1.Add(null, 11.1);
dict1.Add("foo", 8.8);
dict1.Add(null, 11.1);
dict1.Add("bar", 9.8);
dict1.Add("foo", 8.8);
dict1.Add("gib", 7.1);
dict1.Add("S", -9);
dict1.Add(null, 5.5);
dict1.Add("gib", 1.1);
CheckView<string, double>(dict1.RangeFrom("bar", true),
new string[] { "bar", "foo", "gib", "S" },
new double[][] { new double[] { 9.8 }, new double[] { -1.2, 3.5, 8.8 }, new double[] { 1.1, 7.1 }, new double[] { -9 } },
null, 5.5, true, null, null);
CheckView<string, double>(dict1.RangeFrom("bar", false),
new string[] { "foo", "gib", "S" },
new double[][] { new double[] { -1.2, 3.5, 8.8 }, new double[] {1.1, 7.1 }, new double[] { -9 } },
"bar", 9.8, true, null, null);
CheckView<string, double>(dict1.RangeFrom("alpha", false),
new string[] { "bar", "foo", "gib", "S" },
new double[][] { new double[] { 9.8 }, new double[] { -1.2, 3.5, 8.8 }, new double[] { 1.1, 7.1 }, new double[] { -9 } },
"aaa", 5.5, true, null, null);
CheckView<string, double>(dict1.RangeFrom("hello", false),
new string[] { "S" },
new double[][] {new double[] { -9 } },
"foo", 3.5, true, null, null);
CheckView<string, double>(dict1.RangeFrom("S", true),
new string[] { "S" },
new double[][] { new double[] { -9 } },
"foo", 3.5, true, null, null);
CheckView<string, double>(dict1.RangeFrom("Z", true),
new string[] { },
new double[][] { },
"foo", 3.5, true, null, null);
CheckView<string, double>(dict1.RangeFrom("bar", true).Reversed(),
new string[] { "S", "gib", "foo", "bar" },
new double[][] {new double[] { -9 }, new double[] { 1.1, 7.1 }, new double[] { -1.2, 3.5, 8.8 },new double[] { 9.8 } },
"alpha", 5.5, true, null, null);
CheckView<string, double>(dict1.RangeFrom("bar", false). Reversed(),
new string[] { "S", "gib", "foo" },
new double[][] { new double[] { -9 }, new double[] { 1.1, 7.1 }, new double[] { -1.2, 3.5, 8.8 } },
"bar", 9.8, true, null, null);
}
[Test]
public void RangeTo()
{
OrderedMultiDictionary<string, double> dict1 = new OrderedMultiDictionary<string, double>(false);
dict1.Add("foo", 3.5);
dict1.Add("foo", -1.2);
dict1.Add(null, 11.1);
dict1.Add("foo", 8.8);
dict1.Add(null, 11.1);
dict1.Add("bar", 9.8);
dict1.Add("foo", 8.8);
dict1.Add("gib", 7.1);
dict1.Add("S", -9);
dict1.Add(null, 5.5);
dict1.Add("gib", 1.1);
CheckView<string, double>(dict1.RangeTo("gib", true),
new string[] { null, "bar", "foo", "gib" },
new double[][] { new double[] {5.5, 11.1}, new double[] { 9.8 }, new double[] { -1.2, 3.5, 8.8 }, new double[] { 1.1, 7.1 } },
"S", -9, true, null, null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -