How to Test Variadic Functions

Question:
How can I test variadic functions (functions which take a variable number of arguments)?
Answer:

To test variadic functions you can use unit appendix user code to create a wrapper function for each combination of argument types you wish to use to call your variadic function. For example, let's say you want to test a function called formatted_print that works like sprintf but returns a malloc-ed string.

The prototype is:


char* formatted_print (const char* format, ...);


Let's say you want to test this function with 1 and 2 additional arguments of type int and char*, in all possible combinations. You would therefore add the following code to your unit appendix user code. (The function suffixes are based on the specifiers used in the format string which indicate the type of the given argument.)
 

char* formatted_print_d (const char* format, int arg1)
{
  return formatted_print( format, arg1 );
}
char* formatted_print_s (const char* format, const char* arg1)
{
  return formatted_print( format, arg1 );
}
char* formatted_print_d_d (const char* format, int arg1, int arg2)
{
  return formatted_print( format, arg1, arg2 );
}
char* formatted_print_d_s (const char* format, int arg1, const char* arg2)
{
  return formatted_print( format, arg1, arg2 );
}
char* formatted_print_s_d (const char* format, const char* arg1, int arg2)
{
  return formatted_print( format, arg1, arg2 );
}
char* formatted_print_s_s (const char* format, const char* arg1, const char* arg2)
{
  return formatted_print( format, arg1, arg2 );
}


If you add the code to unit appendix user code after you have built the environment, rebuild the environment to have the new functions appear in your test case tree. After rebuilding the environment, you can add test cases for the new functions as you normally would. 
 



Article ID: 1511
Last updated: 2019-11-13
Revision: 6
VectorCAST -> C++ Unit Testing -> How to Test Variadic Functions
https://kb.vector.com/entry/1511/