ClassS04CS141/Test1.Cc

Top | ClassS04CS141 | recent changes | Preferences

#include <iostream.h>

#include "Array.h"
#include "Array.cc"

#include "AssocList.h"
#include "AssocList.cc"

#include "Hash.h"
#include "Hash.cc"

void test_array() {
  Array<int> g;

  {
    Array<int> h;
  
    h[1] = 2;
    h[2] = 4;
    h[3] = 6;

    cout << "here is h:" << endl;

    cout << h << endl;

    g = h;
  }

  Array<int> f(g);

  cout << "here is f:" << endl;

  cout << f << endl;
}

void test_assoc() {
  AssocList<char *,char *> g;

  {
    AssocList<char *,char *> h;
  
    h["hello"] = "goodbye";
    h["dog"] = "dog food";
    h["cat"] = "bird";

    h["hello"] = "hi";

    cout << "here is h:" << endl;

    cout << h << endl;

    g = h;
  }

  AssocList<char *,char *> f(g);

  cout << "here is f:" << endl;

  cout << f << endl;

  cout << "here is f again:" << endl;

  Array<char *> keys(f.keys());

  for (int i = 0;  i < keys.size(); ++i)
    cout << keys[i] << " -> " << f[keys[i]] << endl;
}

void test_hash() {
  HashTable<char *,char *> g;

  {
    HashTable<char *,char *> h;
  
    h["hello"] = "goodbye";
    h["dog"] = "dog food";
    h["cat"] = "bird";

    h["hello"] = "hi";

    cout << "here is h:" << endl;

    cout << h << endl;

    g = h;
  }

  HashTable<char *,char *> f(g);

  cout << "here is f:" << endl;

  cout << f << endl;

  cout << "here is f again:" << endl;

  cout << "keys:" << endl;

  cout << f.keys() << endl;

  cout << "end keys" << endl;

  Array<char *> keys = f.keys();


  cout << "size = " << keys.size() << endl;

  for (int i = 0;  i < keys.size(); ++i)
    cout << i << ": " << keys[i] << " -> " << f[keys[i]] << endl;

  cout << "there was f." << endl;
}

void hard_test_hash() {
  Array<int> a(0);
  HashTable<int,int> h(1);

  int errors = 0;

  for (int i = 0;  i < 1000;  ++i) {
    h[i] = 100-i;
    a[i] = 100-i;
  }

  for (int i = 0;  i < 1000;  ++i) {
    if (a[i] != h[i]) {
      cerr << "ERROR: disagreement at " << i << ": "
	   << a[i] << " != " << h[i] << endl;
      ++errors;
    }
    h.remove(i);
  }
  
  if (h.keys().size() != 0) {
    cerr << "ERROR:  h.keys().size() == " <<
      h.keys().size() << endl;
    ++errors;
  }
  if (errors > 0)
    cout << "hard_test_hash failed. # errors = " << errors << endl;
  else
    cout << "hard_test_hash passed with no errors.\n";
}



main() {
  cout << "TEST ARRAY:\n";
  test_array();

  cout << "TEST ASSOCLIST:\n";
  test_assoc();

  cout << "TEST HASH:\n";
  test_hash();

  cout << "HARD TEST HASH:\n";
  hard_test_hash();

}

Top | ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited April 5, 2004 3:01 pm by Neal (diff)
Search: