📄 interfacetests.cs
字号:
}
// Check adding via the indexer
for (int i = 0; i < keys.Length; ++i)
dict[keys[i]] = values[i];
TestCollection<DictionaryEntry>((ICollection)dict, entries, mustBeInOrder);
TestDictionary<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder);
}
/// <summary>
/// Test an generic IDictionary<K,V> that should contains the given keys and values, possibly in order.
/// </summary>
/// <typeparam name="TKey">Type of the keys</typeparam>
/// <typeparam name="TValue">Type of the values</typeparam>
/// <param name="dict">IDictionary<K,V> to test</param>
/// <param name="keys">key values for the dictionary</param>
/// <param name="values">values for the dictionary</param>
/// <param name="nonKey">A TKey that isn't in the dictionary</param>
/// <param name="mustBeInOrder">True if the entries must be in order.</param>
public static void TestDictionaryGeneric<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey[] keys, TValue[] values, TKey nonKey, bool mustBeInOrder, BinaryPredicate<TKey> keyEquals, BinaryPredicate<TValue> valueEquals)
{
bool result;
TValue val;
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 containment.
for (int i = 0; i < keys.Length; ++i) {
Assert.IsTrue(dict.ContainsKey(keys[i]));
Assert.IsTrue(valueEquals(values[i], dict[keys[i]]));
result = dict.TryGetValue(keys[i], out val);
Assert.IsTrue(result);
Assert.IsTrue(valueEquals(values[i], val));
}
Assert.IsFalse(dict.ContainsKey(nonKey));
result = dict.TryGetValue(nonKey, out val);
Assert.IsFalse(result);
Assert.AreEqual(default(TValue), val);
try {
TValue v = dict[nonKey];
Assert.Fail("Should throw.");
}
catch (Exception e) {
Assert.IsTrue(e is KeyNotFoundException);
}
// Check Keys, Values collections
TestReadonlyCollectionGeneric<TKey>(dict.Keys, keys, mustBeInOrder, null, keyEquals);
TestReadonlyCollectionGeneric<TValue>(dict.Values, values, mustBeInOrder, null, valueEquals);
}
/// <summary>
/// Test an read-only IDictionary<K,V> that should contains the given keys and values, possibly in order.
/// </summary>
/// <typeparam name="TKey">Type of the keys</typeparam>
/// <typeparam name="TValue">Type of the values</typeparam>
/// <param name="dict">IDictionary<K,V> to test</param>
/// <param name="keys">key values for the dictionary</param>
/// <param name="values">values for the dictionary</param>
/// <param name="mustBeInOrder">True if the entries must be in order.</param>
/// <param name="nonKey">A TKey that isn't in the dictionary</param>
/// <param name="name">Name of the dictionary, used in exceptions.</param>
public static void TestReadOnlyDictionaryGeneric<TKey, TValue>(IDictionary<TKey,TValue> dict, TKey[] keys, TValue[] values, TKey nonKey, bool mustBeInOrder, string name,
BinaryPredicate<TKey> keyEquals, BinaryPredicate<TValue> valueEquals)
{
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); };
KeyValuePair<TKey, TValue>[] entries = new KeyValuePair<TKey, TValue>[keys.Length];
for (int i = 0; i < keys.Length; ++i)
entries[i] = new KeyValuePair<TKey, TValue>(keys[i], values[i]);
TestDictionaryGeneric<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder, keyEquals, valueEquals);
TestReadonlyCollectionGeneric<KeyValuePair<TKey,TValue>>((ICollection<KeyValuePair<TKey,TValue>>)dict, entries, mustBeInOrder, name, KeyValueEquals(keyEquals, valueEquals));
// Check exceptions.
try {
dict.Clear();
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
if (keys.Length > 0) {
try {
dict.Add(keys[0], values[0]);
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
try {
dict.Remove(keys[0]);
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
try {
dict[keys[0]] = values[0];
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
}
}
/// <summary>
/// Test an read-write IDictionary<K,V> that should contains the given keys and values, possibly in order.
/// </summary>
/// <typeparam name="TKey">Type of the keys</typeparam>
/// <typeparam name="TValue">Type of the values</typeparam>
/// <param name="dict">IDictionary<K,V> to test</param>
/// <param name="keys">key values for the dictionary</param>
/// <param name="values">values for the dictionary</param>
/// <param name="mustBeInOrder">True if the entries must be in order.</param>
/// <param name="nonKey">A TKey that isn't in the dictionary</param>
/// <param name="name">Name of the dictionary, used in exceptions.</param>
public static void TestReadWriteDictionaryGeneric<TKey, TValue>(IDictionary<TKey,TValue> dict, TKey[] keys, TValue[] values, TKey nonKey, bool mustBeInOrder, string name,
BinaryPredicate<TKey> keyEquals, BinaryPredicate<TValue> valueEquals)
{
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); };
KeyValuePair<TKey, TValue>[] entries = new KeyValuePair<TKey, TValue>[keys.Length];
for (int i = 0; i < keys.Length; ++i)
entries[i] = new KeyValuePair<TKey, TValue>(keys[i], values[i]);
TestDictionaryGeneric<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder, keyEquals, valueEquals);
TestCollectionGeneric<KeyValuePair<TKey, TValue>>((ICollection<KeyValuePair<TKey, TValue>>)dict, entries, mustBeInOrder, KeyValueEquals(keyEquals, valueEquals));
Assert.IsFalse(dict.IsReadOnly);
// Check exceptions for adding existing elements.
for (int i = 0; i < keys.Length; ++i) {
try {
dict.Add(keys[i], values[i]);
Assert.Fail("should have thrown exception");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
}
// Check Clear.
dict.Clear();
Assert.AreEqual(0, dict.Count);
// Check Add().
for (int i = 0; i < keys.Length; ++i)
dict.Add(keys[i], values[i]);
TestDictionaryGeneric<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder, keyEquals, valueEquals);
TestCollectionGeneric<KeyValuePair<TKey, TValue>>((ICollection<KeyValuePair<TKey, TValue>>)dict, entries, mustBeInOrder, KeyValueEquals(keyEquals, valueEquals));
// Check Remove. 2nd remove should return false.
for (int i = 0; i < keys.Length; ++i) {
Assert.IsTrue(dict.Remove(keys[i]));
Assert.IsFalse(dict.Remove(keys[i]));
}
Assert.AreEqual(0, dict.Count);
// Check adding via the indexer
for (int i = 0; i < keys.Length; ++i)
dict[keys[i]] = values[i];
TestDictionaryGeneric<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder, keyEquals, valueEquals);
TestCollectionGeneric<KeyValuePair<TKey, TValue>>((ICollection<KeyValuePair<TKey, TValue>>)dict, entries, mustBeInOrder, KeyValueEquals(keyEquals, valueEquals));
}
/// <summary>
/// Test read-only IList<T> that should contain the given values, possibly in order. Does not change
/// the list. Does not force the list to be read-only.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="coll">IList<T> to test. </param>
/// <param name="valueArray">The values that should be in the list.</param>
public static void TestListGeneric<T>(IList<T> coll, T[] valueArray)
{
TestListGeneric<T>(coll, valueArray, null);
}
public static void TestListGeneric<T>(IList<T> coll, T[] valueArray, BinaryPredicate<T> equals)
{
if (equals == null)
equals = delegate(T x, T y) { return object.Equals(x, y); };
// Check basic read-only collection stuff.
TestCollectionGeneric<T>(coll, valueArray, true, equals);
// Check the indexer getter and IndexOf, backwards
for (int i = coll.Count - 1; i >= 0; --i) {
Assert.IsTrue(equals(valueArray[i], coll[i]));
int index = coll.IndexOf(valueArray[i]);
Assert.IsTrue(index >= 0);
Assert.IsTrue(index == i || (index < i && equals(coll[index], valueArray[i])));
}
// Check the indexer getter and IndexOf, forwards
for (int i = 0; i < valueArray.Length ; ++i) {
Assert.IsTrue(equals(valueArray[i], coll[i]));
int index = coll.IndexOf(valueArray[i]);
Assert.IsTrue(index >= 0);
Assert.IsTrue(index == i || (index < i && equals(coll[index], valueArray[i])));
}
// Check the indexer getter and IndexOf, jumping by 3s
for (int i = 0; i < valueArray.Length; i += 3) {
Assert.IsTrue(equals(valueArray[i], coll[i]));
int index = coll.IndexOf(valueArray[i]);
Assert.IsTrue(index >= 0);
Assert.IsTrue(index == i || (index < i && equals(coll[index], valueArray[i])));
}
// Check exceptions from index out of range.
try {
T dummy = coll[-1];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
T dummy = coll[int.MinValue];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
T dummy = coll[-2];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
T dummy = coll[coll.Count];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
T dummy = coll[int.MaxValue];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
}
/// <summary>
/// Test read-only non-generic IList that should contain the given values, possibly in order. Does not change
/// the list. Does not force the list to be read-only.
/// </summary>
/// <param name="coll">IList to test. </param>
/// <param name="valueArray">The values that should be in the list.</param>
public static void TestList<T>(IList coll, T[] valueArray)
{
// Check basic read-only collection stuff.
TestCollection<T>(coll, valueArray, true);
// Check the indexer getter and IndexOf, backwards
for (int i = coll.Count - 1; i >= 0; --i) {
Assert.AreEqual(valueArray[i], coll[i]);
int index = coll.IndexOf(valueArray[i]);
Assert.IsTrue(coll.Contains(valueArray[i]));
Assert.IsTrue(index >= 0);
Assert.IsTrue(index == i || (index < i && object.Equals(coll[index], valueArray[i])));
}
// Check the indexer getter and IndexOf, forwards
for (int i = 0; i < valueArray.Length; ++i) {
Assert.AreEqual(valueArray[i], coll[i]);
int index = coll.IndexOf(valueArray[i]);
Assert.IsTrue(coll.Contains(valueArray[i]));
Assert.IsTrue(index >= 0);
Assert.IsTrue(index == i || (index < i && object.Equals(coll[index], valueArray[i])));
}
// Check the indexer getter and IndexOf, jumping by 3s
for (int i = 0; i < valueArray.Length; i += 3) {
Assert.AreEqual(valueArray[i], coll[i]);
int index = coll.IndexOf(valueArray[i]);
Assert.IsTrue(coll.Contains(valueArray[i]));
Assert.IsTrue(index >= 0);
Assert.IsTrue(index == i || (index < i && object.Equals(coll[index], valueArray[i])));
}
// Check exceptions from index out of range.
try {
object dummy = coll[-1];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
object dummy = coll[int.MinValue];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
object dummy = coll[-2];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
object dummy = coll[coll.Count];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -