0

Can anyone tell me what this piece of code is doing?

#ifdef TF_UNIT_TEST
/*lint --emacro( (652), TF_STUB) */

#define TF_STUB(fn) __tf_ ## fn

#else

#define TF_STUB(fn) fn
#endif
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Sohrab Ahmad
  • 145
  • 3
  • 8
  • 1
    `##` concatenate two string tokes by preprocessor. [asked](http://stackoverflow.com/questions/5256313/c-c-macro-string-concatenation) – Grijesh Chauhan Jul 26 '13 at 11:59
  • Token pasting operator `##` : http://msdn.microsoft.com/en-us/library/09dwwt6y(v=vs.80).aspx – P0W Jul 26 '13 at 12:02

3 Answers3

4

It's defining functions with two different names depending on whether the TF_UNIT_TEST macro is defined or not.

Functions in the main code body will be declared as eg

int TF_STUB(my_function) (int a, int b)
{
}

If the macro is defined, then the function will be named as __tf_my_function, and if not then it will be named just my_function.

Vicky
  • 12,934
  • 4
  • 46
  • 54
2

Others have got the technical behavior... but in your questions I see an implicit "why?".

This looks like a trick used to help unit testing along. This happens in environments where production calls a function that does something very difficult to replicate, or has undesired side effects, in unit testing done by developers. Thus the developer supplies alternate "safe" functions.

Another reason is that the unit test functions take additional actions before calling the "real" routines. Additional logging or testing of data come to mind... things that make unit testing easier. If you are writing a graphic program that does complex things on a local screen it can be far easier to log the colors, etc., in a text file than decoding the actual video.

Gilbert
  • 3,740
  • 17
  • 19
1

Its a macro definition where

#ifdef TF_UNIT_TEST //If this macro is defined then define follows

#define TF_STUB(fn) __tf_ ## fn //it defining this macro as _tf_fn (concatinating what ever parameter it has in IF_STUB(fn))

#else

#define TF_STUB(fn) fn //Else it defined as fn
#endif

In program it looks like

   #define TF_UNIT_TEST
#ifdef TF_UNIT_TEST

#define TF_STUB(fn) __tf_ ## fn

#else

#define TF_STUB(fn) fn
#endif

int main()
{
        TF_STUB(stackoverflow);
}

compile using follows to see the preprocessed output of your programm

 gcc -E pre1.c

output 
 1 "pre1.c"
 1 "<built-in>"
 1 "<command-line>"
 1 "pre1.c"
 11 "pre1.c"
int main()
{
 __tf_stackoverflow; //its appended
}
pradipta
  • 1,718
  • 2
  • 13
  • 24