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);
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);
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);
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(sd, serialized);
00153                 ensure_equals("parse count", count, 1);
00154                 ensure_equals("String streaming", sd.asString(), expected);
00155         }
00156 
00157         template<> template<>
00158         void sd_object::test<5>()
00159         {
00160                 for(S32 i = 0; i < 100; ++i)
00161                 {
00162                         // gen up a starting point
00163                         typedef std::vector<U8> buf_t;
00164                         buf_t source;
00165                         srand(666 + i);         /* Flawfinder: ignore */
00166                         S32 size = rand() % 1000 + 10;
00167                         std::generate_n(
00168                                 std::back_insert_iterator<buf_t>(source),
00169                                 size,
00170                                 rand);
00171                         std::stringstream str;
00172                         str << "b(" << size << ")\"";
00173                         str.write((const char*)&source[0], size);
00174                         str << "\"";
00175                         LLSD sd;
00176                         S32 count = LLSDSerialize::fromNotation(sd, str);
00177                         ensure_equals("binary parse", count, 1);
00178                         buf_t actual = sd.asBinary();
00179                         ensure_equals("binary size", actual.size(), (size_t)size);
00180                         ensure("binary data", (0 == memcmp(&source[0], &actual[0], size)));
00181                 }
00182         }
00183 
00184         template<> template<>
00185         void sd_object::test<6>()
00186         {
00187                 std::string expected("'{\"task_id\":u1fd77b79-a8e7-25a5-9454-02a4d948ba1c}'\t\n\t\t");
00188                 std::stringstream str;
00189                 str << "s(" << expected.size() << ")'";
00190                 str.write(expected.c_str(), expected.size());
00191                 str << "'";
00192                 LLSD sd;
00193                 S32 count = LLSDSerialize::fromNotation(sd, str);
00194                 ensure_equals("parse count", count, 1);
00195                 std::string actual = sd.asString();
00196                 ensure_equals("string sizes", actual.size(), expected.size());
00197                 ensure_equals("string content", actual, expected);
00198         }
00199 
00200         template<> template<>
00201         void sd_object::test<7>()
00202         {
00203                 std::string msg("come on in");
00204                 std::stringstream stream;
00205                 stream << "{'connect':1, 'message':'" << msg << "',"
00206                            << " 'position':[r45.65,r100.1,r25.5],"
00207                            << " 'look_at':[r0,r1,r0],"
00208                            << " 'agent_access':'PG'}";
00209                 LLSD sd;
00210                 S32 count = LLSDSerialize::fromNotation(sd, stream);
00211                 ensure_equals("parse count", count, 12);
00212                 ensure_equals("bool value", sd["connect"].asBoolean(), true);
00213                 ensure_equals("message value", sd["message"].asString(), msg);
00214                 ensure_equals("pos x", sd["position"][0].asReal(), 45.65);
00215                 ensure_equals("pos y", sd["position"][1].asReal(), 100.1);
00216                 ensure_equals("pos z", sd["position"][2].asReal(), 25.5);
00217                 ensure_equals("look x", sd["look_at"][0].asReal(), 0.0);
00218                 ensure_equals("look y", sd["look_at"][1].asReal(), 1.0);
00219                 ensure_equals("look z", sd["look_at"][2].asReal(), 0.0);
00220         }
00221 
00222         template<> template<>
00223         void sd_object::test<8>()
00224         {
00225                 std::stringstream resp;
00226                 resp << "{'label':'short string test', 'singlechar':'a', 'empty':'', 'endoftest':'end' }";
00227                 LLSD response;
00228                 S32 count = LLSDSerialize::fromNotation(response, resp);
00229                 ensure_equals("parse count", count, 5);
00230                 ensure_equals("sd type", response.type(), LLSD::TypeMap);
00231                 ensure_equals("map element count", response.size(), 4);
00232                 ensure_equals("singlechar", response["singlechar"].asString(), "a");
00233                 ensure_equals("empty", response["empty"].asString(), "");
00234         }
00235 
00236         template<> template<>
00237         void sd_object::test<9>()
00238         {
00239                 std::ostringstream resp;
00240                 resp << "{'label':'short binary test', 'singlebinary':b(1)\"A\", 'singlerawstring':s(1)\"A\", 'endoftest':'end' }";
00241                 std::string str = resp.str();
00242                 LLSD sd;
00243                 LLMemoryStream mstr((U8*)str.c_str(), str.size());
00244                 S32 count = LLSDSerialize::fromNotation(sd, mstr);
00245                 ensure_equals("parse count", count, 5);
00246                 ensure("sd created", sd.isDefined());
00247                 ensure_equals("sd type", sd.type(), LLSD::TypeMap);
00248                 ensure_equals("map element count", sd.size(), 4);
00249                 ensure_equals(
00250                         "label",
00251                         sd["label"].asString(),
00252                         "short binary test");
00253                 std::vector<U8> bin =  sd["singlebinary"].asBinary();
00254                 std::vector<U8> expected;
00255                 expected.resize(1);
00256                 expected[0] = 'A';
00257                 ensure("single binary", (0 == memcmp(&bin[0], &expected[0], 1)));
00258                 ensure_equals(
00259                         "single string",
00260                         sd["singlerawstring"].asString(),
00261                         std::string("A"));
00262                 ensure_equals("end", sd["endoftest"].asString(), "end");
00263         }
00264 
00265         template<> template<>
00266         void sd_object::test<10>()
00267         {
00268 
00269                 std::string message("parcel '' is naughty.");
00270                 std::stringstream str;
00271                 str << "{'message':'" << LLSDNotationFormatter::escapeString(message)
00272                         << "'}";
00273                 std::string expected_str("{'message':'parcel \\'\\' is naughty.'}");
00274                 std::string actual_str = str.str();
00275                 ensure_equals("stream contents", actual_str, expected_str);
00276                 LLSD sd;
00277                 S32 count = LLSDSerialize::fromNotation(sd, str);
00278                 ensure_equals("parse count", count, 2);
00279                 ensure("valid parse", sd.isDefined());
00280                 std::string actual = sd["message"].asString();
00281                 ensure_equals("message contents", actual, message);
00282         }
00283 
00284         template<> template<>
00285         void sd_object::test<11>()
00286         {
00287                 std::string expected("\"\"\"\"''''''\"");
00288                 std::stringstream str;
00289                 str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
00290                 LLSD sd;
00291                 S32 count = LLSDSerialize::fromNotation(sd, str);
00292                 ensure_equals("parse count", count, 1);
00293                 ensure_equals("string value", sd.asString(), expected);
00294         }
00295 
00296         template<> template<>
00297         void sd_object::test<12>()
00298         {
00299                 std::string expected("mytest\\");
00300                 std::stringstream str;
00301                 str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
00302                 LLSD sd;
00303                 S32 count = LLSDSerialize::fromNotation(sd, str);
00304                 ensure_equals("parse count", count, 1);
00305                 ensure_equals("string value", sd.asString(), expected);
00306         }
00307 
00308         template<> template<>
00309         void sd_object::test<13>()
00310         {
00311                 for(S32 i = 0; i < 1000; ++i)
00312                 {
00313                         // gen up a starting point
00314                         std::string expected;
00315                         srand(1337 + i);                /* Flawfinder: ignore */
00316                         S32 size = rand() % 30 + 5;
00317                         std::generate_n(
00318                                 std::back_insert_iterator<std::string>(expected),
00319                                 size,
00320                                 rand);
00321                         std::stringstream str;
00322                         str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
00323                         LLSD sd;
00324                         S32 count = LLSDSerialize::fromNotation(sd, str);
00325                         ensure_equals("parse count", count, 1);
00326                         std::string actual = sd.asString();
00327 /*
00328                         if(actual != expected)
00329                         {
00330                                 llwarns << "iteration " << i << llendl;
00331                                 std::ostringstream e_str;
00332                                 std::string::iterator iter = expected.begin();
00333                                 std::string::iterator end = expected.end();
00334                                 for(; iter != end; ++iter)
00335                                 {
00336                                         e_str << (S32)((U8)(*iter)) << " ";
00337                                 }
00338                                 e_str << std::endl;
00339                                 llsd_serialize_string(e_str, expected);
00340                                 llwarns << "expected size: " << expected.size() << llendl;
00341                                 llwarns << "expected:      " << e_str.str() << llendl;
00342 
00343                                 std::ostringstream a_str;
00344                                 iter = actual.begin();
00345                                 end = actual.end();
00346                                 for(; iter != end; ++iter)
00347                                 {
00348                                         a_str << (S32)((U8)(*iter)) << " ";
00349                                 }
00350                                 a_str << std::endl;
00351                                 llsd_serialize_string(a_str, actual);
00352                                 llwarns << "actual size:   " << actual.size() << llendl;
00353                                 llwarns << "actual:      " << a_str.str() << llendl;
00354                         }
00355 */
00356                         ensure_equals("string value", actual, expected);
00357                 }
00358         }
00359 
00360         template<> template<>
00361         void sd_object::test<14>()
00362         {
00363                 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}]";
00364                 std::istringstream istr;
00365                 istr.str(param);
00366                 LLSD param_sd;
00367                 LLSDSerialize::fromNotation(param_sd, istr);
00368                 ensure_equals("parsed type", param_sd.type(), LLSD::TypeArray);
00369                 LLSD version_sd = param_sd[0];
00370                 ensure_equals("version type", version_sd.type(), LLSD::TypeMap);
00371                 ensure("has version", version_sd.has("version"));
00372                 ensure_equals("version number", version_sd["version"].asInteger(), 1);
00373                 LLSD src_sd = param_sd[1];
00374                 ensure_equals("src type", src_sd.type(), LLSD::TypeMap);
00375                 LLSD dst_sd = param_sd[2];
00376                 ensure_equals("dst type", dst_sd.type(), LLSD::TypeMap);
00377         }
00378 
00379         template<> template<>
00380         void sd_object::test<15>()
00381         {
00382                 std::string val = "[{'failures':!,'successfuls':[u3c115e51-04f4-523c-9fa6-98aff1034730]}]";
00383                 std::istringstream istr;
00384                 istr.str(val);
00385                 LLSD sd;
00386                 LLSDSerialize::fromNotation(sd, istr);
00387                 ensure_equals("parsed type", sd.type(), LLSD::TypeArray);
00388                 ensure_equals("parsed size", sd.size(), 1);
00389                 LLSD failures = sd[0]["failures"];
00390                 ensure("no failures.", failures.isUndefined());
00391                 LLSD success = sd[0]["successfuls"];
00392                 ensure_equals("success type", success.type(), LLSD::TypeArray);
00393                 ensure_equals("success size", success.size(), 1);
00394                 ensure_equals("success instance type", success[0].type(), LLSD::TypeUUID);
00395         }
00396 
00397         template<> template<>
00398         void sd_object::test<16>()
00399         {
00400                 std::string val = "[f,t,0,1,{'foo':t,'bar':f}]";
00401                 std::istringstream istr;
00402                 istr.str(val);
00403                 LLSD sd;
00404                 LLSDSerialize::fromNotation(sd, istr);
00405                 ensure_equals("parsed type", sd.type(), LLSD::TypeArray);
00406                 ensure_equals("parsed size", sd.size(), 5);
00407                 ensure_equals("element 0 false", sd[0].asBoolean(), false);
00408                 ensure_equals("element 1 true", sd[1].asBoolean(), true);
00409                 ensure_equals("element 2 false", sd[2].asBoolean(), false);
00410                 ensure_equals("element 3 true", sd[3].asBoolean(), true);
00411                 LLSD map = sd[4];
00412                 ensure_equals("element 4 type", map.type(), LLSD::TypeMap);
00413                 ensure_equals("map foo type", map["foo"].type(), LLSD::TypeBoolean);
00414                 ensure_equals("map foo value", map["foo"].asBoolean(), true);
00415                 ensure_equals("map bar type", map["bar"].type(), LLSD::TypeBoolean);
00416                 ensure_equals("map bar value", map["bar"].asBoolean(), false);
00417         }
00418 
00419 /*
00420         template<> template<>
00421         void sd_object::test<16>()
00422         {
00423         }
00424 */
00425 }
00426 
00427 #if 0
00428 '{\'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'
00429 #endif
00430 
00431 namespace tut
00432 {
00433         struct mem_data
00434         {
00435         };
00436         typedef test_group<mem_data> mem_test;
00437         typedef mem_test::object mem_object;
00438         tut::mem_test mem_stream("memory_stream");
00439 
00440         template<> template<>
00441         void mem_object::test<1>()
00442         {
00443                 const char HELLO_WORLD[] = "hello world";
00444                 LLMemoryStream mem((U8*)&HELLO_WORLD[0], strlen(HELLO_WORLD));          /* Flawfinder: ignore */
00445                 std::string hello;
00446                 std::string world;
00447                 mem >> hello >> world;
00448                 ensure_equals("first word", hello, std::string("hello"));
00449                 ensure_equals("second word", world, std::string("world"));
00450         }
00451 }
00452 
00453 namespace tut
00454 {
00455         struct U64_data
00456         {
00457         };
00458         typedef test_group<U64_data> U64_test;
00459         typedef U64_test::object U64_object;
00460         tut::U64_test U64_testcase("U64_conversion");
00461 
00462         // U64_to_str
00463         template<> template<>
00464         void U64_object::test<1>()
00465         {
00466                 U64 val;
00467                 std::string val_str;
00468                 char result[256];
00469                 std::string result_str;
00470 
00471                 val = U64L(18446744073709551610); // slightly less than MAX_U64
00472                 val_str = "18446744073709551610";
00473 
00474                 U64_to_str(val, result, sizeof(result));
00475                 result_str = (const char*) result;
00476                 ensure_equals("U64_to_str converted 1.1", val_str, result_str);
00477 
00478                 val = 0;
00479                 val_str = "0";
00480                 U64_to_str(val, result, sizeof(result));
00481                 result_str = (const char*) result;
00482                 ensure_equals("U64_to_str converted 1.2", val_str, result_str);
00483 
00484                 val = U64L(18446744073709551615); // 0xFFFFFFFFFFFFFFFF
00485                 val_str = "18446744073709551615";
00486                 U64_to_str(val, result, sizeof(result));
00487                 result_str = (const char*) result;
00488                 ensure_equals("U64_to_str converted 1.3", val_str, result_str);
00489 
00490                 // overflow - will result in warning at compile time
00491                 val = U64L(18446744073709551615) + 1; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
00492                 val_str = "0";
00493                 U64_to_str(val, result, sizeof(result));
00494                 result_str = (const char*) result;
00495                 ensure_equals("U64_to_str converted 1.4", val_str, result_str);
00496 
00497                 val = U64L(-1); // 0xFFFFFFFFFFFFFFFF == 18446744073709551615
00498                 val_str = "18446744073709551615";
00499                 U64_to_str(val, result, sizeof(result));
00500                 result_str = (const char*) result;
00501                 ensure_equals("U64_to_str converted 1.5", val_str, result_str);
00502 
00503                 val = U64L(10000000000000000000); // testing preserving of 0s
00504                 val_str = "10000000000000000000";
00505                 U64_to_str(val, result, sizeof(result));
00506                 result_str = (const char*) result;
00507                 ensure_equals("U64_to_str converted 1.6", val_str, result_str);
00508 
00509                 val = 1; // testing no leading 0s
00510                 val_str = "1";
00511                 U64_to_str(val, result, sizeof(result));
00512                 result_str = (const char*) result;
00513                 ensure_equals("U64_to_str converted 1.7", val_str, result_str);
00514 
00515                 val = U64L(18446744073709551615); // testing exact sized buffer for result
00516                 val_str = "18446744073709551615";
00517                 memset(result, 'A', sizeof(result)); // initialize buffer with all 'A'
00518                 U64_to_str(val, result, sizeof("18446744073709551615")); //pass in the exact size
00519                 result_str = (const char*) result;
00520                 ensure_equals("U64_to_str converted 1.8", val_str, result_str);
00521 
00522                 val = U64L(18446744073709551615); // testing smaller sized buffer for result
00523                 val_str = "1844";
00524                 memset(result, 'A', sizeof(result)); // initialize buffer with all 'A'
00525                 U64_to_str(val, result, 5); //pass in a size of 5. should only copy first 4 integers and add a null terminator
00526                 result_str = (const char*) result;
00527                 ensure_equals("U64_to_str converted 1.9", val_str, result_str);
00528         }
00529 
00530         // str_to_U64
00531         template<> template<>
00532         void U64_object::test<2>()
00533         {
00534                 U64 val;
00535                 U64 result;
00536 
00537                 val = U64L(18446744073709551610); // slightly less than MAX_U64
00538                 result = str_to_U64("18446744073709551610");
00539                 ensure_equals("str_to_U64 converted 2.1", val, result);
00540 
00541                 val = U64L(0); // empty string
00542                 result = str_to_U64("");
00543                 ensure_equals("str_to_U64 converted 2.2", val, result);
00544 
00545                 val = U64L(0); // 0
00546                 result = str_to_U64("0");
00547                 ensure_equals("str_to_U64 converted 2.3", val, result);
00548 
00549                 val = U64L(18446744073709551615); // 0xFFFFFFFFFFFFFFFF
00550                 result = str_to_U64("18446744073709551615");
00551                 ensure_equals("str_to_U64 converted 2.4", val, result);
00552 
00553                 // overflow - will result in warning at compile time
00554                 val = U64L(18446744073709551615) + 1; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
00555                 result = str_to_U64("18446744073709551616");
00556                 ensure_equals("str_to_U64 converted 2.5", val, result);
00557 
00558                 val = U64L(1234); // process till first non-integral character
00559                 result = str_to_U64("1234A5678");
00560                 ensure_equals("str_to_U64 converted 2.6", val, result);
00561 
00562                 val = U64L(5678); // skip all non-integral characters
00563                 result = str_to_U64("ABCD5678");
00564                 ensure_equals("str_to_U64 converted 2.7", val, result);
00565 
00566                 // should it skip negative sign and process 
00567                 // rest of string or return 0
00568                 val = U64L(1234); // skip initial negative sign 
00569                 result = str_to_U64("-1234");
00570                 ensure_equals("str_to_U64 converted 2.8", val, result);
00571 
00572                 val = U64L(5678); // stop at negative sign in the middle
00573                 result = str_to_U64("5678-1234");
00574                 ensure_equals("str_to_U64 converted 2.9", val, result);
00575 
00576                 val = U64L(0); // no integers
00577                 result = str_to_U64("AaCD");
00578                 ensure_equals("str_to_U64 converted 2.10", val, result);
00579         }
00580 
00581         // U64_to_F64
00582         template<> template<>
00583         void U64_object::test<3>()
00584         {
00585                 F64 val;
00586                 F64 result;
00587 
00588                 result = 18446744073709551610.0;
00589                 val = U64_to_F64(U64L(18446744073709551610));
00590                 ensure_equals("U64_to_F64 converted 3.1", val, result);
00591 
00592                 result = 18446744073709551615.0; // 0xFFFFFFFFFFFFFFFF
00593                 val = U64_to_F64(U64L(18446744073709551615));
00594                 ensure_equals("U64_to_F64 converted 3.2", val, result);
00595 
00596                 result = 0.0; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
00597                 // overflow - will result in warning at compile time
00598                 val = U64_to_F64(U64L(18446744073709551615)+1);
00599                 ensure_equals("U64_to_F64 converted 3.3", val, result);
00600 
00601                 result = 0.0; // 0
00602                 val = U64_to_F64(U64L(0));
00603                 ensure_equals("U64_to_F64 converted 3.4", val, result);
00604 
00605                 result = 1.0; // odd
00606                 val = U64_to_F64(U64L(1));
00607                 ensure_equals("U64_to_F64 converted 3.5", val, result);
00608 
00609                 result = 2.0; // even
00610                 val = U64_to_F64(U64L(2));
00611                 ensure_equals("U64_to_F64 converted 3.6", val, result);
00612 
00613                 result = U64L(0x7FFFFFFFFFFFFFFF) * 1.0L; // 0x7FFFFFFFFFFFFFFF
00614                 val = U64_to_F64(U64L(0x7FFFFFFFFFFFFFFF));
00615                 ensure_equals("U64_to_F64 converted 3.7", val, result);
00616         }
00617 
00618         // llstrtou64 
00619         // seems to be deprecated - could not find it being used 
00620         // anywhere in the tarball - skipping unit tests for now
00621 }
00622 
00623 

Generated on Thu Jul 1 06:08:17 2010 for Second Life Viewer by  doxygen 1.4.7