/*****************************************************************/
/**                                                             **/
/**             (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;

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

End;


main(argc,argv)
   int argc;
   char *argv[];
{
   rect rec_s, rec_r;
   Data (interf) inst1;

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

   rec_s.f3 = (unt *)malloc(sizeof(unt));
   rec_s.f3->tag = 1;
   rec_s.f3->un.f2 = (unt *)malloc(sizeof(unt));
   rec_s.f3->un.f2->tag = 0;
   rec_s.f3->un.f2->un.f1 = 10;
   rec_s.f4 = 11;

   Roprinti("rect", &rec_s, interf);

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

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

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

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

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

   if (cmprect(rec_s, rec_r)) {
      printf("   Records rec_s and rec_r are not equal\n");
      exit(1);
   }

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


int cmpunt(un_s,un_r)
unt *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)
         return (cmpunt(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.f4 != rec_r.f4){
      printf("   rec_r.f4 value is %d (should be %d)\n",
                 rec_r.f4, rec_s.f4);
      return (1);
   }
   if (rec_s.f3 != 0 && rec_r.f3 != 0)
      return (cmpunt(rec_s.f3, rec_r.f3));
   if (rec_s.f3 != 0 || rec_r.f3 != 0)
      return (1);
   return (0);
}
