update dev300-m58
[ooovba.git] / sal / qa / ByteSequence / ByteSequence.cxx
blob0f17f3d1c1865632bd0a61944de953e92a49b520
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ByteSequence.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 #include <Byte_Const.h>
35 #include <rtl/byteseq.h>
37 #include <rtl/byteseq.hxx>
39 #include <cppunit/simpleheader.hxx>
41 using namespace rtl;
43 namespace rtl_ByteSequence
46 //------------------------------------------------------------------------
47 // testing constructors
48 //------------------------------------------------------------------------
50 class ctor : public CppUnit::TestFixture
52 public:
54 void ctor_001()
56 ::rtl::ByteSequence aByteSeq1;
57 ::rtl::ByteSequence aByteSeq2( &kTestEmptyByteSeq );
58 CPPUNIT_ASSERT_MESSAGE
60 "Creates an empty sequence",
61 aByteSeq1.getLength() == 0 &&
62 aByteSeq1 == aByteSeq2
66 void ctor_002()
68 ::rtl::ByteSequence aByteSeq;
69 ::rtl::ByteSequence aByteSeqtmp( aByteSeq );
70 CPPUNIT_ASSERT_MESSAGE
72 "Creates a copy of given sequence",
73 aByteSeq == aByteSeqtmp
78 void ctor_003()
80 ::rtl::ByteSequence aByteSeq( &kTestByteSeq1 );
81 sal_Int32 nNewLen = aByteSeq.getLength();
82 CPPUNIT_ASSERT_MESSAGE
84 "Copy constructor Creates a copy from the C-Handle ",
85 nNewLen == kTestSeqLen1
89 void ctor_003_1()
91 ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 );
92 sal_Int32 nNewLen = aByteSeq.getLength();
93 CPPUNIT_ASSERT_MESSAGE
95 "Copy constructor Creates a copy from the C-Handle: reference count > 1 ",
96 nNewLen == kTestSeqLen2
100 void ctor_004()
102 sal_Int8 * pElements = &kTestByte4;
103 sal_Int32 len = kTestByteCount1;
104 ::rtl::ByteSequence aByteSeq( pElements, len);
105 sal_Int32 nNewLen = aByteSeq.getLength();
106 CPPUNIT_ASSERT_MESSAGE
108 "Creates a copy of given data bytes",
109 aByteSeq[1] == pElements[1] &&
110 len == nNewLen
115 void ctor_005()
117 sal_Int32 len = 50;
118 ::rtl::ByteSequence aByteSeq( len );
119 sal_Int32 nNewLen = aByteSeq.getLength();
120 sal_Bool res = sal_True;
121 for (sal_Int32 i=0; i<len; i++)
123 if (aByteSeq[i] != 0)
124 res = sal_False;
126 CPPUNIT_ASSERT_MESSAGE
128 "Creates sequence of given length and initializes all bytes to 0",
129 nNewLen == len && res
134 void ctor_006()
136 sal_Int32 len = 39;
137 ::rtl::ByteSequence aByteSeq( len , BYTESEQ_NODEFAULT );
138 sal_Int32 nNewLen = aByteSeq.getLength();
139 CPPUNIT_ASSERT_MESSAGE
141 "Creates sequence of given length and does NOT initialize data",
142 nNewLen == len
147 void ctor_007()
149 ::rtl::ByteSequence aByteSeq( &kTestByteSeq3, BYTESEQ_NOACQUIRE );
150 sal_Int32 nNewLen = aByteSeq.getLength();
151 CPPUNIT_ASSERT_MESSAGE
153 "Creates a sequence from a C-Handle without acquiring the handle, thus taking over ownership",
154 nNewLen == kTestSeqLen3
158 CPPUNIT_TEST_SUITE(ctor);
159 CPPUNIT_TEST(ctor_001);
160 CPPUNIT_TEST(ctor_002);
161 CPPUNIT_TEST(ctor_003);
162 CPPUNIT_TEST(ctor_003_1);
163 CPPUNIT_TEST(ctor_004);
164 CPPUNIT_TEST(ctor_005);
165 CPPUNIT_TEST(ctor_006);
166 CPPUNIT_TEST(ctor_007);
167 CPPUNIT_TEST_SUITE_END();
170 class assign : public CppUnit::TestFixture
172 public:
173 // initialise your test code values here.
174 void setUp()
178 void tearDown()
182 // insert your test code here.
183 void assign_001()
185 sal_Int32 len = kTestByteCount1;
186 sal_Int32 len2 = len - 1;
187 sal_Int8 * pElements = &kTestByte;
188 ::rtl::ByteSequence aByteSeq1( kTestByte5, len);
189 ::rtl::ByteSequence aByteSeq2( pElements, len2);
190 aByteSeq2 = aByteSeq1;
191 sal_Int32 nNewLen = aByteSeq2.getLength();
192 CPPUNIT_ASSERT_MESSAGE
194 "Assignment operator: assign longer sequence to another",
195 aByteSeq1 == aByteSeq2 &&
196 nNewLen == len
200 void assign_002()
202 sal_Int32 len = kTestByteCount1 - 1 ;
203 ::rtl::ByteSequence aByteSeq1( len );
204 sal_Int8 * pElements = &kTestByte1;
205 ::rtl::ByteSequence aByteSeq2( pElements, len + 1);
206 aByteSeq2 = aByteSeq1;
207 sal_Int32 nNewLen = aByteSeq2.getLength();
208 CPPUNIT_ASSERT_MESSAGE
210 "Assignment operator: assign shorter sequence to another",
211 aByteSeq1 == aByteSeq2 &&
212 nNewLen == len
216 void assign_003()
218 sal_Int32 len = kTestByteCount1 - 1 ;
219 const sal_Int8 * pElements = &kTestByte2;
220 ::rtl::ByteSequence aByteSeq1( pElements, len + 1 );
221 ::rtl::ByteSequence aByteSeq2( len, BYTESEQ_NODEFAULT );
222 aByteSeq2 = aByteSeq1;
223 sal_Int32 nNewLen = aByteSeq2.getLength();
224 CPPUNIT_ASSERT_MESSAGE
226 "Assignment operator: assign sequence to another sequence having no data initialized",
227 aByteSeq1 == aByteSeq2 &&
228 nNewLen == kTestByteCount1
232 void assign_004()
234 ::rtl::ByteSequence aByteSeq1;
235 sal_Int32 len = kTestByteCount1 ;
236 const sal_Int8 * pElements = &kTestByte;
237 ::rtl::ByteSequence aByteSeq2( pElements, len);
238 aByteSeq2 = aByteSeq1;
239 sal_Int32 nNewLen = aByteSeq2.getLength();
240 CPPUNIT_ASSERT_MESSAGE
242 "Assignment operator: assign empty sequence to another not empty sequence",
243 aByteSeq1 == aByteSeq2 &&
244 nNewLen == 0
248 CPPUNIT_TEST_SUITE(assign);
249 CPPUNIT_TEST(assign_001);
250 CPPUNIT_TEST(assign_002);
251 CPPUNIT_TEST(assign_003);
252 CPPUNIT_TEST(assign_004);
253 CPPUNIT_TEST_SUITE_END();
254 }; // class operator=
257 class equal : public CppUnit::TestFixture
259 public:
260 // initialise your test code values here.
261 void setUp()
265 void tearDown()
269 // insert your test code here.
270 void equal_001()
272 sal_Int32 len = kTestByteCount1 ;
273 sal_Int8 * pElements = &kTestByte;
274 ::rtl::ByteSequence aByteSeq1( pElements, len-1);
275 ::rtl::ByteSequence aByteSeq2( pElements, len);
276 sal_Bool res = aByteSeq1 == aByteSeq2;
277 CPPUNIT_ASSERT_MESSAGE
279 "Equality operator: compare two sequences 1",
280 !res
284 void equal_002()
286 sal_Int32 len = kTestByteCount1 ;
287 const sal_Int8 * pElements = &kTestByte;
288 ::rtl::ByteSequence aByteSeq1( pElements, len);
289 ::rtl::ByteSequence aByteSeq2( pElements, len);
290 sal_Bool res = aByteSeq1 == aByteSeq2;
291 CPPUNIT_ASSERT_MESSAGE
293 "Equality operator: compare two sequences 2",
298 void equal_003()
300 sal_Int32 len = kTestByteCount1 ;
301 const sal_Int8 * pElements = kTestByte5;
302 ::rtl::ByteSequence aByteSeq1( pElements, len);
303 pElements = kTestByte6;
304 ::rtl::ByteSequence aByteSeq2( pElements, len);
305 sal_Bool res = aByteSeq1 == aByteSeq2;
306 CPPUNIT_ASSERT_MESSAGE
308 "Equality operator: compare two sequences 2",
309 !res
312 CPPUNIT_TEST_SUITE(equal);
313 CPPUNIT_TEST(equal_001);
314 CPPUNIT_TEST(equal_002);
315 CPPUNIT_TEST(equal_003);
316 CPPUNIT_TEST_SUITE_END();
317 }; // class equal
320 class notequal : public CppUnit::TestFixture
322 public:
323 // initialise your test code values here.
324 void setUp()
328 void tearDown()
332 // insert your test code here.
333 void notequal_001()
335 sal_Int32 len = kTestByteCount1 ;
336 const sal_Int8 * pElements = &kTestByte;
337 ::rtl::ByteSequence aByteSeq1( pElements, len-1);
338 ::rtl::ByteSequence aByteSeq2( pElements, len);
339 sal_Bool res = aByteSeq1 != aByteSeq2;
340 CPPUNIT_ASSERT_MESSAGE
342 "Equality operator: compare two sequences 1",
347 void notequal_002()
349 sal_Int32 len = kTestByteCount1 ;
350 const sal_Int8 * pElements = &kTestByte;
351 ::rtl::ByteSequence aByteSeq1( pElements, len);
352 ::rtl::ByteSequence aByteSeq2( pElements, len);
353 sal_Bool res = aByteSeq1 != aByteSeq2;
354 CPPUNIT_ASSERT_MESSAGE
356 "Equality operator: compare two sequences 2",
357 !res
361 CPPUNIT_TEST_SUITE(notequal);
362 CPPUNIT_TEST(notequal_001);
363 CPPUNIT_TEST(notequal_002);
364 CPPUNIT_TEST_SUITE_END();
365 }; // class notequal
368 class getArray : public CppUnit::TestFixture
370 public:
371 // initialise your test code values here.
372 void setUp()
376 void tearDown()
380 // insert your test code here.
381 void getArray_001()
383 sal_Int32 len = kTestByteCount1 ;
384 sal_Int8 * pElements = &kTestByte;
385 ::rtl::ByteSequence aByteSeq1( pElements, len);
386 sal_Int8 * pArray = aByteSeq1.getArray();
387 sal_Bool res = sal_True;
388 for (sal_Int32 i = 0; i < len; i++)
390 if (pElements[i] != pArray[i])
391 res = sal_False;
393 CPPUNIT_ASSERT_MESSAGE
395 "Gets the pointer to byte array: one element sequence",
396 res == sal_True
400 void getArray_002()
402 sal_Int8 * pElements = kTestByte6;
403 ::rtl::ByteSequence aByteSeq(pElements, 5);
404 sal_Int8 * pArray = aByteSeq.getArray();
405 sal_Bool res = sal_True;
406 for (sal_Int32 i = 0; i < 5; i++)
408 if (pElements[i] != pArray[i])
409 res = sal_False;
411 CPPUNIT_ASSERT_MESSAGE
413 "Gets the pointer to byte array: more elements sequence",
414 res == sal_True
418 CPPUNIT_TEST_SUITE(getArray);
419 CPPUNIT_TEST(getArray_001);
420 CPPUNIT_TEST(getArray_002);
421 CPPUNIT_TEST_SUITE_END();
422 }; // class getArray
425 class realloc : public CppUnit::TestFixture
427 public:
428 // initialise your test code values here.
429 void setUp()
433 void tearDown()
437 // insert your test code here.
438 void realloc_001()
440 ::rtl::ByteSequence aByteSeq;
441 sal_Int32 nSize = 20;
442 aByteSeq.realloc( nSize );
443 sal_Int32 nNewLen = aByteSeq.getLength();
444 CPPUNIT_ASSERT_MESSAGE
446 "Reallocates sequence to new length: empty sequence",
447 nNewLen == nSize
451 void realloc_002()
453 //reference count > 1
454 ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 ); //34
455 sal_Int32 nSize = 20;
456 aByteSeq.realloc( nSize );
457 sal_Int32 nNewLen = aByteSeq.getLength();
458 CPPUNIT_ASSERT_MESSAGE
460 "Reallocates sequence: reference count > 1 && nSize < nElements",
461 nNewLen == nSize
465 void realloc_003()
467 //reference count > 1
468 ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 ); //34
469 sal_Int32 nSize = kTestSeqLen2 + 5;
470 sal_Int32 nElements = kTestSeqLen2;
471 aByteSeq.realloc( nSize );
472 sal_Int32 nNewLen = aByteSeq.getLength();
473 sal_Bool res = sal_True;
474 for (int i = nSize; i < nElements; i++)
476 sal_Int8 nValue = aByteSeq[i];
477 if (nValue != 0)
478 res = sal_False;
480 CPPUNIT_ASSERT_MESSAGE
482 "Reallocates sequence: reference count > 1 && nSize > nElements",
483 nNewLen == nSize
484 && res == sal_True
488 void realloc_004()
490 sal_Int8 * pElements = &kTestByte3;
491 sal_Int32 len = kTestByteCount3;
492 ::rtl::ByteSequence aByteSeq( pElements, len);
493 sal_Int32 nSize = kTestByteCount3 - 10 ;
494 aByteSeq.realloc( nSize );
495 sal_Int32 nNewLen = aByteSeq.getLength();
496 CPPUNIT_ASSERT_MESSAGE
498 "Reallocates sequence: nSize < nElements",
499 nNewLen == nSize
503 void realloc_005()
505 sal_Int8 * pElements = kTestByte6;
506 sal_Int32 len = 4;
507 ::rtl::ByteSequence aByteSeq( pElements, len);
508 sal_Int32 nSize = len + 10 ;
509 aByteSeq.realloc( nSize );
510 sal_Int32 nNewLen = aByteSeq.getLength();
511 CPPUNIT_ASSERT_MESSAGE
513 "Reallocates sequence: nSize > nElements",
514 nNewLen == nSize
518 CPPUNIT_TEST_SUITE(realloc);
519 CPPUNIT_TEST(realloc_001);
520 CPPUNIT_TEST(realloc_002);
521 CPPUNIT_TEST(realloc_003);
522 CPPUNIT_TEST(realloc_004);
523 CPPUNIT_TEST(realloc_005);
524 //CPPUNIT_TEST(realloc_006);
525 CPPUNIT_TEST_SUITE_END();
526 }; // class realloc
529 class getData : public CppUnit::TestFixture
531 public:
532 // initialise your test code values here.
533 void setUp()
537 void tearDown()
541 // insert your test code here.
542 void getData_001()
544 sal_Int8 * pElements = kTestByte5;
545 ::rtl::ByteSequence aByteSeq(pElements, 4);
546 sal_Bool res = sal_True;
547 if (aByteSeq[0] != kTestByte)
548 res = sal_False;
550 if (aByteSeq[1] != kTestByte1)
551 res = sal_False;
553 if (aByteSeq[2] != kTestByte2)
554 res = sal_False;
556 if (aByteSeq[3] != kTestByte3)
557 res = sal_False;
559 CPPUNIT_ASSERT_MESSAGE
561 "Obtains a reference to byte indexed at given position: empty sequence",
562 res == sal_True
566 void getData_002()
568 ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 );
569 sal_Int8 nValue = aByteSeq[0];
570 CPPUNIT_ASSERT_MESSAGE
572 "Obtains a reference to byte indexed at given position: reference count > 1",
573 nValue == kTestChar2 //not sure what is right,hehe
577 void getData_003()
579 ::rtl::ByteSequence aByteSeq( &kTestByteSeq3 );
580 sal_Int8 nValue = aByteSeq[0];
581 CPPUNIT_ASSERT_MESSAGE
583 "Obtains a reference to byte indexed at given position: reference count = 1",
584 nValue == kTestChar3 //not sure what is right,hehe
588 CPPUNIT_TEST_SUITE(getData);
589 CPPUNIT_TEST(getData_001);
590 CPPUNIT_TEST(getData_002);
591 CPPUNIT_TEST(getData_003);
592 CPPUNIT_TEST_SUITE_END();
593 }; // class getData
595 // -----------------------------------------------------------------------------
596 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::ctor, "rtl_ByteSequence");
597 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::assign, "rtl_ByteSequence");
598 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::equal, "rtl_ByteSequence");
599 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::notequal, "rtl_ByteSequence");
600 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::getArray, "rtl_ByteSequence");
601 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::realloc, "rtl_ByteSequence");
602 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::getData, "rtl_ByteSequence");
603 } // namespace ByteSequence
606 // -----------------------------------------------------------------------------
608 // this macro creates an empty function, which will called by the RegisterAllFunctions()
609 // to let the user the possibility to also register some functions by hand.
610 NOADDITIONAL;