Compare the execution paths of the same code under different inputs

I am debugging a very complex C ++ function that gives me some unexpected results with some inputs. I would like to compare code execution under different inputs to find out which part is causing me an error. A tool that can compare code execution paths is what I'm looking for. Please let me know if such a tool exists. Or else, if there are some methods that I can use to accomplish the same thing?

To describe my problem specifically, here I use a far-fetched example.

Say this is a function in pseudo code ,

double payTax(double income) { if (income < 10000) return noTax(); else if ( 10000 < income < 30000) return levelOneTax(); else if (30000 < income < 48000) return levelTwoTax(); else return levelThreeAboveTax(); } 

Given input 15000, the function calculates the correct tax amount, but somehow input 16000 gives an erroneous tax amount. Presumably, entering 15000 and 16000 will cause the function to go through the same execution paths; on the other hand, if they go in different ways, then something should violate the function. Therefore, a tool that compares execution paths will reveal enough information that could help me quickly identify the error. I am looking for such a tool. It is preferably compatible with Visual Studio 2010. It would be better if such a tool also saved variable values.

PS debugging is the last thing I want to do, because the code base I work with is much larger and more complex than the trivial payTax example.

Please, help. Thanks.

+6
source share
2 answers

The keywords you are looking for are “code coverage” or “coverage analysis” or “code coverage analysis”.

Which tool you use will naturally depend on the rest of your environment.

+3
source

The tool you want is printf or std::cerr !

And you have a significant error in your code: an instruction like if ( 10000 < income < 30000) will not work properly! You want to write it as if( 10000 < income && income < 30000 ) .

And to continue testing simply, use curly braces, as in:

 if( 10000 < income && income < 30000 ) { return levelOneTax(); } else if( ... 

Because then it will be much easier to add debug output, as in:

 if( 10000 < income && income < 30000 ) { std::cerr << "using levelOneTax for income=" << income << std::endl; return levelOneTax(); } else if( ... 

EDIT

BTW: "a tool that compares execution paths would find enough information [...]", but in the sense that you would expect, such a tool would find the RELEVANT information to process. The best you can do is debug and verify that your code does what you expect from it. The code coverage tool is likely to be too large for your case (and also such tools are not cheap).

-1
source

All Articles