/*****************************************************************/
/**                                                             **/
/**             (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;
    rect2 (*) f2;
  } rect1;

  Record {
    Integer(2) f3;
    rect1 f4;
  } rect2;

  Array rect1 arrt[3];
End;


main(argc,argv)
   int argc;
   char *argv[];
{
   arrt arr_s, arr_r;
   Data (interf) inst1;

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

   arr_s[0].f1 = 10;
   arr_s[0].f2 = (rect2 *)malloc(sizeof(rect2));
   arr_s[0].f2->f3 = 11;
   arr_s[0].f2->f4.f1 = 12;
   arr_s[0].f2->f4.f2 = 0;

   arr_s[1].f1 = 13;
   arr_s[1].f2 = 0;

   arr_s[2].f1 = 14;
   arr_s[2].f2 = (rect2 *)malloc(sizeof(rect2));
   arr_s[2].f2->f3 = 15;
   arr_s[2].f2->f4.f1 = 16;
   arr_s[2].f2->f4.f2 = (rect2 *)malloc(sizeof(rect2));
   arr_s[2].f2->f4.f2->f3 = 17;
   arr_s[2].f2->f4.f2->f4.f1 = 18;
   arr_s[2].f2->f4.f2->f4.f2 = 0; 
   
   Roprinti("arrt", arr_s, interf);

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

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

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

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

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

   if (cmparrt(arr_s, arr_r)) {
      printf("   Arrays arr_s and arr_r are not equal\n");
      exit(1);
   }

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


int cmparrt(arr_s,arr_r)
arrt arr_s, arr_r;
{
   int i;

   for (i=0; i<3; i++)
      if (cmprect1(arr_s[i],arr_r[i])) {
         printf("arr_s[%d] != arr_r[%d]\n",i,i);
         return (1);
      }
   return (0);
}


int cmprect1(rec_s,rec_r)
rect1 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);
   }
   if (rec_s.f2 != 0 && rec_r.f2 != 0)
      return (cmprect2(*rec_s.f2,*rec_r.f2));
   if (rec_s.f2 != 0 || rec_r.f2 != 0)
      return (1);
   return (0);
}


int cmprect2(rec_s,rec_r)
rect2 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 (cmprect1(rec_s.f4,rec_r.f4)) {
      printf("   rec_r.f4 is not the same as rec_s.f4\n");
      return(1);
   }
   return(0);
}
