Using Test Doubles

From STRIDE Wiki
Revision as of 22:17, 20 August 2009 by Timd (talk | contribs) (Added tag: 'For Developers...')
Jump to navigation Jump to search

Introduction

The Test Double feature provides a means for intercepting C language functions on the target, and directing the call to a substitute function with identical parameters and return value. The use case is a unit test where the function under test uses a ("C") function during its execution, and this dependency is simulated by a substitute or double function during testing. The unit test is able to control the substitution of the dependency during run time, and thereby verify the behavior of the function under test. The following sample illustrates the relationship of the function under test and a dependency:

// depend.h
int depend(int x);
// depend.c
#include "depend.h"
 
int depend(int x)
{
    return x + x;
}
// test.h
int test(int x, int y);
// test.c
#include "test.h"
#include "depend.h"
 
int test(int x, int y)
{
    return depend(x) * y;
}

In the above sample, test() is the function under test and depend() is a dependency candidate for doubling.


The steps required to achieve doubling of a dependency function are as follows:

  1. Configure the double parameters using SCL pragmas
  2. Create the double intercepts in the IM
  3. Switch to/from the double function during runtime

Configuring the Double Using SCL

The syntax of the scl_func and scl_function pragmas have been expanded to include new optional attributes that allow the specification of function interception parameters.

In the above sample, depend() needs to be SCL captured and enabled for interception in a manner to be doubled.

// depend_scl.h
#include "depend.h"

#pragma scl_function(depend, "DEFINITION", "IMPLICIT", "TEST_GROUP")

Creating Double Intercepts in the IM

If a function has been configured as a double candidate using SCL as outlined in the above step, then the next step is to compile and bind using STRIDE build tools and create the IM that contains the intercept for the double function. The options to s2sinstrument have been updated to support the test double feature.

By default (see s2sinstrument --default_mode option for details) the IM generated code will contain interceptor for any intercept-able function there is not need to specify any special IM --mode options.

s2scompile –I"C:\STRIDE\inc" depend_scl.h
s2sbind --output=test.sidb depend_scl.h.meta 
s2sinstrument –-im_name=test test.sidb

Once the IM has been successfully created, the source file containing the depend() implementation must be altered by defining the Group ID and including the generated Intercept Module mangling header file (xxxIM.h):

// depend.c
#include "depend.h"
/* define the Group ID before including the IM header */
#define TEST_GROUP
#include "strideIM.h"

int depend(int x)
{
    return x + x;
}

Switching the Double Function During Runtime

The test unit will have access to the following STRIDE runtime macros for substituting a stub function for a double candidate.

srDOUBLE_GET(fn, pfnDbl)
srDOUBLE_SET(fn, fnDbl)

Where:

  • fn is the function qualified by scl_function or scl_func as a dependency candidate, as above.
  • pfnDbl is a pointer to a object of type srFnDbl_t, declared to hold the current value of the active double function.
  • fnDbl is a function that is to be the current active double. The function passed in should always match the signature of the dependency candidate specified by fn.

Note: the initial value of the current active double is always the dependency candidate function.

These macros are defined in the STRIDE runtime header file sr.h. The following example shows how they are used in a C++ test unit:

#include <srtest.h>
#include "depend.h"

extern "C" int depend_dbl(int a) { return a * a; }

class Test: public stride::srTest
{ 
public:

    Test()
    {
        srDOUBLE_GET(depend, &m_depend_dbl);
        srDOUBLE_SET(depend, depend_dbl);
    }

    ~Test()
    {
        srDOUBLE_SET(depend, m_depend_dbl);
    }

    int test1(void) { return test(1, 2); }
    int test2(void) { return test(5, 6); }

private:
   srFunctionDouble_t m_depend_dbl;
};
 
#ifdef _SCL
#pragma scl_test_class(Test)
#pragma scl_function(depend, "DEFINITION", "IMPLICIT", "TEST_GROUP")
#endif