1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir *
3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir *
5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir *
7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir *
9*cdf0e10cSrcweir * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir *
11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir *
15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir *
21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir *
26*cdf0e10cSrcweir ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_sal.hxx"
29*cdf0e10cSrcweir #include "sal/config.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "cppunit/TestAssert.h"
32*cdf0e10cSrcweir #include "cppunit/TestFixture.h"
33*cdf0e10cSrcweir #include "cppunit/extensions/HelperMacros.h"
34*cdf0e10cSrcweir #include "cppunit/plugin/TestPlugIn.h"
35*cdf0e10cSrcweir #include "rtl/byteseq.hxx"
36*cdf0e10cSrcweir #include "sal/types.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir class Test: public CppUnit::TestFixture {
41*cdf0e10cSrcweir public:
42*cdf0e10cSrcweir     void test_default() {
43*cdf0e10cSrcweir         rtl::ByteSequence s;
44*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
45*cdf0e10cSrcweir     }
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     void test_size0() {
48*cdf0e10cSrcweir         rtl::ByteSequence s(sal_Int32(0));
49*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
50*cdf0e10cSrcweir     }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     void test_size5() {
53*cdf0e10cSrcweir         rtl::ByteSequence s(5);
54*cdf0e10cSrcweir         sal_Int8 const * p = s.getConstArray();
55*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
56*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
57*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[1]);
58*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[2]);
59*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[3]);
60*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[4]);
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     void test_noinit0() {
64*cdf0e10cSrcweir         rtl::ByteSequence s(0, rtl::BYTESEQ_NODEFAULT);
65*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     void test_noinit5() {
69*cdf0e10cSrcweir         rtl::ByteSequence s(5, rtl::BYTESEQ_NODEFAULT);
70*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     void test_elem0() {
74*cdf0e10cSrcweir         rtl::ByteSequence s(0, 0);
75*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     void test_elem5() {
79*cdf0e10cSrcweir         sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
80*cdf0e10cSrcweir         rtl::ByteSequence s(a, 5);
81*cdf0e10cSrcweir         sal_Int8 const * p = s.getConstArray();
82*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
83*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
84*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(1), p[1]);
85*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(2), p[2]);
86*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(3), p[3]);
87*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(4), p[4]);
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     void test_copy() {
91*cdf0e10cSrcweir         rtl::ByteSequence s1(5);
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             rtl::ByteSequence s2(s1);
94*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s2.getLength());
95*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(s1.getConstArray(), s2.getConstArray());
96*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(s1.getHandle(), s2.getHandle());
97*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s1.getHandle()->nRefCount);
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     void test_fromC() {
103*cdf0e10cSrcweir         sal_Sequence c = { 1, 1, { 0 } };
104*cdf0e10cSrcweir         {
105*cdf0e10cSrcweir             rtl::ByteSequence s(&c);
106*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
107*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(
108*cdf0e10cSrcweir                 static_cast< void const * >(c.elements),
109*cdf0e10cSrcweir                 static_cast< void const * >(s.getConstArray()));
110*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
111*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     void test_noacquire() {
117*cdf0e10cSrcweir         sal_Sequence c = { 2, 1, { 0 } };
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             rtl::ByteSequence s(&c, rtl::BYTESEQ_NOACQUIRE);
120*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
121*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(
122*cdf0e10cSrcweir                 static_cast< void const * >(c.elements),
123*cdf0e10cSrcweir                 static_cast< void const * >(s.getConstArray()));
124*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
125*cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     void test_getArray() {
131*cdf0e10cSrcweir         sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
132*cdf0e10cSrcweir         rtl::ByteSequence s1(a, 5);
133*cdf0e10cSrcweir         rtl::ByteSequence s2(s1);
134*cdf0e10cSrcweir         sal_Int8 * p = s2.getArray();
135*cdf0e10cSrcweir         p[2] = 10;
136*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
137*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s2.getHandle()->nRefCount);
138*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(2), s1.getConstArray()[2]);
139*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_Int8(10), s2.getConstArray()[2]);
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     void test_same0() {
143*cdf0e10cSrcweir         rtl::ByteSequence s1;
144*cdf0e10cSrcweir         rtl::ByteSequence s2;
145*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_True, s1 == s2);
146*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_True, s2 == s1);
147*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_False, s1 != s2);
148*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_False, s2 != s1);
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     void test_diffLen() {
152*cdf0e10cSrcweir         sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
153*cdf0e10cSrcweir         rtl::ByteSequence s1(a, 5);
154*cdf0e10cSrcweir         rtl::ByteSequence s2(a, 4);
155*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_False, s1 == s2);
156*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_False, s2 == s1);
157*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_True, s1 != s2);
158*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_True, s2 != s1);
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     void test_diffElem() {
162*cdf0e10cSrcweir         sal_Int8 const a1[5] = { 0, 1, 2, 3, 4 };
163*cdf0e10cSrcweir         rtl::ByteSequence s1(a1, 5);
164*cdf0e10cSrcweir         sal_Int8 const a2[5] = { 0, 1, 10, 3, 4 };
165*cdf0e10cSrcweir         rtl::ByteSequence s2(a2, 5);
166*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_False, s1 == s2);
167*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_False, s2 == s1);
168*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_True, s1 != s2);
169*cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(sal_True, s2 != s1);
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test);
173*cdf0e10cSrcweir     CPPUNIT_TEST(test_default);
174*cdf0e10cSrcweir     CPPUNIT_TEST(test_size0);
175*cdf0e10cSrcweir     CPPUNIT_TEST(test_size5);
176*cdf0e10cSrcweir     CPPUNIT_TEST(test_noinit0);
177*cdf0e10cSrcweir     CPPUNIT_TEST(test_noinit5);
178*cdf0e10cSrcweir     CPPUNIT_TEST(test_elem0);
179*cdf0e10cSrcweir     CPPUNIT_TEST(test_elem5);
180*cdf0e10cSrcweir     CPPUNIT_TEST(test_copy);
181*cdf0e10cSrcweir     CPPUNIT_TEST(test_fromC);
182*cdf0e10cSrcweir     CPPUNIT_TEST(test_noacquire);
183*cdf0e10cSrcweir     CPPUNIT_TEST(test_getArray);
184*cdf0e10cSrcweir     CPPUNIT_TEST(test_same0);
185*cdf0e10cSrcweir     CPPUNIT_TEST(test_diffLen);
186*cdf0e10cSrcweir     CPPUNIT_TEST(test_diffElem);
187*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
188*cdf0e10cSrcweir };
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(Test);
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir CPPUNIT_PLUGIN_IMPLEMENT();
195