proj home

Files   Classes   Functions   Hierarchy  

stringserializationtest.cpp

Go to the documentation of this file.
00001 #include <sstream>
00002 #include <iostream>
00003 using namespace std;
00004 
00005 #include <point.h>
00006 #include <printvector.h>
00007 #include <stringserialization.h>
00008 #include <stringserializationtest.h>
00009 
00010 string stringserializationtest::doc[] = 
00011 {
00012   "",
00013   "Write and read a double array.",
00014   "Write and read a 2D array of points.",
00015   "Test file serialization.",
00016   "Test vectorfile serialization.",
00017   "Unit test htmlstring."
00018 };
00019 
00020 
00021 void stringserializationtest::test01()
00022 {
00023   double data[5] = {1.0,3.0,5.0,7.0,9.0}; 
00024   vector<double> v1(data,data+5);
00025 
00026   cout << "Writing a vector to a string." << endl;
00027 
00028   bool result;
00029   string str;
00030   result=vectorstring::serialize(str,v1);
00031   cout << SHOW(result) << endl;
00032   cout << "*" << str << "*" << endl;
00033   cout << endl;
00034 
00035   cout << "Reading a vector from a string - expecting result=1." << endl;
00036   vector<double> v3;
00037   result=vectorstring::deserialize(v3,str);
00038   cout << SHOW(result) << endl;
00039   cout << SHOW(v3.size()) << endl;
00040   cout << v3 << endl;
00041   cout << endl;
00042 
00043   cout << "Putting some garbage on the end of the string - expecting result=0." << endl;
00044   str += " crap";
00045   cout << "Reading a vector from a string." << endl;
00046   vector<double> v2;
00047   result=vectorstring::deserialize(v2,str);
00048   cout << SHOW(result) << endl;
00049   cout << SHOW(v2.size()) << endl;
00050   cout << v2 << endl;
00051 }
00052 
00053 void stringserializationtest::test02()
00054 {
00055   cout << "Testing vectorstring serialization with a point2<double> type." << endl;
00056 
00057   typedef point2<double> pt2;
00058 
00059   pt2 data[3] = { pt2(.2,.3), pt2(-.1,0.0), pt2(5.0,4.0) };
00060   vector<pt2> v1(data,data+3);
00061 
00062   bool result;
00063   string str;
00064   result=vectorstring::serialize(str,v1);
00065   cout << SHOW(result) << endl;
00066   cout << "*" << str << "*" << endl;
00067   cout << endl;
00068 
00069   cout << "Reading a vector from a string - expecting result=1." << endl;
00070   vector<pt2> v3;
00071   result=vectorstring::deserialize(v3,str);
00072   cout << SHOW(result) << endl;
00073   cout << SHOW(v3.size()) << endl;
00074   cout << print(v3,"\n") << endl;
00075   cout << endl;
00076 }
00077 
00078 void stringserializationtest::test03()
00079 {
00080   string s1;
00081   s1 += "This is a silly sentence. I have to make a\n";
00082   s1 += "file from a string. Hopefully this will be\n";
00083   s1 += "written out as a file and converted back as\n";
00084   s1 += " a string. Then I will compare the strings.";
00085 
00086   cout << "Writing a string to a file." << endl;
00087   cout << "string begin:" << endl;
00088   cout << s1;
00089   cout << "string end:" << endl;
00090   cout << endl;
00091   stringc filename("temp01.txt");
00092   cout << "filename: " << filename << endl;
00093   cout << "Writing " << filename << endl;
00094   filestring::deserialize(filename,s1);
00095   string s2;
00096   filestring::serialize(s2,filename);
00097   cout << s2 << endl;
00098   cout << SHOW(s1.size()) << endl;
00099   cout << SHOW(s2.size()) << endl;
00100   cout << SHOW((s1==s2)) << endl;
00101 }
00102 
00103 
00104 void stringserializationtest::test04()
00105 {
00106   typedef point2<double> pt2;
00107 
00108   pt2 data[3] = { pt2(.2,.3), pt2(-.1,0.0), pt2(5.0,4.0) };
00109   vector<pt2> v1(data,data+3);
00110 
00111   bool result;
00112   string filename="temp02.txt";
00113 
00114   cout << "Writing 2D vector to file " << filename << endl;
00115   result=vectorfile::serialize(filename,v1);
00116   assert(result);
00117   vector<pt2> v2;
00118   result=vectorfile::deserialize(v2,filename);
00119   cout << v2 << endl;
00120 
00121   cout << SHOW(v1.size()) << endl;
00122   cout << SHOW(v2.size()) << endl;
00123   cout << "Testing if the two vectors are equal." << endl;
00124   cout << SHOW((v1==v2)) << endl;
00125 }
00126 
00127 
00128 int stringserializationtest::unittest01()
00129 {
00130 
00131   string s1="&amp;x1-&gt;free_mem();";
00132 
00133   assertreturnOS(htmlstring::ishtml(s1)); 
00134 
00135   string s2;
00136   htmlstring::serialize(s2,s1);
00137   cout << SHOW(s1) << endl;
00138   cout << SHOW(s2) << endl;
00139   string s3;
00140   htmlstring::serializeInverse(s3,s2);
00141   cout << SHOW(s3) << endl;
00142 
00143   assertreturnOS(s1==s3);
00144 
00145   return 0;
00146 }
00147 
00148 
00149 
00150 
00151 

Generated on Fri Mar 4 00:49:29 2011 for Chelton Evans Source by  doxygen 1.5.8