https://github.com/Unipisa/CMM
Revision c0f12bb6f3ea8f1350371695b42990a5c2eb93f2 authored by Giuseppe Attardi on 02 March 1998, 23:00:00 UTC, committed by CMM Curation Team on 11 December 2019, 14:35:48 UTC
Contributors mentioned in Changelog :
 - Giuseppe Attardi @attardi
 - Tito Flagella @tflagella
 - Pietro Iglio
1 parent 55778ad
Raw File
Tip revision: c0f12bb6f3ea8f1350371695b42990a5c2eb93f2 authored by Giuseppe Attardi on 02 March 1998, 23:00:00 UTC
1.9 -
Tip revision: c0f12bb
test1.cpp
/* Test program for CMM */

/* Externals */

#include <stdio.h>
#include "cmm.h"

#define	VECT_SIZE	1000

struct  cell : public CmmObject 
{
  cell  *car;
  cell  *cdr;
  int  value;
  cell(cell *initcar, cell *initcdr, int initvalue);
#ifdef FIELDTABLE
  CmmFieldTable& getFieldTable()
    {
      static CmmFieldPtr fields[] = { CmmFieldPtr(&cell::car),
				      CmmFieldPtr(&cell::cdr) };
      static CmmFieldTable table = { 2, fields };
      return table;
    }
#else
  void traverse()
    {
      CmmHeap *heap = Cmm::heap;
      heap->scavenge((CmmObject **)&car);
      heap->scavenge((CmmObject **)&cdr);
    }
#endif
};

typedef  cell* CP;

cell::cell(cell *initcar, cell *initcdr, int initvalue)
{
  car = initcar;
  cdr = initcdr;
  value = initvalue;
}

struct vector : public CmmObject 
{
  vector  *car;
  vector  *cdr;
  int  value1;
  char bytes[VECT_SIZE];
  int  value2;
  vector(vector* x, vector* y, int v1, int v2);
  void traverse();
};

typedef  vector* VP;

void vector::traverse()
{
  CmmHeap *heap = Cmm::heap;
  heap->scavenge((CmmObject **)&car);
  heap->scavenge((CmmObject **)&cdr);
}

vector::vector(vector* x, vector* y, int v1, int v2)  
{
  car = x;
  cdr = y;
  value1 = v1;
  value2 = v2;
}

/* Test program */

int  init_global = 2,
array_global[VECT_SIZE];

void
printtree(CP zp)
{
  CP  tp;

  tp = zp;
  while  (tp != NULL)  
    {
      printf("%x: %d  ", tp, tp->value);
      tp = tp->cdr;
    }
  printf("\n");
  tp = zp;
  while  (tp != NULL)  
    {
      printf("%x: %d  ", tp, tp->value);
      tp = tp->car;
    }
  printf("\n");
}

void
listtest1()
{
  int  i, j;
  CP  lp, zp;
  
  printf("List test 1\n");
  lp = NULL;
  for (i = 0; i <= VECT_SIZE ; i++)  
    {
      if  (i % 50 == 0)
	{ printf("."); fflush(stdout); }
      zp = new cell(NULL, lp, i);
      lp = zp;
      Cmm::heap->collect();
      zp = lp;
      for (j = i; j >= 0 ; j--)  
	{
	  if ((zp == NULL) || (zp->value != j))
	    printf("LP is not a good list when j = %d\n", j);
	  zp = zp->cdr;
	}
    }
  printf("\n");		   
}

void
vectortest()
{
  int  i, j;
  VP  lp, zp;
  
  printf("Vector test\n");
  lp = NULL;
  for (i = 0; i <= 100 ; i++)  
    {
      if  (i % 10 == 0)
	{ printf("."); fflush(stdout); }
      zp = new vector(NULL, lp, i, i);
      lp = zp;
      Cmm::heap->collect();
      zp = lp;
      for (j = i; j >= 0 ; j--)
	{
	  if ((zp == NULL) || (zp->value1 != j)  ||  (zp->value2 != j))
	    printf("LP is not a good list when j = %d\n", j);
	  zp = zp->cdr;
	}
    }
  printf("\n");		   
}

CP
treetest()
{
  int  i;
  CP  tp, zp;

  printf("Tree test\n");
  tp = new cell(NULL, NULL, 0);
  for (i = 1; i <= 5; i++)  
    {
      zp = new cell(tp, tp, i);
      tp = zp;
    }
  Cmm::heap->collect();
  zp = new cell(tp, tp, 6);
  Cmm::heap->collect();
  printtree(zp);
  return(zp);
}

void
listtest2()
{
  int  i, j, length = 10000, repeat = 100;
  CP  lp, zp;

  printf("List Test 2\n");
  for (i = 0; i < repeat; i++)  
    {
      if  (i % 50 == 0)
	{ printf("."); fflush(stdout); }
      /* Build the list */
      lp = NULL;
      for  (j = 0; j < length; j++)  
	{
	  zp = new cell(NULL, lp, j);
	  lp = zp;
	}
      /* Check the list */
      zp = lp;
      for (j = length-1; j >= 0 ; j--)  
	{
	  if ((zp == NULL) || (zp->value != j))
	    printf("LP is not a good list when j = %d\n", j);
	  zp = zp->cdr;
	}
    }
  printf("\n");		   
}

CP  gp;		/* A global pointer */

void
main()
{
  /* List construction test */
  listtest1();

  /* List of vectors > 1 page */
  vectortest();

  /* Tree construction test */
  gp = treetest();

  /* 1000 10000 node lists */
  listtest2();

  /* Check that tree is still there */
  printtree(gp);
}
back to top