PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/llcommon/tests/commonmisc_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 671 lines | 614 code | 21 blank | 36 comment | 6 complexity | 449cf17900de7adc1533c0fbee2845f2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file common.cpp
  3. * @author Phoenix
  4. * @date 2005-10-12
  5. * @brief Common templates for test framework
  6. *
  7. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. /**
  29. *
  30. * THOROUGH_DESCRIPTION of common.cpp
  31. *
  32. */
  33. #include <algorithm>
  34. #include <iomanip>
  35. #include <iterator>
  36. #include "linden_common.h"
  37. #include "../llmemorystream.h"
  38. #include "../llsd.h"
  39. #include "../llsdserialize.h"
  40. #include "../u64.h"
  41. #include "../llhash.h"
  42. #include "../test/lltut.h"
  43. #if LL_WINDOWS
  44. // disable overflow warnings
  45. #pragma warning(disable: 4307)
  46. #endif
  47. namespace tut
  48. {
  49. struct sd_data
  50. {
  51. };
  52. typedef test_group<sd_data> sd_test;
  53. typedef sd_test::object sd_object;
  54. tut::sd_test sd("LLSD");
  55. template<> template<>
  56. void sd_object::test<1>()
  57. {
  58. std::ostringstream resp;
  59. resp << "{'connect':true, 'position':[r128,r128,r128], 'look_at':[r0,r1,r0], 'agent_access':'M', 'region_x':i8192, 'region_y':i8192}";
  60. std::string str = resp.str();
  61. LLMemoryStream mstr((U8*)str.c_str(), str.size());
  62. LLSD response;
  63. S32 count = LLSDSerialize::fromNotation(response, mstr, str.size());
  64. ensure("stream parsed", response.isDefined());
  65. ensure_equals("stream parse count", count, 13);
  66. ensure_equals("sd type", response.type(), LLSD::TypeMap);
  67. ensure_equals("map element count", response.size(), 6);
  68. ensure_equals("value connect", response["connect"].asBoolean(), true);
  69. ensure_equals("value region_x", response["region_x"].asInteger(),8192);
  70. ensure_equals("value region_y", response["region_y"].asInteger(),8192);
  71. }
  72. template<> template<>
  73. void sd_object::test<2>()
  74. {
  75. const std::string decoded("random");
  76. //const std::string encoded("cmFuZG9t\n");
  77. const std::string streamed("b(6)\"random\"");
  78. typedef std::vector<U8> buf_t;
  79. buf_t buf;
  80. std::copy(
  81. decoded.begin(),
  82. decoded.end(),
  83. std::back_insert_iterator<buf_t>(buf));
  84. LLSD sd;
  85. sd = buf;
  86. std::stringstream str;
  87. S32 count = LLSDSerialize::toNotation(sd, str);
  88. ensure_equals("output count", count, 1);
  89. std::string actual(str.str());
  90. ensure_equals("formatted binary encoding", actual, streamed);
  91. sd.clear();
  92. LLSDSerialize::fromNotation(sd, str, str.str().size());
  93. std::vector<U8> after;
  94. after = sd.asBinary();
  95. ensure_equals("binary decoded size", after.size(), decoded.size());
  96. ensure("binary decoding", (0 == memcmp(
  97. &after[0],
  98. decoded.c_str(),
  99. decoded.size())));
  100. }
  101. template<> template<>
  102. void sd_object::test<3>()
  103. {
  104. for(S32 i = 0; i < 100; ++i)
  105. {
  106. // gen up a starting point
  107. typedef std::vector<U8> buf_t;
  108. buf_t source;
  109. srand(i); /* Flawfinder: ignore */
  110. S32 size = rand() % 1000 + 10;
  111. std::generate_n(
  112. std::back_insert_iterator<buf_t>(source),
  113. size,
  114. rand);
  115. LLSD sd(source);
  116. std::stringstream str;
  117. S32 count = LLSDSerialize::toNotation(sd, str);
  118. sd.clear();
  119. ensure_equals("format count", count, 1);
  120. LLSD sd2;
  121. count = LLSDSerialize::fromNotation(sd2, str, str.str().size());
  122. ensure_equals("parse count", count, 1);
  123. buf_t dest = sd2.asBinary();
  124. str.str("");
  125. str << "binary encoding size " << i;
  126. ensure_equals(str.str().c_str(), dest.size(), source.size());
  127. str.str("");
  128. str << "binary encoding " << i;
  129. ensure(str.str().c_str(), (source == dest));
  130. }
  131. }
  132. template<> template<>
  133. void sd_object::test<4>()
  134. {
  135. std::ostringstream ostr;
  136. ostr << "{'task_id':u1fd77b79-a8e7-25a5-9454-02a4d948ba1c}\n"
  137. << "{\n\tname\tObject|\n}\n";
  138. std::string expected = ostr.str();
  139. std::stringstream serialized;
  140. serialized << "'" << LLSDNotationFormatter::escapeString(expected)
  141. << "'";
  142. LLSD sd;
  143. S32 count = LLSDSerialize::fromNotation(
  144. sd,
  145. serialized,
  146. serialized.str().size());
  147. ensure_equals("parse count", count, 1);
  148. ensure_equals("String streaming", sd.asString(), expected);
  149. }
  150. template<> template<>
  151. void sd_object::test<5>()
  152. {
  153. for(S32 i = 0; i < 100; ++i)
  154. {
  155. // gen up a starting point
  156. typedef std::vector<U8> buf_t;
  157. buf_t source;
  158. srand(666 + i); /* Flawfinder: ignore */
  159. S32 size = rand() % 1000 + 10;
  160. std::generate_n(
  161. std::back_insert_iterator<buf_t>(source),
  162. size,
  163. rand);
  164. std::stringstream str;
  165. str << "b(" << size << ")\"";
  166. str.write((const char*)&source[0], size);
  167. str << "\"";
  168. LLSD sd;
  169. S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
  170. ensure_equals("binary parse", count, 1);
  171. buf_t actual = sd.asBinary();
  172. ensure_equals("binary size", actual.size(), (size_t)size);
  173. ensure("binary data", (0 == memcmp(&source[0], &actual[0], size)));
  174. }
  175. }
  176. template<> template<>
  177. void sd_object::test<6>()
  178. {
  179. std::string expected("'{\"task_id\":u1fd77b79-a8e7-25a5-9454-02a4d948ba1c}'\t\n\t\t");
  180. std::stringstream str;
  181. str << "s(" << expected.size() << ")'";
  182. str.write(expected.c_str(), expected.size());
  183. str << "'";
  184. LLSD sd;
  185. S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
  186. ensure_equals("parse count", count, 1);
  187. std::string actual = sd.asString();
  188. ensure_equals("string sizes", actual.size(), expected.size());
  189. ensure_equals("string content", actual, expected);
  190. }
  191. template<> template<>
  192. void sd_object::test<7>()
  193. {
  194. std::string msg("come on in");
  195. std::stringstream stream;
  196. stream << "{'connect':1, 'message':'" << msg << "',"
  197. << " 'position':[r45.65,r100.1,r25.5],"
  198. << " 'look_at':[r0,r1,r0],"
  199. << " 'agent_access':'PG'}";
  200. LLSD sd;
  201. S32 count = LLSDSerialize::fromNotation(
  202. sd,
  203. stream,
  204. stream.str().size());
  205. ensure_equals("parse count", count, 12);
  206. ensure_equals("bool value", sd["connect"].asBoolean(), true);
  207. ensure_equals("message value", sd["message"].asString(), msg);
  208. ensure_equals("pos x", sd["position"][0].asReal(), 45.65);
  209. ensure_equals("pos y", sd["position"][1].asReal(), 100.1);
  210. ensure_equals("pos z", sd["position"][2].asReal(), 25.5);
  211. ensure_equals("look x", sd["look_at"][0].asReal(), 0.0);
  212. ensure_equals("look y", sd["look_at"][1].asReal(), 1.0);
  213. ensure_equals("look z", sd["look_at"][2].asReal(), 0.0);
  214. }
  215. template<> template<>
  216. void sd_object::test<8>()
  217. {
  218. std::stringstream resp;
  219. resp << "{'label':'short string test', 'singlechar':'a', 'empty':'', 'endoftest':'end' }";
  220. LLSD response;
  221. S32 count = LLSDSerialize::fromNotation(
  222. response,
  223. resp,
  224. resp.str().size());
  225. ensure_equals("parse count", count, 5);
  226. ensure_equals("sd type", response.type(), LLSD::TypeMap);
  227. ensure_equals("map element count", response.size(), 4);
  228. ensure_equals("singlechar", response["singlechar"].asString(), "a");
  229. ensure_equals("empty", response["empty"].asString(), "");
  230. }
  231. template<> template<>
  232. void sd_object::test<9>()
  233. {
  234. std::ostringstream resp;
  235. resp << "{'label':'short binary test', 'singlebinary':b(1)\"A\", 'singlerawstring':s(1)\"A\", 'endoftest':'end' }";
  236. std::string str = resp.str();
  237. LLSD sd;
  238. LLMemoryStream mstr((U8*)str.c_str(), str.size());
  239. S32 count = LLSDSerialize::fromNotation(sd, mstr, str.size());
  240. ensure_equals("parse count", count, 5);
  241. ensure("sd created", sd.isDefined());
  242. ensure_equals("sd type", sd.type(), LLSD::TypeMap);
  243. ensure_equals("map element count", sd.size(), 4);
  244. ensure_equals(
  245. "label",
  246. sd["label"].asString(),
  247. "short binary test");
  248. std::vector<U8> bin = sd["singlebinary"].asBinary();
  249. std::vector<U8> expected;
  250. expected.resize(1);
  251. expected[0] = 'A';
  252. ensure("single binary", (0 == memcmp(&bin[0], &expected[0], 1)));
  253. ensure_equals(
  254. "single string",
  255. sd["singlerawstring"].asString(),
  256. std::string("A"));
  257. ensure_equals("end", sd["endoftest"].asString(), "end");
  258. }
  259. template<> template<>
  260. void sd_object::test<10>()
  261. {
  262. std::string message("parcel '' is naughty.");
  263. std::stringstream str;
  264. str << "{'message':'" << LLSDNotationFormatter::escapeString(message)
  265. << "'}";
  266. std::string expected_str("{'message':'parcel \\'\\' is naughty.'}");
  267. std::string actual_str = str.str();
  268. ensure_equals("stream contents", actual_str, expected_str);
  269. LLSD sd;
  270. S32 count = LLSDSerialize::fromNotation(sd, str, actual_str.size());
  271. ensure_equals("parse count", count, 2);
  272. ensure("valid parse", sd.isDefined());
  273. std::string actual = sd["message"].asString();
  274. ensure_equals("message contents", actual, message);
  275. }
  276. template<> template<>
  277. void sd_object::test<11>()
  278. {
  279. std::string expected("\"\"\"\"''''''\"");
  280. std::stringstream str;
  281. str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
  282. LLSD sd;
  283. S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
  284. ensure_equals("parse count", count, 1);
  285. ensure_equals("string value", sd.asString(), expected);
  286. }
  287. template<> template<>
  288. void sd_object::test<12>()
  289. {
  290. std::string expected("mytest\\");
  291. std::stringstream str;
  292. str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
  293. LLSD sd;
  294. S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
  295. ensure_equals("parse count", count, 1);
  296. ensure_equals("string value", sd.asString(), expected);
  297. }
  298. template<> template<>
  299. void sd_object::test<13>()
  300. {
  301. for(S32 i = 0; i < 1000; ++i)
  302. {
  303. // gen up a starting point
  304. std::string expected;
  305. srand(1337 + i); /* Flawfinder: ignore */
  306. S32 size = rand() % 30 + 5;
  307. std::generate_n(
  308. std::back_insert_iterator<std::string>(expected),
  309. size,
  310. rand);
  311. std::stringstream str;
  312. str << "'" << LLSDNotationFormatter::escapeString(expected) << "'";
  313. LLSD sd;
  314. S32 count = LLSDSerialize::fromNotation(sd, str, expected.size());
  315. ensure_equals("parse count", count, 1);
  316. std::string actual = sd.asString();
  317. /*
  318. if(actual != expected)
  319. {
  320. llwarns << "iteration " << i << llendl;
  321. std::ostringstream e_str;
  322. std::string::iterator iter = expected.begin();
  323. std::string::iterator end = expected.end();
  324. for(; iter != end; ++iter)
  325. {
  326. e_str << (S32)((U8)(*iter)) << " ";
  327. }
  328. e_str << std::endl;
  329. llsd_serialize_string(e_str, expected);
  330. llwarns << "expected size: " << expected.size() << llendl;
  331. llwarns << "expected: " << e_str.str() << llendl;
  332. std::ostringstream a_str;
  333. iter = actual.begin();
  334. end = actual.end();
  335. for(; iter != end; ++iter)
  336. {
  337. a_str << (S32)((U8)(*iter)) << " ";
  338. }
  339. a_str << std::endl;
  340. llsd_serialize_string(a_str, actual);
  341. llwarns << "actual size: " << actual.size() << llendl;
  342. llwarns << "actual: " << a_str.str() << llendl;
  343. }
  344. */
  345. ensure_equals("string value", actual, expected);
  346. }
  347. }
  348. template<> template<>
  349. void sd_object::test<14>()
  350. {
  351. //#if LL_WINDOWS && _MSC_VER >= 1400
  352. // skip_fail("Fails on VS2005 due to broken LLSDSerialize::fromNotation() parser.");
  353. //#endif
  354. 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}]";
  355. std::istringstream istr;
  356. istr.str(param);
  357. LLSD param_sd;
  358. LLSDSerialize::fromNotation(param_sd, istr, param.size());
  359. ensure_equals("parsed type", param_sd.type(), LLSD::TypeArray);
  360. LLSD version_sd = param_sd[0];
  361. ensure_equals("version type", version_sd.type(), LLSD::TypeMap);
  362. ensure("has version", version_sd.has("version"));
  363. ensure_equals("version number", version_sd["version"].asInteger(), 1);
  364. LLSD src_sd = param_sd[1];
  365. ensure_equals("src type", src_sd.type(), LLSD::TypeMap);
  366. LLSD dst_sd = param_sd[2];
  367. ensure_equals("dst type", dst_sd.type(), LLSD::TypeMap);
  368. }
  369. template<> template<>
  370. void sd_object::test<15>()
  371. {
  372. std::string val = "[{'failures':!,'successfuls':[u3c115e51-04f4-523c-9fa6-98aff1034730]}]";
  373. std::istringstream istr;
  374. istr.str(val);
  375. LLSD sd;
  376. LLSDSerialize::fromNotation(sd, istr, val.size());
  377. ensure_equals("parsed type", sd.type(), LLSD::TypeArray);
  378. ensure_equals("parsed size", sd.size(), 1);
  379. LLSD failures = sd[0]["failures"];
  380. ensure("no failures.", failures.isUndefined());
  381. LLSD success = sd[0]["successfuls"];
  382. ensure_equals("success type", success.type(), LLSD::TypeArray);
  383. ensure_equals("success size", success.size(), 1);
  384. ensure_equals("success instance type", success[0].type(), LLSD::TypeUUID);
  385. }
  386. template<> template<>
  387. void sd_object::test<16>()
  388. {
  389. std::string val = "[f,t,0,1,{'foo':t,'bar':f}]";
  390. std::istringstream istr;
  391. istr.str(val);
  392. LLSD sd;
  393. LLSDSerialize::fromNotation(sd, istr, val.size());
  394. ensure_equals("parsed type", sd.type(), LLSD::TypeArray);
  395. ensure_equals("parsed size", sd.size(), 5);
  396. ensure_equals("element 0 false", sd[0].asBoolean(), false);
  397. ensure_equals("element 1 true", sd[1].asBoolean(), true);
  398. ensure_equals("element 2 false", sd[2].asBoolean(), false);
  399. ensure_equals("element 3 true", sd[3].asBoolean(), true);
  400. LLSD map = sd[4];
  401. ensure_equals("element 4 type", map.type(), LLSD::TypeMap);
  402. ensure_equals("map foo type", map["foo"].type(), LLSD::TypeBoolean);
  403. ensure_equals("map foo value", map["foo"].asBoolean(), true);
  404. ensure_equals("map bar type", map["bar"].type(), LLSD::TypeBoolean);
  405. ensure_equals("map bar value", map["bar"].asBoolean(), false);
  406. }
  407. /*
  408. template<> template<>
  409. void sd_object::test<16>()
  410. {
  411. }
  412. */
  413. }
  414. #if 0
  415. '{\'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'
  416. #endif
  417. namespace tut
  418. {
  419. struct mem_data
  420. {
  421. };
  422. typedef test_group<mem_data> mem_test;
  423. typedef mem_test::object mem_object;
  424. tut::mem_test mem_stream("LLMemoryStream");
  425. template<> template<>
  426. void mem_object::test<1>()
  427. {
  428. const char HELLO_WORLD[] = "hello world";
  429. LLMemoryStream mem((U8*)&HELLO_WORLD[0], strlen(HELLO_WORLD)); /* Flawfinder: ignore */
  430. std::string hello;
  431. std::string world;
  432. mem >> hello >> world;
  433. ensure_equals("first word", hello, std::string("hello"));
  434. ensure_equals("second word", world, std::string("world"));
  435. }
  436. }
  437. namespace tut
  438. {
  439. struct U64_data
  440. {
  441. };
  442. typedef test_group<U64_data> U64_test;
  443. typedef U64_test::object U64_object;
  444. tut::U64_test U64_testcase("U64_conversion");
  445. // U64_to_str
  446. template<> template<>
  447. void U64_object::test<1>()
  448. {
  449. U64 val;
  450. std::string val_str;
  451. char result[256];
  452. std::string result_str;
  453. val = U64L(18446744073709551610); // slightly less than MAX_U64
  454. val_str = "18446744073709551610";
  455. U64_to_str(val, result, sizeof(result));
  456. result_str = (const char*) result;
  457. ensure_equals("U64_to_str converted 1.1", val_str, result_str);
  458. val = 0;
  459. val_str = "0";
  460. U64_to_str(val, result, sizeof(result));
  461. result_str = (const char*) result;
  462. ensure_equals("U64_to_str converted 1.2", val_str, result_str);
  463. val = U64L(18446744073709551615); // 0xFFFFFFFFFFFFFFFF
  464. val_str = "18446744073709551615";
  465. U64_to_str(val, result, sizeof(result));
  466. result_str = (const char*) result;
  467. ensure_equals("U64_to_str converted 1.3", val_str, result_str);
  468. // overflow - will result in warning at compile time
  469. val = U64L(18446744073709551615) + 1; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
  470. val_str = "0";
  471. U64_to_str(val, result, sizeof(result));
  472. result_str = (const char*) result;
  473. ensure_equals("U64_to_str converted 1.4", val_str, result_str);
  474. val = U64L(-1); // 0xFFFFFFFFFFFFFFFF == 18446744073709551615
  475. val_str = "18446744073709551615";
  476. U64_to_str(val, result, sizeof(result));
  477. result_str = (const char*) result;
  478. ensure_equals("U64_to_str converted 1.5", val_str, result_str);
  479. val = U64L(10000000000000000000); // testing preserving of 0s
  480. val_str = "10000000000000000000";
  481. U64_to_str(val, result, sizeof(result));
  482. result_str = (const char*) result;
  483. ensure_equals("U64_to_str converted 1.6", val_str, result_str);
  484. val = 1; // testing no leading 0s
  485. val_str = "1";
  486. U64_to_str(val, result, sizeof(result));
  487. result_str = (const char*) result;
  488. ensure_equals("U64_to_str converted 1.7", val_str, result_str);
  489. val = U64L(18446744073709551615); // testing exact sized buffer for result
  490. val_str = "18446744073709551615";
  491. memset(result, 'A', sizeof(result)); // initialize buffer with all 'A'
  492. U64_to_str(val, result, sizeof("18446744073709551615")); //pass in the exact size
  493. result_str = (const char*) result;
  494. ensure_equals("U64_to_str converted 1.8", val_str, result_str);
  495. val = U64L(18446744073709551615); // testing smaller sized buffer for result
  496. val_str = "1844";
  497. memset(result, 'A', sizeof(result)); // initialize buffer with all 'A'
  498. U64_to_str(val, result, 5); //pass in a size of 5. should only copy first 4 integers and add a null terminator
  499. result_str = (const char*) result;
  500. ensure_equals("U64_to_str converted 1.9", val_str, result_str);
  501. }
  502. // str_to_U64
  503. template<> template<>
  504. void U64_object::test<2>()
  505. {
  506. U64 val;
  507. U64 result;
  508. val = U64L(18446744073709551610); // slightly less than MAX_U64
  509. result = str_to_U64("18446744073709551610");
  510. ensure_equals("str_to_U64 converted 2.1", val, result);
  511. val = U64L(0); // empty string
  512. result = str_to_U64(LLStringUtil::null);
  513. ensure_equals("str_to_U64 converted 2.2", val, result);
  514. val = U64L(0); // 0
  515. result = str_to_U64("0");
  516. ensure_equals("str_to_U64 converted 2.3", val, result);
  517. val = U64L(18446744073709551615); // 0xFFFFFFFFFFFFFFFF
  518. result = str_to_U64("18446744073709551615");
  519. ensure_equals("str_to_U64 converted 2.4", val, result);
  520. // overflow - will result in warning at compile time
  521. val = U64L(18446744073709551615) + 1; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
  522. result = str_to_U64("18446744073709551616");
  523. ensure_equals("str_to_U64 converted 2.5", val, result);
  524. val = U64L(1234); // process till first non-integral character
  525. result = str_to_U64("1234A5678");
  526. ensure_equals("str_to_U64 converted 2.6", val, result);
  527. val = U64L(5678); // skip all non-integral characters
  528. result = str_to_U64("ABCD5678");
  529. ensure_equals("str_to_U64 converted 2.7", val, result);
  530. // should it skip negative sign and process
  531. // rest of string or return 0
  532. val = U64L(1234); // skip initial negative sign
  533. result = str_to_U64("-1234");
  534. ensure_equals("str_to_U64 converted 2.8", val, result);
  535. val = U64L(5678); // stop at negative sign in the middle
  536. result = str_to_U64("5678-1234");
  537. ensure_equals("str_to_U64 converted 2.9", val, result);
  538. val = U64L(0); // no integers
  539. result = str_to_U64("AaCD");
  540. ensure_equals("str_to_U64 converted 2.10", val, result);
  541. }
  542. // U64_to_F64
  543. template<> template<>
  544. void U64_object::test<3>()
  545. {
  546. F64 val;
  547. F64 result;
  548. result = 18446744073709551610.0;
  549. val = U64_to_F64(U64L(18446744073709551610));
  550. ensure_equals("U64_to_F64 converted 3.1", val, result);
  551. result = 18446744073709551615.0; // 0xFFFFFFFFFFFFFFFF
  552. val = U64_to_F64(U64L(18446744073709551615));
  553. ensure_equals("U64_to_F64 converted 3.2", val, result);
  554. result = 0.0; // overflow 0xFFFFFFFFFFFFFFFF + 1 == 0
  555. // overflow - will result in warning at compile time
  556. val = U64_to_F64(U64L(18446744073709551615)+1);
  557. ensure_equals("U64_to_F64 converted 3.3", val, result);
  558. result = 0.0; // 0
  559. val = U64_to_F64(U64L(0));
  560. ensure_equals("U64_to_F64 converted 3.4", val, result);
  561. result = 1.0; // odd
  562. val = U64_to_F64(U64L(1));
  563. ensure_equals("U64_to_F64 converted 3.5", val, result);
  564. result = 2.0; // even
  565. val = U64_to_F64(U64L(2));
  566. ensure_equals("U64_to_F64 converted 3.6", val, result);
  567. result = U64L(0x7FFFFFFFFFFFFFFF) * 1.0L; // 0x7FFFFFFFFFFFFFFF
  568. val = U64_to_F64(U64L(0x7FFFFFFFFFFFFFFF));
  569. ensure_equals("U64_to_F64 converted 3.7", val, result);
  570. }
  571. // llstrtou64
  572. // seems to be deprecated - could not find it being used
  573. // anywhere in the tarball - skipping unit tests for now
  574. }
  575. namespace tut
  576. {
  577. struct hash_data
  578. {
  579. };
  580. typedef test_group<hash_data> hash_test;
  581. typedef hash_test::object hash_object;
  582. tut::hash_test hash_tester("LLHash");
  583. template<> template<>
  584. void hash_object::test<1>()
  585. {
  586. const char * str1 = "test string one";
  587. const char * same_as_str1 = "test string one";
  588. size_t hash1 = llhash(str1);
  589. size_t same_as_hash1 = llhash(same_as_str1);
  590. ensure("Hashes from identical strings should be equal", hash1 == same_as_hash1);
  591. char str[100];
  592. strcpy( str, "Another test" );
  593. size_t hash2 = llhash(str);
  594. strcpy( str, "Different string, same pointer" );
  595. size_t hash3 = llhash(str);
  596. ensure("Hashes from same pointer but different string should not be equal", hash2 != hash3);
  597. }
  598. }