Testing a String Pointer as a Pointer
Automated Testing
-- Tested service parameter declarations
#st_toto *p_toto;
-- By function returned type declaration
#int ret_TestFunction;
ENVIRONMENT ENV_TestFunction
VAR ret_TestFunction, init = 0, ev = init
END ENVIRONMENT -- ENV_TestFunction
USE ENV_TestFunction
TEST 1
FAMILY nominal
ELEMENT
STR *p_toto, init = { a => 0, b => 0, Ptr1 => NIL },
ev= init
STR *p_toto->Ptr1, init = {a=>2,b=>32, Ptr1=>NIL}, ev=
init
VAR ret_TestFunction, init = 0, ev = init
#ret_TestFunction = TestFunction(p_toto);
END ELEMENT
END TEST -- TEST 1
FIN SERVICE -- TestFunction
Testing a String Pointer as a Pointer
Use the string_ptr keyword on a VAR line to work around the ambiguity of the C
language between arrays and pointers.
For example the following VAR line (supposing the declaration
char* string;
) will
generate C code that will copy the string into the memory location pointed by string.
VAR string, init = "foo", ev = init
-- This is the "traditional" way
Of course, if no memory was allocated to the variable, this is not possible.
The following alternative approach causes the string to point to the memory location
containing "foo". The string is then compared to "foo" using a string comparison
function:
VAR string, string_ptr, init = "foo", ev = init
-- Note the additional field in the line
This syntax allows you to initialize the variable to "NIL", and to compare its contents
to a given string after the test.
Initializing Pointer Variables while Preserving the Pointed Value
To initialize a variable as a pointer while keeping the ability to test the value of the
pointed element, use the FORMAT string_ptr statement in your .ptu test script.
This allows you to initialize your variable as a pointer and still perform string
comparisons using str_comp.
Example:
TEST 1
FAMILY nominal
157