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