Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My question is related to C++ but it comes from Java actually. When I was progra

ID: 655528 • Letter: M

Question

My question is related to C++ but it comes from Java actually.

When I was programming Java I had multiple classes, which were derived from a base "Test" class. Their purpose was to test things - run tests (unit tests/not unit tests).

In order to run a test, I had a public static void main in every class. So the matter of running such a test was to click run in Eclipse/Netbeans, whatever good IDE.

I know this question maybe IDE dependent, it actually boils down to a makefiles, but is it possible to maintain a similar structure in C++ IDEs?

Actually my question is: how do you deal with tests?

Do you put a huge main method with some switch/enum based statements to run the tests or make different build configurations for each test, or have different solution to this?

I know this question won't get a straightforward answer, but I'm curious how to deal with it so I can choose something for myself.

Explanation / Answer

The short answer is "you can't do this in C or C++ as easily as you can in Java."

Java lets you get away with having a main() method in every class because each class is its own namespace and can, in theory, be completely self-contained. When you invoke the JVM on a class, you're telling it which namespace contains the main() you want it to run. So, for example, java Foo really means "run Foo.main()."

Languages that compile to native objects don't work that way. Most systems have a bit of code that gets linked into executables and is responsible for doing a bit of pre-flight prep, calling main() and passing the int that it returns back to the operating system as an exit code. (On many Unixy systems, that code is in a file called crt0.o, or "C runtime zero.") There's no simple way to pass in an alternate main() function at runtime because the linker has to sew all of that up beforehand.

If you want to achieve something similar in C or C++, you have a couple of choices:

One way is to use the preprocessor to subvert the usual things main() does and call a method directly if a symbol is defined:

class Foo {
public:
static void test() { // Your Test Here }
};

int main() {
#ifdef TEST_CLASS
TEST_CLASS ## ::test();
#else
// Do whatever main() usually does
#endif
}
Then, to make this work, you'd have to have your IDE define TEST_CLASS to the name of the class whose test you'd like to run, recompile and run.

The other way is to compile all of your objects into a shared library and add exported-to-C symbols to run your tests. Then you'd need a small program that uses the dynamic loader to fish out the symbol for the test you want to run and call it. How you'd do this is very system-dependent and has no basis in a standard, so I'm not going to recommend it or elaborate on how it's done.