Home > User Support > CoverageMaster winAMS FAQ

CoverageMaster winAMS FAQ

Stub Functions

002_06: How can I change the value of a file static variable from a stub function in a different source file?

Question:

The function I am testing uses a static variable. I want to change the value of the static variable from a stub function, but since the source files are different, the static variable cannot be accessed from the stub function. Is there a workaround for this?

Answer:

For example, consider the function test_sample() and the sub-function sub_sample() below.

----- source.c ----
static int val=0; /* cannot access from stub source file */

void test_sample(void)
{
    val = 0; /* initialized */

    sub_sample(); /* the value of "val" is changed within sub_sample() */

    if( val = 0 )
    {
        /* branch procedure 0 */
    }
    else
    {
        /* branch procedure 1 */
    }
}

void sub_sample(void)
{
    /* procedure to change value of "val" */
}
  

Since the stub function to replace sub_sample() resides in a different source file, the static variable "val" cannot be accessed directly. Therefore, we will use the method described below to indirectly access static variable "val".

1. Create a pointer to a global variable of the same type as "static int val" in the stub function source file.
2. Use the pointer within the stub function to change the value.

--AMSTB_SrcFile.c--

int *ptr_val;

void AMSTB_test_sub(void)
{
    static int INPUT_param;   /* create a variable to enter input test data */
    ptr_val[0] = INPUT_param; /* assign the input test data to ptr_val[0] */
}
  

3. Add to the CSV file’s input variables:
  "ptr_val" (add by "Address" setting)
  "ptr_val[0]" (add by "Pointer with Index" setting).
  "INPUT_param"

4. In the test data for "ptr_val" enter the address of the static variable val. Since the address can be entered by symbol name, enter "source.c /val" to enter the address for static variable val. As a result, during test execution ptr_val points to the same address as static variable val, and ptr_val[0] is the same memory as val.

Note: The format for file static variables is [source filename]/[variable name].

5. In "INPUT_param", enter the test data you want to assign to val.

As a result, when executing the stub function for sub_sample(), the data entered for INPUT_param is assigned to ptr_val[0]. But since ptr_val[0] points to the same memory as "val" you can change the value of "val" from the stub function.

 

Related Technical Tips Search

Other technical tips in GAIO's web site can be searched for using the links below.

Search key word link: File static variable
Technical Tips Google Search

 

Information Links