Home > User Support > CoverageMaster winAMS FAQ

CoverageMaster winAMS FAQ

Stub Functions

301_04: How can I modify a static variable in another file from a stub or dummy function?

Question:

Using a stub function I want to modify a static variable of the function being tested. However, I cannot modify the static variable because the stub function is in a different file. How can I modify a static variable in another file from a stub function?

Answer:

Static variables with file-scope have their address statically allocated. Therefore, it is possible to create a pointer to this address to access the static variable from the stub function. Below is an example of the procedure.

1. In the stub file create a global pointer of the same type as the static variable (static int staticValue in the example below so we'll create int* inputPointer).

2. Add the pointer to the Input Variables of the Test CSV file by "Address".

3. Enter the address into the test data for the pointer added by address. Instead of entering the address by numeric value, the address can be specified by symbol name (in the example below, "staticValue").

4. In the stub function assign the desired value to the pointer variable (in the example below, inputPointer[0] = staticValue_data).

Example Code:

main.c (Source file to be tested)

static int staticValue; // file-scope static variable

// Function to be tested
int ParentFunc(void){
    SubFunc(); // subfunction that modifies staticValue
    Output_Value = staticValue;
}

//Subfunction
int SubFunc(void){
    if(staticValue == 0) staticValue = 1;
    else staticValue = 0;
}

----------------------------------------------------------------------------------------------------------------

AMSTB_SrcFile.c (Stub File)

int *inputPointer;    // pointer to access staticValue in main.c
                      // will be set to point to staticValue in the test data
int staticValue_data; // variable to store the test data to assign to staticValue

// Stub function
int AMSTB_SubFunc(){
    inputPointer[0] = staticValue_data; // assign test data through the pointer
}

 


Click to enlarge


Click to enlarge

 

Related Technical Tips Search

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

Search key word link: static variables
Technical Tips Google Search


Information Links