1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_sal.hxx"
25 #include "sal/config.h"
26 
27 #include "cppunit/TestAssert.h"
28 #include "cppunit/TestFixture.h"
29 #include "cppunit/extensions/HelperMacros.h"
30 #include "cppunit/plugin/TestPlugIn.h"
31 #include "rtl/byteseq.hxx"
32 #include "sal/types.h"
33 
34 namespace {
35 
36 class Test: public CppUnit::TestFixture {
37 public:
test_default()38     void test_default() {
39         rtl::ByteSequence s;
40         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
41     }
42 
test_size0()43     void test_size0() {
44         rtl::ByteSequence s(sal_Int32(0));
45         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
46     }
47 
test_size5()48     void test_size5() {
49         rtl::ByteSequence s(5);
50         sal_Int8 const * p = s.getConstArray();
51         CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
52         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
53         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[1]);
54         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[2]);
55         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[3]);
56         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[4]);
57     }
58 
test_noinit0()59     void test_noinit0() {
60         rtl::ByteSequence s(0, rtl::BYTESEQ_NODEFAULT);
61         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
62     }
63 
test_noinit5()64     void test_noinit5() {
65         rtl::ByteSequence s(5, rtl::BYTESEQ_NODEFAULT);
66         CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
67     }
68 
test_elem0()69     void test_elem0() {
70         rtl::ByteSequence s(0, 0);
71         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
72     }
73 
test_elem5()74     void test_elem5() {
75         sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
76         rtl::ByteSequence s(a, 5);
77         sal_Int8 const * p = s.getConstArray();
78         CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
79         CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
80         CPPUNIT_ASSERT_EQUAL(sal_Int8(1), p[1]);
81         CPPUNIT_ASSERT_EQUAL(sal_Int8(2), p[2]);
82         CPPUNIT_ASSERT_EQUAL(sal_Int8(3), p[3]);
83         CPPUNIT_ASSERT_EQUAL(sal_Int8(4), p[4]);
84     }
85 
test_copy()86     void test_copy() {
87         rtl::ByteSequence s1(5);
88         {
89             rtl::ByteSequence s2(s1);
90             CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s2.getLength());
91             CPPUNIT_ASSERT_EQUAL(s1.getConstArray(), s2.getConstArray());
92             CPPUNIT_ASSERT_EQUAL(s1.getHandle(), s2.getHandle());
93             CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s1.getHandle()->nRefCount);
94         }
95         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
96     }
97 
test_fromC()98     void test_fromC() {
99         sal_Sequence c = { 1, 1, { 0 } };
100         {
101             rtl::ByteSequence s(&c);
102             CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
103             CPPUNIT_ASSERT_EQUAL(
104                 static_cast< void const * >(c.elements),
105                 static_cast< void const * >(s.getConstArray()));
106             CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
107             CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
108         }
109         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
110     }
111 
test_noacquire()112     void test_noacquire() {
113         sal_Sequence c = { 2, 1, { 0 } };
114         {
115             rtl::ByteSequence s(&c, rtl::BYTESEQ_NOACQUIRE);
116             CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
117             CPPUNIT_ASSERT_EQUAL(
118                 static_cast< void const * >(c.elements),
119                 static_cast< void const * >(s.getConstArray()));
120             CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
121             CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
122         }
123         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
124     }
125 
test_getArray()126     void test_getArray() {
127         sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
128         rtl::ByteSequence s1(a, 5);
129         rtl::ByteSequence s2(s1);
130         sal_Int8 * p = s2.getArray();
131         p[2] = 10;
132         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
133         CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s2.getHandle()->nRefCount);
134         CPPUNIT_ASSERT_EQUAL(sal_Int8(2), s1.getConstArray()[2]);
135         CPPUNIT_ASSERT_EQUAL(sal_Int8(10), s2.getConstArray()[2]);
136     }
137 
test_same0()138     void test_same0() {
139         rtl::ByteSequence s1;
140         rtl::ByteSequence s2;
141         CPPUNIT_ASSERT_EQUAL(sal_True, s1 == s2);
142         CPPUNIT_ASSERT_EQUAL(sal_True, s2 == s1);
143         CPPUNIT_ASSERT_EQUAL(sal_False, s1 != s2);
144         CPPUNIT_ASSERT_EQUAL(sal_False, s2 != s1);
145     }
146 
test_diffLen()147     void test_diffLen() {
148         sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
149         rtl::ByteSequence s1(a, 5);
150         rtl::ByteSequence s2(a, 4);
151         CPPUNIT_ASSERT_EQUAL(sal_False, s1 == s2);
152         CPPUNIT_ASSERT_EQUAL(sal_False, s2 == s1);
153         CPPUNIT_ASSERT_EQUAL(sal_True, s1 != s2);
154         CPPUNIT_ASSERT_EQUAL(sal_True, s2 != s1);
155     }
156 
test_diffElem()157     void test_diffElem() {
158         sal_Int8 const a1[5] = { 0, 1, 2, 3, 4 };
159         rtl::ByteSequence s1(a1, 5);
160         sal_Int8 const a2[5] = { 0, 1, 10, 3, 4 };
161         rtl::ByteSequence s2(a2, 5);
162         CPPUNIT_ASSERT_EQUAL(sal_False, s1 == s2);
163         CPPUNIT_ASSERT_EQUAL(sal_False, s2 == s1);
164         CPPUNIT_ASSERT_EQUAL(sal_True, s1 != s2);
165         CPPUNIT_ASSERT_EQUAL(sal_True, s2 != s1);
166     }
167 
168     CPPUNIT_TEST_SUITE(Test);
169     CPPUNIT_TEST(test_default);
170     CPPUNIT_TEST(test_size0);
171     CPPUNIT_TEST(test_size5);
172     CPPUNIT_TEST(test_noinit0);
173     CPPUNIT_TEST(test_noinit5);
174     CPPUNIT_TEST(test_elem0);
175     CPPUNIT_TEST(test_elem5);
176     CPPUNIT_TEST(test_copy);
177     CPPUNIT_TEST(test_fromC);
178     CPPUNIT_TEST(test_noacquire);
179     CPPUNIT_TEST(test_getArray);
180     CPPUNIT_TEST(test_same0);
181     CPPUNIT_TEST(test_diffLen);
182     CPPUNIT_TEST(test_diffElem);
183     CPPUNIT_TEST_SUITE_END();
184 };
185 
186 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
187 
188 }
189 
190 CPPUNIT_PLUGIN_IMPLEMENT();
191