
#include <iostream>
using namespace std;

#include <linechopped.h>
#include <linechoppedtest.h>
#include <print.h>



int linechoppedtest::unittest01()
{
  typedef point2<double> pt2;

  typedef line<pt2,double> linetype;

  typedef linechopped< pt2, double > lchop;

  // Create horizontal unit line.
  lchop lc( 
    linetype( pt2(1.0,0.0), pt2(0.0,0.0) ),
    pt2(0.0,1.0) );

  cout << "Printing the t values of the line segment." << endl;
  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl;

  cout << "Chop with vertical lines." << endl;

  lchop::listiter pos;

  assertreturnOS(
    lc.split(
      pos, 
      lc.chain.begin(), 
      linetype(pt2(0.0,1.0),pt2(0.5,0.0))) );

  lchop::listiter pos2 = pos;
  
  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl;
  cout << endl;

  assertreturnOS(
    lc.split(
      pos, 
      lc.chain.begin(), 
      linetype(pt2(0.0,1.0),pt2(1.0/3.0,0.0))));

  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl << endl;

  ++pos2;

  cout << SHOW(*pos2) << endl;
  assertreturnOS(
    lc.split(pos, pos2, linetype(pt2(0.0,1.0),pt2(2.0/3.0,0.0))));
  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl;

  return 0;
}


int linechoppedtest::unittest02()
{
  typedef point2<double> pt2;

  typedef line<pt2,double> linetype;

  typedef linechoppedindexed<pt2,double,uint> lchop;

  typedef pointindexed<pt2,uint> pti;

  pti t2(pt2(0.0,1.0), 5);

  // Create horizontal unit line.
  lchop lc( 
    linetype( pt2(1.0,0.0), pt2(0.0,0.0) ),
    pti(pt2(0.0,1.0), 5) );

  cout << "Printing the t values of the line segment." << endl;
  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl;


  cout << "Chop with vertical lines." << endl;

  lchop::listiter pos;

  bool res;
  res = 
    lc.split(
      pos, 
      lc.chain.begin(), 
      linetype(pt2(0.0,1.0),pt2(0.5,0.0)));

  if (res)
    (*pos).index = 32;

  lchop::listiter pos2 = pos;
  

  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl;
  cout << endl;

  res = 
    lc.split(
      pos, 
      lc.chain.begin(), 
      linetype(pt2(0.0,1.0),pt2(1.0/3.0,0.0)));
  if (res)
    (*pos).index = 37;

  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl << endl;

  ++pos2;

  cout << SHOW(*pos2) << endl;
  res = 
    lc.split(pos, pos2, linetype(pt2(0.0,1.0),pt2(2.0/3.0,0.0)));
  if (res)
    (*pos).index = 197;

  cout << print(lc.chain.begin(),lc.chain.end(), "\n") << endl;

  pos=lc.chain.begin();
  assertreturnOS( *pos == pti(pt2(0.0,1.0/3.0),37) );
  ++pos;
  assertreturnOS( *pos == pti(pt2(1.0/3.0,0.5),32) );
  ++pos;
  assertreturnOS( *pos == pti(pt2(0.5,2.0/3.0),197) );
  ++pos;
  assertreturnOS( *pos == pti(pt2(2.0/3.0,1.0),5) );

  return 0;
}


