00001 #include "config.h" 00002 00003 #ifdef HAVE_UNISTD_H 00004 #include <unistd.h> 00005 #endif 00006 00007 #include <iostream> 00008 #include <string> 00009 #include <cassert> 00010 00011 #include "asserts.h" 00012 #include "types.h" 00013 #include "error.h" 00014 #include "estring.h" 00015 00016 void test1(void) 00017 { 00018 estring s; 00019 } 00020 00021 void test2(void) 00022 { 00023 estring es; 00024 std::string s; 00025 00026 es = static_cast<char>('c'); 00027 s = es; 00028 assert(s == "c"); 00029 assert(es == "c"); 00030 00031 es = "Hello World"; 00032 s = es; 00033 assert(s == "Hello World"); 00034 assert(es == "Hello World"); 00035 00036 s = "The quick brown fox"; 00037 es = s; 00038 assert(es == "The quick brown fox"); 00039 00040 es = static_cast<unsigned int>(6); 00041 assert(es == "6"); 00042 es = static_cast<int>(-5); 00043 assert(es == "-5"); 00044 es = static_cast<int>(5); 00045 assert(es == "5"); 00046 00047 es = static_cast<unsigned short>(7); 00048 assert(es == "7"); 00049 es = static_cast<short>(-7); 00050 assert(es == "-7"); 00051 es = static_cast<short>(7); 00052 assert(es == "7"); 00053 00054 es = static_cast<unsigned long>(8); 00055 assert(es == "8"); 00056 es = static_cast<long>(-8); 00057 assert(es == "-8"); 00058 es = static_cast<long>(8); 00059 assert(es == "8"); 00060 00061 es = static_cast<unsigned long long>(9); 00062 assert(es == "9"); 00063 es = static_cast<long long>(-9); 00064 assert(es == "-9"); 00065 es = static_cast<long long>(9); 00066 assert(es == "9"); 00067 00068 es.precision(1); 00069 es = static_cast<float>(5.9); 00070 assert(es == "5.9"); 00071 es = static_cast<float>(6.2); 00072 es.assign(static_cast<float>(6.2),1); 00073 assert(es == "6.2"); 00074 es = static_cast<float>(-5.9); 00075 assert(es == "-5.9"); 00076 es = static_cast<float>(-6.2); 00077 es.assign(static_cast<float>(-6.2),1); 00078 assert(es == "-6.2"); 00079 00080 00081 es.assign(static_cast<float>(6.2),0); 00082 assert(es == "6"); 00083 es.assign(static_cast<float>(6.2),1); 00084 assert(es == "6.2"); 00085 es.assign(static_cast<float>(6.2),2); 00086 assert(es == "6.20"); 00087 es.assign(static_cast<float>(-6.2),0); 00088 assert(es == "-6"); 00089 es.assign(static_cast<float>(-6.2),1); 00090 assert(es == "-6.2"); 00091 es.assign(static_cast<float>(-6.2),2); 00092 assert(es == "-6.20"); 00093 00094 00095 es = ""; 00096 00097 assert(es.fmt_str(0,estring::left,'<','>') == ""); 00098 assert(es.fmt_str(1,estring::left,'<','>') == ">"); 00099 assert(es.fmt_str(2,estring::left,'<','>') == ">>"); 00100 assert(es.fmt_str(3,estring::left,'<','>') == ">>>"); 00101 00102 assert(es.fmt_str(0,estring::right,'<','>') == ""); 00103 assert(es.fmt_str(1,estring::right,'<','>') == "<"); 00104 assert(es.fmt_str(2,estring::right,'<','>') == "<<"); 00105 assert(es.fmt_str(3,estring::right,'<','>') == "<<<"); 00106 00107 assert(es.fmt_str(0,estring::center,'<','>') == ""); 00108 assert(es.fmt_str(1,estring::center,'<','>') == ">"); 00109 assert(es.fmt_str(2,estring::center,'<','>') == "<>"); 00110 assert(es.fmt_str(3,estring::center,'<','>') == "<>>"); 00111 00112 es = "Hello World"; 00113 00114 assert(es.fmt_str(0,estring::left,'<','>') == ""); 00115 assert(es.fmt_str(1,estring::left,'<','>') == "."); 00116 assert(es.fmt_str(2,estring::left,'<','>') == ".."); 00117 assert(es.fmt_str(3,estring::left,'<','>') == "..."); 00118 assert(es.fmt_str(4,estring::left,'<','>') == "H..."); 00119 assert(es.fmt_str(5,estring::left,'<','>') == "He..."); 00120 assert(es.fmt_str(6,estring::left,'<','>') == "Hel..."); 00121 assert(es.fmt_str(7,estring::left,'<','>') == "Hell..."); 00122 assert(es.fmt_str(8,estring::left,'<','>') == "Hello..."); 00123 assert(es.fmt_str(9,estring::left,'<','>') == "Hello ..."); 00124 assert(es.fmt_str(10,estring::left,'<','>') == "Hello W..."); 00125 assert(es.fmt_str(11,estring::left,'<','>') == "Hello World"); 00126 assert(es.fmt_str(12,estring::left,'<','>') == "Hello World>"); 00127 assert(es.fmt_str(13,estring::left,'<','>') == "Hello World>>"); 00128 00129 assert(es.fmt_str(0,estring::right,'<','>') == ""); 00130 assert(es.fmt_str(1,estring::right,'<','>') == "."); 00131 assert(es.fmt_str(2,estring::right,'<','>') == ".."); 00132 assert(es.fmt_str(3,estring::right,'<','>') == "..."); 00133 assert(es.fmt_str(4,estring::right,'<','>') == "...d"); 00134 assert(es.fmt_str(5,estring::right,'<','>') == "...ld"); 00135 assert(es.fmt_str(6,estring::right,'<','>') == "...rld"); 00136 assert(es.fmt_str(7,estring::right,'<','>') == "...orld"); 00137 assert(es.fmt_str(8,estring::right,'<','>') == "...World"); 00138 assert(es.fmt_str(9,estring::right,'<','>') == "... World"); 00139 assert(es.fmt_str(10,estring::right,'<','>') == "...o World"); 00140 assert(es.fmt_str(11,estring::right,'<','>') == "Hello World"); 00141 assert(es.fmt_str(12,estring::right,'<','>') == "<Hello World"); 00142 assert(es.fmt_str(13,estring::right,'<','>') == "<<Hello World"); 00143 00144 assert(es.fmt_str(0,estring::center,'<','>') == ""); 00145 assert(es.fmt_str(1,estring::center,'<','>') == "."); 00146 assert(es.fmt_str(2,estring::center,'<','>') == ".."); 00147 assert(es.fmt_str(3,estring::center,'<','>') == "..."); 00148 assert(es.fmt_str(4,estring::center,'<','>') == "H..."); 00149 assert(es.fmt_str(5,estring::center,'<','>') == "H...d"); 00150 assert(es.fmt_str(6,estring::center,'<','>') == "He...d"); 00151 assert(es.fmt_str(7,estring::center,'<','>') == "He...ld"); 00152 assert(es.fmt_str(8,estring::center,'<','>') == "Hel...ld"); 00153 assert(es.fmt_str(9,estring::center,'<','>') == "Hel...rld"); 00154 assert(es.fmt_str(10,estring::center,'<','>') == "Hell...rld"); 00155 assert(es.fmt_str(11,estring::center,'<','>') == "Hello World"); 00156 assert(es.fmt_str(12,estring::center,'<','>') == "Hello World>"); 00157 assert(es.fmt_str(13,estring::center,'<','>') == "<Hello World>"); 00158 assert(es.fmt_str(14,estring::center,'<','>') == "<Hello World>>"); 00159 assert(es.fmt_str(15,estring::center,'<','>') == "<<Hello World>>"); 00160 00161 es.assign(static_cast<double>(6.25),2); 00162 assert(es == "6.25"); 00163 00164 assert(es.fmt_str(0,estring::left,'<','>') == ""); 00165 assert(es.fmt_str(1,estring::left,'<','>') == "."); 00166 assert(es.fmt_str(2,estring::left,'<','>') == ".."); 00167 assert(es.fmt_str(3,estring::left,'<','>') == "..."); 00168 assert(es.fmt_str(4,estring::left,'<','>') == "6.25"); 00169 assert(es.fmt_str(5,estring::left,'<','>') == "6.25>"); 00170 assert(es.fmt_str(6,estring::left,'<','>') == "6.25>>"); 00171 assert(es.fmt_str(7,estring::left,'<','>') == "6.25>>>"); 00172 00173 assert(es.fmt_str(0,estring::right,'<','>') == ""); 00174 assert(es.fmt_str(1,estring::right,'<','>') == "."); 00175 assert(es.fmt_str(2,estring::right,'<','>') == ".."); 00176 assert(es.fmt_str(3,estring::right,'<','>') == "..."); 00177 assert(es.fmt_str(4,estring::right,'<','>') == "6.25"); 00178 assert(es.fmt_str(5,estring::right,'<','>') == "<6.25"); 00179 assert(es.fmt_str(6,estring::right,'<','>') == "<<6.25"); 00180 assert(es.fmt_str(7,estring::right,'<','>') == "<<<6.25"); 00181 00182 assert(es.fmt_str(0,estring::center,'<','>') == ""); 00183 assert(es.fmt_str(1,estring::center,'<','>') == "."); 00184 assert(es.fmt_str(2,estring::center,'<','>') == ".."); 00185 assert(es.fmt_str(3,estring::center,'<','>') == "..."); 00186 assert(es.fmt_str(4,estring::center,'<','>') == "6.25"); 00187 assert(es.fmt_str(5,estring::center,'<','>') == "6.25>"); 00188 assert(es.fmt_str(6,estring::center,'<','>') == "<6.25>"); 00189 assert(es.fmt_str(7,estring::center,'<','>') == "<6.25>>"); 00190 } 00191 00192 void test3(void) 00193 { 00194 estring es; 00195 unsigned int uint; 00196 int sint; 00197 float fl; 00198 double dbl; 00199 00200 es = static_cast<unsigned int>(0); 00201 assert(es == "0"); 00202 uint = es; 00203 assert(uint == 0); 00204 00205 es = static_cast<unsigned int>(1); 00206 assert(es == "1"); 00207 uint = es; 00208 assert(uint == 1); 00209 00210 es = static_cast<unsigned int>(9); 00211 assert(es == "9"); 00212 uint = es; 00213 assert(uint == 9); 00214 00215 es = static_cast<unsigned int>(10); 00216 assert(es == "10"); 00217 uint = es; 00218 assert(uint == 10); 00219 00220 es = static_cast<unsigned int>(11); 00221 assert(es == "11"); 00222 uint = es; 00223 assert(uint == 11); 00224 00225 es = static_cast<unsigned int>(99); 00226 assert(es == "99"); 00227 uint = es; 00228 assert(uint == 99); 00229 00230 es = static_cast<unsigned int>(100); 00231 assert(es == "100"); 00232 uint = es; 00233 assert(uint == 100); 00234 00235 es.reset(); 00236 es.base(16); 00237 00238 es = static_cast<unsigned int>(0); 00239 assert(es == "0"); 00240 uint = es; 00241 assert(uint == 0); 00242 00243 es = static_cast<unsigned int>(9); 00244 assert(es == "9"); 00245 uint = es; 00246 assert(uint == 9); 00247 00248 es = static_cast<unsigned int>(10); 00249 assert(es == "a"); 00250 uint = es; 00251 assert(uint == 10); 00252 00253 es = static_cast<unsigned int>(15); 00254 assert(es == "f"); 00255 uint = es; 00256 assert(uint == 15); 00257 00258 es = static_cast<unsigned int>(16); 00259 assert(es == "10"); 00260 uint = es; 00261 assert(uint == 16); 00262 00263 es = static_cast<unsigned int>(17); 00264 assert(es == "11"); 00265 uint = es; 00266 assert(uint == 17); 00267 00268 es = static_cast<int>(0); 00269 assert(es == "0"); 00270 sint = es; 00271 assert(sint == 0); 00272 00273 es = static_cast<int>(-0); 00274 assert(es == "0"); 00275 sint = es; 00276 assert(sint == 0); 00277 00278 es = static_cast<int>(9); 00279 assert(es == "9"); 00280 sint = es; 00281 assert(sint == 9); 00282 00283 es = static_cast<int>(-9); 00284 assert(es == "-9"); 00285 sint = es; 00286 assert(sint == -9); 00287 00288 es = static_cast<int>(10); 00289 assert(es == "a"); 00290 sint = es; 00291 assert(sint == 10); 00292 00293 es = static_cast<int>(-10); 00294 assert(es == "-a"); 00295 sint = es; 00296 assert(sint == -10); 00297 00298 es = static_cast<int>(15); 00299 assert(es == "f"); 00300 sint = es; 00301 assert(sint == 15); 00302 00303 es = static_cast<int>(-15); 00304 assert(es == "-f"); 00305 sint = es; 00306 assert(sint == -15); 00307 00308 es = static_cast<int>(16); 00309 assert(es == "10"); 00310 sint = es; 00311 assert(sint == 16); 00312 00313 es = static_cast<int>(-16); 00314 assert(es == "-10"); 00315 sint = es; 00316 assert(sint == -16); 00317 00318 es = static_cast<int>(17); 00319 assert(es == "11"); 00320 sint = es; 00321 assert(sint == 17); 00322 00323 es = static_cast<int>(-17); 00324 assert(es == "-11"); 00325 sint = es; 00326 assert(sint == -17); 00327 00328 es.reset(); 00329 00330 es = static_cast<float>(6.2); 00331 assert(es == "6.2"); 00332 fl = es; 00333 assert(estring(fl,1) == "6.2"); 00334 00335 es.precision(4); 00336 es = static_cast<double>(-5.9238); 00337 assert(es == "-5.9238"); 00338 dbl = es; 00339 assert(estring(dbl,4) == "-5.9238"); 00340 } 00341 00342 void test4(void) 00343 { 00344 estring es; 00345 estring es2; 00346 union generic_value { 00347 unsigned int ui; 00348 int i; 00349 unsigned short us; 00350 short s; 00351 unsigned long ul; 00352 long l; 00353 unsigned long long ull; 00354 long long ll; 00355 float f; 00356 double d; 00357 }; 00358 generic_value v; 00359 00360 es.base(16); 00361 es = static_cast<int>(16); 00362 assert(es == "10"); 00363 v.i = es; 00364 assert(v.i == 16); 00365 es.base(10); 00366 assert(es == "16"); 00367 v.i = es; 00368 assert(v.i == 16); 00369 00370 assert(es2.get_from_type() == estring::type_unknown); 00371 es2 = es; 00372 assert(es2.get_from_type() == estring::type_int); 00373 assert(es2.base() == es.base()); 00374 assert(es2.get_from_value().value.i == 16); 00375 } 00376 00377 void test5(void) 00378 { 00379 estring es; 00380 00381 es = "Hello World"; 00382 es.lower(); 00383 assert(es == "hello world"); 00384 es = "Hello World"; 00385 es.upper(); 00386 assert(es == "HELLO WORLD"); 00387 } 00388 00389 int main(int argc, char const * argv[]) 00390 { 00391 try { 00392 test1(); 00393 test2(); 00394 test3(); 00395 test4(); 00396 test5(); 00397 } 00398 catch(error e) { 00399 std::cerr << e; 00400 assert(0); 00401 } 00402 catch(...) { 00403 std::cerr << err_unknown; 00404 assert(0); 00405 } 00406 return(0); 00407 } 00408