/*****************************************************************/
/**                                                             **/
/**             (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;
      recur_rec <Optional> (*) f2;
      recur_rec <Optional> (*) f3;
   } recur_rec;

End;


main(argc,argv)
   int argc;
   char *argv[];
{
   recur_rec rec1s,rec2s,rec3s,rec4s,rec5s;
   recur_rec rec1r,rec2r,rec3r,rec4r,rec5r;
   Data (interf) inst1;

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

   rec3s.f1 = 3;
   rec3s.f2 = 0;
   rec3s.__f2_absent = 0;
   rec3s.f3 = 0;
   rec3s.__f3_absent = 0;

   rec4s.f1 = 4;
   rec4s.f2 = 0;
   rec4s.__f2_absent = 0;
   rec4s.__f3_absent = 1;

   rec2s.f1 = 2;
   rec2s.f2 = &rec3s;
   rec2s.__f2_absent = 0;
   rec2s.f3 = &rec4s;
   rec2s.__f3_absent = 0;

   rec5s.f1 = 5;
   rec5s.__f2_absent = 1;
   rec5s.f3 = 0;
   rec5s.__f3_absent = 0;

   rec1s.f1 = 1;
   rec1s.f2 = &rec2s;
   rec1s.__f2_absent = 0;
   rec1s.f3 = &rec5s;
   rec1s.__f3_absent = 0;

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

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

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

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

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

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

   if (cmprec(rec1s,rec1r)) {
      printf("   rec1s is not equal to rec1r\n");
      exit(1);
   }

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


int cmprec(rec1s,rec1r)
recur_rec rec1s,rec1r;
{
   if (rec1s.f1 != rec1r.f1) {
      printf("   rec1s.f1 value is %d (should be %d)\n",
	      rec1r.f1, rec1s.f1);
      return (1);
   }
   if (rec1s.__f2_absent || rec1s.f2 == 0) { 
      if (rec1r.__f2_absent != 1) {
	 printf("   rec1r.__f2_absent is %d (should be 1)\n",
		 rec1r.__f2_absent);
	 return (1);
      }
   }
   if (rec1s.__f2_absent == 0 && rec1s.f2 != 0) { 
      if (rec1r.__f2_absent != 0) {
	 printf("   rec1r.__f2_absent is %d (should be 0)\n",
		 rec1r.__f2_absent);
	 return (1);
      }
      else {
	 if (rec1r.f2 != 0) {
	    if (cmprec(*(recur_rec*)rec1s.f2,
		       *(recur_rec*)rec1r.f2))
	       return (1);
	 }
      }
   }
   if (rec1s.__f3_absent || rec1s.f3 == 0) { 
      if (rec1r.__f3_absent != 1) {
	 printf("   rec1r.__f3_absent is %d (should be 1)\n",
		 rec1r.__f3_absent);
	 return (1);
      }
   }
   if (rec1s.__f3_absent == 0 && rec1s.f3 != 0) { 
      if (rec1r.__f3_absent != 0) {
	 printf("   rec1r.__f3_absent is %d (should be 0)\n",
		 rec1r.__f3_absent);
	 return (1);
      }
      else {
	 if (rec1r.f3 != 0) {
	    if (cmprec(*(recur_rec*)rec1s.f3,
		       *(recur_rec*)rec1r.f3))
	       return (1);
	 }
      }
   }
   return (0);
}
