Therefore, I wrote couple of test cases to explore these two new methods. Check it out!
TestFixtureSetUp
[TestFixture]
public class TestFindInICollection
{
private List
private const int TOTAL_NUMBER = 20;
private const int MAX = 100;
private DomainModel[] _models;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
_numbers = new List
_models = new DomainModel[TOTAL_NUMBER];
Random r = new Random();
Console.WriteLine("Orignal Collection");
Console.WriteLine("Size = " + TOTAL_NUMBER);
//populate the number list with pseudo-random numbers
//populate the model list
for (int i = 0; i < TOTAL_NUMBER; i++)
{
int radNum = r.Next(MAX);
_numbers.Add(radNum);
Console.WriteLine(radNum);
_models[i] = new DomainModel(i, DateTime.Now.ToLongTimeString());
}
}
...
Find numbers smaller then 50 using anonymous delegate
[Test]
public void CanFindAllNumbersUsingAnonymousMethod()
{
//_number is prepopulated with 20 random numbers from 0 to 50
//find numbers smaller then 50
List
Console.WriteLine("Numbers smaller then 50");
Console.WriteLine("Size = " + smallerPartition.Count);
foreach (int n in smallerPartition)
{
Assert.IsTrue(n < 50);
Console.WriteLine(n);
}
}
Find the domain model with id equals to 10
[Test]
public void CanFindModelUsingAnonymousMethod()
{
int id = 10;
DomainModel model = Array.Find
Assert.AreEqual(10, model.Id);
Console.WriteLine(model);
}
Find numbers larger then 50 by calling a helper function that returns a Predicate
[Test]
public void CanFindAllNumbersUsingFunction()
{
//find numbers bigger then 50
List
Console.WriteLine("Numbers bigger then 50");
Console.WriteLine("Size = " + biggerPartition.Count);
foreach (int n in biggerPartition)
{
Assert.IsTrue(n > 50);
Console.WriteLine(n);
}
}
private Predicate
{
return delegate(int testInt) { return testInt > p; };
}
Simple DomainModel Class
class DomainModel
{
private int _id;
private string _label;
public DomainModel(int id, string label)
{
_id = id;
_label = label;
}
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Label
{
get { return _label; }
set { _label = value; }
}
public override string ToString()
{
return _id + " " + _label;
}
}
No comments:
Post a Comment