views:

170

answers:

4

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.

+2  A: 

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:

Vineet Reynolds
sad to hear your search came out as a failure too
yx
@yx, you might want to check the updated answer. I had to jog my memory a bit.
Vineet Reynolds
+2  A: 

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).

Bakkal
yes I am well aware of unit tests and I will be making a lot of them, I was hoping for something in addition to that that maybe can perform logical analysis of the code and determine if they are equivalent. I KNOW for a fact that since I'm not 100% familiar with the system yet, there is some idiosyncrasy just waiting to bite me in the ass.
yx
I am not familiar with *Managed C++* (and by extension its related tools) and I think what you will want to analyze is the old code to understand it and get the bigger picture. As for the latter part, just wear super thick pants, almost a requirement for writing ports ;)
Bakkal
its not the actual proting that is a pain in the ass, its the way the code is written. For instance, the majority of the front end + string checking takes place in a 10000 line long header file... where he checks strings character by character. His time/date checker alone (he does not use any of the library methods, he even has his own stupid atan function), is about 200 lines long.
yx
A: 

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.

Fredrick Pennachi
hahahaha, the demo program in the link you provided crashes about 20% of the way into the first .cpp file, at least it was amusing watching it crash :D
yx
Oh well, I guess it was worth a shot :)It still might not be a bad idea if there are any good C++ to Java generators out there, perhaps someone else knows of one.
Fredrick Pennachi
+1  A: 

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?

Bob Cross