📄 interfacetests.cs
字号:
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
coll.Insert(-1, item);
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
coll.Insert(coll.Count + 1, item);
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
coll.Insert(int.MaxValue, item);
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
coll.RemoveAt(coll.Count);
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
coll.RemoveAt(-1);
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
coll.RemoveAt(int.MaxValue);
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
coll.RemoveAt(coll.Count);
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
// Check operations with bad type.
if (typeof(T) != typeof(object)) {
try {
coll.Add(new object());
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
try {
coll.Insert(0, new object());
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentException);
}
int index = coll.IndexOf(new object());
Assert.AreEqual(-1, index);
coll.Remove(new object());
}
// Insert at the beginning.
coll.Insert(0, item);
Assert.AreEqual(coll[0], item);
Assert.AreEqual(valueArray.Length + 1, coll.Count);
for (int i = 0; i < valueArray.Length; ++i)
Assert.AreEqual(valueArray[i], coll[i + 1]);
// Insert at the end
coll.Insert(valueArray.Length + 1, item);
Assert.AreEqual(coll[valueArray.Length + 1], item);
Assert.AreEqual(valueArray.Length + 2, coll.Count);
for (int i = 0; i < valueArray.Length; ++i)
Assert.AreEqual(valueArray[i], coll[i + 1]);
// Delete at the beginning.
coll.RemoveAt(0);
Assert.AreEqual(coll[valueArray.Length], item);
Assert.AreEqual(valueArray.Length + 1, coll.Count);
for (int i = 0; i < valueArray.Length; ++i)
Assert.AreEqual(valueArray[i], coll[i]);
// Delete at the end.
coll.RemoveAt(valueArray.Length);
Assert.AreEqual(valueArray.Length, coll.Count);
for (int i = 0; i < valueArray.Length; ++i)
Assert.AreEqual(valueArray[i], coll[i]);
// Insert at the middle.
coll.Insert(valueArray.Length / 2, item);
Assert.AreEqual(valueArray.Length + 1, coll.Count);
Assert.AreEqual(item, coll[valueArray.Length / 2]);
for (int i = 0; i < valueArray.Length; ++i) {
if (i < valueArray.Length / 2)
Assert.AreEqual(valueArray[i], coll[i]);
else
Assert.AreEqual(valueArray[i], coll[i+1]);
}
// Delete at the middle.
coll.RemoveAt(valueArray.Length / 2);
Assert.AreEqual(valueArray.Length, coll.Count);
for (int i = 0; i < valueArray.Length; ++i)
Assert.AreEqual(valueArray[i], coll[i]);
// Delete all from the middle.
for (int i = 0; i < valueArray.Length; ++i)
coll.RemoveAt(coll.Count / 2);
Assert.AreEqual(0, coll.Count);
// Build up in order.
for (int i = 0; i < save.Length; ++i) {
coll.Insert(i, save[i]);
}
TestList<T>(coll, valueArray); // Basic read-only list stuff.
coll.Clear();
Assert.AreEqual(0, coll.Count);
// Build up in order with Add
for (int i = 0; i < save.Length; ++i) {
coll.Add(save[i]);
}
TestList<T>(coll, valueArray); // Basic read-only list stuff.
// Remove in order with Remove.
for (int i = 0; i < valueArray.Length; ++i) {
coll.Remove(valueArray[i]);
}
Assert.AreEqual(0, coll.Count);
// Build up in reverse order with Insert
for (int i = 0; i < save.Length; ++i) {
coll.Insert(0, save[save.Length - 1 - i]);
}
TestList<T>(coll, valueArray); // Basic read-only list stuff.
// Check read-write collection stuff.
TestCollection<T>(coll, valueArray, true);
}
/// <summary>
/// Test read-only IList<T> that should contain the given values, possibly in order. Does not change
/// the list. Forces 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>
/// <param name="name">Name of the collection, for exceptions. Null to not check.</param>
public static void TestReadOnlyListGeneric<T>(IList<T> coll, T[] valueArray, string name)
{
TestReadOnlyListGeneric<T>(coll, valueArray, name, null);
}
public static void TestReadOnlyListGeneric<T>(IList<T> coll, T[] valueArray, string name, BinaryPredicate<T> equals)
{
if (equals == null)
equals = delegate(T x, T y) { return object.Equals(x, y); };
// Basic list stuff.
TestListGeneric<T>(coll, valueArray, equals);
TestReadonlyCollectionGeneric<T>(coll, valueArray, true, name, equals);
// Check read only and fixed size bits.
Assert.IsTrue(coll.IsReadOnly);
// Check exceptions.
if (coll.Count > 0) {
try {
coll.Clear();
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
}
try {
coll.Insert(0, default(T));
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
if (coll.Count > 0) {
try {
coll.RemoveAt(0);
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
try {
coll[0] = default(T);
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
}
}
/// <summary>
/// Test read-only non-generic IList; that should contain the given values, possibly in order. Does not change
/// the list. Forces the list to be read-only.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="coll">IList to test. </param>
/// <param name="valueArray">The values that should be in the list.</param>
/// <param name="name">Name of the collection, for exceptions. Null to not check.</param>
public static void TestReadOnlyList<T>(IList coll, T[] valueArray, string name)
{
// Basic list stuff.
TestList<T>(coll, valueArray);
TestCollection<T>(coll, valueArray, true);
// Check read only and fixed size bits.
Assert.IsTrue(coll.IsReadOnly);
Assert.IsTrue(coll.IsFixedSize);
// Check exceptions.
try {
coll.Clear();
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
// Check exceptions.
try {
coll.Clear();
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
try {
coll.Insert(0, default(T));
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
try {
coll.Add(default(T));
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
if (coll.Count > 0) {
try {
coll.RemoveAt(0);
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
try {
coll.Remove(coll[0]);
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
try {
coll[0] = default(T);
Assert.Fail("Should throw exception");
}
catch (Exception e) {
CheckReadonlyCollectionException(e, name);
}
}
}
public static void TestReadWriteMultiDictionaryGeneric<TKey, TValue>(IDictionary<TKey, ICollection<TValue>> dict, TKey[] keys, TValue[][] values, TKey nonKey, TValue nonValue, 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); };
BinaryPredicate<ICollection<TValue>> valueCollectionEquals = CollectionEquals(valueEquals, mustBeInOrder);
TestReadWriteDictionaryGeneric<TKey, ICollection<TValue>>(dict, keys, values, nonKey, mustBeInOrder, name, keyEquals, valueCollectionEquals);
}
public static void TestReadOnlyMultiDictionaryGeneric<TKey, TValue>(IDictionary<TKey, ICollection<TValue>> dict, TKey[] keys, TValue[][] values, TKey nonKey, TValue nonValue, 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); };
BinaryPredicate<ICollection<TValue>> valueCollectionEquals = CollectionEquals(valueEquals, mustBeInOrder);
TestReadOnlyDictionaryGeneric<TKey, ICollection<TValue>>(dict, keys, values, nonKey, mustBeInOrder, name, keyEquals, valueCollectionEquals);
}
p
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -