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

   Record {
      Integer(2)        f1;
      DynamicArray rect f2; 
      rect       (*)    f3; 
   } rect;

End;


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

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

   rec_s.f1 = 1;
   rec_s.f2.len = 2;
   rec_s.f2.val = (rect*)malloc(2*sizeof(rect));
   rec_s.f2.val[0].f1 = 2;
   rec_s.f2.val[0].f2.len = 0;
   rec_s.f2.val[0].f3 = 0;
   rec_s.f2.val[1].f1 = 3;
   rec_s.f2.val[1].f2.len = 0;
   rec_s.f2.val[1].f3 = (rect*)malloc(1*sizeof(rect));
   rec_s.f2.val[1].f3->f1 = 4;
   rec_s.f2.val[1].f3->f2.len = 0;
   rec_s.f2.val[1].f3->f3 = 0;
   rec_s.f3 = (rect*)malloc(1*sizeof(rect));
   rec_s.f3->f1 = 5;
   rec_s.f3->f2.len = 1;
   rec_s.f3->f2.val = (rect*)malloc(1*sizeof(rect));
   rec_s.f3->f2.val[0].f1 = 6;
   rec_s.f3->f2.val[0].f2.len = 3;
   rec_s.f3->f2.val[0].f2.val = (rect*)malloc(3*sizeof(rect));
   rec_s.f3->f2.val[0].f2.val[0].f1 = 7;
   rec_s.f3->f2.val[0].f2.val[0].f2.len = 0;
   rec_s.f3->f2.val[0].f2.val[0].f3 = 0;
   rec_s.f3->f2.val[0].f2.val[1].f1 = 8;
   rec_s.f3->f2.val[0].f2.val[1].f2.len = 0;
   rec_s.f3->f2.val[0].f2.val[1].f3 = 0;
   rec_s.f3->f2.val[0].f2.val[2].f1 = 9;
   rec_s.f3->f2.val[0].f2.val[2].f2.len = 0;
   rec_s.f3->f2.val[0].f2.val[2].f3 = 0;
   rec_s.f3->f3 = 0;

   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 (cmprec(rec_s, rec_r)) {
      printf("   records rec_s and rec_r are not equal\n");
      exit(1);
   }

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


int cmprec(rec_s,rec_r)
rect rec_s, rec_r;
{
   int i;
   if (rec_s.f1 != rec_r.f1) {
      printf("   rec_r.f1 value is %d (should be %d)\n",
	     rec_r.f1, rec_s.f1);
      return (1);
   }
   if (rec_s.f2.len != rec_r.f2.len) {
      printf("   rec_r.f2.len value is %d (should be %d)\n",
	      rec_r.f2.len,rec_s.f2.len);
      return (1);
   }
   for (i=0; i<rec_s.f2.len ; i++) {
      if (cmprec(rec_s.f2.val[i],rec_r.f2.val[i])) {
	 printf("   *(rec_r.f2.val[%d]) and *(rec_s.f2.val[%d]) are not equal\n",
		 i,i);
	 return (1);
      }
   }
   if (rec_s.f3 == 0 && rec_r.f3 == 0)
      return (0);
   if (rec_s.f3 && rec_r.f3) {
      if (cmprec(*(rec_s.f3), *(rec_r.f3))) {
	 printf("   records *(rec_s.f3) and *(rec_r.f3) are not equal\n");
	 return (1);
      }
   }
   return (0);
}
