Service pack 1 for Visual Studio .NET 2003 is finally out! It has fixes for more then 400 issues. (feel a lot better about my own code now, lol)
Check it out
Friday, August 18, 2006
Thursday, August 10, 2006
Weijie on Wheels
Haven't had any time to do any Ruby on Rails lately. Instead I've spend most of my spare time on Wheels. Yay, We finally got our car last Thursday and since then I've put 1000km on it. Gas prices are high these days but we couldn't resist the convenience and mobility it brings. We've been in New Brunswick for a long time but we just realized how beautiful it is, so we plan to travel within New Brunswick for the rest of the summer.
Here are some scenes from our road trip
Here are some scenes from our road trip
Wednesday, August 09, 2006
Find and FindAll in generic collections
In .NET 2.0, searching through generic collections such as Arrays and Lists are easier with the new Find and FindAll methods. Both methods accepts a Predicate parameter, which is a delegate to a method. According to MSDN, these methods run in O(n). In situations like searching for item(s) in a collection, I used to write utility functions with simple bubble sort algorithm. This causes code duplication and O(n^2) is just too slow.
Therefore, I wrote couple of test cases to explore these two new methods. Check it out!
TestFixtureSetUp
[TestFixture]
public class TestFindInICollection
{
private List _numbers;
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 smallerPartition = _numbers.FindAll(delegate(int testInt) { return testInt < 50; });
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(_models, delegate(DomainModel testModel) { return testModel.Id == id; }) ;
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 biggerPartition = _numbers.FindAll(BiggerThen(50));
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 BiggerThen(int p)
{
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;
}
}
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;
}
}
AgileCoder:Operating-Systems
Subscribe to:
Posts (Atom)
Influence: The Psychology of Persuasion
Amazon Page ISBN-13: 978-0061241895 ISBN-10: 006124189X Do I recommend: Yes Key Takeaways Author uses a ...

-
In Feb 2001, 17 prominent members of the software community came together and created the Agile Manifesto. From that moment on, agile develo...
-
I am half way through reading the Applying Domain-Driven Design and Patterns book written by Jimmy Nilsson .I have always wondered how do d...
-
I've been using Ubuntu Linux as my main OS at home. After upgrading to Ubuntu Dapper recently, Firefox refuses to play flash video sound...