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:


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;


In this case, the test becomes:

HEADER list1, 1, 1

##include "list1.h"



  #PT_LIST l;

  #enum node_type t;

  #char s[10];

    TEST 1

    FAMILY nominal


      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);




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 variablesTesting structured variables with C expressionsTesting structured variables with other structured variables