|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Including test "main" programs in modules?
I would like to have small "main" programs at the end of each module I write, which basically run just that module for testing and as examples. At the moment I have to comment these out when applied as part of larger code which is using these modules, or to make separate test programs for each module. Is there an elegant way to allow such test code to be placed after each module, but be ignored when compiling multiple modules? Thanks Dave Simpson
David Simpson <david.simp @met.no> wrote: > I would like to have small "main" programs at the end of each module I > write, which basically run just that module for testing and as > examples. [ ... ] > Is there an elegant way to allow such test code to be placed after > each module, but be ignored when compiling multiple modules? I do that all the time in C with conditional compilation, but in Fortran... I can do it on any one platform but whatever scheme I come up with would certainly break on a new compiler. > At the moment I have to comment these out when applied as > part of larger code which is using these modules, or to make separate > test programs for each module.
I think you're better off with separate test programs. Note that even in C, the GNU people use separate tests ("make check" in automake). Too bad. Personally I like to keep the test code close to the payload. -- pa at panix dot com
On 2007-04-25 08:27:04 -0300, David Simpson <david.simp@met.no> said: > I would like to have small "main" programs at the end of each module I > write, which basically run just that module for testing and as > examples. At the moment I have to comment these out when applied as > part of larger code which is using these modules, or to make separate > test programs for each module. > Is there an elegant way to allow such test code to be placed after > each module, but be ignored when compiling multiple modules? > Thanks > Dave Simpson
You could just call the test code "Mod_Test" and have it be a subroutine. You are left with having to call Mod_Test from your test driver which is a USE and a CALL. The presence of an unused subroutine is not harmful unless its bulk worries the compiler. A logical parameter called "Debug" and if statments allow the internal tests to remain in the source text and easily turned into dead code.
David Simpson wrote: > I would like to have small "main" programs at the end of each module I > write, which basically run just that module for testing and as > examples. At the moment I have to comment these out when applied as > part of larger code which is using these modules, or to make separate > test programs for each module.
The two ways I think of now, are using the C preprocessor, #ifdef TEST #endif You can set environment variables from the command line with most compilers. The other is D lines, I believe inherited by most current compilers from a DEC feature. At least gfortran, g95, and ifort have it. Instead of using C for a comment, you use D in column one for debugging (or testing) code, with a compile time option to treat it as a comment or code. -- glen
On Apr 25, 5:27 am, David Simpson <david.simp@met.no> wrote: > I would like to have small "main" programs at the end of each module I > write, which basically run just that module for testing and as > examples. At the moment I have to comment these out when applied as > part of larger code which is using these modules, or to make separate > test programs for each module. > Is there an elegant way to allow such test code to be placed after > each module, but be ignored when compiling multiple modules? > Thanks > Dave Simpson
I would say preprocessing your code is the best way. Try smth like module abc contains subroutine a1 .... end subroutine a1 #ifdef TEST subroutine testing ... end subroutine testing #endif end module abc then if you compile with -DTEST option you'll get the testing subroutine in. Otherwise it will be ignored. Cheers, Victor.
On 25 Apr, 15:08, Gordon Sande <g.sa@worldnet.att.net> wrote: > On 2007-04-25 08:27:04 -0300, David Simpson <david.simp @met.no> said: > You could just call the test code "Mod_Test" and have it be a > subroutine. You are left with having to call Mod_Test from your > test driver which is a USE and a CALL. The presence of an unused > subroutine is not harmful unless its bulk worries the compiler. > A logical parameter called "Debug" and if statments allow the internal > tests to remain in the source text and easily turned into dead code.
Many thanks! That's sounds like a clean and portable way of doing this :-) Dave
Small update. I tried this, but ran into a problem. I had hoped to have a module named "test_module" (with contains "subroutine test_mod()")at the end of each module file, along with the very simple Tester.f90: program tester use Test_module call test_mod() end program tester but the compiler complains about multiple definition when I try to compile my "real" code. I can modify this idea to have different names, e.g. test_init at the end of my "init" module, test_utils at the end of the "utils" module, etc. and then modify Tester.f90 appropriately. This is still a neat solution, although it would have been nicer if the compiler had recognised that the multiple test_modules were not being used at all for the real work. Dave
|
 |
 |
 |
 |
|