PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/test/llsdmessagereader_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 324 lines | 260 code | 17 blank | 47 comment | 0 complexity | 40acfc160c1f55ca0a0b13cc4fc85d75 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsdmessagereader_tut.cpp
  3. * @date February 2006
  4. * @brief LLSDMessageReader unit tests
  5. *
  6. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include <tut/tut.hpp>
  28. #include "linden_common.h"
  29. #include "lltut.h"
  30. #include "v3dmath.h"
  31. #include "v3math.h"
  32. #include "v4math.h"
  33. #include "llquaternion.h"
  34. #include "message.h"
  35. #include "llsdmessagereader.h"
  36. #include "llsdutil.h"
  37. #include "llsdutil_math.h"
  38. namespace tut
  39. {
  40. struct LLSDMessageReaderTestData {
  41. static void ensureMessageName(const std::string& msg_name,
  42. const LLSD& msg_data,
  43. const std::string& expected_name)
  44. {
  45. LLSDMessageReader msg;
  46. msg.setMessage(LLMessageStringTable::getInstance()->getString(msg_name.c_str()), msg_data);
  47. ensure_equals("Ensure name", std::string(msg.getMessageName()),
  48. expected_name);
  49. }
  50. static void ensureNumberOfBlocks(const LLSD& msg_data,
  51. const std::string& block,
  52. S32 expected_number)
  53. {
  54. LLSDMessageReader msg;
  55. msg.setMessage("fakename", msg_data);
  56. ensure_equals("Ensure number of blocks", msg.getNumberOfBlocks(block.c_str()),
  57. expected_number);
  58. }
  59. static void ensureMessageSize(const LLSD& msg_data,
  60. S32 expected_size)
  61. {
  62. LLSDMessageReader msg;
  63. msg.setMessage("fakename", msg_data);
  64. ensure_equals( "Ensure size", msg.getMessageSize(), expected_size);
  65. }
  66. static void ensureBool(const LLSD& msg_data,
  67. const std::string& block,
  68. const std::string& var,
  69. S32 blocknum,
  70. BOOL expected)
  71. {
  72. LLSDMessageReader msg;
  73. msg.setMessage("fakename", msg_data);
  74. BOOL test_data;
  75. msg.getBOOL(block.c_str(), var.c_str(), test_data, blocknum);
  76. ensure_equals( "Ensure bool field", test_data, expected);
  77. }
  78. };
  79. typedef test_group<LLSDMessageReaderTestData> LLSDMessageReaderTestGroup;
  80. typedef LLSDMessageReaderTestGroup::object LLSDMessageReaderTestObject;
  81. LLSDMessageReaderTestGroup llsdMessageReaderTestGroup("LLSDMessageReader");
  82. template<> template<>
  83. void LLSDMessageReaderTestObject::test<1>()
  84. // construction and test of empty LLSD
  85. {
  86. LLSD message = LLSD::emptyMap();
  87. ensureMessageName("", message, "");
  88. ensureNumberOfBlocks(message, "Fakeblock", 0);
  89. ensureMessageSize(message, 0);
  90. }
  91. template<> template<>
  92. void LLSDMessageReaderTestObject::test<2>()
  93. // construction and test of simple message with one block
  94. {
  95. LLSD message = LLSD::emptyMap();
  96. message["block1"] = LLSD::emptyArray();
  97. message["block1"][0] = LLSD::emptyMap();
  98. message["block1"][0]["Field1"] = 0;
  99. ensureMessageName("name2", message, "name2");
  100. ensureNumberOfBlocks(message, "block1", 1);
  101. ensureMessageSize(message, 0);
  102. }
  103. template<> template<>
  104. void LLSDMessageReaderTestObject::test<3>()
  105. // multiple blocks
  106. {
  107. LLSD message = LLSD::emptyMap();
  108. message["block1"] = LLSD::emptyArray();
  109. BOOL bool_true = TRUE;
  110. BOOL bool_false = FALSE;
  111. message["block1"][0] = LLSD::emptyMap();
  112. message["block1"][0]["BoolField1"] = bool_true;
  113. message["block1"][1] = LLSD::emptyMap();
  114. message["block1"][1]["BoolField1"] = bool_false;
  115. message["block1"][1]["BoolField2"] = bool_true;
  116. ensureMessageName("name3", message, "name3");
  117. ensureBool(message, "block1", "BoolField1", 0, TRUE);
  118. ensureBool(message, "block1", "BoolField1", 1, FALSE);
  119. ensureBool(message, "block1", "BoolField2", 1, TRUE);
  120. ensureNumberOfBlocks(message, "block1", 2);
  121. ensureMessageSize(message, 0);
  122. }
  123. template<typename T>
  124. LLSDMessageReader testType(const T& value)
  125. {
  126. LLSD message = LLSD::emptyMap();
  127. message["block"][0]["var"] = value;
  128. LLSDMessageReader msg;
  129. msg.setMessage("fakename", message);
  130. return msg;
  131. }
  132. template<> template<>
  133. void LLSDMessageReaderTestObject::test<4>()
  134. // S8
  135. {
  136. S8 outValue, inValue = -3;
  137. LLSDMessageReader msg = testType(inValue);
  138. msg.getS8("block", "var", outValue);
  139. ensure_equals("Ensure S8", outValue, inValue);
  140. }
  141. template<> template<>
  142. void
  143. LLSDMessageReaderTestObject::test<5>()
  144. // U8
  145. {
  146. U8 outValue, inValue = 2;
  147. LLSDMessageReader msg = testType(inValue);
  148. msg.getU8("block", "var", outValue);
  149. ensure_equals("Ensure U8", outValue, inValue);
  150. }
  151. template<> template<>
  152. void LLSDMessageReaderTestObject::test<6>()
  153. // S16
  154. {
  155. S16 outValue, inValue = 90;
  156. LLSDMessageReader msg = testType(inValue);
  157. msg.getS16("block", "var", outValue);
  158. ensure_equals("Ensure S16", outValue, inValue);
  159. }
  160. template<> template<>
  161. void LLSDMessageReaderTestObject::test<7>()
  162. // U16
  163. {
  164. U16 outValue, inValue = 3;
  165. LLSDMessageReader msg = testType(inValue);
  166. msg.getU16("block", "var", outValue);
  167. ensure_equals("Ensure S16", outValue, inValue);
  168. }
  169. template<> template<>
  170. void LLSDMessageReaderTestObject::test<8>()
  171. // S32
  172. {
  173. S32 outValue, inValue = 44;
  174. LLSDMessageReader msg = testType(inValue);
  175. msg.getS32("block", "var", outValue);
  176. ensure_equals("Ensure S32", outValue, inValue);
  177. }
  178. template<> template<>
  179. void LLSDMessageReaderTestObject::test<9>()
  180. // F32
  181. {
  182. F32 outValue, inValue = 121.44f;
  183. LLSDMessageReader msg = testType(inValue);
  184. msg.getF32("block", "var", outValue);
  185. ensure_equals("Ensure F32", outValue, inValue);
  186. }
  187. template<> template<>
  188. void LLSDMessageReaderTestObject::test<10>()
  189. // U32
  190. {
  191. U32 outValue, inValue = 88;
  192. LLSD sdValue = ll_sd_from_U32(inValue);
  193. LLSDMessageReader msg = testType(sdValue);
  194. msg.getU32("block", "var", outValue);
  195. ensure_equals("Ensure U32", outValue, inValue);
  196. }
  197. template<> template<>
  198. void LLSDMessageReaderTestObject::test<11>()
  199. // U64
  200. {
  201. U64 outValue, inValue = 121;
  202. LLSD sdValue = ll_sd_from_U64(inValue);
  203. LLSDMessageReader msg = testType(sdValue);
  204. msg.getU64("block", "var", outValue);
  205. ensure_equals("Ensure U64", outValue, inValue);
  206. }
  207. template<> template<>
  208. void LLSDMessageReaderTestObject::test<12>()
  209. // F64
  210. {
  211. F64 outValue, inValue = 3232143.33;
  212. LLSDMessageReader msg = testType(inValue);
  213. msg.getF64("block", "var", outValue);
  214. ensure_equals("Ensure F64", outValue, inValue);
  215. }
  216. template<> template<>
  217. void LLSDMessageReaderTestObject::test<13>()
  218. // String
  219. {
  220. std::string outValue, inValue = "testing";
  221. LLSDMessageReader msg = testType<std::string>(inValue.c_str());
  222. char buffer[MAX_STRING];
  223. msg.getString("block", "var", MAX_STRING, buffer);
  224. outValue = buffer;
  225. ensure_equals("Ensure String", outValue, inValue);
  226. }
  227. template<> template<>
  228. void LLSDMessageReaderTestObject::test<14>()
  229. // Vector3
  230. {
  231. LLVector3 outValue, inValue = LLVector3(1,2,3);
  232. LLSD sdValue = ll_sd_from_vector3(inValue);
  233. LLSDMessageReader msg = testType(sdValue);
  234. msg.getVector3("block", "var", outValue);
  235. ensure_equals("Ensure Vector3", outValue, inValue);
  236. }
  237. template<> template<>
  238. void LLSDMessageReaderTestObject::test<15>()
  239. // Vector4
  240. {
  241. LLVector4 outValue, inValue = LLVector4(1,2,3,4);
  242. LLSD sdValue = ll_sd_from_vector4(inValue);
  243. LLSDMessageReader msg = testType(sdValue);
  244. msg.getVector4("block", "var", outValue);
  245. ensure_equals("Ensure Vector4", outValue, inValue);
  246. }
  247. template<> template<>
  248. void LLSDMessageReaderTestObject::test<16>()
  249. // Vector3d
  250. {
  251. LLVector3d outValue, inValue = LLVector3d(1,2,3);
  252. LLSD sdValue = ll_sd_from_vector3d(inValue);
  253. LLSDMessageReader msg = testType(sdValue);
  254. msg.getVector3d("block", "var", outValue);
  255. ensure_equals("Ensure Vector3d", outValue, inValue);
  256. }
  257. template<> template<>
  258. void LLSDMessageReaderTestObject::test<17>()
  259. // Quaternion
  260. {
  261. LLQuaternion outValue, inValue = LLQuaternion(1,2,3,4);
  262. LLSD sdValue = ll_sd_from_quaternion(inValue);
  263. LLSDMessageReader msg = testType(sdValue);
  264. msg.getQuat("block", "var", outValue);
  265. ensure_equals("Ensure Quaternion", outValue, inValue);
  266. }
  267. template<> template<>
  268. void LLSDMessageReaderTestObject::test<18>()
  269. // UUID
  270. {
  271. LLUUID outValue, inValue;
  272. inValue.generate();
  273. LLSDMessageReader msg = testType(inValue);
  274. msg.getUUID("block", "var", outValue);
  275. ensure_equals("Ensure UUID", outValue, inValue);
  276. }
  277. template<> template<>
  278. void LLSDMessageReaderTestObject::test<19>()
  279. // IPAddr
  280. {
  281. U32 outValue, inValue = 12344556;
  282. LLSD sdValue = ll_sd_from_ipaddr(inValue);
  283. LLSDMessageReader msg = testType(sdValue);
  284. msg.getIPAddr("block", "var", outValue);
  285. ensure_equals("Ensure IPAddr", outValue, inValue);
  286. }
  287. template<> template<>
  288. void LLSDMessageReaderTestObject::test<20>()
  289. // IPPort
  290. {
  291. U16 outValue, inValue = 80;
  292. LLSDMessageReader msg = testType(inValue);
  293. msg.getIPPort("block", "var", outValue);
  294. ensure_equals("Ensure IPPort", outValue, inValue);
  295. }
  296. template<> template<>
  297. void LLSDMessageReaderTestObject::test<21>()
  298. // Binary
  299. {
  300. std::vector<U8> outValue(2), inValue(2);
  301. inValue[0] = 0;
  302. inValue[1] = 1;
  303. LLSDMessageReader msg = testType(inValue);
  304. msg.getBinaryData("block", "var", &(outValue[0]), inValue.size());
  305. ensure_equals("Ensure Binary", outValue, inValue);
  306. }
  307. }