summaryrefslogtreecommitdiff
path: root/indra/llcorehttp/tests/test_bufferarray.hpp
blob: 8a2a64d970c385ab3051d5516bb3c2845f463338 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/** 
 * @file test_bufferarray.hpp
 * @brief unit tests for the LLCore::BufferArray class
 *
 * $LicenseInfo:firstyear=2012&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2012, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */
#ifndef TEST_LLCORE_BUFFER_ARRAY_H_
#define TEST_LLCORE_BUFFER_ARRAY_H_

#include "bufferarray.h"

#include <iostream>

#include "test_allocator.h"


using namespace LLCore;



namespace tut
{

struct BufferArrayTestData
{
	// the test objects inherit from this so the member functions and variables
	// can be referenced directly inside of the test functions.
	size_t mMemTotal;
};

typedef test_group<BufferArrayTestData> BufferArrayTestGroupType;
typedef BufferArrayTestGroupType::object BufferArrayTestObjectType;
BufferArrayTestGroupType BufferArrayTestGroup("BufferArray Tests");

template <> template <>
void BufferArrayTestObjectType::test<1>()
{
	set_test_name("BufferArray construction");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();
	ensure("One ref on construction of BufferArray", ba->getRefCount() == 1);
	ensure("Memory being used", mMemTotal < GetMemTotal());
	ensure("Nothing in BA", 0 == ba->size());

	// Try to read
	char buffer[20];
	size_t read_len(ba->read(0, buffer, sizeof(buffer)));
	ensure("Read returns empty", 0 == read_len);
	
	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure(mMemTotal == GetMemTotal());
}

template <> template <>
void BufferArrayTestObjectType::test<2>()
{
	set_test_name("BufferArray single write");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();

	// write some data to the buffer
	char str1[] = "abcdefghij";
 	char buffer[256];
	
	size_t len = ba->write(0, str1, strlen(str1));
	ensure("Wrote length correct", strlen(str1) == len);
	ensure("Recorded size correct", strlen(str1) == ba->size());

	// read some data back
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(2, buffer, 2);
	ensure("Read length correct", 2 == len);
	ensure("Read content correct", 'c' == buffer[0] && 'd' == buffer[1]);
	ensure("Read didn't overwrite", 'X' == buffer[2]);
	
	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure(mMemTotal == GetMemTotal());
}


template <> template <>
void BufferArrayTestObjectType::test<3>()
{
	set_test_name("BufferArray multiple writes");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();

	// write some data to the buffer
	char str1[] = "abcdefghij";
	size_t str1_len(strlen(str1));
 	char buffer[256];
	
	size_t len = ba->write(0, str1, str1_len);
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", str1_len == ba->size());

	// again...
	len = ba->write(str1_len, str1, strlen(str1));
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", (2 * str1_len) == ba->size());

	// read some data back
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(8, buffer, 4);
	ensure("Read length correct", 4 == len);
	ensure("Read content correct", 'i' == buffer[0] && 'j' == buffer[1]);
	ensure("Read content correct", 'a' == buffer[2] && 'b' == buffer[3]);
	ensure("Read didn't overwrite", 'X' == buffer[4]);

	// Read whole thing
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(0, buffer, sizeof(buffer));
	ensure("Read length correct", (2 * str1_len) == len);
	ensure("Read content correct (3)", 0 == strncmp(buffer, str1, str1_len));
	ensure("Read content correct (4)", 0 == strncmp(&buffer[str1_len], str1, str1_len));
	ensure("Read didn't overwrite (5)", 'X' == buffer[2 * str1_len]);
	
	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure(mMemTotal == GetMemTotal());
}

template <> template <>
void BufferArrayTestObjectType::test<4>()
{
	set_test_name("BufferArray overwriting");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();

	// write some data to the buffer
	char str1[] = "abcdefghij";
	size_t str1_len(strlen(str1));
	char str2[] = "ABCDEFGHIJ";
 	char buffer[256];
	
	size_t len = ba->write(0, str1, str1_len);
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", str1_len == ba->size());

	// again...
	len = ba->write(str1_len, str1, strlen(str1));
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", (2 * str1_len) == ba->size());

	// reposition and overwrite
	len = ba->write(8, str2, 4);
	ensure("Overwrite length correct", 4 == len);

	// Leave position and read verifying content (stale really from seek() days)
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(12, buffer, 4);
	ensure("Read length correct", 4 == len);
	ensure("Read content correct", 'c' == buffer[0] && 'd' == buffer[1]);
	ensure("Read content correct.2", 'e' == buffer[2] && 'f' == buffer[3]);
	ensure("Read didn't overwrite", 'X' == buffer[4]);

	// reposition and check
	len = ba->read(6, buffer, 8);
	ensure("Read length correct.2", 8 == len);
	ensure("Read content correct.3", 'g' == buffer[0] && 'h' == buffer[1]);
	ensure("Read content correct.4", 'A' == buffer[2] && 'B' == buffer[3]);
	ensure("Read content correct.5", 'C' == buffer[4] && 'D' == buffer[5]);
	ensure("Read content correct.6", 'c' == buffer[6] && 'd' == buffer[7]);
	ensure("Read didn't overwrite.7", 'X' == buffer[8]);

	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure(mMemTotal == GetMemTotal());
}

template <> template <>
void BufferArrayTestObjectType::test<5>()
{
	set_test_name("BufferArray multiple writes - sequential reads");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();

	// write some data to the buffer
	char str1[] = "abcdefghij";
	size_t str1_len(strlen(str1));
 	char buffer[256];
	
	size_t len = ba->write(0, str1, str1_len);
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", str1_len == ba->size());

	// again...
	len = ba->write(str1_len, str1, str1_len);
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", (2 * str1_len) == ba->size());

	// read some data back
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(8, buffer, 4);
	ensure("Read length correct", 4 == len);
	ensure("Read content correct", 'i' == buffer[0] && 'j' == buffer[1]);
	ensure("Read content correct.2", 'a' == buffer[2] && 'b' == buffer[3]);
	ensure("Read didn't overwrite", 'X' == buffer[4]);

	// Read some more without repositioning
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(12, buffer, sizeof(buffer));
	ensure("Read length correct", (str1_len - 2) == len);
	ensure("Read content correct.3", 0 == strncmp(buffer, str1+2, str1_len-2));
	ensure("Read didn't overwrite.2", 'X' == buffer[str1_len-1]);
	
	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure(mMemTotal == GetMemTotal());
}

template <> template <>
void BufferArrayTestObjectType::test<6>()
{
	set_test_name("BufferArray overwrite spanning blocks and appending");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();

	// write some data to the buffer
	char str1[] = "abcdefghij";
	size_t str1_len(strlen(str1));
	char str2[] = "ABCDEFGHIJKLMNOPQRST";
	size_t str2_len(strlen(str2));
 	char buffer[256];
	
	size_t len = ba->write(0, str1, str1_len);
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", str1_len == ba->size());

	// again...
	len = ba->write(str1_len, str1, strlen(str1));
	ensure("Wrote length correct", str1_len == len);
	ensure("Recorded size correct", (2 * str1_len) == ba->size());

	// reposition and overwrite
	len = ba->write(8, str2, str2_len);
	ensure("Overwrite length correct", str2_len == len);

	// Leave position and read verifying content
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(8 + str2_len, buffer, 0);
	ensure("Read length correct", 0 == len);
	ensure("Read didn't overwrite", 'X' == buffer[0]);

	// reposition and check
	len = ba->read(0, buffer, sizeof(buffer));
	ensure("Read length correct.2", (str1_len + str2_len - 2) == len);
	ensure("Read content correct", 0 == strncmp(buffer, str1, str1_len-2));
	ensure("Read content correct.2", 0 == strncmp(buffer+str1_len-2, str2, str2_len));
	ensure("Read didn't overwrite.2", 'X' == buffer[str1_len + str2_len - 2]);

	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure("All memory released", mMemTotal == GetMemTotal());
}

template <> template <>
void BufferArrayTestObjectType::test<7>()
{
	set_test_name("BufferArray overwrite spanning blocks and sequential writes");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();

	// write some data to the buffer
	char str1[] = "abcdefghij";
	size_t str1_len(strlen(str1));
	char str2[] = "ABCDEFGHIJKLMNOPQRST";
	size_t str2_len(strlen(str2));
 	char buffer[256];

	// 2x str1
	size_t len = ba->write(0, str1, str1_len);
	len = ba->write(str1_len, str1, str1_len);

	// reposition and overwrite
	len = ba->write(6, str2, 2);
	ensure("Overwrite length correct", 2 == len);

	len = ba->write(8, str2, 2);
	ensure("Overwrite length correct.2", 2 == len);

	len = ba->write(10, str2, 2);
	ensure("Overwrite length correct.3", 2 == len);

	// append some data
	len = ba->append(str2, str2_len);
	ensure("Append length correct", str2_len == len);

	// append some more
	void * out_buf(ba->appendBufferAlloc(str1_len));
	memcpy(out_buf, str1, str1_len);

	// And some final writes
	len = ba->write(3 * str1_len + str2_len, str2, 2);
	ensure("Write length correct.2", 2 == len);
	
	// Check contents
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(0, buffer, sizeof(buffer));
	ensure("Final buffer length correct", (3 * str1_len + str2_len + 2) == len);
	ensure("Read content correct", 0 == strncmp(buffer, str1, 6));
	ensure("Read content correct.2", 0 == strncmp(buffer + 6, str2, 2));
	ensure("Read content correct.3", 0 == strncmp(buffer + 8, str2, 2));
	ensure("Read content correct.4", 0 == strncmp(buffer + 10, str2, 2));
	ensure("Read content correct.5", 0 == strncmp(buffer + str1_len + 2, str1 + 2, str1_len - 2));
	ensure("Read content correct.6", 0 == strncmp(buffer + str1_len + str1_len, str2, str2_len));
	ensure("Read content correct.7", 0 == strncmp(buffer + str1_len + str1_len + str2_len, str1, str1_len));
	ensure("Read content correct.8", 0 == strncmp(buffer + str1_len + str1_len + str2_len + str1_len, str2, 2));
	ensure("Read didn't overwrite", 'X' == buffer[str1_len + str1_len + str2_len + str1_len + 2]);
	
	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure("All memory released", mMemTotal == GetMemTotal());
}

template <> template <>
void BufferArrayTestObjectType::test<8>()
{
	set_test_name("BufferArray zero-length appendBufferAlloc");

	// record the total amount of dynamically allocated memory
	mMemTotal = GetMemTotal();

	// create a new ref counted object with an implicit reference
	BufferArray * ba = new BufferArray();

	// write some data to the buffer
	char str1[] = "abcdefghij";
	size_t str1_len(strlen(str1));
	char str2[] = "ABCDEFGHIJKLMNOPQRST";
	size_t str2_len(strlen(str2));
 	char buffer[256];

	// 2x str1
	size_t len = ba->write(0, str1, str1_len);
	len = ba->write(str1_len, str1, str1_len);

	// zero-length allocate (we allow this with a valid pointer returned)
	void * out_buf(ba->appendBufferAlloc(0));
	ensure("Buffer from zero-length appendBufferAlloc non-NULL", NULL != out_buf);

	// Do it again
	void * out_buf2(ba->appendBufferAlloc(0));
	ensure("Buffer from zero-length appendBufferAlloc non-NULL.2", NULL != out_buf2);
	ensure("Two zero-length appendBufferAlloc buffers distinct", out_buf != out_buf2);

	// And some final writes
	len = ba->write(2 * str1_len, str2, str2_len);
	
	// Check contents
	memset(buffer, 'X', sizeof(buffer));
	len = ba->read(0, buffer, sizeof(buffer));
	ensure("Final buffer length correct", (2 * str1_len + str2_len) == len);
	ensure("Read content correct.1", 0 == strncmp(buffer, str1, str1_len));
	ensure("Read content correct.2", 0 == strncmp(buffer + str1_len, str1, str1_len));
	ensure("Read content correct.3", 0 == strncmp(buffer + str1_len + str1_len, str2, str2_len));
	ensure("Read didn't overwrite", 'X' == buffer[str1_len + str1_len + str2_len]);
	
	// release the implicit reference, causing the object to be released
	ba->release();

	// make sure we didn't leak any memory
	ensure("All memory released", mMemTotal == GetMemTotal());
}

}  // end namespace tut


#endif  // TEST_LLCORE_BUFFER_ARRAY_H_