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:

By default, the product treats all variables of this type as C strings. To specify a different behavior, 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 CH

#int CharPointer(void* 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(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 CH

#int CharPointer(char Char);

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('A')'A'

#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 CH

#int CharPointer(unsigned char 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({'a','b','c','d'})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 CH

#int CharPointer(char* 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("Hello")'H'

#ret = CharPointer(Chars);

END ELEMENT

END TEST -- TEST 1

END SERVICE -- CharPointer1