aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/llbuffer_tut.cpp
blob: cd659ce9b89e37be1ff7bec0f51d1d8a2c33bb90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 * @file llbuffer_tut.cpp
 * @author Adroit
 * @date 2007-03
 * @brief llbuffer test cases.
 *
 * $LicenseInfo:firstyear=2007&license=viewergpl$
 * 
 * Copyright (c) 2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include <tut/tut.h>
#include "linden_common.h"
#include "lltut.h"
#include "llbuffer.h"
#include "llerror.h"
#include "llmemtype.h"


namespace tut
{
	struct buffer
	{
	};

	typedef test_group<buffer> buffer_t;
	typedef buffer_t::object buffer_object_t;
	tut::buffer_t tut_buffer("buffer");

	template<> template<>
	void buffer_object_t::test<1>()
	{
		LLChannelDescriptors channelDescriptors;
		ensure("in() and out() functions Failed", (0 == channelDescriptors.in() && 1 == channelDescriptors.out()));
	
		S32 val = 50;
		LLChannelDescriptors channelDescriptors1(val);
		ensure("LLChannelDescriptors in() and out() functions Failed", (50 == channelDescriptors1.in() && 51 == channelDescriptors1.out()));
	}	
	
	template<> template<>
	void buffer_object_t::test<2>()
	{
		LLSegment segment;
		ensure("LLSegment get functions failed", (0 == segment.getChannel() && NULL == segment.data() && 0 == segment.size()));
		segment.setChannel(50);
		ensure_equals("LLSegment setChannel() function failed", segment.getChannel(), 50);
		ensure("LLSegment isOnChannel() function failed", (TRUE == segment.isOnChannel(50)));
	}	

	template<> template<>
	void buffer_object_t::test<3>()
	{
		S32 channel = 30;
		const char str[] = "SecondLife";
		S32 len = sizeof(str);
		LLSegment segment(channel, (U8*)str, len);
		ensure("LLSegment get functions failed", (30 == segment.getChannel() && len == segment.size() && (U8*)str == segment.data()));
		ensure_memory_matches("LLSegment::data() failed",  segment.data(), segment.size(), (U8*)str, len);
		ensure("LLSegment isOnChannel() function failed", (TRUE == segment.isOnChannel(channel)));
	}	 
	
	template<> template<>
	void buffer_object_t::test<4>()
	{
		S32 channel = 50;
		S32 bigSize = 16384*2;
		char str[] = "SecondLife";
		S32 smallSize = sizeof(str);

		LLSegment segment;
		LLHeapBuffer buf; // use default size of DEFAULT_HEAP_BUFFER_SIZE = 16384

		S32 requestSize;

		requestSize = 16384-1;
		ensure("1. LLHeapBuffer createSegment failed", (TRUE == buf.createSegment(channel, requestSize, segment)) && segment.size() == requestSize);
		// second request for remainign 1 byte

		requestSize = 1;
		ensure("2. LLHeapBuffer createSegment failed", (TRUE == buf.createSegment(channel, requestSize, segment)) && segment.size() == requestSize);

		// it should fail now.
		requestSize = 1;
		ensure("3. LLHeapBuffer createSegment failed", (FALSE == buf.createSegment(channel, requestSize, segment)));

		LLHeapBuffer buf1(bigSize);

		// requst for more than default size but less than total sizeit should fail now.
		requestSize = 16384 + 1;
		ensure("4. LLHeapBuffer createSegment failed", (TRUE == buf1.createSegment(channel, requestSize, segment)) && segment.size() == requestSize);

		LLHeapBuffer buf2((U8*) str, smallSize);
		requestSize = smallSize;
		ensure("5. LLHeapBuffer createSegment failed", (TRUE == buf2.createSegment(channel, requestSize, segment)) && segment.size() == requestSize && memcmp(segment.data(), (U8*) str, requestSize) == 0);
		requestSize = smallSize+1;
		ensure("6. LLHeapBuffer createSegment failed", (FALSE == buf2.createSegment(channel, requestSize, segment)));
	}	

	//makeChannelConsumer()
	template<> template<>
	void buffer_object_t::test<5>()
	{
		LLChannelDescriptors inchannelDescriptors(20);
		LLChannelDescriptors outchannelDescriptors = LLBufferArray::makeChannelConsumer(inchannelDescriptors);	
		ensure("LLBufferArray::makeChannelConsumer() function Failed", (21 == outchannelDescriptors.in()));
	}

	template<> template<>
	void buffer_object_t::test<6>()
	{
		LLBufferArray bufferArray;
		const char array[] = "SecondLife";
		S32 len = strlen(array);
		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
		bufferArray.append(channelDescriptors.in(), (U8*)array, len);
		S32 count = bufferArray.countAfter(channelDescriptors.in(), NULL);
		ensure_equals("Appended size is:", count, len);
	}

	//append() and prepend()
	template<> template<>
	void buffer_object_t::test<7>()
	{
		LLBufferArray bufferArray;
		const char array[] = "SecondLife";
		S32 len = strlen(array);
		const char array1[] = "LindenLabs";
		S32 len1 = strlen(array1);
		
		std::string str(array1);
		str.append(array);
		
		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
		bufferArray.append(channelDescriptors.in(), (U8*)array, len);
		bufferArray.prepend(channelDescriptors.in(), (U8*)array1, len1);
		char buf[100];
		S32 len2 = 20;
		bufferArray.readAfter(channelDescriptors.in(), NULL, (U8*)buf, len2);
		ensure_equals("readAfter length failed", len2, 20);
		
		buf[len2] = '\0';
		ensure_equals("readAfter/prepend/append failed", buf, str);
	}

	//append()
	template<> template<>
	void buffer_object_t::test<8>()
	{
		LLBufferArray bufferArray;
		const char array[] = "SecondLife";
		S32 len = strlen(array);
		const char array1[] = "LindenLabs";
		S32 len1 = strlen(array1);
		
		std::string str(array);
		str.append(array1);
		
		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
		bufferArray.append(channelDescriptors.in(), (U8*)array, len);		
		bufferArray.append(channelDescriptors.in(), (U8*)array1, len1);
		char buf[100];
		S32 len2 = 20;
		bufferArray.readAfter(channelDescriptors.in(), NULL, (U8*)buf, len2);
		ensure_equals("readAfter length failed", len2, 20);
		
		buf[len2] = '\0';
		ensure_equals("readAfter/append/append failed", buf, str);
	}

	template<> template<>
	void buffer_object_t::test<9>()
	{
		LLBufferArray bufferArray;
		const char array[] = "SecondLife";
		S32 len = strlen(array) + 1;
		std::string str(array);
		LLChannelDescriptors channelDescriptors = bufferArray.nextChannel();
		bufferArray.append(channelDescriptors.in(), (U8*)array, len);
		LLBufferArray bufferArray1;
		ensure("Contents are not copied and the source buffer is not empty", (1 == bufferArray1.takeContents(bufferArray)));
		
		char buf[100];
		S32 len2 = len;
		bufferArray1.readAfter(channelDescriptors.in(), NULL, (U8*)buf, len2);	
		ensure_equals("takeContents failed to copy", buf, str);
	}

	//seek()
	template<> template<>
	void buffer_object_t::test<10>()
	{
		const char array[] = "SecondLife is a Virtual World";
		S32 len = strlen(array);
		LLBufferArray bufferArray;
		bufferArray.append(0, (U8*)array, len);
		
		char buf[255];
		S32 len1 = 16;
		U8* last = bufferArray.readAfter(0, 0, (U8*)buf, len1);
		buf[len1] = '\0';
		last = bufferArray.seek(0, last, -2);

		len1 = 15;
		last = bufferArray.readAfter(0, last, (U8*)buf, len1);
		buf[len1] = '\0';
		std::string str(buf);
		ensure_equals("Seek does'nt worked", str, std::string("a Virtual World"));
	}

	template<> template<>
	void buffer_object_t::test<11>()
	{
		const char array[] = "SecondLife is a Virtual World";
		S32 len = strlen(array);
		LLBufferArray bufferArray;
		bufferArray.append(0, (U8*)array, len);
		
		char buf[255];
		S32 len1 = 10;
		U8* last = bufferArray.readAfter(0, 0, (U8*)buf, len1);
		bufferArray.splitAfter(last);
		LLBufferArray::segment_iterator_t iterator = bufferArray.beginSegment();
		++iterator;
		std::string str(((char*)(*iterator).data()), (*iterator).size());
		ensure_equals("Strings are not equal;splitAfter() operation failed", str, std::string(" is a Virtual World"));
	}

	//makeSegment()->eraseSegment()
	template<> template<>
	void buffer_object_t::test<12>()
	{
		LLBufferArray bufferArray;
		LLChannelDescriptors channelDescriptors;
		LLBufferArray::segment_iterator_t it;
		S32 length = 1000;
		it = bufferArray.makeSegment(channelDescriptors.out(), length);
		ensure("makeSegment() function failed", (it != bufferArray.endSegment()));
		ensure("eraseSegment() function failed", bufferArray.eraseSegment(it));
		ensure("eraseSegment() begin/end should now be same", bufferArray.beginSegment() == bufferArray.endSegment());
	}

	// constructSegmentAfter()
	template<> template<>
	void buffer_object_t::test<13>()
	{
		LLBufferArray bufferArray;
		LLBufferArray::segment_iterator_t it;
		LLSegment segment;
		LLBufferArray::segment_iterator_t end = bufferArray.endSegment();
		it = bufferArray.constructSegmentAfter(NULL, segment);
		ensure("constructSegmentAfter() function failed", (it == end));
	}
}