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

  Record {
    Integer(2) f1;
    ObjectIdentifier(*,2) <Freeable> f2;
    PrintableString(*) <Optional> f3;
    GeneralString(*) <Optional Freeable> f4;
    DynamicArray <Optional> rect f5;
    DynamicArray <Freeable> rect f6;
    DynamicArray <Optional Freeable> rect f7;
  } 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.size = 2;
   rec_s.f2.comp = (unsigned long *)malloc(2*sizeof(unsigned long));
   rec_s.f2.comp[0] = 1;
   rec_s.f2.comp[1] = 11;
   rec_s.__f3_absent = 0;
   rec_s.f3 = "This is not a freeable string";
   rec_s.__f4_absent = 1;
   rec_s.__f5_absent = 1;
   rec_s.f6.len = 1;
   rec_s.f6.val = (rect *)malloc(sizeof(rect));
   rec_s.f6.val[0].f1 = 12;
   rec_s.f6.val[0].f2.size = 2;
   rec_s.f6.val[0].f2.comp = (unsigned long*)malloc(2*sizeof(unsigned long));
   rec_s.f6.val[0].f2.comp[0] = 1;
   rec_s.f6.val[0].f2.comp[1] = 13;
   rec_s.f6.val[0].__f3_absent = 1;
   rec_s.f6.val[0].__f4_absent = 0;
   rec_s.f6.val[0].f4.len = 25;
   rec_s.f6.val[0].f4.val = (char *)malloc(25);
   strncpy(rec_s.f6.val[0].f4.val,"This is a freeable string",25);
   rec_s.f6.val[0].__f5_absent = 1;
   rec_s.f6.val[0].f6.len = 0;
   rec_s.f6.val[0].__f7_absent = 1;
   rec_s.__f7_absent = 0;
   rec_s.f7.len = 1;
   rec_s.f7.val = (rect *)malloc(sizeof(rect));
   rec_s.f7.val[0].f1 = 14;
   rec_s.f7.val[0].f2.size = 2;
   rec_s.f7.val[0].f2.comp = (unsigned long*)malloc(2*sizeof(unsigned long));
   rec_s.f7.val[0].f2.comp[0] = 1;
   rec_s.f7.val[0].f2.comp[1] = 15;
   rec_s.f7.val[0].__f3_absent = 1;
   rec_s.f7.val[0].__f4_absent = 1;
   rec_s.f7.val[0].__f5_absent = 1;
   rec_s.f7.val[0].f6.len = 0;
   rec_s.f7.val[0].__f7_absent = 1;

/***************** 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);
   }

   /* Test Rofree() */

   Rofreei("rect",&rec_s,interf);

   if (rec_s.f2.comp)
      printf("Error - free'd reference (rec_s.f2.comp) non-zero\n");

   if (!rec_s.f3)
      printf("Error - unfreeable reference free'd (rec_s.f3)\n");

   if (rec_s.f6.val)
      printf("Error - free'd reference (rec_s.f6.val) non-zero\n");

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


int cmprect(rec_s,rec_r)
rect rec_s, rec_r;
{
   unsigned long l1 = 0, l2 = 2;

   if (rec_s.f1 != rec_r.f1) {
      printf("Value of rec_r.f1 is %d (should be %d)\n",rec_r.f1,rec_s.f1);
      return (1);
   }
   
   if (!__ROr_cmpP_ObjectIdentifier(&rec_s.f2,&rec_r.f2,&l1,&l2)) {
      printf("Value of oid's is not equal\n");
      return (1);
   }

   if (rec_s.__f3_absent != rec_r.__f3_absent) {
      printf("Absent flags for f3 are not the same\n");
      return (1);
   }

   if (!rec_s.__f3_absent && strcmp(rec_s.f3,rec_r.f3)) {
      printf("Value of rec_r.f3 is \"%s\" (should be \"%s\")\n",
             rec_r.f3, rec_s.f3);
      return (1);
   }

   if (rec_s.__f4_absent != rec_r.__f4_absent) {
      printf("Absent flags for f4 are not the same\n");
      return (1);
   }

   if (!rec_s.__f4_absent) {
     if (rec_s.f4.len != rec_r.f4.len) {
       printf("Length of rec_r.f4 is not the same as length of rec_s.f4\n");
       return (1);
     }
   
     if (memcmp(rec_s.f4.val,rec_r.f4.val,rec_s.f4.len)) {
        printf("Value of rec_r.f4 is not the same as value of rec_s.f4\n");
        return (1);
     }
   }

   if (rec_s.__f5_absent != rec_r.__f5_absent) {
      printf("Absent flags for f5 are not the same\n");
      return (1);
   }

   if (!rec_s.__f5_absent && cmpdarrt(rec_s.f5,rec_r.f5)) {
      printf("rec_r.f5 != rec_s.f5\n");
      return (1);
   }
   
   if (cmpdarrt(rec_s.f6,rec_r.f6)) {
      printf("rec_r.f6 != rec_s.f6\n");
      return (1);
   }
   
   if (rec_s.__f7_absent != rec_r.__f7_absent) {
      printf("Absent flags for f7 are not the same\n");
      return (1);
   }

   if (!rec_s.__f7_absent && cmpdarrt(rec_s.f7,rec_r.f7)) {
      printf("rec_r.f7 != rec_s.f7\n");
      return (1);
   }
   
   return (0);
}


int cmpdarrt(s, r)
  struct { unsigned long len; rect *val; } s, r;
{ int i;

   if (s.len != r.len) {
      printf("Dynamic array lengths are not the same\n");
      return (1);
   }

   for (i=0; i<s.len; i++)
     if (cmprect(s.val[i], r.val[i])) {
        printf("Element %d of dynamic arrays are not the same\n",i);
        return (1);
     }

   return (0);
}
