📄 interfacetests.cs
字号:
Assert.Fail("Copy to null should throw exception");
}
catch (Exception e)
{
Assert.IsTrue(e is ArgumentNullException);
}
try
{
coll.CopyTo(newKeys, 3);
Assert.Fail("CopyTo should throw argument exception");
}
catch (Exception e)
{
Assert.IsTrue(e is ArgumentException);
}
try {
coll.CopyTo(newKeys, -1);
Assert.Fail("CopyTo should throw argument out of range exception");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
}
}
// Check collection read-only exceptions
private static void CheckReadonlyCollectionException(Exception e, string name)
{
Assert.IsTrue(e is NotSupportedException);
if (name != null)
Assert.AreEqual(string.Format(Strings.CannotModifyCollection, name), e.Message);
}
/// <summary>
/// Test a readonly ICollection<string> that should contain the given values, possibly in order. Checks only the following items:
/// GetEnumerator, CopyTo, Count, Contains, IsReadOnly
/// </summary>
/// <param name="coll">ICollection<T> to test. </param>
/// <param name="valueArray">The values that should be in the collection.</param>
/// <param name="mustBeInOrder">Must the value be in order?</param>
/// <param name="name">Expected name of the collection, or null for don't check.</param>
public static void TestReadonlyCollectionGeneric<T>(ICollection<T> coll, T[] valueArray, bool mustBeInOrder, string name)
{
TestReadonlyCollectionGeneric<T>(coll, valueArray, mustBeInOrder, null, null);
}
public static void TestReadonlyCollectionGeneric<T>(ICollection<T> coll, T[] valueArray, bool mustBeInOrder, string name, BinaryPredicate<T> equals)
{
TestCollectionGeneric<T>(coll, valueArray, mustBeInOrder, equals);
// Test read-only flag.
Assert.IsTrue(coll.IsReadOnly);
// Check that Clear throws correct exception
if (coll.Count > 0) {
try {
coll.Clear();
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
}
// Check that Add throws correct exception
try {
coll.Add(default(T));
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
// Check throws correct exception
try {
coll.Remove(default(T));
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
}
/// <summary>
/// Test a read-write ICollection<string> that should contain the given values, possibly in order. Destroys the collection in the process.
/// </summary>
/// <param name="coll">ICollection to test. </param>
/// <param name="valueArray">The values that should be in the collection.</param>
/// <param name="mustBeInOrder">Must the values be in order?</param>
public static void TestReadWriteCollectionGeneric<T>(ICollection<T> coll, T[] valueArray, bool mustBeInOrder)
{
TestReadWriteCollectionGeneric<T>(coll, valueArray, mustBeInOrder, null);
}
public static void TestReadWriteCollectionGeneric<T>(ICollection<T> coll, T[] valueArray, bool mustBeInOrder, BinaryPredicate<T> equals)
{
TestCollectionGeneric<T>(coll, valueArray, mustBeInOrder, equals);
// Test read-only flag.
Assert.IsFalse(coll.IsReadOnly);
// Clear and Count.
coll.Clear();
Assert.AreEqual(0, coll.Count);
// Add all the items back.
foreach (T item in valueArray)
coll.Add(item);
Assert.AreEqual(valueArray.Length, coll.Count);
TestCollectionGeneric<T>(coll, valueArray, mustBeInOrder, equals);
// Remove all the items again.
foreach (T item in valueArray)
coll.Remove(item);
Assert.AreEqual(0, coll.Count);
}
/// <summary>
/// Test an IDictionary 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 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 TestDictionary<TKey, TValue>(IDictionary dict, TKey[] keys, TValue[] values, TKey nonKey, bool mustBeInOrder)
{
// Check Count.
Assert.AreEqual(keys.Length, dict.Count);
// Check containment.
for (int i = 0; i < keys.Length; ++i) {
Assert.IsTrue(dict.Contains(keys[i]));
Assert.AreEqual(dict[keys[i]], values[i]);
}
Assert.IsFalse(dict.Contains(nonKey));
Assert.IsNull(dict[nonKey]);
Assert.IsFalse(dict.Contains(new object()));
Assert.IsNull(dict[new object()]);
// Check synchronization
Assert.IsFalse(dict.IsSynchronized);
Assert.IsNotNull(dict.SyncRoot);
// Check Keys, Values collections
TestCollection<TKey>(dict.Keys, keys, mustBeInOrder);
TestCollection<TValue>(dict.Values, values, mustBeInOrder);
// Check DictionaryEnumerator.
int count = 0;
bool[] found = new bool[keys.Length];
IDictionaryEnumerator enumerator = dict.GetEnumerator();
while (enumerator.MoveNext()) {
DictionaryEntry entry = enumerator.Entry;
Assert.AreEqual(enumerator.Entry.Key, enumerator.Key);
Assert.AreEqual(enumerator.Entry.Value, enumerator.Value);
Assert.AreEqual(((DictionaryEntry)(enumerator.Current)).Key, enumerator.Key);
Assert.AreEqual(((DictionaryEntry)(enumerator.Current)).Value, enumerator.Value);
// find the entry.
if (mustBeInOrder) {
Assert.AreEqual(keys[count], enumerator.Key);
Assert.AreEqual(values[count], enumerator.Value);
}
else {
for (int i = 0; i < keys.Length; ++i) {
if ((!found[i]) && object.Equals(keys[i], enumerator.Key) && object.Equals(values[i], enumerator.Value)) {
found[i] = true;
}
}
}
++count;
}
Assert.AreEqual(count, keys.Length);
if (!mustBeInOrder)
for (int i = 0; i < keys.Length; ++i)
Assert.IsTrue(found[i]);
}
/// <summary>
/// Test an read-only IDictionary 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 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 TestReadOnlyDictionary<TKey, TValue>(IDictionary dict, TKey[] keys, TValue[] values, TKey nonKey, bool mustBeInOrder, string name)
{
DictionaryEntry[] entries = new DictionaryEntry[keys.Length];
for (int i = 0; i < keys.Length; ++i)
entries[i] = new DictionaryEntry(keys[i], values[i]);
TestCollection<DictionaryEntry>((ICollection)dict, entries, mustBeInOrder);
TestDictionary<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder);
Assert.IsTrue(dict.IsReadOnly);
Assert.IsTrue(dict.IsFixedSize);
// Check exceptions.
try {
dict.Clear();
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
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 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 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 TestReadWriteDictionary<TKey, TValue>(IDictionary dict, TKey[] keys, TValue[] values, TKey nonKey, bool mustBeInOrder, string name)
{
DictionaryEntry[] entries = new DictionaryEntry[keys.Length];
for (int i = 0; i < keys.Length; ++i)
entries[i] = new DictionaryEntry(keys[i], values[i]);
TestCollection<DictionaryEntry>((ICollection)dict, entries, mustBeInOrder);
TestDictionary<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder);
Assert.IsFalse(dict.IsReadOnly);
Assert.IsFalse(dict.IsFixedSize);
// 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 with incorrect types.
try {
dict.Add(new object(), values[0]);
Assert.Fail("should have thrown exception");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
try {
dict.Add(keys[0], new object());
Assert.Fail("should have thrown exception");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
// Check Add().
for (int i = 0; i < keys.Length; ++i)
dict.Add(keys[i], values[i]);
TestCollection<DictionaryEntry>((ICollection)dict, entries, mustBeInOrder);
TestDictionary<TKey, TValue>(dict, keys, values, nonKey, mustBeInOrder);
// Check Remove. 2nd remove should do nothing.
for (int i = 0; i < keys.Length; ++i) {
dict.Remove(keys[i]);
dict.Remove(keys[i]);
}
// Remove with incorrect type.
dict.Remove(new object());
Assert.AreEqual(0, dict.Count);
// Check indexer with incorrect types.
try {
dict[new object()] = values[0];
Assert.Fail("should have thrown exception");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
try {
dict[keys[0]] = new object();
Assert.Fail("should have thrown exception");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -