1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ByteSequence.cxx,v $
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>
43 namespace rtl_ByteSequence
46 //------------------------------------------------------------------------
47 // testing constructors
48 //------------------------------------------------------------------------
50 class ctor
: public CppUnit::TestFixture
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
68 ::rtl::ByteSequence aByteSeq
;
69 ::rtl::ByteSequence
aByteSeqtmp( aByteSeq
);
70 CPPUNIT_ASSERT_MESSAGE
72 "Creates a copy of given sequence",
73 aByteSeq
== aByteSeqtmp
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
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
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] &&
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)
126 CPPUNIT_ASSERT_MESSAGE
128 "Creates sequence of given length and initializes all bytes to 0",
129 nNewLen
== len
&& res
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",
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
173 // initialise your test code values here.
182 // insert your test code here.
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
&&
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
&&
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
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
&&
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
260 // initialise your test code values here.
269 // insert your test code here.
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",
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",
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",
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();
320 class notequal
: public CppUnit::TestFixture
323 // initialise your test code values here.
332 // insert your test code here.
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",
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",
361 CPPUNIT_TEST_SUITE(notequal
);
362 CPPUNIT_TEST(notequal_001
);
363 CPPUNIT_TEST(notequal_002
);
364 CPPUNIT_TEST_SUITE_END();
368 class getArray
: public CppUnit::TestFixture
371 // initialise your test code values here.
380 // insert your test code here.
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
])
393 CPPUNIT_ASSERT_MESSAGE
395 "Gets the pointer to byte array: one element sequence",
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
])
411 CPPUNIT_ASSERT_MESSAGE
413 "Gets the pointer to byte array: more elements sequence",
418 CPPUNIT_TEST_SUITE(getArray
);
419 CPPUNIT_TEST(getArray_001
);
420 CPPUNIT_TEST(getArray_002
);
421 CPPUNIT_TEST_SUITE_END();
425 class realloc
: public CppUnit::TestFixture
428 // initialise your test code values here.
437 // insert your test code here.
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",
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",
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
];
480 CPPUNIT_ASSERT_MESSAGE
482 "Reallocates sequence: reference count > 1 && nSize > nElements",
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",
505 sal_Int8
* pElements
= kTestByte6
;
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",
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();
529 class getData
: public CppUnit::TestFixture
532 // initialise your test code values here.
541 // insert your test code here.
544 sal_Int8
* pElements
= kTestByte5
;
545 ::rtl::ByteSequence
aByteSeq(pElements
, 4);
546 sal_Bool res
= sal_True
;
547 if (aByteSeq
[0] != kTestByte
)
550 if (aByteSeq
[1] != kTestByte1
)
553 if (aByteSeq
[2] != kTestByte2
)
556 if (aByteSeq
[3] != kTestByte3
)
559 CPPUNIT_ASSERT_MESSAGE
561 "Obtains a reference to byte indexed at given position: empty sequence",
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
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();
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.