#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
using namespace std;

#include <commandline.h>
#include <commandlinetest.h>
#include <print.h>

string commandlinetest::doc[] =
{
  "",
  "Reading a variable from the command line.",
  "The -h option prints the names in the command map.",
  "Tests mapcallback where if a token is present a function is called.",
  "Testing quoting at shell command.",
  "Testing quoting from string."
};

/*

// Commented out because much of the functionality was removed 
//  from the public interface.  Chelton 7th July 2003.

void test01(int argc, char **argv)
{
  cout << "***" << endl;
  cout << "argc=" << argc << endl;
  for (int i=0; i<argc; ++i)
    cout << argv[i] << endl;
}

void test02()
{
  string s("cat=mat");
  string s2 = s;
  string::size_type k = s.find('=');
  cout << k << endl;
  s.erase(k);
  cout << s << endl;
  s2.erase(0,k+1);
  cout << s2 << endl;
}

void test03()
{
  string s2, s3;
  bool res = splitstring(s2,s3,"fk",'=');

  cout << "res=" << (bool)res << endl;

  cout << s2 << endl;
  cout << s3 << endl;
}

void test05(int argc, char **argv)
{
  map<string,string> m;
  readToMap(m,argc,argv);

  double d=-1.0;
  map_var<double&> v(d,m,"x1");

  cout << "found()=" << v.found() << endl;
  cout << "d=" << d << endl;
}

void test06(int argc, char **argv)
{
   map<string,string> m;
   readToMap(m,argc,argv);
 
   string s;
   writeMap(s,m);
   cout << s << endl;
}

*/

void commandlinetest::test01(int argc, char **argv)
{
  commandline c(argc,argv);

  double d=-1.0;
  c.mapvar(d,"d");

  cout << "d=" << d << endl;
}

void commandlinetest::test02(int argc, char **argv)
{
  commandline c(argc,argv);

  // Read in the file pointed to by file= .
  c.readfile("file");

  bool hat=false;
  c.mapvar(hat,"hat");
  bool cat=true;
  c.mapvar(cat,"cat");

  double a=-1.0;
  c.mapvar(a,"a");

  string s;
  c.mapvar(s,"s");

  c.enablehelp() << endl;

  cout << "hat=" << hat << endl;
  cout << "cat=" << cat << endl;
  cout << "s=" << s << endl;
  cout << "a=" << a << endl;
}

class f1
{
public:

  void cat()
    { cout << "cat" << endl; }
  void cat2()
    { cout << "cat" << endl; }

};

typedef void (f1::*pf)();

template< typename T >
void callback(T & x, void ( T::*p )()  )
{
  (x.*p)();
}

void commandlinetest::test03(int argc, char** argv)
{
  cout << "Call ./main prog=17 cat=true to invoke the call" << endl << endl;
  commandline c(argc,argv);

  f1 f;
  c.mapcallback(f,&f1::cat,"cat");

  cout << "Testing the hastoken(string) function." << endl;
  if (c.hastoken("cat"))
    cout << "The token \"cat\" is present." << endl;
  else
    cout << "The token \"cat\" was not in the command line." << endl;
  
}

void commandlinetest::test04(int argc, char** argv)
{
  cout << "Call ./main prog=18 p1=\"1 -2 5\" to invoke the call" << endl << endl;
  commandline c(argc,argv);

  string p1;
  c.mapvar(p1,"p1");
  cout << "p1=*" << p1 << "*" << endl;

  int num(-1);
  c.mapvar(num,"num");
}

int commandlinetest::test05()
{
  string arg1="s1=abc points=\"1 19 32 4\" N=350 -h"; 
cout << SHOW(arg1) << endl << endl;

  string s1;
  int N=-1;
  string points;

  commandline c1(arg1);
  c1.mapvar(s1,"s1");
  c1.mapvar(N,"N");
  c1.mapvar(points,"points");
  c1.enablehelp();
  
  cout << endl;
  cout << "N=*" << N << "*" << endl;
  cout << "s1=*" << s1 << "*" << endl;
  cout << "points=*" << points << "*" << endl;

  assertreturnOS(s1=="abc");
  assertreturnOS(points=="1 19 32 4");
  assertreturnOS(N==350);

  return 0;
}


