common.cpp

Go to the documentation of this file.
00001 
00040 #include "linden_common.h"
00041 #include "lltut.h"
00042 
00043 #include <algorithm>
00044 #include <iomanip>
00045 #include <iterator>
00046 
00047 #include "llmemorystream.h"
00048 #include "llsd.h"
00049 #include "llsdserialize.h"
00050 #include "u64.h"
00051 
00052 namespace tut
00053 {
00054         struct sd_data
00055         {
00056         };
00057         typedef test_group<sd_data> sd_test;
00058         typedef sd_test::object sd_object;
00059         tut::sd_test sd("llsd");
00060 
00061         template<> template<>
00062         void sd_object::test<1>()
00063         {
00064                 std::ostringstream resp;
00065                 resp << "{'connect':true,  'position':[r128,r128,r128], 'look_at':[r0,r1,r0], 'agent_access':'M', 'region_x':i8192, 'region_y':i8192}";
00066                 std::string str = resp.str();
00067                 LLMemoryStream mstr((U8*)str.c_str(), str.size());
00068                 LLSD response;
00069                 S32 count = LLSDSerialize::fromNotation(response, mstr, str.size());
00070                 ensure("stream parsed", response.isDefined());
00071                 ensure_equals("stream parse count", count, 13);
00072                 ensure_equals("sd type", response.type(), LLSD::TypeMap);
00073                 ensure_equals("map element count", response.size(), 6);
00074                 ensure_equals("value connect", response["connect"].asBoolean(), true);
00075                 ensure_equals("value region_x", response["region_x"].asInteger(),8192);
00076                 ensure_equals("value region_y", response["region_y"].asInteger(),8192);
00077         }
00078 
00079         template<> template<>
00080         void sd_object::test<2>()
00081         {
00082                 const std::string decoded("random");
00083                 //const std::string encoded("cmFuZG9t\n");
00084                 const std::string streamed("b(6)\"random\"");
00085                 typedef std::vector<U8> buf_t;
00086                 buf_t buf;
00087                 std::copy(
00088                         decoded.begin(),
00089                         decoded.end(),
00090                         std::back_insert_iterator<buf_t>(buf));
00091                 LLSD sd;
00092                 sd = buf;
00093                 std::stringstream str;
00094                 S32 count = LLSDSerialize::toNotation(sd, str);
00095                 ensure_equals("output count", count, 1);
00096                 std::string actual(str.str());
00097                 ensure_equals("formatted binary encoding", actual, streamed);
00098                 sd.clear();
00099                 LLSDSerialize::fromNotation(sd, str, str.str().size());
00100                 std::vector<U8> after;
00101                 after = sd.asBinary();
00102                 ensure_equals("binary decoded size", after.size(), decoded.size());
00103                 ensure("binary decoding", (0 == memcmp(
00104                                                                            &after[0],
00105                                                                            decoded.c_str(),
00106                                                                            decoded.size())));
00107         }
00108 
00109         template<> template<>
00110         void sd_object::test<3>()
00111         {
00112                 for(S32 i = 0; i < 100; ++i)
00113                 {
00114                         // gen up a starting point
00115                         typedef std::vector<U8> buf_t;
00116                         buf_t source;
00117                         srand(i);               /* Flawfinder: ignore */
00118                         S32 size = rand() % 1000 + 10;
00119                         std::generate_n(
00120                                 std::back_insert_iterator<buf_t>(source),
00121                                 size,
00122                                 rand);
00123                         LLSD sd(source);
00124                         std::stringstream str;
00125                         S32 count = LLSDSerialize::toNotation(sd, str);
00126                         sd.clear();
00127                         ensure_equals("format count", count, 1);
00128                         LLSD sd2;
00129                         count = LLSDSerialize::fromNotation(sd2, str, str.str().size());
00130                         ensure_equals("parse count", count, 1);
00131                         buf_t dest = sd2.asBinary();
00132                         str.str("");
00133                         str << "binary encoding size " << i;
00134                         ensure_equals(str.str().c_str(), dest.size(), source.size());
00135                         str.str("");
00136                         str << "binary encoding " << i;
00137                         ensure(str.str().c_str(), (source == dest));
00138                 }
00139         }
00140 
00141         template<> template<>
00142         void sd_object::test<4>()
00143         {
00144                 std::ostringstream ostr;
00145                 ostr << "{'task_id':u1fd77b79-a8e7-25a5-9454-02a4d948ba1c}\n"
00146                          << "{\n\tname\tObject|\n}\n";
00147                 std::string expected = ostr.str();
00148                 std::stringstream serialized;
00149                 serialized << "'" << LLSDNotationFormatter::escapeString(expected)
00150                            << "'";
00151                 LLSD sd;
00152                 S32 count = LLSDSerialize::fromNotation(
00153                         sd,
00154                         serialized,
00155                         serialized.str().size());
00156                 ensure_equals("parse count", count, 1);
00157                 ensure_equals("String streaming", sd.asString(), expected);
00158         }
00159 
00160         template<> template<>
00161         void sd_object::test<5>()
00162         {
00163                 for(S32 i = 0; i < 100; ++i)
00164                 {
00165                         // gen up a starting point
00166                         typedef std::vector<U8> buf_t;
00167                         buf_t source;
00168                         srand(666 + i);         /* Flawfinder: ignore */
00169                         S32 size = rand() % 1000 + 10;
00170                         std::generate_n(
00171                                 std::back_insert_iterator<buf_t>(source),
00172                                 size,
00173                                 rand);
00174                         std::stringstream str;
00175                         str << "b(" << size << ")\"";
00176                         str.write((const char*)&source[0], size);
00177                         str << "\"";
00178                         LLSD sd;
00179                         S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
00180                         ensure_equals("binary parse", count, 1);
00181                         buf_t actual = sd.asBinary();
00182                         ensure_equals("binary size", actual.size(), (size_t)size);
00183                         ensure("binary data", (0 == memcmp(&source[0], &actual[0], size)));
00184                 }
00185         }
00186 
00187         template<> template<>
00188         void sd_object::test<6>()
00189         {
00190                 std::string expected("'{\"task_id\":u1fd77b79-a8e7-25a5-9454-02a4d948ba1c}'\t\n\t\t");
00191                 std::stringstream str;
00192                 str << "s(" << expected.size() << ")'";
00193                 str.write(expected.c_str(), expected.size());
00194                 str << "'";
00195                 LLSD sd;
00196                 S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
00197                 ensure_equals("parse count", count, 1);
00198                 std::string actual = sd.asString();
00199                 ensure_equals("string sizes", actual.size(), expected.size());
00200                 ensure_equals("string content", actual, expected);
00201         }
00202 
00203         template<> template<>
00204         void sd_object::test<7>()
00205         {
00206                 std::string msg("come on in");
00207                 std::stringstream stream;
00208                 stream << "{'connect':1, 'message':'" << msg << "',"
00209                            << " 'position':[r45.65,r100.1,r25.5],"
00210                            << " 'look_at':[r0,r1,r0],"
00211                            << " 'agent_access':'PG'}";
00212                 LLSD sd;
00213                 S32 count = LLSDSerialize::fromNotation(
00214                         sd,
00215                         stream,
00216                         stream.str().size());
00217                 ensure_equals("parse count", count, 12);
00218                 ensure_equals("bool value", sd["connect"].asBoolean(), true);
00219                 ensure_equals("message value", sd["message"].asString(), msg);
00220                 ensure_equals("pos x", sd["position"][0].asReal(), 45.65);
00221                 ensure_equals("pos y", sd["position"][1].asReal(), 100.1);
00222                 ensure_equals("pos z", sd["position"][2].asReal(), 25.5);
00223                 ensure_equals("look x", sd["look_at"][0].asReal(), 0.0);
00224                 ensure_equals("look y", sd["look_at"][1].asReal(), 1.0);
00225                 ensure_equals("look z", sd["look_at"][2].asReal(), 0.0);
00226         }
00227 
00228         template<> template<>
00229         void sd_object::test<8>()
00230         {
00231                 std::stringstream resp;
00232                 resp << "{'label':'short string test', 'singlechar':'a', 'empty':'', 'endoftest':'end' }";
00233                 LLSD response;
00234                 S32 count = LLSDSerialize::fromNotation(
00235                         response,
00236                         resp,
00237                         resp.str().size());
00238                 ensure_equals("parse count", count, 5);
00239                 ensure_equals("sd type", response.type(), LLSD::TypeMap);
00240                 ensure_equals("map element count", response.size(), 4);
00241                 ensure_equals("singlechar", response["singlechar"].asString(), "a");
00242                 ensure_equals("empty", response["empty"].asString(), "");
00243         }
00244 
00245         template<> template<>
00246         void sd_object::test<9>()
00247         {
00248                 std::ostringstream resp;
00249                 resp << "{'label':'short binary test', 'singlebinary':b(1)\"A\", 'singlerawstring':s(1)\"A\", 'endoftest':'end' }";
00250                 std::string str = resp.str();
00251                 LLSD sd;
00252                 LLMemoryStream mstr((U8*)str.c_str(), str.size());
00253                 S32 count = LLSDSerialize::fromNotation(sd, mstr, str.size());
00254                 ensure_equals("parse count", count, 5);
00255                 ensure("sd created", sd.isDefined());
00256                 ensure_equals("sd type", sd.type(), LLSD::TypeMap);
00257                 ensure_equals("map element count", sd.size(), 4);
00258                 ensure_equals(
00259                         "label",
00260                         sd["label"].asString(),
00261                         "short binary test");
00262                 std::vector<U8> bin =  sd["singlebinary"].asBinary();
00263                 std::vector<U8> expected;
00264                 expected.resize(1);
00265                 expected[0] = 'A';
00266                 ensure("single binary", (0 == memcmp(&bin[0], &expected[0], 1)));
00267                 ensure_equals(
00268                         "single string",
00269                         sd["singlerawstring"].asString(),
00270                         std::string("A"));
00271                 ensure_equals("end", sd["endoftest"].asString(), "end");
00272         }
00273 
00274         template<> template<>
00275         void sd_object::test<10>()
00276         {
00277 
00278                 std::string message("parcel '' is naughty.");
00279                 std::stringstream str;
00280                 str << "{'message':'" << LLSDNotationFormatter::escapeString(message)
00281                         << "'}";
00282                 std::string expected_str("{'message':'parcel \\'\\' is naughty.'}");
00283                 std::string actual_str = str.str();
00284                 ensure_equals("stream contents", actual_str, expected_str);
00285                 LLSD sd;
00286                 S32 count = LLSDSerialize::fromNotation(sd, str, actual_str.size());
00287                 ensure_equals("parse count", count, 2);
00288                 ensure("valid parse", sd.isDefined());
00289                 std::string actual = sd["message"].asString();
00290                 ensure_equals("message contents", actual, message);
00291         }
00292 
00293         template<> template<>
00294         void sd_object::test<11>()
00295         {
00296                 std::string expected("\"\"\"\"''''''\"");
00297                 std::stringstream str;
00298                 str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
00299                 LLSD sd;
00300                 S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
00301                 ensure_equals("parse count", count, 1);
00302                 ensure_equals("string value", sd.asString(), expected);
00303         }
00304 
00305         template<> template<>
00306         void sd_object::test<12>()
00307         {
00308                 std::string expected("mytest\\");
00309                 std::stringstream str;
00310                 str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
00311                 LLSD sd;
00312                 S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
00313                 ensure_equals("parse count", count, 1);
00314                 ensure_equals("string value", sd.asString(), expected);
00315         }
00316 
00317         template<> template<>
00318         void sd_object::test<13>()
00319         {
00320                 for(S32 i = 0; i < 1000; ++i)
00321                 {
00322                         // gen up a starting point
00323                         std::string expected;
00324                         srand(1337 + i);                /* Flawfinder: ignore */
00325                         S32 size = rand() % 30 + 5;
00326                         std::generate_n(
00327                                 std::back_insert_iterator<std::string>(expected),
00328                                 size,
00329                                 rand);
00330                         std::stringstream str;
00331                         str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
00332                         LLSD sd;
00333                         S32 count = LLSDSerialize::fromNotation(sd, str, expected.size());
00334                         ensure_equals("parse count", count, 1);
00335                         std::string actual = sd.asString();
00336 /*
00337                         if(actual != expected)
00338                         {
00339                                 llwarns << "iteration " << i << llendl;
00340                                 std::ostringstream e_str;
00341                                 std::string::iterator iter = expected.begin();
00342                                 std::string::iterator end = expected.end();
00343                                 for(; iter != end; ++iter)
00344                                 {
00345                                         e_str << (S32)((U8)(*iter)) << " ";
00346                                 }
00347                                 e_str << std::endl;
00348                                 llsd_serialize_string(e_str, expected);
00349                                 llwarns << "expected size: " << expected.size() << llendl;
00350                                 llwarns << "expected:      " << e_str.str() << llendl;
00351 
00352                                 std::ostringstream a_str;
00353                                 iter = actual.begin();
00354                                 end = actual.end();
00355                                 for(; iter != end; ++iter)
00356                                 {
00357                                         a_str << (S32)((U8)(*iter)) << " ";
00358                                 }
00359                                 a_str << std::endl;
00360                                 llsd_serialize_string(a_str, actual);
00361                                 llwarns << "actual size:   " << actual.size() << llendl;
00362                                 llwarns << "actual:      " << a_str.str() << llendl;
00363                         }
00364 */
00365                         ensure_equals("string value", actual, expected);
00366                 }
00367         }
00368 
00369         template<> template<>
00370         void sd_object::test<14>()
00371         {
00372                 std::string param = "[{'version':i1},{'data':{'binary_bucket':b(0)\"\"},'from_id':u3c115e51-04f4-523c-9fa6-98aff1034730,'from_name':'Phoenix Linden','id':u004e45e5-5576-277a-fba7-859d6a4cb5c8,'message':'hey','offline':i0,'timestamp':i0,'to_id':u3c5f1bb4-5182-7546-6401-1d329b4ff2f8,'type':i0},{'agent_id':u3c115e51-04f4-523c-9fa6-98aff1034730,'god_level':i0,'limited_to_estate':i1}]";
00373                 std::istringstream istr;
00374                 istr.str(param);
00375                 LLSD param_sd;
00376                 LLSDSerialize::fromNotation(param_sd, istr, param.size());
00377                 ensure_equals("parsed type", param_sd.type(), LLSD::TypeArray);
00378                 LLSD version_sd = param_sd[0];
00379                 ensure_equals("version type", version_sd.type(), LLSD::TypeMap);
00380                 ensure("has version", version_sd.has("version"));
00381                 ensure_equals("version number", version_sd["version"].asInteger(), 1);
00382                 LLSD src_sd = param_sd[1];
00383                 ensure_equals("src type", src_sd.type(), LLSD::TypeMap);
00384                 LLSD dst_sd = param_sd[2];
00385                 ensure_equals("dst type", dst_sd.type(), LLSD::TypeMap);
00386         }
00387 
00388         template<> template<>
00389         void sd_object::test<15>()
00390         {
00391                 std::string val = "[{'failures':!,'successfuls':[u3c115e51-04f4-523c-9fa6-98aff1034730]}]";
00392                 std::istringstream istr;
00393                 istr.str(val);
00394                 LLSD sd;
00395                 LLSDSerialize::fromNotation(sd, istr, val.size());
00396                 ensure_equals("parsed type", sd.type(), LLSD::TypeArray);
00397                 ensure_equals("parsed size", sd.size(), 1);
00398                 LLSD failures = sd[0]["failures"];
00399                 ensure("no failures.", failures.isUndefined());
00400                 LLSD success = sd[0]["successfuls"];
00401                 ensure_equals("success type", success.type(), LLSD::TypeArray);
00402                 ensure_equals("success size", success.size(), 1);
00403                 ensure_equals("success instance type", success[0].type(), LLSD::TypeUUID);
00404         }
00405 
00406         template<> template<>
00407         void sd_object::test<16>()
00408         {
00409                 std::string val = "[f,t,0,1,{'foo':t,'bar':f}]";
00410                 std::istringstream istr;
00411                 istr.str(val);
00412                 LLSD sd;
00413                 LLSDSerialize::fromNotation(sd, istr, val.size());
00414                 ensure_equals("parsed type", sd.type(), LLSD::TypeArray);
00415                 ensure_equals("parsed size", sd.size(), 5);
00416                 ensure_equals("element 0 false", sd[0].asBoolean(), false);
00417                 ensure_equals("element 1 true", sd[1].asBoolean(), true);
00418                 ensure_equals("element 2 false", sd[2].asBoolean(), false);
00419                 ensure_equals("element 3 true", sd[3].asBoolean(), true);
00420                 LLSD map = sd[4];
00421                 ensure_equals("element 4 type", map.type(), LLSD::TypeMap);
00422                 ensure_equals("map foo type", map["foo"].type(), LLSD::TypeBoolean);
00423                 ensure_equals("map foo value", map["foo"].asBoolean(), true);
00424                 ensure_equals("map bar type", map["bar"].type(), LLSD::TypeBoolean);
00425                 ensure_equals("map bar value", map["bar"].asBoolean(), false);
00426         }
00427 
00428 /*
00429         template<> template<>
00430         void sd_object::test<16>()
00431         {
00432         }
00433 */
00434 }
00435 
00436 #if 0
00437 '{\'task_id\':u1fd77b79-a8e7-25a5-9454-02a4d948ba1c}\n{\n\tname\tObject|\n\tpermissions 0\n\t{\n\t\tbase_mask\t7fffffff\n\t\towner_mask\t7fffffff\n\t\tgroup_mask\t00000000\n\t\teveryone_mask\t00000000\n\t\tnext_owner_mask\t00082000\n\t\tcreator_id\t3c115e51-04f4-523c-9fa6-98aff1034730\n\t\towner_id\t3c115e51-04f4-523c-9fa6-98aff1034730\n\t\tlast_owner_id\t00000000-0000-0000-0000-000000000000\n\t\tgroup_id\t00000000-0000-0000-0000-000000000000\n\t}\n\tlocal_id\t10284\n\ttotal_crc\t35\n\ttype\t1\n\ttask_valid\t2\n\ttravel_access\t21\n\tdisplayopts\t2\n\tdisplaytype\tv\n\tpos\t0\t0\t0\n\toldpos\t0\t0\t0\n\trotation\t4.371139183945160766597837e-08\t1\t4.371139183945160766597837e-08\t0\n\tvelocity\t0\t0\t0\n\tangvel\t0\t0\t0\n\tscale\t0.2816932\t0.2816932\t0.2816932\n\tsit_offset\t0\t0\t0\n\tcamera_eye_offset\t0\t0\t0\n\tcamera_at_offset\t0\t0\t0\n\tsit_quat\t0\t0\t0\t1\n\tsit_hint\t0\n\tstate\t80\n\tmaterial\t3\n\tsoundid\t00000000-0000-0000-0000-000000000000\n\tsoundgain\t0\n\tsoundradius\t0\n\tsoundflags\t0\n\ttextcolor\t0 0 0 1\n\tselected\t0\n\tselector\t00000000-0000-0000-0000-000000000000\n\tusephysics\t0\n\trotate_x\t1\n\trotate_y\t1\n\trotate_z\t1\n\tphantom\t0\n\tremote_script_access_pin\t0\n\tvolume_detect\t0\n\tblock_grabs\t0\n\tdie_at_edge\t0\n\treturn_at_edge\t0\n\ttemporary\t0\n\tsandbox\t0\n\tsandboxhome\t0\t0\t0\n\tshape 0\n\t{\n\t\tpath 0\n\t\t{\n\t\t\tcurve\t16\n\t\t\tbegin\t0\n\t\t\tend\t1\n\t\t\tscale_x\t1\n\t\t\tscale_y\t1\n\t\t\tshear_x\t0\n\t\t\tshear_y\t0\n\t\t\ttwist\t0\n\t\t\ttwist_begin\t0\n\t\t\tradius_offset\t0\n\t\t\ttaper_x\t0\n\t\t\ttaper_y\t0\n\t\t\trevolutions\t1\n\t\t\tskew\t0\n\t\t}\n\t\tprofile 0\n\t\t{\n\t\t\tcurve\t1\n\t\t\tbegin\t0\n\t\t\tend\t1\n\t\t\thollow\t0\n\t\t}\n\t}\n\tfaces\t6\n\t{\n\t\timageid\t89556747-24cb-43ed-920b-47caed15465f\n\t\tcolors\t1 1 1 1\n\t\tscales\t0.56\n\t\tscalet\t0.56\n\t\toffsets\t0\n\t\toffsett\t0\n\t\timagerot\t0\n\t\tbump\t0\n\t\tfullbright\t0\n\t\tmedia_flags\t0\n\t}\n\t{\n\t\timageid\t89556747-24cb-43ed-920b-47caed15465f\n\t\tcolors\t1 1 1 1\n\t\tscales\t0.56\n\t\tscalet\t0.56\n\t\toffsets\t0\n\t\toffsett\t0\n\t\timagerot\t0\n\t\tbump\t0\n\t\tfullbright\t0\n\t\tmedia_flags\t0\n\t}\n\t{\n\t\timageid\t89556747-24cb-43ed-920b-47caed15465f\n\t\tcolors\t1 1 1 1\n\t\tscales\t0.56\n\t\tscalet\t0.56\n\t\toffsets\t0\n\t\toffsett\t0\n\t\timagerot\t0\n\t\tbump\t0\n\t\tfullbright\t0\n\t\tmedia_flags\t0\n\t}\n\t{\n\t\timageid\t89556747-24cb-43ed-920b-47caed15465f\n\t\tcolors\t1 1 1 1\n\t\tscales\t0.56\n\t\tscalet\t0.56\n\t\toffsets\t0\n\t\toffsett\t0\n\t\timagerot\t0\n\t\tbump\t0\n\t\tfullbright\t0\n\t\tmedia_flags\t0\n\t}\n\t{\n\t\timageid\t89556747-24cb-43ed-920b-47caed15465f\n\t\tcolors\t1 1 1 1\n\t\tscales\t0.56\n\t\tscalet\t0.56\n\t\toffsets\t0\n\t\toffsett\t0\n\t\timagerot\t0\n\t\tbump\t0\n\t\tfullbright\t0\n\t\tmedia_flags\t0\n\t}\n\t{\n\t\timageid\t89556747-24cb-43ed-920b-47caed15465f\n\t\tcolors\t1 1 1 1\n\t\tscales\t0.56\n\t\tscalet\t0.56\n\t\toffsets\t0\n\t\toffsett\t0\n\t\timagerot\t0\n\t\tbump\t0\n\t\tfullbright\t0\n\t\tmedia_flags\t0\n\t}\n\tps_next_crc\t1\n\tgpw_bias\t1\n\tip\t0\n\tcomplete\tTRUE\n\tdelay\t50000\n\tnextstart\t1132625972249870\n\tbirthtime\t1132625953120694\n\treztime\t1132625953120694\n\tparceltime\t1132625953120694\n\ttax_rate\t1.01615\n\tnamevalue\tAttachmentOrientation VEC3 RW DS -3.141593, 0.000000, -3.141593\n\tnamevalue\tAttachmentOffset VEC3 RW DS 0.000000, 0.000000, 0.000000\n\tnamevalue\tAttachPt U32 RW S 5\n\tnamevalue\tAttachItemID STRING RW SV 1f9975c0-2951-1b93-dd83-46e2b932fcc8\n\tscratchpad\t0\n\t{\n\t\n\t}\n\tsale_info\t0\n\t{\n\t\tsale_type\tnot\n\t\tsale_price\t10\n\t}\n\torig_asset_id\t52019cdd-b464-ba19-e66d-3da751fef9da\n\torig_item_id\t1f9975c0-2951-1b93-dd83-46e2b932fcc8\n\tcorrect_family_id\t00000000-0000-0000-0000-000000000000\n\thas_rezzed\t0\n\tpre_link_base_mask\t7fffffff\n\tdefault_pay_price\t-2\t1\t5\t10\t20\n}\n'
00438 #endif
00439 
00440 namespace tut
00441 {
00442         struct mem_data
00443         {
00444         };
00445         typedef test_group<mem_data> mem_test;
00446         typedef mem_test::object mem_object;
00447         tut::mem_test mem_stream("memory_stream");
00448 
00449         template<> template<>
00450         void mem_object::test<1>()
00451         {
00452                 const char HELLO_WORLD[] = "hello world";
00453                 LLMemoryStream mem((U8*)&HELLO_WORLD[0], strlen(HELLO_WORLD));          /* Flawfinder: ignore */
00454                 std::string hello;
00455                 std::string world;
00456                 mem >> hello >> world;
00457                 ensure_equals("first word", hello, std::string("hello"));
00458                 ensure_equals("second word", world, std::string("world"));
00459         }
00460 }
00461 
00462 namespace tut
00463 {
00464         struct U64_data
00465         {
00466         };
00467         typedef test_group<U64_data> U64_test;
00468         typedef U64_test::object U64_object;
00469         tut::U64_test U64_testcase("U64_conversion");
00470 
00471         // U64_to_str
00472         template<> template<>
00473         void U64_object::test<1>()
00474         {
00475                 U64 val;
00476                 std::string val_str;
00477                 char result[256];
00478                 std::string result_str;
00479 
00480                 val = U64L(18446744073709551610); // slightly less than MAX_U64
00481                 val_str = "18446744073709551610";
00482 
00483                 U64_to_str(val, result, sizeof(result));
00484                 result_str = (const char*) result;
00485                 ensure_equals("U64_to_str converted 1.1", val_str, result_str);
00486 
00487                 val = 0;
00488                 val_str = "0";
00489                 U64_to_str(val, result, sizeof(result));
00490                 result_str = (const char*) result;
00491                 ensure_equals("U64_to_str converted 1.2", val_str, result_str);
00492 
00493                 val = U64L(18446744073709551615); // 0xFFFFFFFFFFFFFFFF
00494                 val_str = "18446744073709551615";
00495                 U64_to_str(val, result, sizeof(result));
00496                 result_str = (const char*) result;
00497                 ensure_equals("U64_to_str converted 1.3", val_str, result_str);
00498 
00499                 // overflow - will result in warning at compile time
00500                 val = U64L(18446744073709551615) + 1; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
00501                 val_str = "0";
00502                 U64_to_str(val, result, sizeof(result));
00503                 result_str = (const char*) result;
00504                 ensure_equals("U64_to_str converted 1.4", val_str, result_str);
00505 
00506                 val = U64L(-1); // 0xFFFFFFFFFFFFFFFF == 18446744073709551615
00507                 val_str = "18446744073709551615";
00508                 U64_to_str(val, result, sizeof(result));
00509                 result_str = (const char*) result;
00510                 ensure_equals("U64_to_str converted 1.5", val_str, result_str);
00511 
00512                 val = U64L(10000000000000000000); // testing preserving of 0s
00513                 val_str = "10000000000000000000";
00514                 U64_to_str(val, result, sizeof(result));
00515                 result_str = (const char*) result;
00516                 ensure_equals("U64_to_str converted 1.6", val_str, result_str);
00517 
00518                 val = 1; // testing no leading 0s
00519                 val_str = "1";
00520                 U64_to_str(val, result, sizeof(result));
00521                 result_str = (const char*) result;
00522                 ensure_equals("U64_to_str converted 1.7", val_str, result_str);
00523 
00524                 val = U64L(18446744073709551615); // testing exact sized buffer for result
00525                 val_str = "18446744073709551615";
00526                 memset(result, 'A', sizeof(result)); // initialize buffer with all 'A'
00527                 U64_to_str(val, result, sizeof("18446744073709551615")); //pass in the exact size
00528                 result_str = (const char*) result;
00529                 ensure_equals("U64_to_str converted 1.8", val_str, result_str);
00530 
00531                 val = U64L(18446744073709551615); // testing smaller sized buffer for result
00532                 val_str = "1844";
00533                 memset(result, 'A', sizeof(result)); // initialize buffer with all 'A'
00534                 U64_to_str(val, result, 5); //pass in a size of 5. should only copy first 4 integers and add a null terminator
00535                 result_str = (const char*) result;
00536                 ensure_equals("U64_to_str converted 1.9", val_str, result_str);
00537         }
00538 
00539         // str_to_U64
00540         template<> template<>
00541         void U64_object::test<2>()
00542         {
00543                 U64 val;
00544                 U64 result;
00545 
00546                 val = U64L(18446744073709551610); // slightly less than MAX_U64
00547                 result = str_to_U64("18446744073709551610");
00548                 ensure_equals("str_to_U64 converted 2.1", val, result);
00549 
00550                 val = U64L(0); // empty string
00551                 result = str_to_U64("");
00552                 ensure_equals("str_to_U64 converted 2.2", val, result);
00553 
00554                 val = U64L(0); // 0
00555                 result = str_to_U64("0");
00556                 ensure_equals("str_to_U64 converted 2.3", val, result);
00557 
00558                 val = U64L(18446744073709551615); // 0xFFFFFFFFFFFFFFFF
00559                 result = str_to_U64("18446744073709551615");
00560                 ensure_equals("str_to_U64 converted 2.4", val, result);
00561 
00562                 // overflow - will result in warning at compile time
00563                 val = U64L(18446744073709551615) + 1; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
00564                 result = str_to_U64("18446744073709551616");
00565                 ensure_equals("str_to_U64 converted 2.5", val, result);
00566 
00567                 val = U64L(1234); // process till first non-integral character
00568                 result = str_to_U64("1234A5678");
00569                 ensure_equals("str_to_U64 converted 2.6", val, result);
00570 
00571                 val = U64L(5678); // skip all non-integral characters
00572                 result = str_to_U64("ABCD5678");
00573                 ensure_equals("str_to_U64 converted 2.7", val, result);
00574 
00575                 // should it skip negative sign and process 
00576                 // rest of string or return 0
00577                 val = U64L(1234); // skip initial negative sign 
00578                 result = str_to_U64("-1234");
00579                 ensure_equals("str_to_U64 converted 2.8", val, result);
00580 
00581                 val = U64L(5678); // stop at negative sign in the middle
00582                 result = str_to_U64("5678-1234");
00583                 ensure_equals("str_to_U64 converted 2.9", val, result);
00584 
00585                 val = U64L(0); // no integers
00586                 result = str_to_U64("AaCD");
00587                 ensure_equals("str_to_U64 converted 2.10", val, result);
00588         }
00589 
00590         // U64_to_F64
00591         template<> template<>
00592         void U64_object::test<3>()
00593         {
00594                 F64 val;
00595                 F64 result;
00596 
00597                 result = 18446744073709551610.0;
00598                 val = U64_to_F64(U64L(18446744073709551610));
00599                 ensure_equals("U64_to_F64 converted 3.1", val, result);
00600 
00601                 result = 18446744073709551615.0; // 0xFFFFFFFFFFFFFFFF
00602                 val = U64_to_F64(U64L(18446744073709551615));
00603                 ensure_equals("U64_to_F64 converted 3.2", val, result);
00604 
00605                 result = 0.0; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
00606                 // overflow - will result in warning at compile time
00607                 val = U64_to_F64(U64L(18446744073709551615)+1);
00608                 ensure_equals("U64_to_F64 converted 3.3", val, result);
00609 
00610                 result = 0.0; // 0
00611                 val = U64_to_F64(U64L(0));
00612                 ensure_equals("U64_to_F64 converted 3.4", val, result);
00613 
00614                 result = 1.0; // odd
00615                 val = U64_to_F64(U64L(1));
00616                 ensure_equals("U64_to_F64 converted 3.5", val, result);
00617 
00618                 result = 2.0; // even
00619                 val = U64_to_F64(U64L(2));
00620                 ensure_equals("U64_to_F64 converted 3.6", val, result);
00621 
00622                 result = U64L(0x7FFFFFFFFFFFFFFF) * 1.0L; // 0x7FFFFFFFFFFFFFFF
00623                 val = U64_to_F64(U64L(0x7FFFFFFFFFFFFFFF));
00624                 ensure_equals("U64_to_F64 converted 3.7", val, result);
00625         }
00626 
00627         // llstrtou64 
00628         // seems to be deprecated - could not find it being used 
00629         // anywhere in the tarball - skipping unit tests for now
00630 }
00631 
00632 

Generated on Fri May 16 08:34:28 2008 for SecondLife by  doxygen 1.5.5