#include <stream.h>
#include <Pointer.h>

class TestObject1 : public Indexable {
public:
  virtual char* classname() { return "TestObject1"; }
  virtual IOstatus readContents()  { cin  >> nr_; return Indexable::readContents(); }
  virtual IOstatus writeContents() { cout << nr_; return Indexable::writeContents(); }
  virtual Listable* duplicate() { return new TestObject1(*this); }
private:
  int nr_;
};

class TestObject2 : public Indexable {
public:
  virtual char* classname() { return "TestObject2"; }
  virtual IOstatus readContents()  { cin  >> nr_; return Indexable::readContents(); }
  virtual IOstatus writeContents() { cout << nr_; return Indexable::writeContents(); }
  virtual Listable* duplicate() { return new TestObject2(*this); }
private:
  int nr_;
};

void testPointer()
{
  cerr << "Testing Pointer...\n";

  Index a(new TestObject1);
  Index b(new TestObject2);
  if (a.read() != ok) cout << "Failure to return proper IOstatus in Index\n";
  if (b.read() != ok) cout << "Failure to return proper IOstatus in Index\n";

  Pointer p(a), q(b, "hello");
  p.write();
  q.write();
  if (p.read() == ok) cout << "Failure to return proper IOstatus in Pointer\n";

  Key k;
  if (k.read() != ok) cout << "Failure to return proper IOstatus in Key\n";
  if (p.read() != ok) cout << "Failure to return proper IOstatus in Pointer\n";
  p.write();

  p.lookup()->write();
  q.lookup()->write();

  cout << "p == q (before assignment) ?  " << (p == q) << "\n";
  p = q;
  cout << "p == q (after assignment) ?  " << (p == q) << "\n";
  p.write();
  q.write();
  p.lookup()->write();
}
