Wednesday, September 4, 2013

Pex, a GTAC presentation

Having watched about Pex in the GTAC 2013 presentations, I thought it was a rather interesting technology.  The basic idea is that you tell Pex to generate test cases for a particular method and it will find bugs in your implementation.  It tries to be relatively exhaustive within equivalence classed systems.  It really is only meant for unit testing, however I could imagine it being used to generate interesting test cases on a wider basis.

The interesting thing outside of the particular testing technology is that it can also be used as a game, training tool or for interviewing.  In particular, you can take a previously created secret implementation, and ask for someone to re-implement the solution.  Since Pex knows how to test compiled .net code, Pex just finds interesting values based upon the secret implementation (E.G. the oracle) and then sees if the output it creates equals the output you generate for the same value.  If they are not equal, your implementation is wrong.  Since you can retry almost instantly it becomes relatively easy to generate possible solutions until your implementation matches that of the secret implementation.  Having tried a good number of the challenges, I must say my current favourite is making English words plural.  While this isn't exactly how I generate automated test cases, it certainly made me think about other possible input values I should be testing.

Let me know if you solve it in the comments and if you want, post your own solution.  I have a solution, but I'll resist posting it for now.  In some future point (maybe a few weeks from now), I'll post a comment with my solution.

1 comment:

  1. Here is my solution:

    using System;

    public class Program {
    public static string Puzzle(string s) {
    String ret = "s";
    String tmpS = s;
    tmpS = tmpS.Replace("\0","");//remove nulls

    String previousChar = "";


    if(s.EndsWith("s"))
    ret = "es";
    if(!String.IsNullOrEmpty(tmpS))
    {
    if(tmpS == "y") {
    ret = "ies";
    s = s.Substring(0, s.Length-1);//remove y

    }else if(tmpS.EndsWith("y")) {
    ret = "ies";
    previousChar = tmpS.Substring(tmpS.LastIndexOf("y") - 1, 1);
    foreach (var item in new String[] {"a","i","o","u","e"}) {
    if(previousChar == item)
    ret = "s";
    }
    if(ret=="ies")
    s = s.Substring(0, s.Length-1);//remove y
    }
    if(tmpS.Length>2 && tmpS[tmpS.Length-1]==tmpS[tmpS.Length-2]) {// ssses not ssss
    ret = "es";
    }
    }
    foreach (var item in new String[] {"o","h"}) {
    if(s.EndsWith(item))
    ret = "es";
    }
    // Can you write code that returns the plural form of an English noun?
    return s + ret;
    }
    }

    ReplyDelete