// testPerson.cc    P. Conrad for CISC220, 06J
// test of Person_C base class


#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include "runTests.h"

#include "person.h"

int main(void)
{

  RunTests_C test;


  Person_C x("Phill","Conrad",123);

  test.assertEquals(x.getFname(),"Phill");
  test.assertEquals(x.getLname(),"Conrad");
  test.assertEquals(x.getId(),123);

  // now test the copy constructor and overloaded assignment operator

  Person_C y(x); // tests the copy constructor's copying
  test.assertEquals(y.getFname(),"Phill");
  test.assertEquals(y.getLname(),"Conrad");
  test.assertEquals(y.getId(),123);

  // now test the independence of x and y--i.e. make sure
  // we did a deep copy and not a shallow one

  y.setFname("Fred");
  y.setLname("Flintstone");
  y.setId(456);

  test.assertEquals(y.getFname(),"Fred");
  test.assertEquals(y.getLname(),"Flintstone");
  test.assertEquals(y.getId(),456);

  test.assertEquals(x.getFname(),"Phill");
  test.assertEquals(x.getLname(),"Conrad");
  test.assertEquals(x.getId(),123);

  Person_C z = x; // does it use copy constructor or overloaded =?

  test.assertEquals(z.getFname(),"Phill");
  test.assertEquals(z.getLname(),"Conrad");
  test.assertEquals(z.getId(),123);

  test.assertEquals(x.getFname(),"Phill");
  test.assertEquals(x.getLname(),"Conrad");
  test.assertEquals(x.getId(),123);

  // test the overloaded = sign

  Person_C a("Adam","Smith",789);
  Person_C b("Betty","Davis",321);

  {
    // go into an inner block to test the destructor

    Person_C temp("Temp","Temp",999);
    
    temp = a;
    a = b;
    b = temp;

    test.assertEquals(temp.getFname(),"Adam");
    test.assertEquals(temp.getLname(),"Smith");
    test.assertEquals(temp.getId(),789);

    // this tests the destructor, since it will be called
    // at this point for the temp object
    // at this point, the most we can do is hope it doesn't seg fault
    // later we'll see better ways to test a destructor, and check
    // for memory leaks
  }
  
  test.assertEquals(a.getFname(),"Betty");
  test.assertEquals(a.getLname(),"Davis");
  test.assertEquals(a.getId(),321);
  
  test.assertEquals(b.getFname(),"Adam");
  test.assertEquals(b.getLname(),"Smith");
  test.assertEquals(b.getId(),789);

  // finally, test chaining and self assignment

  a = a;

  test.assertEquals(a.getFname(),"Betty");
  test.assertEquals(a.getLname(),"Davis");
  test.assertEquals(a.getId(),321);

  a = b = x;

  test.assertEquals(x.getFname(),"Phill");
  test.assertEquals(x.getLname(),"Conrad");
  test.assertEquals(x.getId(),123);

  test.assertEquals(a.getFname(),"Phill");
  test.assertEquals(a.getLname(),"Conrad");
  test.assertEquals(a.getId(),123);

  test.assertEquals(b.getFname(),"Phill");
  test.assertEquals(b.getLname(),"Conrad");
  test.assertEquals(b.getId(),123);


  // finish the test by printing a report

  test.print(cerr);
  test.finish();
}



