/*
 * This file is a (rather silly) demonstration of the use of the
 * C++ Dynamic Object library.  It is a also reasonably thorough test
 * of the library (except that it only tests it with one data size).
 *
 * There are no restrictions on this code; however, if you make any
 * changes, I request that you document them so that I do not get
 * credit or blame for your modifications.
 *
 * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
 * and MIT-Project Athena, 1990.
 */

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "DynObject.h"

static char random_string[] = "This is a random string.";
static char insert1[] = "This will be put at the beginning.";
static char insert2[] = "(parenthetical remark!) ";
static char insert3[] = "  This follows the random string.";

void test_copy(DynObject);

main()
{
     int	i, s;
     char	d, *data;

     DynObject& obj = *new DynObject(sizeof(char), 8);
     if (! &obj) {
	  fprintf(stderr, "test: create failed.\n");
	  exit(1);
     }
     
     obj.Debug(1);
     obj.Paranoid(1);

     if (obj[-5] || obj[0] || obj[1000]) {
	  fprintf(stderr, "test: Get did not fail when it should have.\n");
	  exit(1);
     }

     if (obj.Delete(-1) != DYN_BADINDEX ||
	 obj.Delete(0) != DYN_BADINDEX ||
	 obj.Delete(100) != DYN_BADINDEX) {
	  fprintf(stderr, "test: Delete did not fail when it should have.\n");
	  exit(1);
     }

     printf("Size of empty object: %d\n", obj.Size());

     for (i=0; i<14; i++) {
	  d = (char) i;
	  if (obj.Add(&d) != DYN_OK) {
	       fprintf(stderr, "test: Adding %d failed.\n", i);
	       exit(1);
	  }
     }

     if (obj.Append(random_string, strlen(random_string)+1) != DYN_OK) {
	  fprintf(stderr, "test: appending array failed.\n");
	  exit(1);
     }
     
     if (obj.Delete(obj.High() / 2) != DYN_OK) {
	  fprintf(stderr, "test: deleting element failed.\n");
	  exit(1);
     }

     if (obj.Delete(obj.High() * 2) == DYN_OK) {
	  fprintf(stderr, "test: delete should have failed here.\n");
	  exit(1);
     }

     d = 200;
     if ((obj += &d) != DYN_OK) {
	  fprintf(stderr, "test: Adding %d failed.\n", i);
	  exit(1);
     }

     data = (char *) obj[0];
     s = obj.Size();
     for (i=0; i < s; i++)
	  printf("Element %d is %d.\n", i, (unsigned char) data[i]);

     data = (char *) obj[13];
     printf("Element 13 is %d.\n", (unsigned char) *data);

     data = (char *) obj[obj.Size()];
     if (data) {
	  fprintf(stderr, "DynGet did not return NULL when it should have.\n");
	  exit(1);
     }

     printf("This should be the random string: \"%s\"\n", obj[14]);

     if (obj.Insert(-1, "foo", 4) != DYN_BADINDEX ||
	 obj.Insert(obj.Size() + 1, "foo", 4) != DYN_BADINDEX ||
	 obj.Insert(0, "foo", -1) != DYN_BADVALUE) {
	  fprintf(stderr, "Insert did not fail when it should have.\n");
	  exit(1);
     }

     if (obj.Insert(obj.Size()-2, insert3, strlen(insert3) + 1) != DYN_OK) {
	  fprintf(stderr, "DynInsert to end failed.\n");
	  exit(1);
     }  

     if (obj.Insert(19, insert2, strlen(insert2)) != DYN_OK) {
	  fprintf(stderr, "Insert to middle failed.\n");
	  exit(1);
     }
     
     if (obj.Insert(0, insert1, strlen(insert1)+1) != DYN_OK) {
	  fprintf(stderr, "DynInsert to start failed.\n");
	  exit(1);
     }	

     printf("A new random string: \"%s\"\n", obj[14 + strlen(insert1) + 1]);
     printf("This was put at the beginning: \"%s\"\n", obj[0]);

     test_copy(obj);

     delete &obj;

     return 0;
}

void test_copy(DynObject obj)
{
     DynObject obj2(0, 0);
     
     printf("\nThe next two lines should be the same as above:\n");
     printf("A new random string: \"%s\"\n", obj[14 + strlen(insert1) + 1]);
     printf("This was put at the beginning: \"%s\"\n", obj[0]);

     obj2 = obj;

     printf("\nOne more time:\n");
     printf("A new random string: \"%s\"\n", obj[14 + strlen(insert1) + 1]);
     printf("This was put at the beginning: \"%s\"\n", obj[0]);
}
