Monday, March 10, 2008

Unit Testing Graphics Operations

We decided to verify our graphic operations here by doing a binary comparison.
Here is a simple implementation using Linq.

Setting up the expected value:

Bitmap aBitmap = new Bitmap(181,46); //exact size of the bitmap to reduce the number of bits
Graphics aGraphic = Graphics.FromImage(aBitmap);
target.DrawGrid(aGraphic); //the operation you want to verify
MemoryStream actual = new MemoryStream();
string aBunch = string.Join(",", actual.ToArray().Select(b => b.ToString()).ToArray());

Copy the string aBunch. We will use this to create the expected byte array.
create the byte array like this but use your pasted values instead of my values
byte[] expected = new byte[] {66,77,78... };

Finishing the unit test:

Bitmap aBitmap = new Bitmap(181,46);
Graphics aGraphic = Graphics.FromImage(aBitmap);
MemoryStream actual = new MemoryStream();
aBitmap.Save(actual,ImageFormat.Bmp); // get our actual result.
byte[] expected = getExpected("DrawGridTest");

Assert.IsTrue(actual.ToArray().SequenceEqual(expected),"Binary comparison failed.");

This method is getting expected from a function because Visual Studio responds slowly when there are long lines of text. I created a partial class containing the instantiation of expected (I'm using a switch statement). I thought about putting the results in a resource or a file, but I'm preferring code for now.


Anonymous said...

Hey very nice blog!!….I’m an instant fan, I have bookmarked you and I’ll be checking back on a regular….See ya

Anonymous said...

i absolutely adore your own posting choice, very exciting,
don't quit as well as keep posting as a result it simply just that is worth to follow it,
looking forward to look at a lot more of your content pieces, good bye ;)