Small introspective program

Dave Mankins dm at think.com
Wed Mar 13 10:14:23 AEST 1991


This is not quite the same thing, but it is a hack I have found very useful
when spinning small test programs.  

You start the program (e.g., ``foo.c'') with:

#ifdef notdef

  cc $0 -g -o `basename $0 .c` -Ineeded-includes -lneeded-libraries 

  exit;

#endif notdef

#include <stdio.h>
...

(all bound to an emacs function, insert-self-compile, of course)

Then invoke it with: 

	sh foo.c

This is much less labor-intensive than editing a Makefile (and re-editing it,
if the source file should move to another directory).

Even in semi-formally maintained code, I have found this most useful in files
that make up components of libraries, in this form:

#ifdef notdef

  cc $0 -DTEST -g -o `basename $0 .c` -Ineeded-includes -lneeded-libraries 

  exit;

#endif notdef

	/* library module source ... */

#ifdef TEST

main() {
	.... code to test the call library functions with test inputs and 
	     check the results ....
}

#endif /* TEST */

This allows your library modules to be self-testing, a great time-saver in the
later phases of development.
--
david mankins (dm at think.com)



More information about the Alt.sources mailing list