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 behavior, you must use one of the following methods.
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
SERVICE CharPointer1
#char *Chars;
#int ret;
TEST 1
ELEMENT
FORMAT Chars = void*
VAR Chars, init = NIL, ev = init
VAR ret, init = 0, ev = 0
#ret = CharPointer(Chars);
END ELEMENT
END TEST -- TEST 1
END SERVICE -- CharPointer1
Define the type as _inout, as in the following example.
HEADER charp, ,
#extern int CharPointer(char* pChar);
BEGIN
SERVICE CharPointer1
#char AChar;
#int ret;
TEST 1
ELEMENT
VAR AChar, init = 'A', ev = init
VAR ret, init = 0, ev = 'A'
#ret = CharPointer(&AChar);
END ELEMENT
END TEST -- TEST 1
END SERVICE -- CharPointer1
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
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'
#ret = CharPointer(Chars);
END ELEMENT
END TEST -- TEST 1
END SERVICE -- CharPointer1
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
SERVICE CharPointer1
#char Chars[10];
#int ret;
TEST 1
ELEMENT
VAR Chars, init = "Hello", ev = init
VAR ret, init = 0, ev = 'H'
#ret = CharPointer(Chars);
END ELEMENT
END TEST -- TEST 1
END SERVICE -- CharPointer1