/*****************************************************************/
/**                                                             **/
/**             (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;
      Record {
	 Integer(2) ff1;
	 recur_rec (*) ff2;
      } f2;
      Union {
	 Integer(2) fu1;
	 recur_rec (*) fu2;
      } f3;
      Array 
	 Record {
	    Integer(2) faf1;
	    recur_rec (*) faf2;
	 } 
      f4[2];
      DynamicArray 
	 Union {
	    Integer(2) fdu1;
	    recur_rec (*) fdu2;
	 } 
      f5;
   } recur_rec;

   Union {
      Integer(2) fdu1;
      recur_rec  fdu2;
   } 
   u_type;

End;


main(argc,argv)
   int argc;
   char *argv[];
{
   int i;
   recur_rec rec_7s,rec_12s,rec_18s,rec_24s,rec_30s,rec_s;
   recur_rec rec_7r,rec_12r,rec_18r,rec_24r,rec_30r,rec_r;
   Data (interf) inst1;

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

   rec_7s.f1 = 7;
   rec_7s.f2.ff1 = 8;
   rec_7s.f2.ff2 = 0;
   rec_7s.f3.tag = 0;
   rec_7s.f3.un.fu1 = 9;
   rec_7s.f4[0].faf1 = 10;
   rec_7s.f4[0].faf2 = 0;
   rec_7s.f4[1].faf1 = 11;
   rec_7s.f4[1].faf2 = 0;
   rec_7s.f5.len = 0;

   rec_12s.f1 = 12;
   rec_12s.f2.ff1 = 13;
   rec_12s.f2.ff2 = 0;
   rec_12s.f3.tag = 0;
   rec_12s.f3.un.fu1 = 14;
   rec_12s.f4[0].faf1 = 15;
   rec_12s.f4[0].faf2 = 0;
   rec_12s.f4[1].faf1 = 16;
   rec_12s.f4[1].faf2 = 0;
   rec_12s.f5.len = 0;

   rec_18s.f1 = 18;
   rec_18s.f2.ff1 = 19;
   rec_18s.f2.ff2 = 0;
   rec_18s.f3.tag = 0;
   rec_18s.f3.un.fu1 = 20;
   rec_18s.f4[0].faf1 = 21;
   rec_18s.f4[0].faf2 = 0;
   rec_18s.f4[1].faf1 = 22;
   rec_18s.f4[1].faf2 = 0;
   rec_18s.f5.len = 0;

   rec_24s.f1 = 24;
   rec_24s.f2.ff1 = 25;
   rec_24s.f2.ff2 = 0;
   rec_24s.f3.tag = 0;
   rec_24s.f3.un.fu1 = 26;
   rec_24s.f4[0].faf1 = 27;
   rec_24s.f4[0].faf2 = 0;
   rec_24s.f4[1].faf1 = 28;
   rec_24s.f4[1].faf2 = 0;
   rec_24s.f5.len = 0;

   rec_30s.f1 = 30;
   rec_30s.f2.ff1 = 31;
   rec_30s.f2.ff2 = 0;
   rec_30s.f3.tag = 0;
   rec_30s.f3.un.fu1 = 32;
   rec_30s.f4[0].faf1 = 33;
   rec_30s.f4[0].faf2 = 0;
   rec_30s.f4[1].faf1 = 34;
   rec_30s.f4[1].faf2 = 0;
   rec_30s.f5.len = 0;

   rec_s.f1 = 5;

   rec_s.f2.ff1 = 6;
   rec_s.f2.ff2 = &rec_7s;

   rec_s.f3.tag = 1;
   rec_s.f3.un.fu2 = &rec_12s;

   rec_s.f4[0].faf1 = 17;
   rec_s.f4[0].faf2 = &rec_18s;
   rec_s.f4[1].faf1 = 23;
   rec_s.f4[1].faf2 = &rec_24s;

   rec_s.f5.len = 2;
   rec_s.f5.val = (void*)malloc(sizeof(u_type)*2);
   rec_s.f5.val[0].tag = 0;
   rec_s.f5.val[0].un.fdu1 = 29;
   rec_s.f5.val[1].tag = 1;
   rec_s.f5.val[1].un.fdu2 = &rec_30s;

   rec_r.f5.len = 2;
   rec_r.f5.val = (void*)malloc(sizeof(u_type)*2);

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

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

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

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

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

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

   if (rec_s.f1 != rec_r.f1) {
      printf("   rec_r.f1 value is %d (should be %d)\n",
	      rec_r.f1, rec_s.f1);
      exit(1);
   }
   if (rec_s.f2.ff1 != rec_r.f2.ff1) {
      printf("   rec_r.f2.ff1 is %d (should be %d)\n",
	      rec_r.f2.ff1, rec_s.f2.ff1);
      exit(1);
   }
   if (comprec(rec_s.f2.ff2, rec_r.f2.ff2)) {
      printf("   rec_s.f2.ff2 is %d (should be %d)\n",
	      rec_r.f2.ff2, rec_s.f2.ff2);
      exit(1);
   }
   if (rec_s.f3.tag != rec_r.f3.tag) {
      printf("   rec_r.f3.tag is %d (should be %d)\n",
	      rec_r.f3.tag, rec_s.f3.tag);
      exit(1);
   }
   switch(rec_r.f3.tag) {
   case 0:
      if(rec_s.f3.un.fu1 != rec_r.f3.un.fu1) {
	 printf("   rec_r.f3.un.fu1 is %d (should be %d)\n",
		 rec_r.f3.un.fu1, rec_s.f3.un.fu1);
	 exit(1);
      }
      break;
   case 1:
      if (comprec(rec_s.f3.un.fu2,rec_r.f3.un.fu2)) {
	 printf("   rec_r.f3.un.fu2 is %d (should be %d)\n",
		 rec_r.f3.un.fu2, rec_s.f3.un.fu2);
	 exit(1);
      }
      break;
   default:
	 printf("   rec_r.f3.tag is %d (should be 0 or 1)\n");
	 exit(1);
   }
   if (rec_s.f4[0].faf1 != rec_r.f4[0].faf1) {
      printf("   rec_r.f4[0].faf1 is %d (should be %d)\n",
	      rec_r.f4[0].faf1, rec_s.f4[0].faf1);
      exit(1);
   }
   if (comprec(rec_s.f4[0].faf2, rec_r.f4[0].faf2)) {
      printf("   rec_r.f4[0].faf2 is %d (should be %d)\n",
	      rec_r.f4[0].faf2, rec_s.f4[0].faf2);
      exit(1);
   }
   if (rec_s.f4[1].faf1 != rec_r.f4[1].faf1) {
      printf("   rec_r.f4[1].faf1 is %d (should be %d)\n",
	      rec_r.f4[1].faf1, rec_s.f4[1].faf1);
      exit(1);
   }
   if (comprec(rec_s.f4[1].faf2, rec_r.f4[1].faf2)) {
      printf("   rec_r.f4[1].faf2 is %d (should be %d)\n",
	      rec_r.f4[1].faf2, rec_s.f4[1].faf2);
      exit(1);
   }
   if (rec_s.f5.len != rec_r.f5.len) {
      printf("   rec_r.f5.len is %d (should be %d)\n",
	      rec_r.f5.len, rec_s.f5.len);
      exit(1);
   }
   for (i = 0; i< rec_s.f5.len; i++) {
      if (rec_s.f5.val[i].tag != rec_r.f5.val[i].tag) {
	 printf("   rec_r.f5.val[i].tag is %d (should be %d)\n",
		 rec_r.f5.val[i].tag, rec_s.f5.val[i].tag);
	 exit(1);
      }
      switch(rec_r.f5.val[i].tag) {
      case 0:
	 if(rec_s.f5.val[i].un.fdu1 != rec_r.f5.val[i].un.fdu1) {
	    printf("   rec_r.f5.val[i].un.fdu1 is %d (should be %d)\n",
		    rec_r.f5.val[i].un.fdu1, rec_s.f5.val[i].un.fdu1);
	    exit(1);
	 }
	 break;
      case 1:
	 if (comprec(rec_s.f5.val[i].un.fdu2,rec_r.f5.val[i].un.fdu2)) {
	    printf("   rec_r.f5.val[i].un.fdu2 is %d (should be %d)\n",
		    rec_r.f5.val[i].un.fdu2, rec_s.f5.val[i].un.fdu2);
	    exit(1);
	 }
	 break;
      default:
	 printf("   rec_r.f5.val[i].tag is %d (should be 0 or 1)\n");
	 exit(1);
      }
   }

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


int comprec(recs,recr)
recur_rec *recs,*recr;
{
   int i;

   if ((recs && (recr == 0)) || ((recs == 0) && recr))
      return 1;
   if ((recs== 0) && (recr == 0))
      return 0;
   if (recs->f1 != recr->f1)
      return 1;
   if (recs->f2.ff1 != recr->f2.ff1)
      return 1;
   if (recs->f2.ff2 && recr->f2.ff2)
      comprec(recs->f2.ff2,recr->f2.ff2);
   if (recs->f3.tag != recr->f3.tag)
      return 1;
   switch(recs->f3.tag) {
   case 0:
      if(recs->f3.un.fu1 != recr->f3.un.fu1)
	 return 1;
      break;
   case 1:
      if (comprec(recs->f3.un.fu2,recr->f3.un.fu2))
	 return 1;
      break;
   default:
      return 1;
   }
   if (recs->f4[0].faf1 != recr->f4[0].faf1)
      return 1;
   if (comprec(recs->f4[0].faf2, recr->f4[0].faf2))
      return 1;
   if (recs->f4[1].faf1 != recr->f4[1].faf1)
      return 1;
   if (comprec(recs->f4[1].faf2, recr->f4[1].faf2))
      return 1;
   if (recs->f5.len != recr->f5.len)
      return 1;
   for (i = 0; i< recs->f5.len; i++) {
      if (recs->f5.val[i].tag != recr->f5.val[i].tag)
	 return 1;
      switch(recs->f5.val[i].tag) {
      case 0:
	 if(recs->f5.val[i].un.fdu1 != recr->f5.val[i].un.fdu1)
	    return 1;
	 break;
      case 1:
	 if (comprec(recs->f5.val[i].un.fdu2,recr->f5.val[i].un.fdu2))
	    return 1;
	 break;
      default:
	 return 1;
      }
   }
   return 0;
}
