Saturday, December 10, 2005

Unit Testing IO

A little tricky no?

It's bad form in unit testing to write to files outside of the code ... but you still want to make sure you loaders and writers work. If, like me, you make use of serialization you can serialize to memory then unserailize and den-duhr nicely unit tested - check the object equals the original one.

This requires:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

Here's an example of one my unit tests.

public void SerializesDataOkay()
TurnManager tm = new TurnManager();
tm.TurnNumber = 2;

BinaryFormatter formatter = new BinaryFormatter();

MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, tm);
stream.Seek(0, 0); // return to start of stream

TurnManager newTm = (TurnManager)formatter.Deserialize(stream);

Assert.IsTrue(newTm.TurnNumber == 2);

Remember the stream.Seek(0,0) or you'll be at the end of the stream and when you try and load you'll get a crash with a exception that's something like: "end of stream encountered before serialization finished".

I fixed up a bug in the clock code that had problems with repeated pausing. I wrote a unit test first and then fixed, yay for me.

For now I've let my graphics problem rest.

Some kind soul on The Game Programming Wiki message boards suggested clamping. And this cleared up a little of my graphics bug but something still remains.

The bottom red line of the texture dissapears when I move the camera to a certain position. I have the feeling this bug isn't probably quite obvious to someone who knows what they're doing and is something to do with my wonky camera setup.

I ripped the graphics out and made a very simple form demo to demonstrate the bug. Here it is

EDIT: Places problem posted now numbering 3:

(about a day ago)

2. The Game Development Wiki
(this morning, very fast helpful response but still no resolution)

3. USENET microsoft.public.win32.programmer.directx.managed
(Just now, fingers crossed)

EDIT 4: Microsfot MSDN help forums

I've finished my other todo and I've now deep in the heart of my map class hacking away dead things and redoing others. This time I'm adding unit-test plating as I go. In a round about way it's all to refactor one function that's comically bad and could probably be seen on The Daily WTF.

Post a Comment