📄 multidictionarybasetests.cs
字号:
++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));
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 })));
}
// Test IDictionary<TKey,ICollection<TValue>> implementation
InterfaceTests.TestMultiDictionaryGeneric<TKey,TValue>(dict, keys, values, nonKey, nonValue, true, null, null);
}
// Check the contents of a ReadOnly Multi-Dictionary non-destructively. Keys and Values must be in order.
internal static void CheckOrderedReadOnlyMultiDictionaryContents<TKey, TValue>(ReadOnlyMultiDictionaryBase<TKey, TValue> dict, TKey[] keys, TValue[][] values, TKey nonKey, TValue nonValue, string name, 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));
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 })));
}
// Test IDictionary<TKey,IEnumerable<TValue>> implementation
InterfaceTests.TestReadOnlyMultiDictionaryGeneric<TKey, TValue>(dict, keys, values, nonKey, nonValue, true, name, null, null);
}
private ReadWriteTestMultiDictionary<string, int> CreateTestReadWriteDictionary()
{
string[] s_array = new string[] { "Eric", "Clapton", "Rules", "The", "World" };
int[][] i_array = new int[][] { new int[] { 1, 9, 11 },
new int[] { 6, 10},
new int[] { 4},
new int[] { 1, 2, 3, 4, 5, 6},
new int[] { 8}};
List<string> s_list = new List<string>(s_array);
List<List<int>> i_list = new List<List<int>>();
foreach (int[] arr in i_array) {
i_list.Add(new List<int>(arr));
}
return new ReadWriteTestMultiDictionary<string, int>(s_list, i_list);
}
private ReadOnlyTestMultiDictionary<string, int> CreateTestReadOnlyDictionary()
{
string[] s_array = new string[] { "Eric", "Clapton", "Rules", "The", "World" };
int[][] i_array = new int[][] { new int[] { 1, 9, 11 },
new int[] { 6, 10},
new int[] { 4},
new int[] { 1, 2, 3, 4, 5, 6},
new int[] { 8}};
List<string> s_list = new List<string>(s_array);
List<List<int>> i_list = new List<List<int>>();
foreach (int[] arr in i_array) {
i_list.Add(new List<int>(arr));
}
return new ReadOnlyTestMultiDictionary<string, int>(s_list, i_list);
}
[Test]
public void ReadWriteDictionary()
{
string[] s_array = new string[] { "Eric", "Clapton", "Rules", "The", "World" };
int[][] i_array = new int[][] { new int[] { 1, 9, 11 },
new int[] { 6, 10},
new int[] { 4},
new int[] { 1, 2, 3, 4, 5, 6},
new int[] { 8}};
ReadWriteTestMultiDictionary<string, int> dict = CreateTestReadWriteDictionary();
CheckOrderedMultiDictionaryContents(dict, s_array, i_array, "foo", 113, null, null);
InterfaceTests.TestReadWriteMultiDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", 113, true, "ReadWriteTestMultiDictionary", null, null);
InterfaceTests.TestReadWriteMultiDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", 113, false, "ReadWriteTestMultiDictionary", null, null);
}
[Test]
public void ReadOnlyDictionary()
{
string[] s_array = new string[] { "Eric", "Clapton", "Rules", "The", "World" };
int[][] i_array = new int[][] { new int[] { 1, 9, 11 },
new int[] { 6, 10},
new int[] { 4},
new int[] { 1, 2, 3, 4, 5, 6},
new int[] { 8}};
ReadOnlyTestMultiDictionary<string, int> dict = CreateTestReadOnlyDictionary();
CheckOrderedReadOnlyMultiDictionaryContents(dict, s_array, i_array, "foo", 113, "ReadOnlyTestMultiDictionary", null, null);
InterfaceTests.TestReadOnlyMultiDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", 113, true, "ReadOnlyTestMultiDictionary", null, null);
InterfaceTests.TestReadOnlyMultiDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", 113, false, "ReadOnlyTestMultiDictionary", null, null);
}
[Test]
public void Add()
{
ReadWriteTestMultiDictionary<string, int> dict = CreateTestReadWriteDictionary();
dict.Add(new KeyValuePair<string,ICollection<int>>("Rules", new int[] {9, -8, 18}));
dict.Add(new KeyValuePair<string, ICollection<int>>("World", new OrderedBag<int>(new int[] { })));
dict.Add(new KeyValuePair<string, ICollection<int>>("Bizzle", new List<int>(new int[] { 3, 2, 1 })));
dict.Add(new KeyValuePair<string, ICollection<int>>("Dazzle", new BigList<int>(new int[] { })));
dict.Add("Eric", 16);
dict.Add("The", 11);
dict.Add("The", 22);
dict.Add("Fizzle", 1);
dict.Add("Fizzle", 11);
dict.Add("Gizzle", -7);
dict.Add("Bizzle", 8);
string[] s_array = new string[] { "Eric", "Clapton", "Rules", "The", "World", "Bizzle", "Fizzle", "Gizzle" };
int[][] i_array = new int[][] {
new int[] { 1, 9, 11, 16 },
new int[] { 6, 10},
new int[] { 4, 9, -8, 18},
new int[] { 1, 2, 3, 4, 5, 6, 11, 22},
new int[] { 8},
new int[] {3, 2, 1, 8},
new int[] {1, 11},
new int[] {-7}};
CheckOrderedMultiDictionaryContents(dict, s_array, i_array, "foo", 113, null, null);
}
[Test]
public void AddMany()
{
ReadWriteTestMultiDictionary<string, int> dict = CreateTestReadWriteDictionary();
dict.AddMany("Rules", AlgorithmsTests.EnumerableFromArray(new int[] { 9, -8, 18 }));
dict.AddMany("World", AlgorithmsTests.EnumerableFromArray(new int[] { }));
dict.AddMany("Bizzle", AlgorithmsTests.EnumerableFromArray(new int[] { 3, 2, 1 }));
dict.AddMany("Dazzle", AlgorithmsTests.EnumerableFromArray(new int[] { }));
string[] s_array = new string[] { "Eric", "Clapton", "Rules", "The", "World", "Bizzle" };
int[][] i_array = new int[][] {
new int[] { 1, 9, 11 },
new int[] { 6, 10},
new int[] { 4, 9, -8, 18},
new int[] { 1, 2, 3, 4, 5, 6},
new int[] { 8},
new int[] {3, 2, 1}};
CheckOrderedMultiDictionaryContents(dict, s_array, i_array, "foo", 113, null, null);
}
[Test]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -