Stub simulation

Component Testing for C

Stub simulation is based on the idea that certain functions are to be simulated and are therefore replaced with other functions which are generated in the test driver. These generated functions, or stubs, have the same interface as the simulated functions, but the body of the functions is replaced.

These stubs have the following roles:

  • To store input values to simulated functions

  • To assign output values from simulated functions

To generate these stubs, the Test Script Compiler must have the following information:

  • The prototypes of the functions that are to be simulated from the stub point of view.

  • A method of passing each parameter (input, output, or input/output).

When using the Component Testing Wizard, you specify the functions that you want to stub. This automatically adds the corresponding code to the .ptu test script. On execution of the test, Component Testing for C generates the stub in the test driver, which includes:

  • a variable array for the input values of the stub

  • a variable array for the output values of the stub

  • a body declaration for the stub function

Function Prototypes

When generating a stub for a function, HCL® OneTest Embedded considers both the original and the simulation version of the first prototype of the function that is encountered, which can be:

  • The declaration of the function in an included header file.

  • The declaration DEFINE STUB statement in the .ptu test script, which declares how the stub is used by the application under test and how the check code is generated.

If the first declaration is not found HCL® OneTest Embedded considers that original is identical to the simulated function.

Both can differ when the original prototype does not declare explicitly how the application uses it. For example, a void * parameter can be used as char* or int *.

It is possible to stub a function that is located in the source file under test. In this case, the source file must be included in the .ptu test script. If an existing body of stubbed function is encountered in the source code under test, HCL® OneTest Embedded renames the existing body to _atu_stub_ <function-name> and the stubbed version of the function is used in the test driver.

An example is provided in the StubInUseFunc test node of the Stub C example project.

Note: To comply with the DO178B standard, the source code under test must be compiled separately from the .ptu. If you choose to include the source file in the .ptu script, then you will need to justify this with the DO178B authority.

Passing Parameters

Passing parameters by pointer can lead to problems of ambiguity regarding the data actually passed to the function. For example, a parameter that is described in a prototype by int *x can be passed in the following way:

int *x as input ==> f(x)

int x as output or input/output ==> f(&x)

int x[10] as input ==> f(x)

int x[10] as output or input/output ==> f(x)

Therefore, to describe the stubs, you should specify the following:

  • The data type in the calling function

  • The method of passing the data

Example

An example project called Stub C is available from the Examples section of the Start page. This example demonstrates the use of stubs in Component Testing for C. See Example projects for more information.

Related Topics

Stub Definition in C | Stub Usage in C | Sizing Stubs | Replacing Stubs | Advanced Stubs | Example projects