📄 algorithmstests.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.Generic;
using System.Collections;
using NUnit.Framework;
namespace Wintellect.PowerCollections.Tests
{
[TestFixture]
public class AlgorithmsTests
{
// Create an IEnumerable that enumerates an array. Make sure that only enumerable stuff
// is used and no downcasts to ICollection are taken advantage of.
public static IEnumerable<T> EnumerableFromArray<T>(T[] array)
{
foreach (T t in array)
yield return t;
}
[Test]
public void Exists()
{
IEnumerable<double> coll1 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
IEnumerable<double> coll2 = EnumerableFromArray(new double[] { });
Assert.IsTrue(Algorithms.Exists(coll1, delegate(double d) { return d > 100; }));
Assert.IsTrue(Algorithms.Exists(coll1, delegate(double d) { return Math.Abs(d) == 0.04; }));
Assert.IsFalse(Algorithms.Exists(coll1, delegate(double d) { return d < -10.0; }));
Assert.IsFalse(Algorithms.Exists(coll2, delegate(double d) { return Math.Abs(d) == 0.04; }));
}
[Test]
public void TrueForAll()
{
IEnumerable<double> coll1 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
IEnumerable<double> coll2 = EnumerableFromArray(new double[] { });
Assert.IsFalse(Algorithms.TrueForAll(coll1, delegate(double d) { return d > 100; }));
Assert.IsFalse(Algorithms.TrueForAll(coll1, delegate(double d) { return Math.Abs(d) < 10; }));
Assert.IsTrue(Algorithms.TrueForAll(coll1, delegate(double d) { return d > -10; }));
Assert.IsTrue(Algorithms.TrueForAll(coll1, delegate(double d) { return Math.Abs(d) < 200; }));
Assert.IsTrue(Algorithms.TrueForAll(coll2, delegate(double d) { return Math.Abs(d) == 0.04; }));
}
[Test]
public void CountWhere()
{
IEnumerable<double> coll1 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
IEnumerable<double> coll2 = EnumerableFromArray(new double[] { });
Assert.AreEqual(0, Algorithms.CountWhere(coll1, delegate(double d) { return d > 200; }));
Assert.AreEqual(6, Algorithms.CountWhere(coll1, delegate(double d) { return Math.Abs(d) < 10; }));
Assert.AreEqual(8, Algorithms.CountWhere(coll1, delegate(double d) { return d > -10; }));
Assert.AreEqual(4, Algorithms.CountWhere(coll1, delegate(double d) { return Math.Abs(d) > 5; }));
Assert.AreEqual(0, Algorithms.CountWhere(coll2, delegate(double d) { return Math.Abs(d) < 10; }));
}
[Test]
public void Count()
{
IEnumerable<double> coll1 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
IEnumerable<double> coll2 = EnumerableFromArray(new double[] { });
IEnumerable<double> coll3 = new List<double>(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
IEnumerable<double> coll4 = new List<double>(new double[] { });
Assert.AreEqual(8, Algorithms.Count(coll1));
Assert.AreEqual(0, Algorithms.Count(coll2));
Assert.AreEqual(8, Algorithms.Count(coll3));
Assert.AreEqual(0, Algorithms.Count(coll4));
}
[Test]
public void RemoveWhenTrueCollection()
{
List<double> d_list = new List<double>(new double[]{ 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
ICollection<double> removed;
removed = Algorithms.RemoveWhere((ICollection<double>)d_list, delegate(double d) { return Math.Abs(d) > 5; });
InterfaceTests.TestReadWriteCollectionGeneric(d_list, new double[] { 4.5, 1.2, -0.04, 1.78}, true);
InterfaceTests.TestReadWriteCollectionGeneric(removed, new double[] { 7.6, -7.6, 10.11, 187.4 }, true);
d_list = new List<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
removed = Algorithms.RemoveWhere((ICollection<double>)d_list, delegate(double d) { return d == 0; });
InterfaceTests.TestReadWriteCollectionGeneric(d_list, new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 }, true);
InterfaceTests.TestReadWriteCollectionGeneric(removed, new double[] {}, true);
d_list = new List<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
removed = Algorithms.RemoveWhere((ICollection<double>)d_list, delegate(double d) { return d < 200; });
InterfaceTests.TestReadWriteCollectionGeneric(removed, new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 }, true);
Assert.AreEqual(0, d_list.Count);
}
[Test]
public void RemoveWhenTrueList()
{
IList<double> d_list = new List<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
ICollection<double> removed;
removed = Algorithms.RemoveWhere(d_list, delegate(double d) { return Math.Abs(d) > 5; });
InterfaceTests.TestListGeneric(d_list, new double[] { 4.5, 1.2, -0.04, 1.78 });
InterfaceTests.TestReadWriteCollectionGeneric(removed, new double[] { 7.6, -7.6, 10.11, 187.4 }, true);
d_list = new List<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
removed = Algorithms.RemoveWhere(d_list, delegate(double d) { return d == 0; });
InterfaceTests.TestListGeneric(d_list, new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
InterfaceTests.TestReadWriteCollectionGeneric(removed, new double[] { }, true);
d_list = new List<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
removed = Algorithms.RemoveWhere(d_list, delegate(double d) { return d < 200; });
InterfaceTests.TestReadWriteCollectionGeneric<double>(removed, new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 }, true);
Assert.AreEqual(0, d_list.Count);
d_list = new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 };
removed = Algorithms.RemoveWhere<double>(d_list, delegate(double d) { return Math.Abs(d) > 5; });
InterfaceTests.TestEnumerableElements<double>(d_list, new double[] { 4.5, 1.2, -0.04, 1.78, 0, 0, 0, 0 });
InterfaceTests.TestReadWriteCollectionGeneric<double>(removed, new double[] { 7.6, -7.6, 10.11, 187.4 }, true);
}
[Test]
public void FindAll()
{
IEnumerable<double> coll1 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
double[] expected = { 7.6, -7.6, 10.11, 187.4 };
int i;
i = 0;
foreach (double x in Algorithms.FindWhere(coll1, delegate(double d) { return Math.Abs(d) > 5; })) {
Assert.AreEqual(expected[i], x);
++i;
}
Assert.AreEqual(expected.Length, i);
}
[Test]
public void Convert()
{
IEnumerable<double> coll1 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
IEnumerable<int> result1 = Algorithms.Convert<double, int>(coll1, delegate(double x) { return (int)Math.Round(x); });
InterfaceTests.TestEnumerableElements(result1, new int[] { 4, 1, 8, 0, -8, 2, 10, 187 });
IEnumerable<double> coll2 = EnumerableFromArray(new double[0]);
IEnumerable<int> result2 = Algorithms.Convert<double, int>(coll2, delegate(double x) { return (int)Math.Round(x); });
InterfaceTests.TestEnumerableElements(result2, new int[0]);
IEnumerable<double> coll3 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
IEnumerable<double> result3 = Algorithms.Convert<double, double>(coll3, Math.Abs);
InterfaceTests.TestEnumerableElements(result3, new double[] { 4.5, 1.2, 7.6, 0.04, 7.6, 1.78, 10.11, 187.4 });
}
[Test]
public void ForEach()
{
IEnumerable<string> coll1 = EnumerableFromArray(new string[] { "foo", "bar", "hello", "sailor" });
string s = "";
Algorithms.ForEach(coll1, delegate(string x) { s += "!" + x; });
Assert.AreEqual(s, "!foo!bar!hello!sailor");
IEnumerable<string> coll2 = EnumerableFromArray(new string[0]);
s = "";
Algorithms.ForEach(coll2, delegate(string x) { s += "!" + x; });
Assert.AreEqual(s, "");
}
[Test]
public void Partition()
{
List<double> list;
int index;
Predicate<double> isNegative = delegate(double x) { return x < 0; };
list = new List<double>();
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(0, index);
Assert.AreEqual(0, list.Count);
list = new List<double>(new double[] { -3.1 });
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(1, index);
Assert.AreEqual(1, list.Count);
Assert.AreEqual(-3.1, list[0]);
list = new List<double>(new double[] { 3.1 });
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(0, index);
Assert.AreEqual(1, list.Count);
Assert.AreEqual(3.1, list[0]);
list = new List<double>(new double[] { -2, 3.1 });
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(1, index);
Assert.AreEqual(2, list.Count);
Assert.AreEqual(-2, list[0]);
Assert.AreEqual(3.1, list[1]);
list = new List<double>(new double[] { 2, -3.1 });
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(1, index);
Assert.AreEqual(2, list.Count);
Assert.AreEqual(-3.1, list[0]);
Assert.AreEqual(2, list[1]);
list = new List<double>(new double[] { -2, -3.1 });
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(2, index);
InterfaceTests.TestEnumerableElementsAnyOrder(list, new double[] { -2, -3.1 });
list = new List<double>(new double[] { 2, 3.1 });
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(0, index);
InterfaceTests.TestEnumerableElementsAnyOrder(list, new double[] { 2, 3.1 });
list = new List<double>(new double[] { 2, 6, -8, -7, 3, -1, -2, 4, 2 });
index = Algorithms.Partition(list, isNegative);
Assert.AreEqual(4, index);
InterfaceTests.TestEnumerableElementsAnyOrder(list.GetRange(0, index), new double[] { -8, -7, -1, -2 });
InterfaceTests.TestEnumerableElementsAnyOrder(list.GetRange(index, list.Count - index), new double[] {2, 6, 3, 4, 2 });
double[] array = new double[] { 2, 6, -8, -7, 3, -1, -2, 4, 2 };
index = Algorithms.Partition(array, isNegative);
Assert.AreEqual(4, index);
InterfaceTests.TestEnumerableElementsAnyOrder(Algorithms.Range(array, 0, index), new double[] { -8, -7, -1, -2 });
InterfaceTests.TestEnumerableElementsAnyOrder(Algorithms.Range(array, index, list.Count - index), new double[] { 2, 6, 3, 4, 2 });
}
[Test]
public void StablePartition()
{
List<double> list;
int index;
Predicate<double> isNegative = delegate(double x) { return x < 0; };
list = new List<double>();
index = Algorithms.StablePartition(list, isNegative);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -