C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

module stub generator for testing?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
Marco
Guest





PostPosted: Tue Dec 13, 2005 12:49 pm    Post subject: module stub generator for testing? Reply with quote



I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

thanks

Marco
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
usenet@zevv.nl
Guest





PostPosted: Tue Jan 10, 2006 3:35 am    Post subject: Re: module stub generator for testing? Reply with quote



Quote:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

Not aware of anything that does precisely this, but it'd be trivial to
write in either C or Perl. Even in less then 2 days.


--
:wq
^X^Cy^K^X^C^C^C^C
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Back to top
Chuck F.
Guest





PostPosted: Tue Jan 10, 2006 3:35 am    Post subject: Re: module stub generator for testing? Reply with quote



Marco wrote:
Quote:

I am looking for an open-source or free tool that parses a C
header file (.h) for a module and creates a .c file with the
functions stubbed out ( ideally with dummy returns for
non-void). This will be used for testing purposes.

I have googled with little success. ( for a C++ class example
google "stubgen").

C++ is off-topic here. The tool required is your editor. Simply
eliminate the macros, typedefs, etc. and append a variant of:

{
return 0;
}

to each function header. Rename the resultant file from .h to .c.
Add a line "#include <original.h>" at the head.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archives/2005/11/sonys_drm_rootk.html
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Back to top
Malcolm
Guest





PostPosted: Tue Jan 10, 2006 3:35 am    Post subject: Re: module stub generator for testing? Reply with quote

"Marco" <prenom_nomus (AT) yahoo (DOT) com> wrote
Quote:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

Unfortunately it is not possible to write a general-case "stub generator".

C functions are allowed to modify values passed to them by pointers, and the
structures pointed to can be arbitrarily complex.
Also, it is permitted to call functions by indirection.
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Back to top
Michael Tiomkin
Guest





PostPosted: Tue Jan 10, 2006 3:35 am    Post subject: Re: module stub generator for testing? Reply with quote

Marco wrote:
Quote:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

I think you can cannibalize this stubgen using a couple of scripts:

1. The first script adds a line
class TemporaryUglyClass {
before the .h file, and the line
};
after the .h file. Don't change the file, just send all the stuff to
to the standard output. It's just 3 lines of awk or shell script.
2. The 2nd script reads the result of 'stubgen', changes any
"Method: TemporaryUglyClass::" string to :Function: " string and
removes all other "TemporaryUglyClass::" strings. It's also 3 lines of
awk.
3. The 3rd script runs the first script on your .h file, runs 'stubgen'
on the result of the 1st script, and runs the 2nd script on the result
of 'stubgen'. This can be one line of a shell script.
1st_scr Input_file_name | stubgen | 2nd_scr > stubs_file_name.c

Michael
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Back to top
Nick C
Guest





PostPosted: Tue Jan 10, 2006 3:36 am    Post subject: Re: module stub generator for testing? Reply with quote

I would recomment getting a perl book. This is possible in a few lines...

"Marco" <prenom_nomus (AT) yahoo (DOT) com> wrote

Quote:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

thanks

Marco
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.
--

comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Back to top
Rahul Chandok
Guest





PostPosted: Tue Jan 10, 2006 3:38 am    Post subject: Re: module stub generator for testing? Reply with quote

Hi,

You can try the following logic :

1) Open your header file in Read mode.
2) Create the c stub file. (Open in write mode)
3) Read the header file line by line and search for character "(" or
")" in the line.
4) If you don't find this character then ignore the line, otherwise
write the whole line to the C file, Don't write the last character ";"
to the c file.
5) Then search for the void in the beggining of the line, if it is not
there then
write the following in the c file
"{
return 0;
}"

else
just write the following in the .c file
"{
return;
}"

HTH
Rahul
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
WillerZ
Guest





PostPosted: Tue Jan 10, 2006 3:38 am    Post subject: Re: module stub generator for testing? Reply with quote

Marco wrote:
Quote:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

This 2-line unix shell trick will work to an extent (the extent is
described below):

prompt$ grep void module.h | sed -e 's!;! { }!' > module-stub.c

prompt$ grep ';' module.h | grep -v '}' | grep -v typedef | sed -e 's!;!
{ }!' | sed -e 's!\([[:alnum:]]+\)\(.*\)!\1\2 { \1 a; return a;
}!' >> module-stub.c

It assumes that all of the function prototypes are single-lines and that
they have parameter names and not just types. It also assumes that a
couple of shell lines I wrote without testing are going to do what I
think they do. You may just need to change the number of characters
if it doesn't. The other caveat with this is that it'll break on any
function which returns a non-typedef'd enum, struct or union -- it's
quite easy to fix that if you're at a shell prompt where you can play
around, but as I'm not it's likely I'd introduce (more) confusion if I
tried.
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Back to top
Jasen Betts
Guest





PostPosted: Wed Jan 25, 2006 11:10 am    Post subject: Re: module stub generator for testing? Reply with quote

there is no simple general solution

consider the following header file


#ifndef __foo
# define __foo

# ifdef bar
# define goo
# define gux
# endif
/*
ignore getchar();
*/
# ifdef gux

void blah(void);

# undef gux
# else

void blah(int x);

# endif

#endif



Bye.
Jasen
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Francis Glassborow
Guest





PostPosted: Wed Jan 25, 2006 11:10 am    Post subject: Re: module stub generator for testing? Reply with quote

In message <clcm-20060109-0027 (AT) plethora (DOT) net>, Rahul Chandok
<chandok.r (AT) gmail (DOT) com> writes
Quote:
5) Then search for the void in the beggining of the line, if it is not
there then
write the following in the c file
"{
return 0;
"

struct X {
int i;
int j;
} myfunc();

I think fails under your method.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Back to top
Marco
Guest





PostPosted: Wed Jan 25, 2006 11:13 am    Post subject: Re: module stub generator for testing? Reply with quote

update (and thanks to WillerZ for an alternative solution)
by the way I consider "trivial" as maybe an hour or two to do, this
wasn't because I only program in Perl once in a while.
=============================================
ANN: C module stub generator

There are times when you would like to compile and unit test your code
on a Linux/Windows/Mac desktop when the target hardware or a hardware
sim is not available.

This script will allow you to create code stubs for RTOS calls,
unimplemented code, etc. It can create a global variable for your unit
test driver to manipulate the return from a stubbed function.

It can be found at comp.sources.d , it is written in perl.

Please note that you have to remove the REM on the first line (pretty
obvious) and it has been noted that the posted code gets wrapped around

incorrectly (try opening up your window width or get it via a
newsreader). The getpwnam function is unimplemented on Windows so hack
in something you like. I created this on Linux.

If someone would like to host it, feel free to post a clean version on
a web-site somewhere (I don't have a web-site).

comp.sources.d listing here:
http://tinyurl.com/atsqm


enjoy
--
comp.lang.c.moderated - moderation address: [email]clcm (AT) plethora (DOT) net[/email] -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group