Files Classes Functions Hierarchy
00001 #include <fstream> 00002 #include <iostream> 00003 #include <map> 00004 #include <sstream> 00005 #include <string> 00006 using namespace std; 00007 00008 #include <commandline.h> 00009 #include <commandlinetest.h> 00010 #include <print.h> 00011 00012 string commandlinetest::doc[] = 00013 { 00014 "", 00015 "Reading a variable from the command line.", 00016 "The -h option prints the names in the command map.", 00017 "Tests mapcallback where if a token is present a function is called.", 00018 "Testing quoting at shell command.", 00019 "Testing quoting from string." 00020 }; 00021 00022 /* 00023 00024 // Commented out because much of the functionality was removed 00025 // from the public interface. Chelton 7th July 2003. 00026 00027 void test01(int argc, char **argv) 00028 { 00029 cout << "***" << endl; 00030 cout << "argc=" << argc << endl; 00031 for (int i=0; i<argc; ++i) 00032 cout << argv[i] << endl; 00033 } 00034 00035 void test02() 00036 { 00037 string s("cat=mat"); 00038 string s2 = s; 00039 string::size_type k = s.find('='); 00040 cout << k << endl; 00041 s.erase(k); 00042 cout << s << endl; 00043 s2.erase(0,k+1); 00044 cout << s2 << endl; 00045 } 00046 00047 void test03() 00048 { 00049 string s2, s3; 00050 bool res = splitstring(s2,s3,"fk",'='); 00051 00052 cout << "res=" << (bool)res << endl; 00053 00054 cout << s2 << endl; 00055 cout << s3 << endl; 00056 } 00057 00058 void test05(int argc, char **argv) 00059 { 00060 map<string,string> m; 00061 readToMap(m,argc,argv); 00062 00063 double d=-1.0; 00064 map_var<double&> v(d,m,"x1"); 00065 00066 cout << "found()=" << v.found() << endl; 00067 cout << "d=" << d << endl; 00068 } 00069 00070 void test06(int argc, char **argv) 00071 { 00072 map<string,string> m; 00073 readToMap(m,argc,argv); 00074 00075 string s; 00076 writeMap(s,m); 00077 cout << s << endl; 00078 } 00079 00080 */ 00081 00082 void commandlinetest::test01(int argc, char **argv) 00083 { 00084 commandline c(argc,argv); 00085 00086 double d=-1.0; 00087 c.mapvar(d,"d"); 00088 00089 cout << "d=" << d << endl; 00090 } 00091 00092 void commandlinetest::test02(int argc, char **argv) 00093 { 00094 commandline c(argc,argv); 00095 00096 // Read in the file pointed to by file= . 00097 c.readfile("file"); 00098 00099 bool hat=false; 00100 c.mapvar(hat,"hat"); 00101 bool cat=true; 00102 c.mapvar(cat,"cat"); 00103 00104 double a=-1.0; 00105 c.mapvar(a,"a"); 00106 00107 string s; 00108 c.mapvar(s,"s"); 00109 00110 c.enablehelp() << endl; 00111 00112 cout << "hat=" << hat << endl; 00113 cout << "cat=" << cat << endl; 00114 cout << "s=" << s << endl; 00115 cout << "a=" << a << endl; 00116 } 00117 00118 class f1 00119 { 00120 public: 00121 00122 void cat() 00123 { cout << "cat" << endl; } 00124 void cat2() 00125 { cout << "cat" << endl; } 00126 00127 }; 00128 00129 typedef void (f1::*pf)(); 00130 00131 template< typename T > 00132 void callback(T & x, void ( T::*p )() ) 00133 { 00134 (x.*p)(); 00135 } 00136 00137 void commandlinetest::test03(int argc, char** argv) 00138 { 00139 cout << "Call ./main prog=17 cat=true to invoke the call" << endl << endl; 00140 commandline c(argc,argv); 00141 00142 f1 f; 00143 c.mapcallback(f,&f1::cat,"cat"); 00144 00145 cout << "Testing the hastoken(string) function." << endl; 00146 if (c.hastoken("cat")) 00147 cout << "The token \"cat\" is present." << endl; 00148 else 00149 cout << "The token \"cat\" was not in the command line." << endl; 00150 00151 } 00152 00153 void commandlinetest::test04(int argc, char** argv) 00154 { 00155 cout << "Call ./main prog=18 p1=\"1 -2 5\" to invoke the call" << endl << endl; 00156 commandline c(argc,argv); 00157 00158 string p1; 00159 c.mapvar(p1,"p1"); 00160 cout << "p1=*" << p1 << "*" << endl; 00161 00162 int num(-1); 00163 c.mapvar(num,"num"); 00164 } 00165 00166 int commandlinetest::test05() 00167 { 00168 string arg1="s1=abc points=\"1 19 32 4\" N=350 -h"; 00169 cout << SHOW(arg1) << endl << endl; 00170 00171 string s1; 00172 int N=-1; 00173 string points; 00174 00175 commandline c1(arg1); 00176 c1.mapvar(s1,"s1"); 00177 c1.mapvar(N,"N"); 00178 c1.mapvar(points,"points"); 00179 c1.enablehelp(); 00180 00181 cout << endl; 00182 cout << "N=*" << N << "*" << endl; 00183 cout << "s1=*" << s1 << "*" << endl; 00184 cout << "points=*" << points << "*" << endl; 00185 00186 assertreturnOS(s1=="abc"); 00187 assertreturnOS(points=="1 19 32 4"); 00188 assertreturnOS(N==350); 00189 00190 return 0; 00191 } 00192
1.5.8