Exploding Link Stubs in C 55
When you’re working in a batch of legacy C code, it’s often hard to build and test just a part of it. You may want to build some set of functions, but then you discover that they call other functions, and those functions call yet other functions. When link dependencies are very bad, you may discover that you’re linking in the entire system.
Fortunately, there is a way out of this. Pick a set of files that appear to be a “component” and attempt to build them into an executable along with a simple main function.
You will get link errors.
Create a file called <componentname>_stubs.c
and then go through each of the link errors. Look up the full declaration of the variable or function the error describes. Create a stub for each of them in the stub file.
Here’s an example of a function stub:
int irc_send_ppr(struct ppr *pprn, int nSize)
{
}
The only problem with this is that you have to place something in the stub function. For functions which return values you need to, at the very least, provide a return value. But what should it be?
int irc_send_ppr(struct ppr *pprn, int nSize)
{
/* is negative -1 okay? If it isn’t, how will we know? */
return -1;
}
There is alternative ++:
int irc_send_ppr(struct ppr *pprn, int nSize)
{
assert(!”irc_send_ppr boom!!”);
}
This is an exploding link stub. When you have it in place, you will know when a function you’re testing calls a stub. When you see the boom you go to the code and replace the assert with a better stub implementation.
When you use link stubs, you have to be able to build your component two different ways. Production builds link to the rest of the code. Test builds link to the stubs and a testing main. There’s no need to stub out all of the external functions that a component uses. Many low level functions can remain direct calls, but stubbing out calls to other high level components can give you a decent amount of leverage as you try to get an area under test.
Fortunately, for any given component you only have to go through the massive grunt work once. Once you do, you can reap the reward of easier testing in that component forever.
++ Note: If you are using C99, you can genericize this code by using the __func__
predefined macro. In any function, __func__
expands to the name of the enclosing function.
If you are using any modern C or C++ compiler, you can genericize this code by using the non-standard but universally supported keyword FUNCTION. In any function, FUNCTION expands to the name of the enclosing function, just like func, but in the form of a string literal ready for string concatenation. (C99’s func keyword is not a macro, and does not expand to a string literal so it cannot be concatenated with adjacent strings.)
#define EXPLODE assert(!”Boom in ” FUNCTION);
Actually, to make this really easy, you don’t need to get the right types. Your C compiler/linker will give you a list of undefined dependencies. You can then use some sed/awk/perl/whatever magic to transform this into a C file that will solve these. All you need to do is generate a file like this:
It doesn’t matter that their signatures aren’t right; the linker only sees them as symbols. I’ve been using this method successfully in my own project where I need to pull code out of a large ecosystem (the kernel) and link it into another.
@Anonymous: Thanks. It’s a shame that
__func__
is defined the way it is.@Sam: Thanks for bringing that up. I’ve done that in the past (and since forgotten about it). Great way to get the initial link. The only thing is, I want to get the real signature in whenever I write a test that explodes a stub. Fair call, though, that you don’t have to do it all at once.
The dependency breaking techniques in your book have been great for getting me ‘unstuck’. Thanks!
This is Interface Discovery. It can be a very useful way of reasoning about a system—http://connextra.com/aboutUs/mockobjects.pdf
just have a try
I use to read your posts.They’re extremely informative and useful.Many thanks for giving us this kind of quality expertise.Waiting for the following fascinating posts.
Most problems happens before testing, nice post anyway!
would you mind updating your blog with more information?
would you mind updating your blog with more information?
Thank you very much for this article.I like its.As to me it’s good job.
one day give patients’ bodies louis vuitton ties for men the ability to make the rubber watches missing enzyme. However, gene therapy nike girls trials suffered an enormous blow gucci shoes for women with the death of 18-year-old Hermes Crocodile Jesse Gelsinger, a young man
you can think of traits as analogous to Java interfaces, but with implementations.
This is a great inspiring article.I am pretty much pleased with your good work. ugg classic cardy You put really very helpful information. Keep it up. Keep blogging. ugg outlet
Apple’s official white iPhone 4 is likely to release untile next year. But in this autumn there’s already got white iphone 4 available.
JAY Follow laptop accessories the detail tips below, you can increase the laptop battery life of a year or more. 1. The laptop battery first thing you should care about the
When link dependencies are very bad, you may discover that you’re linking in the entire system.
This is a superb posting, I identified your weblog web page executing study google for any related topic and came to this. I never thought I would agree with this opinion, but I’m starting to view things from a different view. I have to research more on this as it seems very interesting. I couldnt uncover to substantially supplemental info and details on this write-up, so it was pleasant to find this 1. I’ll in all probability finish up currently being again once more to appear at another content articles that you’ve got yet another time. yhm20110408 Disount Christian Louboutin
It is another informative post. I like it. Thanks. roofing venice
your post is useful for us. Thanks
internette görüntülü olarak okey oyunu oyna, gerçek kisilerle tanis, turnuva heyecanini yasa.
Thank you for blog I read it always :-)
The Monster Beats By Dr. Dre Studio Headphones are classic shoes.You will be proud of owning them. Don’t hesitate!Take Monster Beats By Dr. Dre Pro home!
Have the christian louboutin patent leather pumps is a happy thing.
We’ve go through your own exceptional blog which posting on-line, have numerous good friends combined with me to examining your own article content. Whatever we are, as well as whatever we would like, the principle concept you’ve recently been displayed was satisfied by us. Thanks much for sharing so perfect point of view, and it create us get a feeling of relaxed and ease.
When I see a lot of methods and C codes It’s hard for me to understand some codes. That’s an example of me interpreting without reading. You have to analyse that thoroughly and quickly.
When I see a lot of methods and C codes It’s hard for me to understand some codes. That’s an example of me interpreting without reading. You have to analyse that thoroughly and quickly.
All among us realise that if you MBT boots or shoes within get hold of, Jimmy Choo Sandaleseducation-women’ vertisements mbt tunisha providing may easily really encourages lymphatic circulation,Jimmy Choo Bottines you’ chemical in all probability more significant receive boots or shoes clearance retail store as a result of MBT while it a good number of at no cost submitting in combination with MBT boots or shoes are almost always pay for and also profit designed notnaxcanada goose outlet.
When I at first left a comment I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three notification emails with the same comment. Is there any way you can take away people from that service? Thanks a lot!
It’s great when you find relevant content .I can’t stay without admiring your post its really awesome thanks for sharing such a nice content.
This is very much impressed with the great services in this blog that to using the great technology in this blog. I am really interesting info is visible in this blog that to sharing the great services in this blog that to using the great services in this blog. This is very much impressed with the great technology is visible in this blog that to sharing the great services in this blog Executive Director Job Description| General Manager Job Description| Consultant Job Description Webmaster Job Description
This is very much enjoyed for the great services in this website. I am very much happy for providing the great technology is visible in this website that to using the great services in this blog Webmaster Job Description
Just want to say what a great blog you got here!I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!
People were so keen to get investment. In those days, there was quite significant unemployment in Northern Ireland, and that had been the general pattern in Northern Ireland for many, many years. John Hume
I liked you blog so im going bookmark it with my prefered websites, you have posted an amazing posts so thank you I liked you blog so im going bookmark it with my prefered websites, you have posted an amazing posts so thank you
I learned something new just by trying your codes and tweaking them just a little bit, it was really all a good help, thanks for those info and kudos!
Thank you for the efforts you been putting on making your site such an interesting and informative place to browse through. I’ll be visiting your site again to gather some more valuable information.
explore a way to backup iPhone contacts
I have been searching all over the blogs for a relevant article related to this topic for a paper I am writing. I’ll be sure to come back to your blog for other valuable information next time I need solid information on this subject. layered hairstyles
This article is GREAT it can be EXCELLENT JOB and what a great tool!
Siècle Série classique américain de los angeles marque Ralph Lauren hommes d’hiver communiqué de 2012, los angeles principale saison des longues de design guy cuir, créateurs intelligents ajouté fermeture éclair dans los angeles couche moyenne de cuir, veste durante cuir cuando longtemps devient instantanément courtroom. Los angeles couleur beige et noir également, put créer united nations design parfait guy scandinaves.http://www.vetementpoloralphlauren2012.net
This article is GREAT it can be EXCELLENT JOB and what a great tool!
“It is perfect time to make some plans for the future and it’s time to be happy. I have read this post and I really really like the topic about gadgets. “
Australia Beats By Dre Studio dr dre beats headphones beats studio beats pro beats solo hd pro headphones music Official store Monster Beats By Dre Pro
I’ll be visiting your site again to gather some more valuable information.
Thanks for taking this opportunity to converse about this, I feel fervently about this and I like learning about this subject. Please, as you gain facts, please update this blog with more information. I have found it extremely useful.
I like very much your way of presentation.. I got more useful information on this blog.. Thanks to sharing the useful information….
Exploding Link Stubs in C 46 good post24
Exploding Link Stubs in C 47 hoo,good article!!I like the post!93
I like very much your way of presentation.. I got more useful information on this blog.. Thanks to sharing the useful information….
I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing
Finding the perfect pair of mens shoes for casual wear Christian Louboutin sale nyc often turns out to be a very tedious job. Still, it Christian Louboutin silver peep toe pumps is one of the important aspects, if you really want Christian Louboutin slingback pumps to look fashionable. Here are few wisdom to assist you in the task.
Wings headset stand out at the end of the party, we put on this pair of wings headphones. It can make head “long” a pair of Christian louboutin Miami, let you absolutely become PROM queen.
Purses and bags are some of the most famous accessories for women?
http://www.outfitscosplay.com/cosplay-catalog/devil-may-cry-cosplay Deluxe Devil May Cry Cosplay Costumes for Sale.Find your favorite characters and cosplay outfits from all the popular anime and games.
With more than 20 years of experience, Intertech provides an extensive integrated operational ability from design to production of molds 100% made in Taiwan. Additional to our own mold making factory, we also cooperate with our team vendors to form a very strong working force in Taiwan.
For the overseas market, we work very closely with local representatives in order to take care of the technical communication and after-sales service to our customers. We also participate in the EUROMOLD & FAKUMA exhibitions and meet our customers every year in Europe. By concentrating on mold “niche markets”, we play a very useful mold maker role from the Far East whenever customers want to develop their new projects. We provide services from A to Z to our customers on a very economic cost and effect basis.