Can Pex automatically discover type overflow / underflow conditions?
Asked Answered
T

1

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?

Throaty answered 14/10, 2012 at 9:35 Comment(0)
E
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.

Expansible answered 14/10, 2012 at 16:40 Comment(3)
It can even cause hash collisions when using a hash table. It is that good.Slackjawed
@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.Throaty
Take a look at channel9.msdn.com/Blogs/Peli/…, 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.Expansible

© 2022 - 2024 — McMap. All rights reserved.