summaryrefslogtreecommitdiff
path: root/sal/qa/ByteSequence/ByteSequence.cxx
blob: e74d43b2830ffe9837c947857d5579f08dc50fd5 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

#ifndef _BYTE_CONST_H_
#include <Byte_Const.h>
#endif

#ifndef _RTL_BYTESEQ_H_
#include <rtl/byteseq.h>
#endif

#include <rtl/byteseq.hxx>

#include <cppunit/simpleheader.hxx>

using namespace rtl;

namespace rtl_ByteSequence
{

//------------------------------------------------------------------------
// testing constructors
//------------------------------------------------------------------------

class  ctor : public CppUnit::TestFixture
    {
    public:
        
        void ctor_001()
        {
            ::rtl::ByteSequence aByteSeq1;
        ::rtl::ByteSequence aByteSeq2( &kTestEmptyByteSeq );
            CPPUNIT_ASSERT_MESSAGE
            (
                "Creates an empty sequence",
                aByteSeq1.getLength() == 0 && 
                aByteSeq1 == aByteSeq2
            );
        }
        
        void ctor_002()
        {
            ::rtl::ByteSequence aByteSeq;
            ::rtl::ByteSequence aByteSeqtmp( aByteSeq );
            CPPUNIT_ASSERT_MESSAGE
            (
                "Creates a copy of given sequence",
                aByteSeq == aByteSeqtmp 
            );
         
        }
        
        void ctor_003()
        {
            ::rtl::ByteSequence aByteSeq( &kTestByteSeq1 );
        sal_Int32 nNewLen = aByteSeq.getLength();
            CPPUNIT_ASSERT_MESSAGE
            (
                "Copy constructor Creates a copy from the C-Handle ",
                nNewLen == kTestSeqLen1
        );
        }
    
    void ctor_003_1()
        {
            ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 );
        sal_Int32 nNewLen = aByteSeq.getLength();
            CPPUNIT_ASSERT_MESSAGE
            (
                "Copy constructor Creates a copy from the C-Handle: reference count > 1 ",
                nNewLen == kTestSeqLen2 
        );
        }

        void ctor_004()
        {
            sal_Int8 * pElements = &kTestByte4;
        sal_Int32 len = kTestByteCount1;
        ::rtl::ByteSequence aByteSeq( pElements, len);
        sal_Int32 nNewLen = aByteSeq.getLength();
            CPPUNIT_ASSERT_MESSAGE
            (
                "Creates a copy of given data bytes",
                aByteSeq[1] == pElements[1] &&
                len == nNewLen
                         
            );
        }
        
    void ctor_005()
        {
        sal_Int32 len = 50;
            ::rtl::ByteSequence aByteSeq( len );
        sal_Int32 nNewLen = aByteSeq.getLength();
        sal_Bool res = sal_True;
        for (sal_Int32 i=0; i<len; i++)
        {
                if (aByteSeq[i] != 0)
                    res = sal_False;
            }
            CPPUNIT_ASSERT_MESSAGE
            (
                "Creates sequence of given length and initializes all bytes to 0",
                nNewLen == len && res
                
            );
        }

        void ctor_006()
        {
        sal_Int32 len = 39;
            ::rtl::ByteSequence aByteSeq( len , BYTESEQ_NODEFAULT );
            sal_Int32 nNewLen = aByteSeq.getLength();
            CPPUNIT_ASSERT_MESSAGE
            (
                "Creates sequence of given length and does NOT initialize data",
                nNewLen == len

            );
        }

        void ctor_007()
        {
            ::rtl::ByteSequence aByteSeq( &kTestByteSeq3, BYTESEQ_NOACQUIRE );
        sal_Int32 nNewLen = aByteSeq.getLength();
            CPPUNIT_ASSERT_MESSAGE
            (
                "Creates a sequence from a C-Handle without acquiring the handle, thus taking over ownership",
                nNewLen == kTestSeqLen3		            
            );
        }
        
        CPPUNIT_TEST_SUITE(ctor);
        CPPUNIT_TEST(ctor_001);
        CPPUNIT_TEST(ctor_002);
        CPPUNIT_TEST(ctor_003);
    CPPUNIT_TEST(ctor_003_1);
        CPPUNIT_TEST(ctor_004);
        CPPUNIT_TEST(ctor_005);
        CPPUNIT_TEST(ctor_006);
        CPPUNIT_TEST(ctor_007);
        CPPUNIT_TEST_SUITE_END();
    };

class assign : public CppUnit::TestFixture
{
public:
    // initialise your test code values here.
    void setUp()
    {
    }

    void tearDown()
    {
    }

    // insert your test code here.
    void assign_001()
    {
    sal_Int32 len = kTestByteCount1;
    sal_Int32 len2 = len - 1;
    sal_Int8 * pElements = &kTestByte;
    ::rtl::ByteSequence aByteSeq1( kTestByte5, len);
    ::rtl::ByteSequence aByteSeq2( pElements, len2);
    aByteSeq2 = aByteSeq1;
    sal_Int32 nNewLen = aByteSeq2.getLength();
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Assignment operator: assign longer sequence to another",
            aByteSeq1 == aByteSeq2 && 
            nNewLen == len	    
        );
    }

    void assign_002()
    {
        sal_Int32 len = kTestByteCount1 - 1 ;
        ::rtl::ByteSequence aByteSeq1( len );
    sal_Int8 * pElements = &kTestByte1;
    ::rtl::ByteSequence aByteSeq2( pElements, len + 1);
    aByteSeq2 = aByteSeq1;
    sal_Int32 nNewLen = aByteSeq2.getLength();
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Assignment operator: assign shorter sequence to another",
            aByteSeq1 == aByteSeq2 && 
            nNewLen == len
        );
    }
    
    void assign_003()
    {
        sal_Int32 len = kTestByteCount1 - 1 ;
    const sal_Int8 * pElements = &kTestByte2;
        ::rtl::ByteSequence aByteSeq1( pElements, len + 1 );
    ::rtl::ByteSequence aByteSeq2( len, BYTESEQ_NODEFAULT );
    aByteSeq2 = aByteSeq1;
    sal_Int32 nNewLen = aByteSeq2.getLength();
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Assignment operator: assign sequence to another sequence having no data initialized",
            aByteSeq1 == aByteSeq2 && 
            nNewLen == kTestByteCount1	    
        );
    }

    void assign_004()
    {
        ::rtl::ByteSequence aByteSeq1;
    sal_Int32 len = kTestByteCount1 ;
    const sal_Int8 * pElements = &kTestByte;
    ::rtl::ByteSequence aByteSeq2( pElements, len);
    aByteSeq2 = aByteSeq1;
    sal_Int32 nNewLen = aByteSeq2.getLength();
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Assignment operator: assign empty sequence to another not empty sequence",
            aByteSeq1 == aByteSeq2 && 
        nNewLen == 0	    
        );
    }

    CPPUNIT_TEST_SUITE(assign);
    CPPUNIT_TEST(assign_001);
    CPPUNIT_TEST(assign_002);
    CPPUNIT_TEST(assign_003);
    CPPUNIT_TEST(assign_004);
    CPPUNIT_TEST_SUITE_END();
}; // class operator=


class equal : public CppUnit::TestFixture
{
public:
    // initialise your test code values here.
    void setUp()
    {
    }

    void tearDown()
    {
    }

    // insert your test code here.
    void equal_001()
    {
    sal_Int32 len = kTestByteCount1 ;
        sal_Int8 * pElements = &kTestByte;
    ::rtl::ByteSequence aByteSeq1( pElements, len-1);
    ::rtl::ByteSequence aByteSeq2( pElements, len);
    sal_Bool res = aByteSeq1 == aByteSeq2;
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Equality operator: compare two sequences 1",
            !res
        );
    }

    void equal_002()
    {
        sal_Int32 len = kTestByteCount1 ;
        const sal_Int8 * pElements = &kTestByte;
    ::rtl::ByteSequence aByteSeq1( pElements, len);
    ::rtl::ByteSequence aByteSeq2( pElements, len);
    sal_Bool res = aByteSeq1 == aByteSeq2;
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Equality operator: compare two sequences 2",
            res
        );
    }

    void equal_003()
    {
        sal_Int32 len = kTestByteCount1 ;
        const sal_Int8 * pElements = kTestByte5;
    ::rtl::ByteSequence aByteSeq1( pElements, len);
    pElements = kTestByte6;
    ::rtl::ByteSequence aByteSeq2( pElements, len);
    sal_Bool res = aByteSeq1 == aByteSeq2;
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Equality operator: compare two sequences 2",
            !res
        );
    }
    CPPUNIT_TEST_SUITE(equal);
    CPPUNIT_TEST(equal_001);
    CPPUNIT_TEST(equal_002);
    CPPUNIT_TEST(equal_003);
    CPPUNIT_TEST_SUITE_END();
}; // class equal


class notequal : public CppUnit::TestFixture
{
public:
    // initialise your test code values here.
    void setUp()
    {
    }

    void tearDown()
    {
    }

    // insert your test code here.
    void notequal_001()
    {
        sal_Int32 len = kTestByteCount1 ;
        const sal_Int8 * pElements = &kTestByte;
    ::rtl::ByteSequence aByteSeq1( pElements, len-1);
    ::rtl::ByteSequence aByteSeq2( pElements, len);
    sal_Bool res = aByteSeq1 != aByteSeq2;
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Equality operator: compare two sequences 1",
            res
        );
    }

    void notequal_002()
    {
        sal_Int32 len = kTestByteCount1 ;
        const sal_Int8 * pElements = &kTestByte;
    ::rtl::ByteSequence aByteSeq1( pElements, len);
    ::rtl::ByteSequence aByteSeq2( pElements, len);
    sal_Bool res = aByteSeq1 != aByteSeq2;
        CPPUNIT_ASSERT_MESSAGE
        (   
            "Equality operator: compare two sequences 2",
            !res
        );
    }

    CPPUNIT_TEST_SUITE(notequal);
    CPPUNIT_TEST(notequal_001);
    CPPUNIT_TEST(notequal_002);
    CPPUNIT_TEST_SUITE_END();
}; // class notequal


class getArray : public CppUnit::TestFixture
{
public:
    // initialise your test code values here.
    void setUp()
    {
    }

    void tearDown()
    {
    }

    // insert your test code here.
    void getArray_001()
    {
        sal_Int32 len = kTestByteCount1 ;
        sal_Int8 * pElements = &kTestByte;
    ::rtl::ByteSequence aByteSeq1( pElements, len);
    sal_Int8 * pArray = aByteSeq1.getArray();
    sal_Bool res = sal_True;
    for (sal_Int32 i = 0; i < len; i++)
    {
        if (pElements[i] != pArray[i])
        res = sal_False;
    }
    CPPUNIT_ASSERT_MESSAGE
        (   
            "Gets the pointer to byte array: one element sequence",
        res == sal_True
        );
    }

    void getArray_002()
    {
    sal_Int8 * pElements = kTestByte6;
        ::rtl::ByteSequence aByteSeq(pElements, 5);
    sal_Int8 * pArray = aByteSeq.getArray();
    sal_Bool res = sal_True;
    for (sal_Int32 i = 0; i < 5; i++)
    {
        if (pElements[i] != pArray[i])
        res = sal_False;
    }
    CPPUNIT_ASSERT_MESSAGE
        (        
            "Gets the pointer to byte array: more elements sequence",
            res == sal_True
        );
    }

    CPPUNIT_TEST_SUITE(getArray);
    CPPUNIT_TEST(getArray_001);
    CPPUNIT_TEST(getArray_002);
    CPPUNIT_TEST_SUITE_END();
}; // class getArray


class realloc : public CppUnit::TestFixture
{
public:
    // initialise your test code values here.
    void setUp()
    {
    }

    void tearDown()
    {
    }

    // insert your test code here.
    void realloc_001()
    {
        ::rtl::ByteSequence aByteSeq;
    sal_Int32 nSize = 20;
    aByteSeq.realloc( nSize );
    sal_Int32 nNewLen = aByteSeq.getLength();
        CPPUNIT_ASSERT_MESSAGE
        (        
            "Reallocates sequence to new length: empty sequence",
            nNewLen == nSize 
        );
    }

    void realloc_002()
    {
    //reference count > 1
    ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 );  //34
    sal_Int32 nSize = 20;
    aByteSeq.realloc( nSize );
    sal_Int32 nNewLen = aByteSeq.getLength();
    CPPUNIT_ASSERT_MESSAGE
        (        
            "Reallocates sequence: reference count > 1 && nSize < nElements",
            nNewLen == nSize 
        );
    }
    
    void realloc_003()
    {
    //reference count > 1
    ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 );  //34
    sal_Int32 nSize = kTestSeqLen2 + 5;
    sal_Int32 nElements = kTestSeqLen2;
    aByteSeq.realloc( nSize );
    sal_Int32 nNewLen = aByteSeq.getLength();
    sal_Bool res = sal_True;
    for (int i = nSize; i < nElements; i++)
    {
            sal_Int8 nValue = aByteSeq[i];
        if (nValue != 0)
                res = sal_False;
        }
    CPPUNIT_ASSERT_MESSAGE
        (        
            "Reallocates sequence: reference count > 1 && nSize > nElements",
            nNewLen == nSize 
        && res == sal_True
        );
    }

    void realloc_004()
    {
    sal_Int8 * pElements = &kTestByte3;
    sal_Int32 len = kTestByteCount3;
    ::rtl::ByteSequence aByteSeq( pElements, len);
    sal_Int32 nSize = kTestByteCount3 - 10 ;
    aByteSeq.realloc( nSize );
    sal_Int32 nNewLen = aByteSeq.getLength();
    CPPUNIT_ASSERT_MESSAGE
        (        
            "Reallocates sequence: nSize < nElements",
            nNewLen == nSize 
        );
    }
    
    void realloc_005()
    {
    sal_Int8 * pElements = kTestByte6;
    sal_Int32 len = 4;
    ::rtl::ByteSequence aByteSeq( pElements, len);
    sal_Int32 nSize = len + 10 ;
    aByteSeq.realloc( nSize );
    sal_Int32 nNewLen = aByteSeq.getLength();
    CPPUNIT_ASSERT_MESSAGE
        (        
            "Reallocates sequence: nSize > nElements",
            nNewLen == nSize 
        );
    }
    
    CPPUNIT_TEST_SUITE(realloc);
    CPPUNIT_TEST(realloc_001);
    CPPUNIT_TEST(realloc_002);
    CPPUNIT_TEST(realloc_003);
    CPPUNIT_TEST(realloc_004);
    CPPUNIT_TEST(realloc_005);
    //CPPUNIT_TEST(realloc_006);
    CPPUNIT_TEST_SUITE_END();
}; // class realloc


class getData : public CppUnit::TestFixture
{
public:
    // initialise your test code values here.
    void setUp()
    {
    }

    void tearDown()
    {
    }

    // insert your test code here.
    void getData_001()
    {
    sal_Int8 * pElements = kTestByte5;
        ::rtl::ByteSequence aByteSeq(pElements, 4);
    sal_Bool res = sal_True;
    if (aByteSeq[0] != kTestByte)
            res = sal_False;
    
        if (aByteSeq[1] != kTestByte1)
            res = sal_False;
      
    if (aByteSeq[2] != kTestByte2)
            res = sal_False;

    if (aByteSeq[3] != kTestByte3)
            res = sal_False;

    CPPUNIT_ASSERT_MESSAGE
        (        
            "Obtains a reference to byte indexed at given position: empty sequence",
            res == sal_True
        );
    }

    void getData_002()
    {
        ::rtl::ByteSequence aByteSeq( &kTestByteSeq2 );
    sal_Int8 nValue = aByteSeq[0];
        CPPUNIT_ASSERT_MESSAGE
        (
            "Obtains a reference to byte indexed at given position: reference count > 1",
            nValue == kTestChar2 //not sure what is right,hehe
    );
    }

    void getData_003()
    {
        ::rtl::ByteSequence aByteSeq( &kTestByteSeq3 );
    sal_Int8 nValue = aByteSeq[0];
        CPPUNIT_ASSERT_MESSAGE
        (
            "Obtains a reference to byte indexed at given position: reference count = 1",
            nValue == kTestChar3 //not sure what is right,hehe
    );
    }

    CPPUNIT_TEST_SUITE(getData);
    CPPUNIT_TEST(getData_001);
    CPPUNIT_TEST(getData_002);
    CPPUNIT_TEST(getData_003);
    CPPUNIT_TEST_SUITE_END();
}; // class getData

// -----------------------------------------------------------------------------
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::ctor, "rtl_ByteSequence");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::assign, "rtl_ByteSequence");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::equal, "rtl_ByteSequence");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::notequal, "rtl_ByteSequence");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::getArray, "rtl_ByteSequence");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::realloc, "rtl_ByteSequence");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_ByteSequence::getData, "rtl_ByteSequence");
} // namespace ByteSequence


// -----------------------------------------------------------------------------

// this macro creates an empty function, which will called by the RegisterAllFunctions()
// to let the user the possibility to also register some functions by hand.
NOADDITIONAL;