Home > Archive > Fortran > April 2007 > Including test "main" programs in modules?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Including test "main" programs in modules?
|
|
| David Simpson 2007-04-25, 8:04 am |
| 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
| |
| Pierre Asselin 2007-04-25, 8:04 am |
| David Simpson <david.simpson@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
| |
| Gordon Sande 2007-04-25, 7:05 pm |
| On 2007-04-25 08:27:04 -0300, David Simpson <david.simpson@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.
| |
| Fly Away 2007-04-26, 4:08 am |
| 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.
| |
| David Simpson 2007-04-26, 4:08 am |
| 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
| |
| David Simpson 2007-04-26, 4:08 am |
| 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
|
|
|
|
|