llsd_new_tut.cpp

Go to the documentation of this file.
00001 
00033 #include <tut/tut.h>
00034 #include "linden_common.h"
00035 #include "lltut.h"
00036 
00037 #include "llsdtraits.h"
00038 #include "llstring.h"
00039 
00040 namespace tut
00041 {
00042         class SDCleanupCheck
00043         {
00044         private:
00045                 U32     mOutstandingAtStart;
00046         public:
00047                 SDCleanupCheck() : mOutstandingAtStart(LLSD::outstandingCount()) { }
00048                 ~SDCleanupCheck()
00049                 {
00050                         ensure_equals("SDCleanupCheck",
00051                                 LLSD::outstandingCount(), mOutstandingAtStart);
00052                 }
00053         };
00054 
00055         class SDAllocationCheck : public SDCleanupCheck
00056         {
00057         private:
00058                 std::string mMessage;
00059                 U32 mExpectedAllocations;
00060                 U32 mAllocationAtStart;
00061         public:
00062                 SDAllocationCheck(const std::string& message, int expectedAllocations)
00063                         : mMessage(message),
00064                         mExpectedAllocations(expectedAllocations),
00065                         mAllocationAtStart(LLSD::allocationCount())
00066                         { }
00067                 ~SDAllocationCheck()
00068                 {
00069                         ensure_equals(mMessage + " SDAllocationCheck",
00070                                 LLSD::allocationCount() - mAllocationAtStart,
00071                                 mExpectedAllocations);
00072                 }
00073         };
00074         
00075         struct SDTestData {
00076                 template<class T>
00077                 static void ensureTypeAndValue(const char* msg, const LLSD& actual,
00078                         T expectedValue)
00079                 {
00080                         LLSDTraits<T> traits;
00081                         
00082                         std::string s(msg);
00083                         
00084                         ensure(                 s + " type",    traits.checkType(actual));
00085                         ensure_equals(  s + " value",   traits.get(actual), expectedValue);
00086                 }
00087         };
00088         
00089         typedef test_group<SDTestData>  SDTestGroup;
00090         typedef SDTestGroup::object             SDTestObject;
00091 
00092         SDTestGroup sdTestGroup("LLSD(new)");
00093         
00094         template<> template<>
00095         void SDTestObject::test<1>()
00096                 // construction and test of undefined
00097         {
00098                 SDCleanupCheck check;
00099                 
00100                 LLSD u;
00101                 ensure("is undefined", u.isUndefined());
00102         }
00103         
00104         template<> template<>
00105         void SDTestObject::test<2>()
00106                 // setting and fetching scalar types
00107         {
00108                 SDCleanupCheck check;
00109                 
00110                 LLSD v;
00111                 
00112                 v = true;               ensureTypeAndValue("set true", v, true);
00113                 v = false;              ensureTypeAndValue("set false", v, false);
00114                 v = true;               ensureTypeAndValue("set true again", v, true);
00115                 
00116                 v = 42;                 ensureTypeAndValue("set to 42", v, 42);
00117                 v = 0;                  ensureTypeAndValue("set to zero", v, 0);
00118                 v = -12345;             ensureTypeAndValue("set to neg", v, -12345);
00119                 v = 2000000000; ensureTypeAndValue("set to big", v, 2000000000);
00120                 
00121                 v = 3.14159265359;
00122                                                 ensureTypeAndValue("set to pi", v, 3.14159265359);
00123                                                 ensure_not_equals("isn't float", v.asReal(),
00124                                                         (float)3.14159265359);
00125                 v = 6.7e256;    ensureTypeAndValue("set to big", v, 6.7e256);
00126                 
00127                 LLUUID nullUUID;
00128                 LLUUID newUUID;
00129                 newUUID.generate();
00130                 
00131                 v = nullUUID;   ensureTypeAndValue("set to null UUID", v, nullUUID);
00132                 v = newUUID;    ensureTypeAndValue("set to new UUID", v, newUUID);
00133                 v = nullUUID;   ensureTypeAndValue("set to null again", v, nullUUID);
00134                 
00135                 // strings must be tested with three (!) types of string objects
00136                 std::string s = "now is the time";
00137                 LLString ls = "for all good zorks";
00138                 const char* cs = "to come to the air of their planet";
00139 
00140                 v = s;                  ensureTypeAndValue("set to std::string", v, s);         
00141                 v = ls;                 ensureTypeAndValue("set to LLString", v, ls);
00142                 v = cs;                 ensureTypeAndValue("set to const char*", v, cs);
00143         
00144                 LLDate epoch;
00145                 LLDate aDay("2001-10-22T10:11:12.00Z");
00146                 
00147                 v = epoch;              ensureTypeAndValue("set to epoch", v, epoch);
00148                 v = aDay;               ensureTypeAndValue("set to a day", v, aDay);
00149                 
00150                 LLURI path("http://slurl.com/secondlife/Ambleside/57/104/26/");
00151                 
00152                 v = path;               ensureTypeAndValue("set to a uri", v, path);
00153                 
00154                 const char source[] = "once in a blue moon";
00155                 std::vector<U8> data;
00156                 copy(&source[0], &source[sizeof(source)], back_inserter(data));
00157                 
00158                 v = data;               ensureTypeAndValue("set to data", v, data);
00159                 
00160                 v.clear();
00161                 ensure("reset to undefined", v.type() == LLSD::TypeUndefined);
00162         }
00163         
00164         template<> template<>
00165         void SDTestObject::test<3>()
00166                 // construction via scalar values
00167                 // tests both constructor and initialize forms
00168         {
00169                 SDCleanupCheck check;
00170                 
00171                 LLSD b1(true);  ensureTypeAndValue("construct boolean", b1, true);
00172                 LLSD b2 = true; ensureTypeAndValue("initialize  boolean", b2, true);
00173                 LLSD i1(42);    ensureTypeAndValue("construct int", i1, 42);
00174                 LLSD i2 =42;    ensureTypeAndValue("initialize  int", i2, 42);
00175                 LLSD d1(1.2);   ensureTypeAndValue("construct double", d1, 1.2);
00176                 LLSD d2 = 1.2;  ensureTypeAndValue("initialize double", d2, 1.2);
00177                 
00178                 LLUUID newUUID;
00179                 newUUID.generate();
00180                 LLSD u1(newUUID);
00181                                                 ensureTypeAndValue("construct UUID", u1, newUUID);
00182                 LLSD u2 = newUUID;
00183                                                 ensureTypeAndValue("initialize UUID", u2, newUUID);
00184                 
00185                 LLSD ss1(std::string("abc"));
00186                                                 ensureTypeAndValue("construct std::string", ss1, "abc");
00187                 LLSD ss2 = std::string("abc");
00188                                                 ensureTypeAndValue("initialize std::string",ss2, "abc");
00189                 LLSD sl1(LLString("def"));
00190                                                 ensureTypeAndValue("construct LLString", sl1, "def");
00191                 LLSD sl2 = LLString("def");
00192                                                 ensureTypeAndValue("initialize LLString", sl2, "def");
00193                 LLSD sc1("ghi");
00194                                                 ensureTypeAndValue("construct const char*", sc1, "ghi");
00195                 LLSD sc2 = "ghi";
00196                                                 ensureTypeAndValue("initialize const char*",sc2, "ghi");
00197 
00198                 LLDate aDay("2001-10-22T10:11:12.00Z");
00199                 LLSD t1(aDay);  ensureTypeAndValue("construct LLDate", t1, aDay);
00200                 LLSD t2 = aDay; ensureTypeAndValue("initialize LLDate", t2, aDay);
00201 
00202                 LLURI path("http://slurl.com/secondlife/Ambleside/57/104/26/");
00203                 LLSD p1(path);  ensureTypeAndValue("construct LLURI", p1, path);
00204                 LLSD p2 = path; ensureTypeAndValue("initialize LLURI", p2, path);
00205 
00206                 const char source[] = "once in a blue moon";
00207                 std::vector<U8> data;
00208                 copy(&source[0], &source[sizeof(source)], back_inserter(data));
00209                 LLSD x1(data);  ensureTypeAndValue("construct vector<U8>", x1, data);
00210                 LLSD x2 = data; ensureTypeAndValue("initialize vector<U8>", x2, data);
00211         }
00212         
00213         void checkConversions(const char* msg, const LLSD& v,
00214                 LLSD::Boolean eBoolean, LLSD::Integer eInteger,
00215                 LLSD::Real eReal, const LLSD::String& eString)
00216         {
00217                 std::string s(msg);
00218                 
00219                 ensure_equals(s+" to bool",     v.asBoolean(),  eBoolean);
00220                 ensure_equals(s+" to int",      v.asInteger(),  eInteger);
00221                 if (eReal == eReal)
00222                 {
00223                         ensure_equals(s+" to real",     v.asReal(),             eReal);
00224                         ensure_equals(s+" to string",   v.asString(),   eString);
00225                 }
00226                 else
00227                 {
00228 // TODO: Fix on windows....
00229 #ifndef LL_WINDOWS
00230 # if !defined(fpclassify) && __GNUC__ >= 3
00231 #   define FPCLASSIFY_NAMESPACE std::
00232 # else
00233 #   define FPCLASSIFY_NAMESPACE
00234 # endif
00235                         int left  = FPCLASSIFY_NAMESPACE fpclassify(v.asReal());
00236                         int right = FPCLASSIFY_NAMESPACE fpclassify(eReal);
00237 
00238                         ensure_equals(s+" to real",     left,                   right);
00239                         ensure_equals(s+" to string",   v.asString(),   eString);
00240 #endif
00241                 }
00242         }
00243         
00244         template<> template<>
00245         void SDTestObject::test<4>()
00246                 // conversion between undefined and basic scalar types:
00247                 //      boolean, integer, real and string
00248         {
00249                 SDCleanupCheck check;
00250                 
00251                 LLSD v;                 checkConversions("untitled", v, false, 0, 0.0, "");
00252                 
00253                 v = false;              checkConversions("false", v, false, 0, 0.0, "");
00254                 v = true;               checkConversions("true", v, true, 1, 1.0, "true");
00255                 
00256                 v = 0;                  checkConversions("zero", v, false, 0, 0.0, "0");
00257                 v = 1;                  checkConversions("one", v, true, 1, 1.0, "1");
00258                 v = -33;                checkConversions("neg33", v, true, -33, -33.0, "-33");
00259                 
00260                 v = 0.0;                checkConversions("0.0", v, false, 0, 0.0, "0");
00261                 v = 0.5;                checkConversions("point5", v, true, 0, 0.5, "0.5");
00262                 v = 0.9;                checkConversions("point9", v, true, 0, 0.9, "0.9");
00263                 v = -3.9;               checkConversions("neg3dot9", v, true, -3, -3.9, "-3.9");
00264                 v = sqrt(-1.0); checkConversions("NaN", v, false, 0, sqrt(-1.0), "nan");
00265                 
00266                 v = "";                 checkConversions("empty", v, false, 0, 0.0, "");
00267                 v = "0";                checkConversions("digit0", v, true, 0, 0.0, "0");
00268                 v = "10";               checkConversions("digit10", v, true, 10, 10.0, "10");
00269                 v = "-2.345";   checkConversions("decdigits", v,
00270                                                         true, -2, -2.345, "-2.345");
00271                 v = "apple";    checkConversions("apple", v, true, 0, 0.0, "apple");
00272                 v = "33bob";    checkConversions("digialpha", v, true, 0, 0.0, "33bob");
00273                 v = " ";                checkConversions("space", v, true, 0, 0.0, " ");
00274                 v = "\n";               checkConversions("newline", v, true, 0, 0.0, "\n");
00275         }
00276         
00277         template<class T>
00278         void checkRoundTrip(const std::string& msg, const LLSD& actual,
00279                 const char* sExpected, T vExpected)
00280         {
00281                 std::string str = actual.asString();
00282                 
00283                 if (sExpected) {
00284                         ensure_equals(msg + " string", str, sExpected);
00285                 }
00286                 
00287                 LLSD u(str);
00288                 LLSDTraits<T> traits;
00289                 
00290                 ensure_equals(msg + " value", traits.get(u), vExpected);
00291         }
00292         
00293         
00294         template<> template<>
00295         void SDTestObject::test<5>()
00296                 // conversion of String to and from UUID, Date and URI.
00297         {
00298                 SDCleanupCheck check;
00299                 
00300                 LLSD v;
00301                 
00302                 LLUUID nullUUID;
00303                 LLUUID someUUID;
00304                 someUUID.generate();
00305                 
00306                 v = nullUUID;   checkRoundTrip("null uuid", v,
00307                                                         "00000000-0000-0000-0000-000000000000", nullUUID);
00308                 v = someUUID;   checkRoundTrip("random uuid", v, 0, someUUID);
00309                 
00310                 LLDate epoch;
00311                 LLDate beta("2003-04-30T04:00:00Z");
00312                 LLDate oneOh("2003-06-23T04:00:00Z");
00313                 
00314                 v = epoch;              checkRoundTrip("epoch date", v, 0, epoch);
00315                 v = beta;               checkRoundTrip("beta date", v,
00316                                                         "2003-04-30T04:00:00Z", beta);
00317                 v = oneOh;              checkRoundTrip("1.0 date", v,
00318                                                         "2003-06-23T04:00:00Z", oneOh);
00319                 
00320                 LLURI empty;
00321                 LLURI path("http://slurl.com/secondlife/Ambleside/57/104/26/");
00322                 LLURI mail("mailto:zero.linden@secondlife.com");
00323                 
00324                 v = empty;              checkRoundTrip("empty URI", v, 0, empty);
00325                 v = path;               checkRoundTrip("path URI", v,
00326                                                         "http://slurl.com/secondlife/Ambleside/57/104/26/",
00327                                                         path);
00328                 v = mail;               checkRoundTrip("mail URI", v,
00329                                                         "mailto:zero.linden@secondlife.com", mail);
00330         }
00331         
00332         template<> template<>
00333         void SDTestObject::test<6>()
00334                 // copy construction and assignment
00335                 // checking for shared values after constr. or assignment
00336                 // checking in both the same type and change of type case
00337         {
00338                 SDCleanupCheck check;
00339                 
00340                 {
00341                         LLSD v = 42;
00342                 
00343                         LLSD w0(v);
00344                         ensureTypeAndValue("int constr.", w0, 42);
00345                         
00346                         LLSD w1(v);
00347                         w1 = 13;
00348                         ensureTypeAndValue("int constr. change case 1", w1, 13);
00349                         ensureTypeAndValue("int constr. change case 2", v, 42);
00350                         
00351                         LLSD w2(v);
00352                         v = 7;
00353                         ensureTypeAndValue("int constr. change case 3", w2, 42);
00354                         ensureTypeAndValue("int constr. change case 4", v, 7);
00355                 }
00356 
00357                 {
00358                         LLSD v = 42;
00359                 
00360                         LLSD w1(v);
00361                         w1 = "bob";
00362                         ensureTypeAndValue("string constr. change case 1", w1, "bob");
00363                         ensureTypeAndValue("string constr. change case 2", v, 42);
00364                         
00365                         LLSD w2(v);
00366                         v = "amy";
00367                         ensureTypeAndValue("string constr. change case 3", w2, 42);
00368                         ensureTypeAndValue("string constr. change case 4", v, "amy");
00369                 }
00370 
00371                 {
00372                         LLSD v = 42;
00373                 
00374                         LLSD w0;
00375                         w0 = v;
00376                         ensureTypeAndValue("int assign", w0, 42);
00377                         
00378                         LLSD w1;
00379                         w1 = v;
00380                         w1 = 13;
00381                         ensureTypeAndValue("int assign change case 1", w1, 13);
00382                         ensureTypeAndValue("int assign change case 2", v, 42);
00383                         
00384                         LLSD w2;
00385                         w2 = v;
00386                         v = 7;
00387                         ensureTypeAndValue("int assign change case 3", w2, 42);
00388                         ensureTypeAndValue("int assign change case 4", v, 7);
00389                 }
00390 
00391                 {
00392                         LLSD v = 42;
00393                 
00394                         LLSD w1;
00395                         w1 = v;
00396                         w1 = "bob";
00397                         ensureTypeAndValue("string assign change case 1", w1, "bob");
00398                         ensureTypeAndValue("string assign change case 2", v, 42);
00399                         
00400                         LLSD w2;
00401                         w2 = v;
00402                         v = "amy";
00403                         ensureTypeAndValue("string assign change case 3", w2, 42);
00404                         ensureTypeAndValue("string assign change case 4", v, "amy");
00405                 }
00406         }
00407         
00408         
00409         template<> template<>
00410         void SDTestObject::test<7>()
00411                 // Test assignment and casting to various scalar types.  These
00412                 // assignments should invoke the right conversion without it being
00413                 // mentioned explicitly.  The few exceptions are marked SAD.
00414         {
00415                 SDCleanupCheck check;
00416                 
00417                 LLSD v("  42.375");
00418                 
00419                 bool b = false;
00420                 b = v;                          ensure_equals("assign to bool", b, true);
00421                 b = (bool)v;            ensure_equals("cast to bool", b, true);
00422                 
00423                 int i = 99;
00424                 i = v;                          ensure_equals("assign to int", i, 42);
00425                 i = (int)v;                     ensure_equals("cast to int", i, 42);
00426                 
00427                 double d = 3.14159;
00428                 d = v;                          ensure_equals("assign to double", d, 42.375);
00429                 d = (double)v;          ensure_equals("cast to double", d, 42.375);
00430                 
00431                 std::string s = "yo";
00432 // SAD  s = v;                          ensure_equals("assign to string", s, "  42.375");
00433                 s = (std::string)v;     ensure_equals("cast to string", s, "  42.375");
00434 
00435                 LLString t = "yo";
00436 // SAD  t = v;                          ensure_equals("assign to LLString", t, "  42.375");
00437                 t = (LLString)v;        ensure_equals("cast to LLString", t, "  42.375");
00438                 
00439                 std::string uuidStr = "b1e50c2b-b627-4d23-8a86-a65d97b6319b";
00440                 v = uuidStr;
00441                 LLUUID u;
00442                 u = v;
00443                                         ensure_equals("assign to LLUUID", u, LLUUID(uuidStr));
00444 // SAD  u = (LLUUID)v;
00445 //                                      ensure_equals("cast to LLUUID", u, LLUUID(uuidStr));
00446                 
00447                 std::string dateStr = "2005-10-24T15:00:00Z";
00448                 v = dateStr;
00449                 LLDate date;
00450                 date = v;
00451                                         ensure_equals("assign to LLDate", date.asString(), dateStr);
00452 // SAD  date = (LLDate)v;
00453 //                                      ensure_equals("cast to LLDate", date.asString(), dateStr);
00454                 
00455                 std::string uriStr = "http://secondlife.com";
00456                 v = uriStr;
00457                 LLURI uri;
00458                 uri = v;
00459                                         ensure_equals("assign to LLURI", uri.asString(), uriStr);
00460 // SAD  uri = (LLURI)v;
00461 //                                      ensure_equals("cast to LLURI", uri.asString(), uriStr);
00462         }
00463         
00464         template<> template<>
00465         void SDTestObject::test<8>()
00466                 // Test construction of various scalar types from LLSD.
00467                 // Test both construction and initialization forms.
00468                 // These should invoke the right conversion without it being
00469                 // mentioned explicitly.  The few exceptions are marked SAD.
00470         {
00471                 SDCleanupCheck check;
00472                 
00473                 LLSD v("  42.375");
00474                 
00475                 bool b1(v);             ensure_equals("contruct bool", b1, true);
00476                 bool b2 = v;    ensure_equals("initialize bool", b2, true);
00477                                 
00478                 int i1(v);              ensure_equals("contruct int", i1, 42);
00479                 int i2 = v;             ensure_equals("initialize int", i2, 42);
00480                 
00481                 double d1(v);   ensure_equals("contruct double", d1, 42.375);
00482                 double d2 = v;  ensure_equals("initialize double", d2, 42.375);
00483                 
00484                 std::string s1(v);
00485                 std::string s2 = v;
00486                                                 ensure_equals("contruct string", s1, "  42.375");
00487                                                 ensure_equals("initialize string", s2, "  42.375");
00488 
00489                 LLString t1(v);
00490                 LLString t2 = v.asString();             // SAD
00491                                                 ensure_equals("contruct LLString", t1, "  42.375");
00492                                                 ensure_equals("initialize LLString", t2, "  42.375");
00493 
00494                 std::string uuidStr = "b1e50c2b-b627-4d23-8a86-a65d97b6319b";
00495                 v = uuidStr;
00496                 LLUUID uuid1(v.asUUID());               // SAD
00497                 LLUUID uuid2 = v;
00498                                 ensure_equals("contruct LLUUID", uuid1, LLUUID(uuidStr));
00499                                 ensure_equals("initialize LLUUID", uuid2, LLUUID(uuidStr));
00500 
00501                 std::string dateStr = "2005-10-24T15:00:00Z";
00502                 v = dateStr;
00503                 LLDate date1(v.asDate());               // SAD
00504                 LLDate date2 = v;
00505                                 ensure_equals("contruct LLDate", date1.asString(), dateStr);
00506                                 ensure_equals("initialize LLDate", date2.asString(), dateStr);
00507                                 
00508                 std::string uriStr = "http://secondlife.com";
00509                 v = uriStr;
00510                 LLURI uri1(v.asURI());                  // SAD
00511                 LLURI uri2 = v;
00512                                 ensure_equals("contruct LLURI", uri1.asString(), uriStr);
00513                                 ensure_equals("initialize LLURI", uri2.asString(), uriStr);
00514         }
00515         
00516         
00517         template<> template<>
00518         void SDTestObject::test<9>()
00519                 // test to make sure v is interpreted as a bool in a various
00520                 // scenarios.
00521         {
00522                 SDCleanupCheck check;
00523                 
00524                 LLSD v = "0";
00525                 // magic value that is interpreted as boolean true, but integer false!
00526                 
00527                 ensure_equals("trinary operator bool", (v ? true : false), true);
00528                 ensure_equals("convert to int, then bool",
00529                                                                                         ((int)v ? true : false), false);
00530 
00531                 if(v)
00532                 {
00533                         ensure("if converted to bool", true);
00534                 }
00535                 else
00536                 {
00537                         fail("bool did not convert to a bool in if statement.");
00538                 }
00539 
00540                 if(!v)
00541                 {
00542                         fail("bool did not convert to a bool in negated if statement.");
00543                 }
00544         }
00545         
00546         template<> template<>
00547         void SDTestObject::test<10>()
00548                 // map operations
00549         {
00550                 SDCleanupCheck check;
00551                 
00552                 LLSD v;
00553                 ensure("undefined has no members", !v.has("amy"));
00554                 ensure("undefined get() is undefined", v.get("bob").isUndefined());
00555                 
00556                 v = LLSD::emptyMap();
00557                 ensure("empty map is a map", v.isMap());
00558                 ensure("empty map has no members", !v.has("cam"));
00559                 ensure("empty map get() is undefined", v.get("don").isUndefined());
00560                 
00561                 v.clear();
00562                 v.insert("eli", 43);
00563                 ensure("insert converts to map", v.isMap());
00564                 ensure("inserted key is present", v.has("eli"));
00565                 ensureTypeAndValue("inserted value", v.get("eli"), 43);
00566                 
00567                 v.insert("fra", false);
00568                 ensure("first key still present", v.has("eli"));
00569                 ensure("second key is present", v.has("fra"));
00570                 ensureTypeAndValue("first value", v.get("eli"), 43);
00571                 ensureTypeAndValue("second value", v.get("fra"), false);
00572                 
00573                 v.erase("eli");
00574                 ensure("first key now gone", !v.has("eli"));
00575                 ensure("second key still present", v.has("fra"));
00576                 ensure("first value gone", v.get("eli").isUndefined());
00577                 ensureTypeAndValue("second value sill there", v.get("fra"), false);
00578                 
00579                 v.erase("fra");
00580                 ensure("second key now gone", !v.has("fra"));
00581                 ensure("second value gone", v.get("fra").isUndefined());
00582                 
00583                 v["gil"] = (std::string)"good morning";
00584                 ensure("third key present", v.has("gil"));
00585                 ensureTypeAndValue("third key value", v.get("gil"), "good morning");
00586                 
00587                 const LLSD& cv = v;     // FIX ME IF POSSIBLE
00588                 ensure("missing key", cv["ham"].isUndefined());
00589                 ensure("key not present", !v.has("ham"));
00590         
00591                 LLSD w = 43;
00592                 const LLSD& cw = w;     // FIX ME IF POSSIBLE
00593                 int i = cw["ian"];
00594                 ensureTypeAndValue("other missing value", i, 0);
00595                 ensure("other missing key", !w.has("ian"));
00596                 ensure("no conversion", w.isInteger());
00597                 
00598                 LLSD x;
00599                 x = v;
00600                 ensure("copy map type", x.isMap());
00601                 ensureTypeAndValue("copy map value gil", x.get("gil"), "good morning");
00602         }
00603         
00604         
00605         template<> template<>
00606         void SDTestObject::test<11>()
00607                 // array operations
00608         {
00609                 SDCleanupCheck check;
00610                 
00611                 LLSD v;
00612                 ensure_equals("undefined has no size", v.size(), 0);
00613                 ensure("undefined get() is undefined", v.get(0).isUndefined());
00614                 
00615                 v = LLSD::emptyArray();
00616                 ensure("empty array is an array", v.isArray());
00617                 ensure_equals("empty array has no size", v.size(), 0);
00618                 ensure("empty map get() is undefined", v.get(0).isUndefined());
00619                 
00620                 v.clear();
00621                 v.append(88);
00622                 v.append("noodle");
00623                 v.append(true);
00624                 ensure_equals("appened array size", v.size(), 3);
00625                 ensure("append array is an array", v.isArray());
00626                 ensureTypeAndValue("append 0", v[0], 88);
00627                 ensureTypeAndValue("append 1", v[1], "noodle");
00628                 ensureTypeAndValue("append 2", v[2], true);
00629                 
00630                 v.insert(0, 77);
00631                 v.insert(2, "soba");
00632                 v.insert(4, false);
00633                 ensure_equals("inserted array size", v.size(), 6);
00634                 ensureTypeAndValue("post insert 0", v[0], 77);
00635                 ensureTypeAndValue("post insert 1", v[1], 88);
00636                 ensureTypeAndValue("post insert 2", v[2], "soba");
00637                 ensureTypeAndValue("post insert 3", v[3], "noodle");
00638                 ensureTypeAndValue("post insert 4", v[4], false);
00639                 ensureTypeAndValue("post insert 5", v[5], true);
00640                 
00641                 ensureTypeAndValue("get 1", v.get(1), 88);
00642                 v.set(1, "hot");
00643                 ensureTypeAndValue("set 1", v.get(1), "hot");
00644                 
00645                 v.erase(3);
00646                 ensure_equals("post erase array size", v.size(), 5);
00647                 ensureTypeAndValue("post erase 0", v[0], 77);
00648                 ensureTypeAndValue("post erase 1", v[1], "hot");
00649                 ensureTypeAndValue("post erase 2", v[2], "soba");
00650                 ensureTypeAndValue("post erase 3", v[3], false);
00651                 ensureTypeAndValue("post erase 4", v[4], true);
00652                 
00653                 v.append(34);
00654                 ensure_equals("size after append", v.size(), 6);
00655                 ensureTypeAndValue("post append 5", v[5], 34);
00656 
00657                 LLSD w;
00658                 w = v;
00659                 ensure("copy array type", w.isArray());
00660                 ensure_equals("copy array size", w.size(), 6);
00661                 ensureTypeAndValue("copy array 0", w[0], 77);
00662                 ensureTypeAndValue("copy array 1", w[1], "hot");
00663                 ensureTypeAndValue("copy array 2", w[2], "soba");
00664                 ensureTypeAndValue("copy array 3", w[3], false);
00665                 ensureTypeAndValue("copy array 4", w[4], true);
00666                 ensureTypeAndValue("copy array 5", w[5], 34);
00667         }
00668 
00669 
00670         template<> template<>
00671         void SDTestObject::test<12>()
00672                 // no sharing
00673         {
00674                 SDCleanupCheck check;
00675                 
00676                 LLSD a = 99;
00677                 LLSD b = a;
00678                 a = 34;
00679                 ensureTypeAndValue("top level original changed",        a, 34);
00680                 ensureTypeAndValue("top level copy unaltered",          b, 99);
00681                 b = a;
00682                 b = 66;
00683                 ensureTypeAndValue("top level original unaltered",      a, 34);
00684                 ensureTypeAndValue("top level copy changed",            b, 66);
00685 
00686                 a[0] = "uno";
00687                 a[1] = 99;
00688                 a[2] = 1.414;
00689                 b = a;
00690                 a[1] = 34;
00691                 ensureTypeAndValue("array member original changed",     a[1], 34);
00692                 ensureTypeAndValue("array member copy unaltered",       b[1], 99);
00693                 b = a;
00694                 b[1] = 66;
00695                 ensureTypeAndValue("array member original unaltered", a[1], 34);
00696                 ensureTypeAndValue("array member copy changed",         b[1], 66);
00697                 
00698                 a["alpha"] = "uno";
00699                 a["beta"] = 99;
00700                 a["gamma"] = 1.414;
00701                 b = a;
00702                 a["beta"] = 34;
00703                 ensureTypeAndValue("map member original changed",       a["beta"], 34);
00704                 ensureTypeAndValue("map member copy unaltered",         b["beta"], 99);
00705                 b = a;
00706                 b["beta"] = 66;
00707                 ensureTypeAndValue("map member original unaltered",     a["beta"], 34);
00708                 ensureTypeAndValue("map member copy changed",           b["beta"], 66);
00709         }
00710         
00711         template<> template<>
00712         void SDTestObject::test<13>()
00713                 // sharing implementation
00714         {
00715                 SDCleanupCheck check;
00716                 
00717                 {
00718                         SDAllocationCheck check("copy construct undefinded", 0);
00719                         LLSD v;
00720                         LLSD w = v;
00721                 }
00722                 
00723                 {
00724                         SDAllocationCheck check("assign undefined", 0);
00725                         LLSD v;
00726                         LLSD w;
00727                         w = v;
00728                 }
00729                 
00730                 {
00731                         SDAllocationCheck check("assign integer value", 1);
00732                         LLSD v = 45;
00733                         v = 33;
00734                         v = 0;
00735                 }
00736 
00737                 {
00738                         SDAllocationCheck check("copy construct integer", 1);
00739                         LLSD v = 45;
00740                         LLSD w = v;
00741                 }
00742 
00743                 {
00744                         SDAllocationCheck check("assign integer", 1);
00745                         LLSD v = 45;
00746                         LLSD w;
00747                         w = v;
00748                 }
00749                 
00750                 {
00751                         SDAllocationCheck check("avoids extra clone", 2);
00752                         LLSD v = 45;
00753                         LLSD w = v;
00754                         w = "nice day";
00755                 }
00756         }
00757 
00758         /* TO DO:
00759                 conversion of undefined to UUID, Date, URI and Binary
00760                 conversion of undefined to map and array
00761                 test map operations
00762                 test array operations
00763                 test array extension
00764                 
00765                 test copying and assign maps and arrays (clone)
00766                 test iteration over map
00767                 test iteration over array
00768                 test iteration over scalar
00769 
00770                 test empty map and empty array are indeed shared
00771                 test serializations
00772         */
00773 }
00774 

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