// binTreeTest.cc    TDD for simple binary tree class
// P. Conrad for CISC220, 06J

#include "runTests.h"
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

// Now also do #include <string> and #include <sstream>
// This allows us to write to a string (C++ style string) as if it were 
// a file.  That way, we can test our print functions

#include <string>
using std::string;

#include <sstream>
using std::ostringstream;

#include "bintree.h"

void printHeader(const char * const file, int line)
{
  cout << "\n=== Tests from " << file << " at line " << line << "===" << endl;
}

int main(void)
{

  RunTests_C test;

  printHeader(__FILE__,__LINE__);

  BinTree t1(1);

  BinTree t2(1,
	     new BinTree(2),
	     new BinTree(3));

  {
    ostringstream tinorder,tpostorder,tpreorder;
    printHeader(__FILE__,__LINE__);
    t1.preOrder(tpreorder);
    test.assertEquals(tpreorder.str(),"1 ");
    
    t1.inOrder(tinorder);
    test.assertEquals(tinorder.str(),"1 ");
    
    t1.postOrder(tpostorder);
    test.assertEquals(tpostorder.str(),"1 ");
  }
  
  {
    printHeader(__FILE__,__LINE__);
    ostringstream tprint;
    tprint << t1;
    test.assertEquals(tprint.str(),"(1 () ())");
  }
  
  {
    ostringstream tinorder,tpostorder,tpreorder;

    printHeader(__FILE__,__LINE__);
    t2.preOrder(tpreorder);
    test.assertEquals(tpreorder.str(),"1 2 3 ");
    
    t2.inOrder(tinorder);
    test.assertEquals(tinorder.str(),"2 1 3 ");
    
    t2.postOrder(tpostorder);
    test.assertEquals(tpostorder.str(),"2 3 1 ");
  }
  
  {
    printHeader(__FILE__,__LINE__);
    ostringstream oss;
    oss << t2;
    test.assertEquals
      (oss.str(),
       "(1 (2 () ()) (3 () ()))");
  }
  

  BinTree t3(1,
	     new BinTree(2,
			 new BinTree(3, 
				     new BinTree(4),
				     NULL),
			 new BinTree(5,
				     NULL,
				     new BinTree(6,
						 new BinTree(7),
						 new BinTree(8)
						 )
				     )
			 ),
	     new BinTree(9)
	     );


  
  {
    ostringstream tinorder,tpostorder,tpreorder;

    printHeader(__FILE__,__LINE__);
    t3.preOrder(tpreorder);
    test.assertEquals(tpreorder.str(),"1 2 3 4 5 6 7 8 9 ");
    
    t3.inOrder(tinorder);
    test.assertEquals(tinorder.str(),"4 3 2 5 7 6 8 1 9 ");
    
    t3.postOrder(tpostorder);
    test.assertEquals(tpostorder.str(),"4 3 7 8 6 5 2 9 1 ");
  }
  
  {
    printHeader(__FILE__,__LINE__);
    ostringstream oss;
    oss << t3;
    test.assertEquals
      (oss.str(),
       "(1 (2 (3 (4 () ()) ()) (5 () (6 (7 () ()) (8 () ())))) (9 () ()))");
  }
  
  
  test.print();
  test.finish();

}




