Stub Definition
Component Testing for C
The following simulation describes a set of function prototypes to be simulated in an instruction block called DEFINE STUB ... END DEFINE:
HEADER file, 1, 1
BEGIN
DEFINE STUB file
#int open_file(char _in f[100]);
#int create_file(char _in f[100]);
#int read_file(int _in fd, char _out l[100]);
#int write_file(int fd, char _in l[100]);
#int close_file(int fd);
END DEFINE
The prototype of each simulated function is described in ANSI form. The following information is given for each parameter:
-
The type of the calling function (char f[100] for example, meaning that the calling function supplies a character string as a parameter to the open_file function)
-
The method of passing the parameter, which can take the following values:
-
_in for an input parameter
-
_out for an output parameter
-
_inout for an input/output parameter
These values describe how the parameter is used by the called function, and, therefore, the nature of the test to be run in the stub.
-
The _in parameters only will be tested.
-
The _out parameters will not be tested but will be given values by a new expression in the stub.
-
The _inout parameters will be tested and then given values by a new expression.
Any returned parameters are always taken to be _out parameters.
You must always define stubs after the BEGIN instruction and outside any SERVICE block.
Modifying Stub Variable Values
You can define stubs so that the variable pointed to is updated with different values in each test case. For example, to stub the following function:
extern void function_b(unsigned char * param_1);
Declare the stub as follows:
DEFINE STUB code_c
#void function_b(unsigned char _out param_1);
END DEFINE
Note Any _out parameter is automatically a pointer, therefore the asterisk is not necessary.
To return '255' in the first test case and 'a' in the second test case, you would write the following in your test script:
SERVICE function_a
SERVICE_TYPE extern
-- By function returned type declaration
#int ret_function_a;
TEST 1
FAMILY nominal
ELEMENT
VAR ret_function_a, init = 0, ev = 1
STUB function_b (255)
#ret_function_a = function_a();
END ELEMENT
END TEST -- TEST 1
TEST 2
FAMILY nominal
ELEMENT
VAR ret_function_a, init = 1, ev = 0
STUB function_b ('a')
#ret_function_a = function_a();
END ELEMENT
END TEST -- TEST 2
END SERVICE -- function_a
Simulating Global Variables
The simulated file can also contain global variables that are used by the functions under test. In this case, as with simulated functions, you can simulate the global variables by declaring them in the DEFINE STUB block, as shown in the following example:
DEFINE STUB file
#int fic_errno; /* simulated global variable */
#char fic_err_msg[100]; /* simulated global variable */
#int open_file(char _in f[100]);
#int create_file(char _in f[100]);
#int read_file(int _in fd, char _out l[100]);
#int write_file(int fd, char _in l[100]);
#int close_file(int fd);
END DEFINE
The global variables are created as if they existed in the simulated file. The global variables must be initialized within the .ptu test script.