home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GEMini Atari
/
GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso
/
files
/
gnu
/
g__lib
/
tmap.cc
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-23
|
6KB
|
276 lines
/*
a test file for Maps
*/
#include <stream.h>
#include <assert.h>
#define tassert(ex) { cerr << #ex; \
if ((ex)) cerr << " OK\n"; \
else cerr << " Fail\n"; }
#include "int.int.Map.h"
unsigned int hash(int x) { return multiplicativehash(x) ; }
int SIZE;
int *nums;
int *odds;
int *perm;
void add(int x[], int y[], intintMap& a)
{
for (int i = 0; i < SIZE; ++i) a[x[i]] = y[i];
}
#include <MLCG.h>
MLCG randgen;
void permute(int x[])
{
for (int i = 1; i < SIZE; ++i)
{
int j = randgen.asLong() % (i + 1);
int tmp = x[i]; x[i] = x[j]; x[j] = tmp;
}
}
void makenums()
{
for (int i = 0; i < SIZE; ++i) nums[i] = i + 1;
}
void makeodds()
{
for (int i = 0; i < SIZE; ++i) odds[i] = 2 * i + 1;
permute(odds);
}
void makeperm()
{
for (int i = 0; i < SIZE; ++i) perm[i] = i + 1;
permute(perm);
}
void printMap(intintMap& a)
{
int maxprint = 20;
cout << "[";
int k = 0;
for (Pix i = a.first(); i != 0 && k < maxprint; a.next(i),++k)
cout << "(" << a.key(i) << ", " << a.contents(i) << ") ";
if (i != 0) cout << "...]\n";
else cout << "]\n";
}
#include "int.int.SplayMap.h"
void Splaytest()
{
intintSplayMap a(-1);
add(nums, perm, a);
intintSplayMap b(-1);
add(perm, nums, b);
intintSplayMap c(-1);
add(perm, odds, c);
intintSplayMap d(a);
add(nums, nums, d);
cout << "a: "; printMap(a);
cout << "b: "; printMap(b);
cout << "c: "; printMap(c);
cout << "d: "; printMap(d);
assert(a.length() == SIZE);
assert(b.length() == SIZE);
assert(c.length() == SIZE);
assert(d.length() == SIZE);
for (int j = 1; j <= SIZE; ++j) assert(a.contains(j));
assert(a[SIZE+1] = -1);
for (j = 1; j <= SIZE; ++j) assert(b.contains(j));
for (j = 1; j <= SIZE; ++j) assert(b[a[j]] == j);
for (j = 1; j <= SIZE; ++j) assert(a[b[j]] == j);
for (j = 1; j <= SIZE; ++j) assert((c[j] & 1) != 0);
for (j = 1; j <= SIZE; ++j) assert(d[j] == j);
d.del(1);
assert(!d.contains(1));
for (j = 1; j <= SIZE; ++j) d.del(j);
assert(d.empty());
assert(a.OK());
assert(b.OK());
assert(c.OK());
assert(d.OK());
}
#include "int.int.VHMap.h"
void VHtest()
{
intintVHMap a(-1, SIZE);
add(nums, perm, a);
intintVHMap b(-1, SIZE);
add(perm, nums, b);
intintVHMap c(-1, SIZE);
add(perm, odds, c);
intintVHMap d(a);
add(nums, nums, d);
cout << "a: "; printMap(a);
cout << "b: "; printMap(b);
cout << "c: "; printMap(c);
cout << "d: "; printMap(d);
assert(a.length() == SIZE);
assert(b.length() == SIZE);
assert(c.length() == SIZE);
assert(d.length() == SIZE);
for (int j = 1; j <= SIZE; ++j) assert(a.contains(j));
assert(a[SIZE+1] = -1);
for (j = 1; j <= SIZE; ++j) assert(b.contains(j));
for (j = 1; j <= SIZE; ++j) assert(b[a[j]] == j);
for (j = 1; j <= SIZE; ++j) assert(a[b[j]] == j);
for (j = 1; j <= SIZE; ++j) assert((c[j] & 1) != 0);
for (j = 1; j <= SIZE; ++j) assert(d[j] == j);
d.del(1);
assert(!d.contains(1));
for (j = 1; j <= SIZE; ++j) d.del(j);
assert(d.empty());
assert(a.OK());
assert(b.OK());
assert(c.OK());
assert(d.OK());
}
#include "int.int.CHMap.h"
void CHtest()
{
intintCHMap a(-1, SIZE);
add(nums, perm, a);
intintCHMap b(-1, SIZE);
add(perm, nums, b);
intintCHMap c(-1, SIZE);
add(perm, odds, c);
intintCHMap d(a);
add(nums, nums, d);
cout << "a: "; printMap(a);
cout << "b: "; printMap(b);
cout << "c: "; printMap(c);
cout << "d: "; printMap(d);
assert(a.length() == SIZE);
assert(b.length() == SIZE);
assert(c.length() == SIZE);
assert(d.length() == SIZE);
for (int j = 1; j <= SIZE; ++j) assert(a.contains(j));
assert(a[SIZE+1] = -1);
for (j = 1; j <= SIZE; ++j) assert(b.contains(j));
for (j = 1; j <= SIZE; ++j) assert(b[a[j]] == j);
for (j = 1; j <= SIZE; ++j) assert(a[b[j]] == j);
for (j = 1; j <= SIZE; ++j) assert((c[j] & 1) != 0);
for (j = 1; j <= SIZE; ++j) assert(d[j] == j);
d.del(1);
assert(!d.contains(1));
for (j = 1; j <= SIZE; ++j) d.del(j);
assert(d.empty());
assert(a.OK());
assert(b.OK());
assert(c.OK());
assert(d.OK());
}
#include "int.int.AVLMap.h"
void AVLtest()
{
intintAVLMap a(-1);
add(nums, perm, a);
intintAVLMap b(-1);
add(perm, nums, b);
intintAVLMap c(-1);
add(perm, odds, c);
intintAVLMap d(a);
add(nums, nums, d);
cout << "a: "; printMap(a);
cout << "b: "; printMap(b);
cout << "c: "; printMap(c);
cout << "d: "; printMap(d);
assert(a.length() == SIZE);
assert(b.length() == SIZE);
assert(c.length() == SIZE);
assert(d.length() == SIZE);
for (int j = 1; j <= SIZE; ++j) assert(a.contains(j));
assert(a[SIZE+1] = -1);
for (j = 1; j <= SIZE; ++j) assert(b.contains(j));
for (j = 1; j <= SIZE; ++j) assert(b[a[j]] == j);
for (j = 1; j <= SIZE; ++j) assert(a[b[j]] == j);
for (j = 1; j <= SIZE; ++j) assert((c[j] & 1) != 0);
for (j = 1; j <= SIZE; ++j) assert(d[j] == j);
d.del(1);
assert(!d.contains(1));
for (j = 1; j <= SIZE; ++j) d.del(j);
assert(d.empty());
assert(a.OK());
assert(b.OK());
assert(c.OK());
assert(d.OK());
}
double return_elapsed_time ( double );
double start_timer ( );
main(int argv, char** argc)
{
if (argv > 1)
{
SIZE = abs(atoi(argc[1]));
SIZE &= ~1;
}
else
SIZE = 100;
nums = new int[SIZE];
odds = new int[SIZE];
perm = new int[SIZE];
makenums();
makeodds();
makeperm();
start_timer();
cout << "Splaytest\n"; Splaytest();
cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
start_timer();
cout << "VHtest\n"; VHtest();
cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
start_timer();
cout << "CHtest\n"; CHtest();
cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
start_timer();
cout << "AVLtest\n"; AVLtest();
cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
}