Developing on Staxmanade

i4o & Silverlight Unit Tests (A little more work than the i4o library)

Follow up to my post on i4o & Silverlight (compiles first try)...

 

I took a stab at porting the i4o unit tests to Silverlight which was quite a bit more work than I initially expected.

After creating a Silverlight Unit Test project and linking the original test files into the Silverlight project, I compiled...

  • First the VB using statement wasn't even needed, so I removed that. using Microsoft.VisualBasic;
  • Second there is no System.Diagnostics.Stopwatch() class in Silverlight, so I basically implemented a quick one using DateTime to get the unit tests to compile in Silverlight. Here's the class, except the Frequency property has been commented out (didn't spend time to figure how to make that correct, or what is correct???)
public class Stopwatch
{
private DateTime _StartUtcDateTime;
private DateTime? _EndUtcDateTime;
private bool _IsRunning = false;

//public static readonly long Frequency { get { throw new NotImplementedException(); } }
public static readonly bool IsHighResolution = false;
public Stopwatch()
{}

public TimeSpan Elapsed
{
get
{
if (_EndUtcDateTime.HasValue)
{
return new TimeSpan(_EndUtcDateTime.Value.Ticks - _StartUtcDateTime.Ticks);
}
else
{
return new TimeSpan(DateTime.UtcNow.Ticks - _StartUtcDateTime.Ticks);
}
}
}

public long ElapsedMilliseconds { get { return Elapsed.Milliseconds; } }
public long ElapsedTicks { get { return Elapsed.Ticks; } }
public bool IsRunning { get { return _IsRunning; } }

public static long GetTimestamp()
{
return DateTime.Now.Ticks;
}

public void Reset()
{
_EndUtcDateTime = null;
_StartUtcDateTime = DateTime.UtcNow;
}

public void Start()
{
_EndUtcDateTime = null;
_IsRunning = true;
this._StartUtcDateTime = DateTime.UtcNow;
}

public static Stopwatch StartNew()
{
var w = new Stopwatch();
w.Start();
return w;
}

public void Stop()
{
_EndUtcDateTime = DateTime.UtcNow;
_IsRunning = false;
}
}


The only other issue that came up was some of the Stopwatch dependent tests happened so fast that they would fail intermittently... the quick hack/solution for this was to up the iteration count of whatever they were testing.


After all the above taking care of all the above issues, I was able to get the unit tests to pass.


image

Comments

Tiaan
You might also want to look at my implementation of the Stopwatch for Silverlight, which supports resuming.
Tiaan
For the Frequency property's implementation, you probably just need to return the System.TimeSpan.TicksPerSecond value.

Comments