PageRenderTime 42ms CodeModel.GetById 2ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/test/llbuffer_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 271 lines | 199 code | 36 blank | 36 comment | 39 complexity | 067626470747fe328013a85a2360c51e MD5 | raw file
  1/**
  2 * @file llbuffer_tut.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief llbuffer test cases.
  6 *
  7 * $LicenseInfo:firstyear=2007&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#include <tut/tut.hpp>
 30#include "linden_common.h"
 31#include "lltut.h"
 32#include "llbuffer.h"
 33#include "llerror.h"
 34#include "llmemtype.h"
 35
 36
 37namespace tut
 38{
 39	struct buffer
 40	{
 41	};
 42
 43	typedef test_group<buffer> buffer_t;
 44	typedef buffer_t::object buffer_object_t;
 45	tut::buffer_t tut_buffer("buffer");
 46
 47	template<> template<>
 48	void buffer_object_t::test<1>()
 49	{
 50		LLChannelDescriptors channelDescriptors;
 51		ensure("in() and out() functions Failed", (0 == channelDescriptors.in() && 1 == channelDescriptors.out()));
 52	
 53		S32 val = 50;
 54		LLChannelDescriptors channelDescriptors1(val);
 55		ensure("LLChannelDescriptors in() and out() functions Failed", (50 == channelDescriptors1.in() && 51 == channelDescriptors1.out()));
 56	}	
 57	
 58	template<> template<>
 59	void buffer_object_t::test<2>()
 60	{
 61		LLSegment segment;
 62		ensure("LLSegment get functions failed", (0 == segment.getChannel() && NULL == segment.data() && 0 == segment.size()));
 63		segment.setChannel(50);
 64		ensure_equals("LLSegment setChannel() function failed", segment.getChannel(), 50);
 65		ensure("LLSegment isOnChannel() function failed", (TRUE == segment.isOnChannel(50)));
 66	}	
 67
 68	template<> template<>
 69	void buffer_object_t::test<3>()
 70	{
 71		S32 channel = 30;
 72		const char str[] = "SecondLife";
 73		S32 len = sizeof(str);
 74		LLSegment segment(channel, (U8*)str, len);
 75		ensure("LLSegment get functions failed", (30 == segment.getChannel() && len == segment.size() && (U8*)str == segment.data()));
 76		ensure_memory_matches("LLSegment::data() failed",  segment.data(), segment.size(), (U8*)str, len);
 77		ensure("LLSegment isOnChannel() function failed", (TRUE == segment.isOnChannel(channel)));
 78	}	 
 79	
 80	template<> template<>
 81	void buffer_object_t::test<4>()
 82	{
 83		S32 channel = 50;
 84		S32 bigSize = 16384*2;
 85		char str[] = "SecondLife";
 86		S32 smallSize = sizeof(str);
 87
 88		LLSegment segment;
 89		LLHeapBuffer buf; // use default size of DEFAULT_HEAP_BUFFER_SIZE = 16384
 90
 91		S32 requestSize;
 92
 93		requestSize = 16384-1;
 94		ensure("1. LLHeapBuffer createSegment failed", (TRUE == buf.createSegment(channel, requestSize, segment)) && segment.size() == requestSize);
 95		// second request for remainign 1 byte
 96
 97		requestSize = 1;
 98		ensure("2. LLHeapBuffer createSegment failed", (TRUE == buf.createSegment(channel, requestSize, segment)) && segment.size() == requestSize);
 99
100		// it should fail now.
101		requestSize = 1;
102		ensure("3. LLHeapBuffer createSegment failed", (FALSE == buf.createSegment(channel, requestSize, segment)));
103
104		LLHeapBuffer buf1(bigSize);
105
106		// requst for more than default size but less than total sizeit should fail now.
107		requestSize = 16384 + 1;
108		ensure("4. LLHeapBuffer createSegment failed", (TRUE == buf1.createSegment(channel, requestSize, segment)) && segment.size() == requestSize);
109
110		LLHeapBuffer buf2((U8*) str, smallSize);
111		requestSize = smallSize;
112		ensure("5. LLHeapBuffer createSegment failed", (TRUE == buf2.createSegment(channel, requestSize, segment)) && segment.size() == requestSize && memcmp(segment.data(), (U8*) str, requestSize) == 0);
113		requestSize = smallSize+1;
114		ensure("6. LLHeapBuffer createSegment failed", (FALSE == buf2.createSegment(channel, requestSize, segment)));
115	}	
116
117	//makeChannelConsumer()
118	template<> template<>
119	void buffer_object_t::test<5>()
120	{
121		LLChannelDescriptors inchannelDescriptors(20);
122		LLChannelDescriptors outchannelDescriptors = LLBufferArray::makeChannelConsumer(inchannelDescriptors);	
123		ensure("LLBufferArray::makeChannelConsumer() function Failed", (21 == outchannelDescriptors.in()));
124	}
125
126	template<> template<>
127	void buffer_object_t::test<6>()
128	{
129		LLBufferArray bufferArray;
130		const char array[] = "SecondLife";
131		S32 len = strlen(array);
132		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
133		bufferArray.append(channelDescriptors.in(), (U8*)array, len);
134		S32 count = bufferArray.countAfter(channelDescriptors.in(), NULL);
135		ensure_equals("Appended size is:", count, len);
136	}
137
138	//append() and prepend()
139	template<> template<>
140	void buffer_object_t::test<7>()
141	{
142		LLBufferArray bufferArray;
143		const char array[] = "SecondLife";
144		S32 len = strlen(array);
145		const char array1[] = "LindenLabs";
146		S32 len1 = strlen(array1);
147		
148		std::string str(array1);
149		str.append(array);
150		
151		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
152		bufferArray.append(channelDescriptors.in(), (U8*)array, len);
153		bufferArray.prepend(channelDescriptors.in(), (U8*)array1, len1);
154		char buf[100];
155		S32 len2 = 20;
156		bufferArray.readAfter(channelDescriptors.in(), NULL, (U8*)buf, len2);
157		ensure_equals("readAfter length failed", len2, 20);
158		
159		buf[len2] = '\0';
160		ensure_equals("readAfter/prepend/append failed", buf, str);
161	}
162
163	//append()
164	template<> template<>
165	void buffer_object_t::test<8>()
166	{
167		LLBufferArray bufferArray;
168		const char array[] = "SecondLife";
169		S32 len = strlen(array);
170		const char array1[] = "LindenLabs";
171		S32 len1 = strlen(array1);
172		
173		std::string str(array);
174		str.append(array1);
175		
176		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
177		bufferArray.append(channelDescriptors.in(), (U8*)array, len);		
178		bufferArray.append(channelDescriptors.in(), (U8*)array1, len1);
179		char buf[100];
180		S32 len2 = 20;
181		bufferArray.readAfter(channelDescriptors.in(), NULL, (U8*)buf, len2);
182		ensure_equals("readAfter length failed", len2, 20);
183		
184		buf[len2] = '\0';
185		ensure_equals("readAfter/append/append failed", buf, str);
186	}
187
188	template<> template<>
189	void buffer_object_t::test<9>()
190	{
191		LLBufferArray bufferArray;
192		const char array[] = "SecondLife";
193		S32 len = strlen(array) + 1;
194		std::string str(array);
195		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
196		bufferArray.append(channelDescriptors.in(), (U8*)array, len);
197		LLBufferArray bufferArray1;
198		ensure("Contents are not copied and the source buffer is not empty", (1 == bufferArray1.takeContents(bufferArray)));
199		
200		char buf[100];
201		S32 len2 = len;
202		bufferArray1.readAfter(channelDescriptors.in(), NULL, (U8*)buf, len2);	
203		ensure_equals("takeContents failed to copy", buf, str);
204	}
205
206	//seek()
207	template<> template<>
208	void buffer_object_t::test<10>()
209	{
210		const char array[] = "SecondLife is a Virtual World";
211		S32 len = strlen(array);
212		LLBufferArray bufferArray;
213		bufferArray.append(0, (U8*)array, len);
214		
215		char buf[255];
216		S32 len1 = 16;
217		U8* last = bufferArray.readAfter(0, 0, (U8*)buf, len1);
218		buf[len1] = '\0';
219		last = bufferArray.seek(0, last, -2);
220
221		len1 = 15;
222		last = bufferArray.readAfter(0, last, (U8*)buf, len1);
223		buf[len1] = '\0';
224		std::string str(buf);
225		ensure_equals("Seek does'nt worked", str, std::string("a Virtual World"));
226	}
227
228	template<> template<>
229	void buffer_object_t::test<11>()
230	{
231		const char array[] = "SecondLife is a Virtual World";
232		S32 len = strlen(array);
233		LLBufferArray bufferArray;
234		bufferArray.append(0, (U8*)array, len);
235		
236		char buf[255];
237		S32 len1 = 10;
238		U8* last = bufferArray.readAfter(0, 0, (U8*)buf, len1);
239		bufferArray.splitAfter(last);
240		LLBufferArray::segment_iterator_t iterator = bufferArray.beginSegment();
241		++iterator;
242		std::string str(((char*)(*iterator).data()), (*iterator).size());
243		ensure_equals("Strings are not equal;splitAfter() operation failed", str, std::string(" is a Virtual World"));
244	}
245
246	//makeSegment()->eraseSegment()
247	template<> template<>
248	void buffer_object_t::test<12>()
249	{
250		LLBufferArray bufferArray;
251		LLChannelDescriptors channelDescriptors;
252		LLBufferArray::segment_iterator_t it;
253		S32 length = 1000;
254		it = bufferArray.makeSegment(channelDescriptors.out(), length);
255		ensure("makeSegment() function failed", (it != bufferArray.endSegment()));
256		ensure("eraseSegment() function failed", bufferArray.eraseSegment(it));
257		ensure("eraseSegment() begin/end should now be same", bufferArray.beginSegment() == bufferArray.endSegment());
258	}
259
260	// constructSegmentAfter()
261	template<> template<>
262	void buffer_object_t::test<13>()
263	{
264		LLBufferArray bufferArray;
265		LLBufferArray::segment_iterator_t it;
266		LLSegment segment;
267		LLBufferArray::segment_iterator_t end = bufferArray.endSegment();
268		it = bufferArray.constructSegmentAfter(NULL, segment);
269		ensure("constructSegmentAfter() function failed", (it == end));
270	}
271}