PageRenderTime 52ms CodeModel.GetById 23ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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
 28#include <tut/tut.hpp>
 29#include "linden_common.h"
 30#include "lltut.h"
 31#include "v3dmath.h"
 32#include "v3math.h"
 33#include "v4math.h"
 34#include "llquaternion.h"
 35
 36#include "message.h"
 37#include "llsdmessagereader.h"
 38#include "llsdutil.h"
 39#include "llsdutil_math.h"
 40
 41namespace tut
 42{	
 43	struct LLSDMessageReaderTestData {
 44		static void ensureMessageName(const std::string& msg_name,
 45									const LLSD& msg_data,
 46									const std::string& expected_name)
 47		{
 48			LLSDMessageReader msg;
 49			msg.setMessage(LLMessageStringTable::getInstance()->getString(msg_name.c_str()), msg_data);
 50			ensure_equals("Ensure name", std::string(msg.getMessageName()), 
 51						  expected_name);
 52		}
 53
 54		static void ensureNumberOfBlocks(const LLSD& msg_data,
 55									const std::string& block,
 56									S32 expected_number)
 57		{
 58			LLSDMessageReader msg;
 59			msg.setMessage("fakename", msg_data);
 60			ensure_equals("Ensure number of blocks", msg.getNumberOfBlocks(block.c_str()), 
 61						  expected_number);
 62		}
 63
 64		static void ensureMessageSize(const LLSD& msg_data,
 65									S32 expected_size)
 66		{
 67			LLSDMessageReader msg;
 68			msg.setMessage("fakename", msg_data);
 69			ensure_equals(	"Ensure size",	msg.getMessageSize(), expected_size);
 70		}
 71
 72		static void ensureBool(const LLSD& msg_data,
 73								const std::string& block,
 74								const std::string& var,
 75								S32 blocknum,
 76								BOOL expected)
 77		{
 78			LLSDMessageReader msg;
 79			msg.setMessage("fakename", msg_data);
 80			BOOL test_data;
 81			msg.getBOOL(block.c_str(), var.c_str(), test_data, blocknum);
 82 			ensure_equals(	"Ensure bool field", test_data, expected);
 83 		}
 84 	};
 85	
 86 	typedef test_group<LLSDMessageReaderTestData>	LLSDMessageReaderTestGroup;
 87 	typedef LLSDMessageReaderTestGroup::object		LLSDMessageReaderTestObject;
 88 	LLSDMessageReaderTestGroup llsdMessageReaderTestGroup("LLSDMessageReader");
 89	
 90 	template<> template<>
 91 	void LLSDMessageReaderTestObject::test<1>()
 92 		// construction and test of empty LLSD
 93 	{
 94 		LLSD message = LLSD::emptyMap();
 95
 96 		ensureMessageName("", message, "");
 97 		ensureNumberOfBlocks(message, "Fakeblock", 0);
 98 		ensureMessageSize(message, 0);
 99 	}
100	
101 	template<> template<>
102 	void LLSDMessageReaderTestObject::test<2>()
103 		// construction and test of simple message with one block
104 	{
105 		LLSD message = LLSD::emptyMap();
106 		message["block1"] = LLSD::emptyArray();
107 		message["block1"][0] = LLSD::emptyMap();
108 		message["block1"][0]["Field1"] = 0;
109
110 		ensureMessageName("name2", message, "name2");
111 		ensureNumberOfBlocks(message, "block1", 1);
112 		ensureMessageSize(message, 0);
113 	}
114	
115 	template<> template<>
116 	void LLSDMessageReaderTestObject::test<3>()
117 		// multiple blocks
118 	{
119 		LLSD message = LLSD::emptyMap();
120 		message["block1"] = LLSD::emptyArray();
121 		BOOL bool_true = TRUE;
122 		BOOL bool_false = FALSE;
123 		message["block1"][0] = LLSD::emptyMap();
124 		message["block1"][0]["BoolField1"] = bool_true;
125		message["block1"][1] = LLSD::emptyMap();
126 		message["block1"][1]["BoolField1"] = bool_false;
127 		message["block1"][1]["BoolField2"] = bool_true;
128
129 		ensureMessageName("name3", message, "name3");
130 		ensureBool(message, "block1", "BoolField1", 0, TRUE);
131 		ensureBool(message, "block1", "BoolField1", 1, FALSE);
132 		ensureBool(message, "block1", "BoolField2", 1, TRUE);
133 		ensureNumberOfBlocks(message, "block1", 2);
134 		ensureMessageSize(message, 0);
135 	}
136	
137 	template<typename T>
138 	LLSDMessageReader testType(const T& value)
139 	{
140 		LLSD message = LLSD::emptyMap();
141 		message["block"][0]["var"] = value;
142 		LLSDMessageReader msg;
143 		msg.setMessage("fakename", message);
144 		return msg;
145 	}
146
147 	template<> template<>
148 	void LLSDMessageReaderTestObject::test<4>()
149 		// S8
150 	{
151 		S8 outValue, inValue = -3;
152 		LLSDMessageReader msg = testType(inValue);
153 		msg.getS8("block", "var", outValue);
154 		ensure_equals("Ensure S8", outValue, inValue);
155 	}
156 	template<> template<>
157 	void 
158	LLSDMessageReaderTestObject::test<5>()
159		// U8
160	{
161		U8 outValue, inValue = 2;
162		LLSDMessageReader msg = testType(inValue);
163		msg.getU8("block", "var", outValue);
164		ensure_equals("Ensure U8", outValue, inValue);
165	}
166	template<> template<>
167	void LLSDMessageReaderTestObject::test<6>()
168		// S16
169	{
170		S16 outValue, inValue = 90;
171		LLSDMessageReader msg = testType(inValue);
172		msg.getS16("block", "var", outValue);
173		ensure_equals("Ensure S16", outValue, inValue);
174	}
175	template<> template<>
176	void LLSDMessageReaderTestObject::test<7>()
177		// U16
178	{
179		U16 outValue, inValue = 3;
180		LLSDMessageReader msg = testType(inValue);
181		msg.getU16("block", "var", outValue);
182		ensure_equals("Ensure S16", outValue, inValue);
183	}
184	template<> template<>
185	void LLSDMessageReaderTestObject::test<8>()
186		// S32
187	{
188		S32 outValue, inValue = 44;
189		LLSDMessageReader msg = testType(inValue);
190		msg.getS32("block", "var", outValue);
191		ensure_equals("Ensure S32", outValue, inValue);
192	}
193	template<> template<>
194	void LLSDMessageReaderTestObject::test<9>()
195		// F32
196	{
197		F32 outValue, inValue = 121.44f;
198		LLSDMessageReader msg = testType(inValue);
199		msg.getF32("block", "var", outValue);
200		ensure_equals("Ensure F32", outValue, inValue);
201	}
202	template<> template<>
203	void LLSDMessageReaderTestObject::test<10>()
204		// U32
205	{
206		U32 outValue, inValue = 88;
207		LLSD sdValue = ll_sd_from_U32(inValue);
208		LLSDMessageReader msg = testType(sdValue);
209		msg.getU32("block", "var", outValue);
210		ensure_equals("Ensure U32", outValue, inValue);
211	}
212	template<> template<>
213	void LLSDMessageReaderTestObject::test<11>()
214		// U64
215	{
216		U64 outValue, inValue = 121;
217		LLSD sdValue = ll_sd_from_U64(inValue);
218		LLSDMessageReader msg = testType(sdValue);
219		msg.getU64("block", "var", outValue);
220		ensure_equals("Ensure U64", outValue, inValue);
221	}
222	template<> template<>
223	void LLSDMessageReaderTestObject::test<12>()
224		// F64
225	{
226		F64 outValue, inValue = 3232143.33;
227		LLSDMessageReader msg = testType(inValue);
228		msg.getF64("block", "var", outValue);
229		ensure_equals("Ensure F64", outValue, inValue);
230	}
231	template<> template<>
232	void LLSDMessageReaderTestObject::test<13>()
233		// String
234	{
235		 std::string outValue, inValue = "testing";
236		LLSDMessageReader msg = testType<std::string>(inValue.c_str());
237		
238		char buffer[MAX_STRING];
239		msg.getString("block", "var", MAX_STRING, buffer);
240		outValue = buffer;
241		ensure_equals("Ensure String", outValue, inValue);
242	}
243	template<> template<>
244	void LLSDMessageReaderTestObject::test<14>()
245		// Vector3
246	{
247		 LLVector3 outValue, inValue = LLVector3(1,2,3);
248		LLSD sdValue = ll_sd_from_vector3(inValue);
249		LLSDMessageReader msg = testType(sdValue);
250		msg.getVector3("block", "var", outValue);
251		ensure_equals("Ensure Vector3", outValue, inValue);
252	}
253	template<> template<>
254	void LLSDMessageReaderTestObject::test<15>()
255		// Vector4
256	{
257		LLVector4 outValue, inValue = LLVector4(1,2,3,4);
258		LLSD sdValue = ll_sd_from_vector4(inValue);
259		LLSDMessageReader msg = testType(sdValue);
260		msg.getVector4("block", "var", outValue);
261		ensure_equals("Ensure Vector4", outValue, inValue);
262	}
263	template<> template<>
264	void LLSDMessageReaderTestObject::test<16>()
265		// Vector3d
266	{
267		 LLVector3d outValue, inValue = LLVector3d(1,2,3);
268		 LLSD sdValue = ll_sd_from_vector3d(inValue);
269		LLSDMessageReader msg = testType(sdValue);
270		msg.getVector3d("block", "var", outValue);
271		ensure_equals("Ensure Vector3d", outValue, inValue);
272	}
273	template<> template<>
274	void LLSDMessageReaderTestObject::test<17>()
275		// Quaternion
276	{
277		 LLQuaternion outValue, inValue = LLQuaternion(1,2,3,4);
278		LLSD sdValue = ll_sd_from_quaternion(inValue);
279		LLSDMessageReader msg = testType(sdValue);
280		msg.getQuat("block", "var", outValue);
281		ensure_equals("Ensure Quaternion", outValue, inValue);
282	}
283	template<> template<>
284	void LLSDMessageReaderTestObject::test<18>()
285		// UUID
286	{
287		LLUUID outValue, inValue;
288		inValue.generate();
289		LLSDMessageReader msg = testType(inValue);
290		msg.getUUID("block", "var", outValue);
291		ensure_equals("Ensure UUID", outValue, inValue);
292	}
293	template<> template<>
294	void LLSDMessageReaderTestObject::test<19>()
295		// IPAddr
296	{
297		U32 outValue, inValue = 12344556;
298		LLSD sdValue = ll_sd_from_ipaddr(inValue);
299		LLSDMessageReader msg = testType(sdValue);
300		msg.getIPAddr("block", "var", outValue);
301		ensure_equals("Ensure IPAddr", outValue, inValue);
302	}
303	template<> template<>
304	void LLSDMessageReaderTestObject::test<20>()
305		// IPPort
306	{
307		U16 outValue, inValue = 80;
308		LLSDMessageReader msg = testType(inValue);
309		msg.getIPPort("block", "var", outValue);
310		ensure_equals("Ensure IPPort", outValue, inValue);
311	}
312	template<> template<>
313	void LLSDMessageReaderTestObject::test<21>()
314		// Binary 
315	{
316		std::vector<U8> outValue(2), inValue(2);
317		inValue[0] = 0;
318		inValue[1] = 1;
319	  
320		LLSDMessageReader msg = testType(inValue);
321		msg.getBinaryData("block", "var", &(outValue[0]), inValue.size());
322		ensure_equals("Ensure Binary", outValue, inValue);
323	}
324}