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

   Integer(4) int1type;

   Record {
      Integer(4) r1;
      Enumerated(4) r2;
   }rec1type;

   Array Integer(4) arr1type[10];

   Union {
      Boolean(1) u1;
      Enumerated(4) u2;
   }un1type;

   Invoke ike 52 Integer(4);

End;

Interface interf2;

   Integer(4) int2type;

   Record {
      Integer(4) r1;
      Enumerated(4) r2;
   }rec2type;

   Array Integer(4) arr2type[10];

   Union {
      Boolean(1) u1;
      Enumerated(4) u2;
   }un2type;

   Invoke ike 52 Integer(4);

End;

Interface interf3;

   Integer(4) int3type;

   Record {
      Integer(4) r1;
      Enumerated(4) r2;
   }rec3type;

   Array Integer(4) arr3type[10];

   Union {
      Boolean(1) u1;
      Enumerated(4) u2;
   }un3type;

   Invoke ike 52 Integer(4);

End;


main(argc,argv)
   int argc;
   char *argv[];
{
   int1type int1_s, int1_r;
   int2type int2_s, int2_r;
   int3type int3_s, int3_r;
   rec1type rec1_s, rec1_r;
   rec2type rec2_s, rec2_r;
   rec3type rec3_s, rec3_r;
   arr1type arr1_s, arr1_r;
   arr2type arr2_s, arr2_r;
   arr3type arr3_s, arr3_r;
   un1type un1_s, un1_r;
   un2type un2_s, un2_r;
   un3type un3_s, un3_r;
   Data (interf1) inst1;
   Data (interf2) inst2;
   Data (interf3) inst3;
   int i;

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

   int1_s = 1;
   int2_s = 2;
   int3_s = 3;
   rec1_s.r1 = 11;
   rec1_s.r2 = 12;
   rec2_s.r1 = 21;
   rec2_s.r2 = 22;
   rec3_s.r1 = 31;
   rec3_s.r2 = 32;
   arr1_s[0] = 100;
   arr1_s[1] = 101;
   arr1_s[2] = 102;
   arr2_s[0] = 200;
   arr2_s[1] = 201;
   arr2_s[2] = 202;
   arr3_s[0] = 300;
   arr3_s[1] = 301;
   arr3_s[2] = 302;
   un1_s.tag = 1;
   un1_s.un.u2 = 401;
   un2_s.tag = 1;
   un2_s.un.u2 = 501;
   un3_s.tag = 0;
   un3_s.un.u1 = 1;

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

   Bind_STUB(inst1,argv[1],"w",Asn_1_ber);
   Flush_inst(inst1);
   Encode inst1
   {
      Flush_inst(inst1);
      int1_s => int1type;
      Encode inst1
      {
	 Flush_inst(inst1);
	 rec1_s => rec1type;
	 Encode inst1
	 {
	    Flush_inst(inst1);
	    arr1_s => arr1type;
	    Encode inst1
	    {
	       Flush_inst(inst1);
	       un1_s => un1type;
	       Flush_inst(inst1);
	    }
	    Flush_inst(inst1);
	 }
	 Flush_inst(inst1);
      }
      Flush_inst(inst1);
   }
   Flush_inst(inst1);
   Unbind(inst1);

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

   Bind_STUB(inst1,argv[1],"r",Asn_1_ber);
      Decode inst1
      {
	 On int1type => (int1_r)
	 for (i=0; i<3 ; i++) {
	    Decode inst1
	    {
	       On rec1type => (rec1_r);
	       On arr1type => (arr1_r);
	       On un1type => (un1_r);
	    }
	 }
      }
   Unbind(inst1);

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

   if (int1_s != int1_r) {
      printf("   int1 value was bad\n");
      exit(1);
   }
   if (cmprec(rec1_s , rec1_r)) {
      printf("   rec1 value was bad\n");
      exit(1);
   }
   if (cmparr(arr1_s , arr1_r, 3)) {
      printf("   arr1 value was bad\n");
      exit(1);
   }
   if (cmpun(un1_s , un1_r)) {
      printf("   un1 value was bad\n");
      exit(1);
   }

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


int cmprec(v1,v2)
rec1type v1,v2;
{
   if (v1.r1 != v2.r1)
      return 1;
   if (v1.r2 != v2.r2)
      return 1;
   return 0;
}


int cmparr(v1,v2,num)
arr1type v1,v2;
int num;
{
   int i;
   for (i=0; i<num; i++) {
      if (v1[i] != v2[i])
	 return 1;
   }
   return 0;
}


int cmpun(v1,v2)
un1type v1,v2;
{
    switch(v2.tag) {
    case 0:
       if (v1.un.u1 != v2.un.u1)
	  return 1;
       break;
    case 1:
       if (v1.un.u2 != v2.un.u2)
	  return 1;
       break;
    default:
       printf("bad tag value\n");
       return 1;
    }
    return 0;
}
