C Unions

Component Testing for C

If the structured variable involves a C union (defined using the union instruction) rather than a structure (defined using the struct instruction), you need to specify which field in the union is tested. The initial and test value only relates to one of the fields in the union, whereas, for a structure, it relates to all the fields.

The list.c example demonstrates this if you modify the structure of the list, such that the value stored at each node is an integer, a floating-point number, or a character string:

list1.h:

enum node_type { INTEGER, REAL, STRING };

typedef struct t_list {

enum node_type type;

union {

long integer_value;

double real_value;

char * string_value;

} value;

struct t_list * next;

struct t_list * prev;

} T_LIST, * PT_LIST;

In this case, the test becomes:

HEADER list1, 1, 1

##include "list1.h"

BEGIN

SERVICE push1

#PT_LIST l;

#enum node_type t;

#char s[10];

TEST 1

FAMILY nominal

ELEMENT

VAR l, init = NIL, ev = NONIL

VAR t, init = my_string, ev = init

VAR *l, init == ,

& ev = {STRING,{string_value=>"myfoo"}, NIL,NIL}

VAR s, init = "myfoo", ev = init

#l = push1(l, t, s);

END ELEMENT

END TEST

END SERVICE

The use of string_value => indicates that the chosen field in the union is string_value.

If no field is specified, the first field in the union is taken by default.

Related Topics

Testing variables | Testing structured variables | Testing structured variables with C expressions | Testing structured variables with other structured variables