/*****************************************************************/
/**                                                             **/
/**             (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;
    rect (*) f2;
  } unt;

  Record {
    Integer(2) f3;
    rect (*) f4;
  } rect;

End;


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

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

   un_s.tag = 1;
   un_s.un.f2 = (rect *) malloc(sizeof(rect));
   un_s.un.f2->f3 = 10;
   un_s.un.f2->f4 = (rect *) malloc(sizeof(rect));
   un_s.un.f2->f4->f3 = 11;
   un_s.un.f2->f4->f4 = 0;

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

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

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

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

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

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

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

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


int cmpunt(un_s,un_r)
unt *un_s, *un_r;
{
   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)
         return (cmprect(*un_s->un.f2,*un_r->un.f2));
      if (un_s->un.f2 != 0 || un_r->un.f2 != 0)
         return (1);
      break;
   }
   return (0);
}


int cmprect(rec_s,rec_r)
rect rec_s, rec_r;
{
   if (rec_s.f3 != rec_r.f3){
      printf("   rec_r.f3 value is %d (should be %d)\n",
                 rec_r.f3, rec_s.f3);
      return (1);
   }
   if (rec_s.f4 != 0 && rec_r.f4 != 0)
      return (cmprect(*rec_s.f4,*rec_r.f4));
   if (rec_s.f4 != 0 || rec_r.f4 != 0)
      return (1);
   return (0);
}
