Friday 28 May 2010

Code Contracts and Assert() weirdness

One of my first steps was to build a set of tests to verify the correctness of my implementation of the Contracts namespace. However, a curious problem occured with Asserts in the Microsoft System.Diagnostics.Contracts namespace. By default a failed Assert shows a dialog box with an error message and a stack-trace. This behaviour can be changed by adding/removing listeners in System.Diagnostics.Debug.Listeners - for example, by clearing all the listeners a failed assert is ignored:

System.Diagnostics.Debug.Listeners.Clear();
System.Diagnostics.Debug.Assert(false);
This code does not show a dialog box with the assert information because the default listener has been removed. It is completely possibly to add custom listeners that perform whatever behaviour is required.

However, the weirdness is that when using the Contract.Assert() method in System.Diagnostics.Contracts namespace, the assert behaviour cannot be modified using the Listeners collection. Although it appears to be a normal assert, it cannot be - it does not use the Debug.Listeners collection to define its behaviour, and there doesn't appear to be any other Listeners collection that could effect the behaviour.

Therefore the following code:

System.Diagnostics.Debug.Listeners.Clear();
System.Diagnostics.Contracts.Contract.Assert(false);
shows an assert dialog box.

This means it is not possible to run a test on the Microsoft implementation which uses a custom listener to check that the asserts generated are correct. So my strategy of running a set of tests against the Microsoft implementation and my implementation to ensure they produce the same behaviour is not going to work.

Alas.

Beginnings

This summer I am pleased to be part of Google Summer of Code 2010. I will be working on the Mono project, implementing the .NET4 Code Contract Verifiers, mentored by Marek Safar. This is a large body of work, and I am not considering trying to fully implement every part of it. By the end of the summer I expect to have:
  • The System.Diagnostics.Contracts namespace fully implemented in corlib.
  • A working runtime verifier that supports pre-conditions.
  • A working static verifier that supports pre-conditions.
  • Tests and documentation of the above!
Of course,  I hope to implement a lot more than this, so let's see how things go...