📄 interfacetests.cs
字号:
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement
// contained in the file "License.txt" accompanying this file.
//******************************
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
namespace Wintellect.PowerCollections.Tests {
/// <summary>
/// A collection of generic tests for various interfaces.
/// </summary>
internal static class InterfaceTests
{
public static BinaryPredicate<KeyValuePair<TKey,TValue>> KeyValueEquals<TKey, TValue>(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); };
return delegate(KeyValuePair<TKey, TValue> pair1, KeyValuePair<TKey, TValue> pair2) {
return keyEquals(pair1.Key, pair2.Key) && valueEquals(pair1.Value, pair2.Value);
};
}
public static BinaryPredicate<KeyValuePair<TKey, TValue>> KeyValueEquals<TKey, TValue>()
{
return KeyValueEquals<TKey, TValue>(null, null);
}
public static BinaryPredicate<ICollection<T>> CollectionEquals<T>(BinaryPredicate<T> equals, bool inOrder)
{
if (inOrder) {
return delegate(ICollection<T> enum1, ICollection<T> enum2) {
if (enum1 == null || enum2 == null)
return (enum1 == enum2);
else
return Algorithms.EqualCollections(enum1, enum2, equals);
};
}
else {
return delegate(ICollection<T> enum1, ICollection<T> enum2) {
if (enum1 == null || enum2 == null)
return (enum1 == enum2);
T[] expected = Algorithms.ToArray(enum2);
bool[] found = new bool[expected.Length];
int i = 0;
foreach (T item in enum1) {
int index;
for (index = 0; index < expected.Length; ++index) {
if (!found[index] && equals(expected[index], item))
break;
}
if (index >= expected.Length)
return false;
if (!equals(expected[index], item))
return false;
found[index] = true;
++i;
}
if (expected.Length != i)
return false;
else
return true;
};
}
}
/// <summary>
/// Test an IEnumerable should contain the given values in order
/// </summary>
public static void TestEnumerableElements<T>(IEnumerable<T> e, T[] expected)
{
TestEnumerableElements<T>(e, expected, null);
}
public static void TestEnumerableElements<T>(IEnumerable<T> e, T[] expected, BinaryPredicate<T> equals)
{
if (equals == null)
equals = delegate(T x, T y) { return object.Equals(x, y); };
int i = 0;
foreach (T item in e) {
Assert.IsTrue(equals(expected[i], item));
++i;
}
Assert.AreEqual(expected.Length, i);
}
/// <summary>
/// Test an IEnumerable should contain the given values in any order
/// </summary>
public static void TestEnumerableElementsAnyOrder<T>(IEnumerable<T> e, T[] expected)
{
TestEnumerableElementsAnyOrder<T>(e, expected, null);
}
public static void TestEnumerableElementsAnyOrder<T>(IEnumerable<T> e, T[] expected, BinaryPredicate<T> equals)
{
if (equals == null)
equals = delegate(T x, T y) { return object.Equals(x, y); };
bool[] found = new bool[expected.Length];
int i = 0;
foreach (T item in e) {
int index;
for (index = 0; index < expected.Length; ++index) {
if (!found[index] && equals(expected[index], item))
break;
}
Assert.IsTrue(index < expected.Length);
Assert.IsTrue(equals(expected[index], item));
found[index] = true;
++i;
}
Assert.AreEqual(expected.Length, i);
}
/// <summary>
/// Test an ICollection that should contain the given values, possibly in order.
/// </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 TestCollection<T>(ICollection coll, T[] valueArray, bool mustBeInOrder)
{
T[] values = (T[])valueArray.Clone(); // clone the array so we can destroy it.
// Check ICollection.Count.
Assert.AreEqual(values.Length, coll.Count);
// Check ICollection.GetEnumerator().
int i = 0, j;
foreach (T s in coll)
{
if (mustBeInOrder)
{
Assert.AreEqual(values[i], s);
}
else
{
bool found = false;
for (j = 0; j < values.Length; ++j)
{
if (object.Equals(values[j],s))
{
found = true;
values[j] = default(T);
break;
}
}
Assert.IsTrue(found);
}
++i;
}
// Check IsSyncronized, SyncRoot.
Assert.IsFalse(coll.IsSynchronized);
Assert.IsNotNull(coll.SyncRoot);
// Check CopyTo.
values = (T[])valueArray.Clone(); // clone the array so we can destroy it.
T[] newKeys = new T[coll.Count + 2];
coll.CopyTo(newKeys, 1);
for (i = 0, j = 1; i < coll.Count; ++i, ++j)
{
if (mustBeInOrder)
{
Assert.AreEqual(values[i], newKeys[j]);
}
else
{
bool found = false;
for (int k = 0; k < values.Length; ++k)
{
if (object.Equals(values[k], newKeys[j]))
{
found = true;
values[k] = default(T);
break;
}
}
Assert.IsTrue(found);
}
}
// Shouldn't have disturbed the values around what was filled in.
Assert.AreEqual(default(T), newKeys[0]);
Assert.AreEqual(default(T), newKeys[coll.Count + 1]);
// Check CopyTo exceptions.
if (coll.Count > 0) {
try
{
coll.CopyTo(null, 0);
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);
}
}
}
/// /// <summary>
/// Test an ICollection<string> that should contain the given values, possibly in order. Checks only the following items:
/// GetEnumerator, CopyTo, Count, Contains
/// </summary>
/// <param name="coll">ICollection to test. </param>
/// <param name="valueArray">The elements that should be in the collection.</param>
/// <param name="mustBeInOrder">Must the elements be in order?</param>
/// <param name="equals">Predicate to test for equality; null for default.</param>
private static void TestCollectionGeneric<T>(ICollection<T> coll, T[] values, bool mustBeInOrder, BinaryPredicate<T> equals)
{
if (equals == null)
equals = delegate(T x, T y) { return object.Equals(x, y); };
bool[] used = new bool[values.Length];
// Check ICollection.Count.
Assert.AreEqual(values.Length, coll.Count);
// Check ICollection.GetEnumerator().
int i = 0, j;
foreach (T s in coll)
{
if (mustBeInOrder)
{
Assert.IsTrue(equals(values[i], s));
}
else
{
bool found = false;
for (j = 0; j < values.Length; ++j)
{
if (!used[j] && equals(values[j],s))
{
found = true;
used[j] = true;
break;
}
}
Assert.IsTrue(found);
}
++i;
}
// Check Contains
foreach (T s in values) {
Assert.IsTrue(coll.Contains(s));
}
// Check CopyTo.
used = new bool[values.Length];
T[] newKeys = new T[coll.Count + 2];
coll.CopyTo(newKeys, 1);
for (i = 0, j = 1; i < coll.Count; ++i, ++j)
{
if (mustBeInOrder)
{
Assert.IsTrue(equals(values[i], newKeys[j]));
}
else
{
bool found = false;
for (int k = 0; k < values.Length; ++k)
{
if (!used[k] && equals(values[k], newKeys[j]))
{
found = true;
used[k] = true;
break;
}
}
Assert.IsTrue(found);
}
}
// Shouldn't have distubed the values around what was filled in.
Assert.IsTrue(equals(default(T), newKeys[0]));
Assert.IsTrue(equals(default(T), newKeys[coll.Count + 1]));
if (coll.Count != 0)
{
// Check CopyTo exceptions.
try
{
coll.CopyTo(null, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -