Simulating Functions with char* parameters

Component Testing for C

You can use Component Testing for C to stub functions that take a parameter of the char* type.

The char* type causes problems with the Component Testing feature because of the ambiguity built into the C programming language. The char* type can represent:

·         Pointers

·         Pointers to a single char

·         Arrays of characters of indeterminate size

·         Arrays of characters of which the last character is the character \0, a C string.

By default, the product treats all variables of this type as C strings. To specify a different behaviour, you must use one of the following methods.

Pointers

Use the FORMAT command to specify that the test required is that of a pointer. For example:

HEADER charp, ,
#extern int CharPointer(char* pChar);
BEGIN
DEFINE STUB Stubs

#int CharPointer (void* _in pChar);

END DEFINE


SERVICE CharPointer1


#char  *Chars;
#int ret;

TEST 1

ELEMENT
FORMAT Chars = void*
VAR Chars,   init = NIL,  ev = init
VAR ret,     init = 0,    ev = 0
STUB CharPointer 1 => (NIL)0


#ret = CharPointer(Chars);
END ELEMENT

END TEST -- TEST 1

END SERVICE -- CharPointer1

Pointers to a Single char

Define the type as _inout, as in the following example.

HEADER charp, ,
#extern int CharPointer(char* pChar);
BEGIN
DEFINE STUB Stubs

#int CharPointer (char _inout pChar);

END DEFINE


SERVICE CharPointer1


#char  AChar;
#int ret;

TEST 1

ELEMENT
VAR AChar,  init = 'a', ev = init
VAR ret,    init = 0,   ev = 'A'
STUB CharPointer 1 => ( ('a','A') ) 0


#ret = CharPointer(&AChar);
END ELEMENT

END TEST -- TEST 1

END SERVICE -- CharPointer1

Arrays of Characters of Indeterminate Size

Use the FORMAT command to specify that the array is in fact an array of unsigned chars not chars, as the product considers that char arrays are C strings. For example:

HEADER charp, ,
#extern int CharPointer(char* pChar);
BEGIN
DEFINE STUB Stubs

#int CharPointer (unsigned char _in Chars[4]);

END DEFINE

 


SERVICE CharPointer1


#char  Chars[4];
#int ret;

TEST 1

ELEMENT
FORMAT Chars = unsigned char[4]
ARRAY Chars,  init = {'a','b','c','d'},  ev = init
VAR ret,    init = 0,                  ev = 'a'
STUB CharPointer 1 => ({0x61,'b',99,‘\144'})0,


#ret = CharPointer(Chars);
END ELEMENT

END TEST -- TEST 1

END SERVICE -- CharPointer1

C strings

Use an array of characters in which the last character is the character '\0', a C string.

HEADER charp, ,
#extern int CharPointer(char* pChar);
BEGIN
DEFINE STUB Stubs

#int CharPointer (char* _in pChar);

END DEFINE


SERVICE CharPointer1

#char  Chars[10];
#int ret;

TEST 1

ELEMENT
VAR Chars,  init = "Hello",  ev  = init  
VAR ret,    init = 0,        ev = 'H'
STUB CharPointer 1 => ("abcd")0


#ret = CharPointer(Chars);
END ELEMENT

END TEST -- TEST 1

END SERVICE -- CharPointer1

 

Points to note

The function being stubbed must be declared before the DEFINE STUB statement block. This can be done either through an include file or explicitly as above. The declaration of the stub in the DEFINE STUB section may differ from the prototype of the function but this will only affect the behaviour of the stub not the definition of the function. The definition of the function is always taken from the first declaration seen.