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

  DynamicArray rect darrt;

  Record {
    Integer(2) f1;
    darrt f2;
  } 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 = 10;
   rec_s.f2.len = 3;
   rec_s.f2.val = (rect *)malloc(3 * sizeof(rect));
   rec_s.f2.val[0].f1 = 11;
   rec_s.f2.val[0].f2.len = 1;
   rec_s.f2.val[0].f2.val = (rect *)malloc(sizeof(rect));
   rec_s.f2.val[0].f2.val[0].f1 = 12;
   rec_s.f2.val[0].f2.val[0].f2.len = 0;
   rec_s.f2.val[1].f1 = 13;
   rec_s.f2.val[1].f2.len = 0;
   rec_s.f2.val[2].f1 = 14;
   rec_s.f2.val[2].f2.len = 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 (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 cmpdynarr(arr_s,arr_r)
darrt arr_s, arr_r;
{
   int i;

   if (arr_s.len != arr_r.len) {
      printf("   arr_r.len is %d (should be %d)\n");
      return (1);
   }
   for (i=0; i<arr_s.len; i++)
      if (cmprect(arr_s.val[i],arr_r.val[i])) {
         printf("arr_s.val[%d] != arr_r.val[%d]\n",i,i);
         return (1);
      }
   return (0);
}


int cmprect(rec_s,rec_r)
rect rec_s, rec_r;
{
   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);
   }
   return (cmpdynarr(rec_s.f2, rec_r.f2));
}
