I have recently inherited a program written in Managed C++ from some guy who just retired. After spending some time digging through it, I can honestly say that at least 95% of it belongs on thedailywtf. However, I am now tasked with modifying it and porting it over to a language I'm comfortable with.
The program itself takes in many many parameters (50+) read in from a file and performs a series of calculations and spits out a report. Once I'm done plowing my way through pages of 3 letter variables (many of them reused for totally unrelated purposes), I need to come up with a way to test my code to make sure it comes up with the same answers as the previous code.
This is most likely wishful thinking, but are there any automated code analysis tools out there that can aid me in this task in any way or form?
I will be porting it over to Java.
I need to come up with a way to test
my code to make sure it comes up with
the same answers as the previous code.
Unit Testing
Since you mentioned that it is about a program that takes parameters in a file and outputs a report, you could generate a few inputs that give sufficient coverage to the portions you want to unit test against the output from the old code.
If something breaks don't forget there is also a chance you have discovered a bug in the old code.
http://www.junit.org/
Progressive Port
I'd go for porting it piece by piece, I kind of feel it will fit your problem since in a report you can grab specific fields and ignore those that you have not ported yet (assuming to output one field/calculation you do not need the whole program to be ported) and verify it against the correct value from the old code (or other correct source in case of bugs in the old code).
I think you should start off writing tests to ensure that the ported code does not break any of the existing report's behavior.
As for automated code analysis tools, the ones most likely to aid you in this area are the data flow analysis tools, which can hypothetically model the flow of data from the file into the variables and out to the report. Unfortunately, a search for such tools resulted in a failure a few weeks back, for me. You might want to have hand-drawn DFDs in places, just to make sure you don't trip over something. Graphviz can help in this area.
It might help copying over the functionality over to Java (I know this sounds perverse), but you could refactor the code carefully under the watchful eyes of PMD, FindBugs and similar code quality assurance tools in Java.
EDIT: I had forgotten the part where in tools of a certain category could be of aid - these are the code slicing tools in the Java world, and I must admit that I haven't used them so far. Here's a list that might help:
- JSlice
- Other related tools listed on the JSlice page.
I asked a somewhat related question some time ago that makes me think of a possible approach: Is there a Findbugs and / or PMD equivalent for C/C++?
What occurs to me is that, if this code is so janky, perhaps it would be a good idea to spend some time to try to repair / refactor it in place. Obviously, if you're headed to Java, it's a hard thing to convince yourself to spend time on improving C++ code that has a short lifespan. That said, if you replace some of the obviously stupid portions with improved code, it'll likely be shorter, tighter, more obviously correct and a whole lot easier to port and to analyze as you try to convince yourself that the new code is functionally equivalent to the old code.
The most useful tools suggested in answers to my question were Splint and Cppcheck.
There was another broader question asked some time ago that may contribute related ideas: What open source C++ static analysis tools are available?
Disclaimer: I've never had to do anything like this before.
Couldn't you first use a C++ to Java converter (a quick google search led me to a product which claims to be able to do that at a very reasonable price if it does in fact work C++ to Java Converter).
Then, create your unit tests and begin refactoring.