00001 #include "FCam/TagValue.h" 00002 #include "FCam/Time.h" 00003 00004 #include <iostream> 00005 #include <sstream> 00006 00007 template<typename T> 00008 std::ostream &operator<<(std::ostream &out, std::vector<T> v) { 00009 out << "["; 00010 for (size_t i = 0; i < v.size(); i++) { 00011 if (i) out << ", "; 00012 out << v[i]; 00013 } 00014 out << "]"; 00015 return out; 00016 } 00017 00018 template<typename T> 00019 void test(T val) { 00020 FCam::TagValue t; 00021 t = val; 00022 T y = t; 00023 std::cout << std::endl; 00024 std::cout.precision(5); 00025 std::cout << "Input = " << val << std::endl; 00026 std::cout << "TagValue = " << t << std::endl; 00027 std::cout.precision(5); 00028 std::cout << "Output = " << y << std::endl; 00029 } 00030 00031 template<typename S, typename T> 00032 void testCast(S val) { 00033 FCam::TagValue t = val; 00034 T casted = t; 00035 std::cout << "Input = " << val << std::endl; 00036 std::cout << "TagValue = " << t << std::endl; 00037 std::cout << "Output = " << casted << std::endl; 00038 } 00039 00040 template<typename S> 00041 void testAllCasts(S val) { 00042 testCast<S, int>(val); 00043 testCast<S, float>(val); 00044 testCast<S, double>(val); 00045 testCast<S, FCam::Time>(val); 00046 testCast<S, std::string>(val); 00047 testCast<S, std::vector<int> >(val); 00048 testCast<S, std::vector<float> >(val); 00049 testCast<S, std::vector<double> >(val); 00050 testCast<S, std::vector<FCam::Time> >(val); 00051 testCast<S, std::vector<std::string> >(val); 00052 } 00053 00054 void testParse(std::string str) { 00055 std::istringstream isstr(str); 00056 FCam::TagValue t; 00057 isstr >> t; 00058 std::string theRest; 00059 isstr >> theRest; 00060 std::cout << str << " -> " << t << " + " << theRest << std::endl; 00061 } 00062 00063 template<typename T> 00064 void testBinary(T val) { 00065 FCam::TagValue t; 00066 t = val; 00067 std::string blob = t.toBlob(); 00068 FCam::TagValue newt = FCam::TagValue::fromString(blob); 00069 T newVal = newt; 00070 std::cout << t << " -> " << newt << std::endl; 00071 if (newVal != val) { 00072 std::cout << "ERROR! Value did not survive the round trip" << std::endl; 00073 } 00074 } 00075 00076 int main() { 00077 00078 std::cout << "Testing null" << std::endl; 00079 00080 FCam::TagValue none; 00081 std::cout << none << ", " << none.toString() << std::endl; 00082 00083 std::cout << std::endl << "Testing scalar types" << std::endl; 00084 00085 test(42); 00086 test(42.0f); 00087 test(42.0); 00088 00089 std::string str = "Hello, world!"; 00090 test(str); 00091 00092 FCam::Time time = FCam::Time::now(); 00093 test(time); 00094 00095 std::string silly(200, ' '); 00096 for (int i = 0; i < 200; i++) silly[i] = (char)(i+56); 00097 test(silly); 00098 00099 std::cout << std::endl << "Testing vector types" << std::endl; 00100 00101 std::vector<int> vi; 00102 for (int i = 0; i < 5; i++) { 00103 vi.push_back(rand()); 00104 } 00105 test(vi); 00106 00107 std::vector<float> vf; 00108 for (int i = 0; i < 5; i++) { 00109 vf.push_back(rand()/1000.0f); 00110 } 00111 test(vf); 00112 00113 std::vector<double> vd; 00114 for (int i = 0; i < 5; i++) { 00115 vd.push_back(rand()/1000.0); 00116 } 00117 test(vd); 00118 00119 00120 std::vector<FCam::Time> vt; 00121 for (int i = 0; i < 5; i++) { 00122 usleep(1000); 00123 vt.push_back(FCam::Time::now() + i*1000000); 00124 } 00125 test(vt); 00126 00127 std::vector<std::string> vs; 00128 vs.push_back(std::string("one")); 00129 vs.push_back(std::string("two")); 00130 vs.push_back(std::string("three")); 00131 vs.push_back(std::string("four")); 00132 vs.push_back(std::string("five")); 00133 test(vs); 00134 00135 std::cout << std::endl << "Testing bad casts" << std::endl; 00136 00137 testAllCasts(42); 00138 testAllCasts(42.0f); 00139 testAllCasts(42.0); 00140 testAllCasts(str); 00141 testAllCasts(time); 00142 testAllCasts(vi); 00143 testAllCasts(vf); 00144 testAllCasts(vd); 00145 testAllCasts(vs); 00146 testAllCasts(vt); 00147 00148 std::cout << std::endl << "Testing good parses" << std::endl; 00149 testParse("None"); 00150 testParse("123"); 00151 testParse("123.0"); 00152 testParse("123.0f"); 00153 testParse("-1"); 00154 testParse("-1."); 00155 testParse("-1.0"); 00156 testParse("-1.0f"); 00157 testParse("-1.0e+3"); 00158 testParse("1.0e-3"); 00159 testParse("1.0e-3f"); 00160 testParse("1.0e-30f"); 00161 testParse("5eggs"); 00162 testParse("5.2eggs"); 00163 testParse("5.2e+fggs"); 00164 testParse("\"Hello, world!\""); 00165 testParse("\"Hello, world! \\\" \\n \n\a\b\t\\a\\b\\t\""); 00166 FCam::TagValue sillyTag = silly; 00167 testParse(sillyTag.toString()); 00168 FCam::TagValue currentTime = FCam::Time::now(); 00169 testParse(currentTime.toString()); 00170 testParse("[1,2 , 3 , 5 ] la la la"); 00171 testParse("[ -1.34,2.23423e+4 , 3.9 , 5 ] la la la"); 00172 testParse("[ -1.34f,2.23423e+4 , 3.9 , 5 ] la la la"); 00173 testParse("[\"Hello\", \",\", \"world!\"]"); 00174 testParse("[\"He,,,,llo\", \",\", \"wo]]rld!\"]"); 00175 testParse("[(1,2), (3,4), (123, 43)]"); 00176 00177 00178 std::cout << std::endl << "Testing bad parses" << std::endl; 00179 testParse("--3"); 00180 testParse("Nonf"); 00181 testParse("[None,None,None]"); 00182 testParse("[3,3.4,3.65]"); 00183 testParse("[3.2,4.6,2.arglebargle]"); 00184 testParse("\"askjlasdjlkdasjlkdas"); 00185 testParse("\"askjlasdjlkdasjlkdas\\"); 00186 testParse("(1.3,5.3)"); 00187 testParse("(1,2,3)"); 00188 testParse("(1,2"); 00189 00190 std::cout << std::endl << "Testing good parses of the binary format" << std::endl; 00191 testBinary(123); 00192 testBinary(123.0); 00193 testBinary(123.0f); 00194 testBinary(str); 00195 testBinary(FCam::Time::now()); 00196 testBinary(vi); 00197 testBinary(vf); 00198 testBinary(vd); 00199 testBinary(vs); 00200 testBinary(vt); 00201 00202 std::cout << std::endl << "Testing bad parses of the binary format" << std::endl; 00203 std::string bad = std::string("b ") + silly; 00204 bad[1] = (char)5; 00205 bad[2] = bad[3] = '\0'; 00206 testParse(bad); 00207 bad[2] = 'x'; 00208 bad[3] = 'y'; 00209 testParse(bad); 00210 00211 std::vector<double> v; 00212 FCam::TagValue bigVec = v; 00213 std::vector<double> &bv = bigVec; 00214 bv.resize(100000); 00215 for (int i = 0; i < 100000; i++) { 00216 bv[i] = (double)rand(); 00217 } 00218 FCam::Time t1 = FCam::Time::now(); 00219 FCam::TagValue newVec = bigVec.fromString(bigVec.toString()); 00220 FCam::Time t2 = FCam::Time::now(); 00221 FCam::TagValue newVec2 = bigVec.fromString(bigVec.toBlob()); 00222 FCam::Time t3 = FCam::Time::now(); 00223 00224 std::cout << "Encoding a decoding a huge double array using human readable format: " << (t2-t1) << std::endl; 00225 std::cout << "Encoding a decoding a huge double array using binary format: " << (t3-t2) << std::endl; 00226 std::cout << "Speedup: " << ((t2-t1))/(t3-t2) << "x" << std::endl; 00227 return 0; 00228 00229 }