1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <cppunit/TestAssert.h>
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/plugin/TestPlugIn.h>
25 #include <rtl/byteseq.hxx>
29 class Test
: public CppUnit::TestFixture
{
33 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s
.getLength());
37 rtl::ByteSequence
s(sal_Int32(0));
38 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s
.getLength());
42 rtl::ByteSequence
s(5);
43 sal_Int8
const * p
= s
.getConstArray();
44 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s
.getLength());
45 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p
[0]);
46 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p
[1]);
47 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p
[2]);
48 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p
[3]);
49 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p
[4]);
53 rtl::ByteSequence
s(0, rtl::BYTESEQ_NODEFAULT
);
54 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s
.getLength());
58 rtl::ByteSequence
s(5, rtl::BYTESEQ_NODEFAULT
);
59 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s
.getLength());
63 rtl::ByteSequence
s(nullptr, 0);
64 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s
.getLength());
68 sal_Int8
const a
[5] = { 0, 1, 2, 3, 4 };
69 rtl::ByteSequence
s(a
, 5);
70 sal_Int8
const * p
= s
.getConstArray();
71 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s
.getLength());
72 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p
[0]);
73 CPPUNIT_ASSERT_EQUAL(sal_Int8(1), p
[1]);
74 CPPUNIT_ASSERT_EQUAL(sal_Int8(2), p
[2]);
75 CPPUNIT_ASSERT_EQUAL(sal_Int8(3), p
[3]);
76 CPPUNIT_ASSERT_EQUAL(sal_Int8(4), p
[4]);
80 rtl::ByteSequence
s1(5);
82 rtl::ByteSequence
s2(s1
);
83 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s2
.getLength());
84 CPPUNIT_ASSERT_EQUAL(s1
.getConstArray(), s2
.getConstArray());
85 CPPUNIT_ASSERT_EQUAL(s1
.getHandle(), s2
.getHandle());
86 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s1
.getHandle()->nRefCount
);
88 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1
.getHandle()->nRefCount
);
92 sal_Sequence c
= { 1, 1, { 0 } };
94 rtl::ByteSequence
s(&c
);
95 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s
.getLength());
97 static_cast< void const * >(c
.elements
),
98 static_cast< void const * >(s
.getConstArray()));
99 CPPUNIT_ASSERT_EQUAL(&c
, s
.getHandle());
100 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c
.nRefCount
);
102 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c
.nRefCount
);
105 void test_noacquire() {
106 sal_Sequence c
= { 2, 1, { 0 } };
108 rtl::ByteSequence
s(&c
, rtl::BYTESEQ_NOACQUIRE
);
109 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s
.getLength());
110 CPPUNIT_ASSERT_EQUAL(
111 static_cast< void const * >(c
.elements
),
112 static_cast< void const * >(s
.getConstArray()));
113 CPPUNIT_ASSERT_EQUAL(&c
, s
.getHandle());
114 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c
.nRefCount
);
116 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c
.nRefCount
);
119 void test_getArray() {
120 sal_Int8
const a
[5] = { 0, 1, 2, 3, 4 };
121 rtl::ByteSequence
s1(a
, 5);
122 rtl::ByteSequence
s2(s1
);
123 sal_Int8
* p
= s2
.getArray();
125 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1
.getHandle()->nRefCount
);
126 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s2
.getHandle()->nRefCount
);
127 CPPUNIT_ASSERT_EQUAL(sal_Int8(2), s1
.getConstArray()[2]);
128 CPPUNIT_ASSERT_EQUAL(sal_Int8(10), s2
.getConstArray()[2]);
132 rtl::ByteSequence s1
;
133 rtl::ByteSequence s2
;
134 CPPUNIT_ASSERT_EQUAL(true, s1
== s2
);
135 CPPUNIT_ASSERT_EQUAL(true, s2
== s1
);
136 CPPUNIT_ASSERT_EQUAL(false, s1
!= s2
);
137 CPPUNIT_ASSERT_EQUAL(false, s2
!= s1
);
140 void test_diffLen() {
141 sal_Int8
const a
[5] = { 0, 1, 2, 3, 4 };
142 rtl::ByteSequence
s1(a
, 5);
143 rtl::ByteSequence
s2(a
, 4);
144 CPPUNIT_ASSERT_EQUAL(false, s1
== s2
);
145 CPPUNIT_ASSERT_EQUAL(false, s2
== s1
);
146 CPPUNIT_ASSERT_EQUAL(true, s1
!= s2
);
147 CPPUNIT_ASSERT_EQUAL(true, s2
!= s1
);
150 void test_diffElem() {
151 sal_Int8
const a1
[5] = { 0, 1, 2, 3, 4 };
152 rtl::ByteSequence
s1(a1
, 5);
153 sal_Int8
const a2
[5] = { 0, 1, 10, 3, 4 };
154 rtl::ByteSequence
s2(a2
, 5);
155 CPPUNIT_ASSERT_EQUAL(false, s1
== s2
);
156 CPPUNIT_ASSERT_EQUAL(false, s2
== s1
);
157 CPPUNIT_ASSERT_EQUAL(true, s1
!= s2
);
158 CPPUNIT_ASSERT_EQUAL(true, s2
!= s1
);
161 CPPUNIT_TEST_SUITE(Test
);
162 CPPUNIT_TEST(test_default
);
163 CPPUNIT_TEST(test_size0
);
164 CPPUNIT_TEST(test_size5
);
165 CPPUNIT_TEST(test_noinit0
);
166 CPPUNIT_TEST(test_noinit5
);
167 CPPUNIT_TEST(test_elem0
);
168 CPPUNIT_TEST(test_elem5
);
169 CPPUNIT_TEST(test_copy
);
170 CPPUNIT_TEST(test_fromC
);
171 CPPUNIT_TEST(test_noacquire
);
172 CPPUNIT_TEST(test_getArray
);
173 CPPUNIT_TEST(test_same0
);
174 CPPUNIT_TEST(test_diffLen
);
175 CPPUNIT_TEST(test_diffElem
);
176 CPPUNIT_TEST_SUITE_END();
179 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */