// This may look like C code, but it is really -*- C++ -*-

//
//  monopoly-template  --  Copyright (c) University of Aizu 1994
//
// [# Edit, Date, User, Module #]
#include <stream.h>
#include <List.h>
#include <malloc.h>

class TestObject : public Listable {
public:
  virtual char* classname() { return "TestObject"; }
  virtual IOstatus readContents()  { cin  >> a_; return ok; }
  virtual IOstatus writeContents() { cout << a_; return ok; }
  virtual Listable* duplicate() { return new TestObject(*this); }
private:
  char a_;
};

void testMemory()
{
  cerr << "Testing copy operations and memory leaks\n";
  struct mallinfo mem;
  int all, max = 0;

  for (int i = 0; i < 10; i++) {
    cerr << "Pass Nr. " << i << "\n";
    {
      List* a = new List(new TestObject);
      a->read();
      List* b = new List(*a);
      b->write();
      mem = mallinfo();
      cerr << " copy b from a " << mem.allocated << "\n";
      a->read();
      *b = *a;
      b->write();
      mem = mallinfo();
      cerr << " assignment b=a " << mem.allocated << "\n";
      a->clear();
      mem = mallinfo();
      cerr << " a->clear() : " << mem.allocated << "\n";
      a->write();
      delete a;
      delete b;
      cerr << " deleted a and b...\n";
    }
    mem = mallinfo();
    cerr << " End of Loop : " << all << "\n";
    all = mem.allocated;
    if (max > 0 && max != all) cout << "MEMORY LEAK!!\n";
    max = all;
  }
}


// End of file
