/*****************************************************************/
/**                                                             **/
/**             (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;
    unt2 (*) f2;
  } unt1;

  Union {
    unt1 f3;
    Integer(2) f4;
  } unt2;

End;


main(argc,argv)
   int argc;
   char *argv[];
{
   unt1 un_s, un_r;
   Data (interf) inst1;

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

   un_s.tag = 1;
   un_s.un.f2 = (unt2 *) malloc(sizeof(unt2));
   un_s.un.f2->tag = 0;
   un_s.un.f2->un.f3.tag = 1;
   un_s.un.f2->un.f3.un.f2 = (unt2 *)malloc(sizeof(unt2));
   un_s.un.f2->un.f3.un.f2->tag = 1;
   un_s.un.f2->un.f3.un.f2->un.f3.tag = 0;
   un_s.un.f2->un.f3.un.f2->un.f3.un.f1 = 10;

   Roprinti("unt1", &un_s, interf);

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

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

/***************** uneive it back ********************/

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

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

   if (cmpunt1(&un_s, &un_r)) {
      printf("   Records un_s and un_r are not equal\n");
      exit(1);
   }

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


int cmpunt1(un_s,un_r)
unt1 *un_s, *un_r;
{
   if (un_s->tag != un_r->tag) {
      printf("   un_r->tag value is %d (should be %d)\n",
                 un_r->tag,un_s->tag);
      return(1);
   }
   switch (un_s->tag) {
   case 0:
      if (un_s->un.f1 != un_r->un.f1){
         printf("   un_r->un.f1 value is %d (should be %d)\n",
                    un_r->un.f1, un_s->un.f1);
         return (1);
      }
      break;
   case 1:
      if ((un_s->un.f2 == 0 && un_r->un.f2 != 0) || 
          (un_s->un.f2 != 0 && un_r->un.f2 == 0))
         return (1);
      if (un_s->un.f2 != 0 && un_r->un.f2 != 0)
         return (cmpunt2(*un_s->un.f2, *un_r->un.f2));
      break;
   }
   return (0);
}


int cmpunt2(un_s,un_r)
unt2 un_s, un_r;
{
   if (un_s.tag != un_r.tag) {
      printf("   un_r.tag value is %d (should be %d)\n",
                 un_r.tag, un_s.tag);
      return (1);
   }
   switch (un_s.tag) {
   case 0:
      if (cmpunt1(&un_s.un.f3, &un_r.un.f3)) {
         printf("   un_s.un.f3 and un_r.un.f3 are not equal\n");
         return (1);
      }
      break;
   case 1:
      if (un_s.un.f4 != un_r.un.f4) {
         printf("   un_r.un.f4 value is %d (should be %d)\n",
                    un_r.un.f4, un_s.un.f4);
         return (1);
      }
      break;
   }
   return (0);
}
