6

Suppose there is a method like this (C#):

public static int Add(int x, int y)
{
    return x + y;
}

If the sum does not fit into the int data type it is probably an error situation which is worth a unit test. Is Pex able to identify such errors and generate unit tests for those?

Viktor Lukashov
  • 338
  • 1
  • 6

1 Answers1

5

Yes, it's very good at it. When introducing Pex they will frequently use the bug found in the Java library's binary sort routine where it would overflow for very large arrays when finding the new midpoint. A related set of bugs are the leap year bugs and they will commonly use a function from one of Microsoft's own products (Azure?) which displayed one of these in the wild.

Pex catches many different classes of bugs including Overflows, Underflows, Null References, Invalid Argument, and even any custom exceptions you throw in your application. I highly recommend reading the some of the getting started guides and then combine Pex with Code Contracts to make your life much easier in the long run.

Bryan Anderson
  • 15,969
  • 8
  • 68
  • 83
  • 2
    It can even cause hash collisions when using a hash table. It is that good. – usr Oct 14 '12 at 17:31
  • @BryanAnderson, could you share a link for a relevant example? I have tried to run Pex auto-discovery on the method from the question but it did not detect the overflow / underflow conditions. – Viktor Lukashov Oct 16 '12 at 20:45
  • 1
    Take a look at http://channel9.msdn.com/Blogs/Peli/Getting-started-with-Pex-in-Visual-Studio-2008, it's older now but still a good introduction. You also might need to compile with over/underflow checks on to test your add method, I believe they tend to be off by default for most builds. – Bryan Anderson Oct 17 '12 at 05:50