/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

Interface interf;

  Union {
    Integer(2) f1;
    unt (*) f2;
  } unt;

  Array unt arrt[3];

End;


main(argc,argv)
   int argc;
   char *argv[];
{
   arrt arr_s,arr_r;
   Data (interf) inst1;

   if (argc != 2)
      exit(1);

   arr_s[0].tag = 0;
   arr_s[0].un.f1 = 10;

   arr_s[1].tag = 1;
   arr_s[1].un.f2 = (unt *)malloc(sizeof(unt));
   arr_s[1].un.f2->tag = 1;
   arr_s[1].un.f2->un.f2 = (unt *)malloc(sizeof(unt));
   arr_s[1].un.f2->un.f2->tag = 0;
   arr_s[1].un.f2->un.f2->un.f1 = 11;

   arr_s[2].tag = 1;
   arr_s[2].un.f2 = (unt *)malloc(sizeof(unt));
   arr_s[2].un.f2->tag = 0;
   arr_s[2].un.f2->un.f1 = 12;

   Roprinti("arrt", arr_s, interf);

/***************** send it out ************************/

   Bind_STUB(inst1,argv[1],"w",Asn_1_ber);
   Encode inst1
   {
      arr_s => arrt;
   }
   Unbind(inst1);

/***************** receive it back ********************/

   Bind_STUB(inst1,argv[1],"r",Asn_1_ber);
   Decode inst1
      {
         On arrt => (arr_r);
      }
   Unbind(inst1);

/***************** report results *********************/

   if (cmparr(arr_s, arr_r)) {
      printf("   arrays arr_s and arr_r are not equal\n");
      exit(1);
   }

   printf("test completed\n");
   return (0);
}


int cmpunt(unt_s,unt_r)
unt unt_s, unt_r;
{
   if (unt_s.tag != unt_r.tag) {
      printf("   unt_r.tag value is %d (should be %d)\n",
             unt_r.tag,unt_s.tag);
      return (1);
   }
   switch (unt_s.tag) {
   case 0:
     if (unt_s.un.f1 != unt_r.un.f1){
       printf("   unt_r.un.f1 value is %d (should be %d)\n",
                 unt_r.un.f1, unt_s.un.f1);
       return (1);
     }
     break;
   case 1:
     if ((unt_s.un.f2 == 0 && unt_r.un.f2 != 0) || 
         (unt_s.un.f2 != 0 && unt_r.un.f2 == 0))
       return (1);
     if (unt_s.un.f2)
       return (cmpunt(*unt_s.un.f2, *unt_r.un.f2));
     break;
   }
   return (0);
}


int cmparr(arr_s,arr_r)
arrt arr_s, arr_r;
{
   int i;
   for (i=0; i<3 ; i++) {
     if (cmpunt(arr_s[i],arr_r[i]))
       return (1);
   }
   return (0);
}
