// Oldham, Jeffrey D.
// 2000Mar17
// CS1321

// Test Dynamic Array Implementations

// Test copy constructor.

#include <iostream.h>
#include <stdlib.h>		// has EXIT_SUCCESS
#include <mcheck.h>		// helps detect memory leaks
#include "dynamicArray.h"

ostream& printArray(ostream& out, dynamicArray & d, const char msg[]) {
  out << msg << "\t" << d.size() << endl;
  for (dynamicArray::length_pos i = 0; i < d.size(); ++i)
    out << i << ":\t" << d.get(i) << endl;
  return out;
}


int main(int argc, char *argv[])
{
  mtrace();                     // turn on memory leak detection
  if (argc != 2) {
    cerr << argv[0] << ": number-of-pushes\n";
    throw "missing command-line argument";
  }
  dynamicArray::length_pos nuPushes =
    strtoul(argv[1], static_cast<char **>(0), 0);
				// Do not check for errors.  Bad!
  if (nuPushes < 4) {
    cerr << argv[0] << ": number-of-pushes must be at least 4\n";
    throw "too small number of pushes";
  }

  dynamicArray d1;
  for (dynamicArray::length_pos i = 0; i < nuPushes; ++i)
    d1.push_back(2*i+1);

  // Test copy constructor.
  dynamicArray d2(d1);
  if (d2.size() != d1.size())
    cerr << "After copying, d1 and d2 have different sizes!\n";
  printArray(cout, d1, "d1 after copy");
  printArray(cout, d2, "d2 after copy");

  // Check for distinct arrays.
  d1.set(1, -2);
  d2.set(d2.size()-2, -3);
  printArray(cout, d1, "d1 after set");
  printArray(cout, d2, "d2 after set");

  return EXIT_SUCCESS;
}