head     1.1;
branch   ;
access   ;
symbols  ;
locks    ; strict;
comment  @// @;


1.1
date     92.08.20.11.22.34;  author thoth;  state Exp;
branches ;
next     ;


desc
@a test of boolean operations on random pointsets.
@



1.1
log
@Initial revision
@
text
@// Emacs: -*- C++ -*-
static char rcsid[] = "$Id$";

//
// $Log$
//

#include <stdlib.h>

#include "IntPoint.h"
#include "IntPointSet.h"
#include "YoderIPS.h"
#include "IPSIter.h"
#include <ACG.h>

#include "generic-tests.h"

IntPoint
make_random_point(int dimensions, int bottom, int top, ACG& gen)
{
    int   range;

    if (top < bottom) {
	int temp = top;
	top = bottom;
	bottom = temp;
    }
    range = top - bottom + 1;    

    switch (dimensions) {
    case 0:
	return IntPoint();
    case 1:
	return IntPoint(bottom +  (unsigned long)gen.asLong() % range);
    case 2:
	return IntPoint(bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range);
    case 3:
	return IntPoint(bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range);
    case 4:
	return IntPoint(bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range);
    case 5:
	return IntPoint(bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range,
			  bottom +  (unsigned long)gen.asLong() % range);
    default:
	return IntPoint(0, 0, 0, 0, 0);    
    }
}

void
test_arr_ps(IntPointSet ps, IntPoint *arr, unsigned len)
{
  unsigned i;

  for (i=0; i<len; i++) {
#if 0
    cout << arr[i] << ' ';
#endif
    if (!ps.contains(arr[i])) {
      cout << "pointset missing "<<arr[i]<<" at index "<<i<<'\n'<<flush;
    }
  }
  cout << '\n';

  IPSIter	iter;
  iter = ps;

  IntPoint	ip;
  i=0;
  while (iter(ip)) {
    int	cmp;
#if 0
    cout << ip << ' ';
#endif
    for (i=0; i<len && 1==(cmp=Pointcmp(ip,arr[i])); i++)
      ;
    if (0!=cmp) {
      cout << "pointset extra "<<ip<<'\n'<<flush;
    }
  }

}

int main(int argc, char**argv)
{
    unsigned size;
    size = (argc<2) ? 2 : atoi(argv[1]);
    
    unsigned l1, l2;
    l1 = (argc<3) ? 40 : atoi(argv[2]);
    l2 = (argc<4) ? 40 : atoi(argv[3]);
    
    IntPoint	*a1 = new IntPoint[l1];
    IntPoint	*a2 = new IntPoint[l2];
    
    ACG		generator(42, 771);
    
    cout << "ready to fill the arrays\n" << flush;
    
    unsigned i;
    for (i=0; i<l1; i++)
	a1[i] = make_random_point(size, 0, 9, generator);
    cout << "array 1 filled\n" << flush;
    for (i=0; i<l2; i++)
	a2[i] = make_random_point(size, 0, 9, generator);
    cout << "array 2 filled\n" << flush;
    
#if 0
    // this uses a private constructor which is illegal, but g++ lets it slide
    IntPointSet	ps1(YoderIPS(a1,l1));
    cout << "ips 1 constructed\n" <<flush;
    IntPointSet	ps2(YoderIPS(a2,l2));
    cout << "ips 2 constructed\n" <<flush;
#else
    IntPointSet	ps1(a1,l1);
    IntPointSet	ps2(a2,l2);
#endif
    
    SortIntPointArray(a1, l1);
    cout << "array 1 sorted\n" << flush;
    SortIntPointArray(a2, l2);
    cout << "array 2 sorted\n" << flush;
    
    test_arr_ps(ps1, a1, l1);
    cout << "ips 1 tested\n"<< flush;
    test_arr_ps(ps2, a2, l2);
    cout << "ips 2 tested\n"<< flush;
    
#if 0
    cout << "outputting ips 1\n";
    ps1.output(cout);
    cout << "outputting ips 2\n";
    ps2.output(cout);
#endif
    
    test_iter(ps1);
    test_iter(ps2);
    
    cout << "dimensions: " << ps1.dim() << ' ' << ps2.dim() << '\n' << flush;
    IntPointSet temp(ps1|ps2);
    test_union(ps1,ps2,temp);
    test_iter(temp);
    
    temp = ps1&ps2;
    test_intersection(ps1,ps2,temp);
    test_iter(temp);
    
    temp = ps1^ps2;
    test_XOR(ps1,ps2,temp);
    test_iter(temp);
    
    temp = ps1-ps2;
#if 0
    {
	IPSIter	iter(temp);
	IntPoint	ip;
	while (iter(ip)) {
	    cout << ip << ' ';
	}
	cout << "ps1-ps2\n";
    }
#endif
    test_minus(ps1,ps2,temp);
    test_iter(temp);
}
@
