Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
26 : #include "sal/types.h"
27 :
28 : namespace {
29 :
30 84 : class Test: public CppUnit::TestFixture {
31 : public:
32 2 : void test_default() {
33 2 : rtl::ByteSequence s;
34 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
35 2 : }
36 :
37 2 : void test_size0() {
38 2 : rtl::ByteSequence s(sal_Int32(0));
39 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
40 2 : }
41 :
42 2 : void test_size5() {
43 2 : rtl::ByteSequence s(5);
44 2 : sal_Int8 const * p = s.getConstArray();
45 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
46 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
47 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[1]);
48 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[2]);
49 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[3]);
50 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[4]);
51 2 : }
52 :
53 2 : void test_noinit0() {
54 2 : rtl::ByteSequence s(0, rtl::BYTESEQ_NODEFAULT);
55 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
56 2 : }
57 :
58 2 : void test_noinit5() {
59 2 : rtl::ByteSequence s(5, rtl::BYTESEQ_NODEFAULT);
60 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
61 2 : }
62 :
63 2 : void test_elem0() {
64 2 : rtl::ByteSequence s(0, 0);
65 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
66 2 : }
67 :
68 2 : void test_elem5() {
69 2 : sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
70 2 : rtl::ByteSequence s(a, 5);
71 2 : sal_Int8 const * p = s.getConstArray();
72 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
73 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
74 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(1), p[1]);
75 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(2), p[2]);
76 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(3), p[3]);
77 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(4), p[4]);
78 2 : }
79 :
80 2 : void test_copy() {
81 2 : rtl::ByteSequence s1(5);
82 : {
83 2 : rtl::ByteSequence s2(s1);
84 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s2.getLength());
85 2 : CPPUNIT_ASSERT_EQUAL(s1.getConstArray(), s2.getConstArray());
86 2 : CPPUNIT_ASSERT_EQUAL(s1.getHandle(), s2.getHandle());
87 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s1.getHandle()->nRefCount);
88 : }
89 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
90 2 : }
91 :
92 2 : void test_fromC() {
93 2 : sal_Sequence c = { 1, 1, { 0 } };
94 : {
95 2 : rtl::ByteSequence s(&c);
96 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
97 4 : CPPUNIT_ASSERT_EQUAL(
98 : static_cast< void const * >(c.elements),
99 2 : static_cast< void const * >(s.getConstArray()));
100 2 : CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
101 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
102 : }
103 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
104 2 : }
105 :
106 2 : void test_noacquire() {
107 2 : sal_Sequence c = { 2, 1, { 0 } };
108 : {
109 2 : rtl::ByteSequence s(&c, rtl::BYTESEQ_NOACQUIRE);
110 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
111 4 : CPPUNIT_ASSERT_EQUAL(
112 : static_cast< void const * >(c.elements),
113 2 : static_cast< void const * >(s.getConstArray()));
114 2 : CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
115 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
116 : }
117 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
118 2 : }
119 :
120 2 : void test_getArray() {
121 2 : sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
122 2 : rtl::ByteSequence s1(a, 5);
123 2 : rtl::ByteSequence s2(s1);
124 2 : sal_Int8 * p = s2.getArray();
125 2 : p[2] = 10;
126 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
127 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s2.getHandle()->nRefCount);
128 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(2), s1.getConstArray()[2]);
129 2 : CPPUNIT_ASSERT_EQUAL(sal_Int8(10), s2.getConstArray()[2]);
130 2 : }
131 :
132 2 : void test_same0() {
133 2 : rtl::ByteSequence s1;
134 2 : rtl::ByteSequence s2;
135 2 : CPPUNIT_ASSERT_EQUAL(sal_True, s1 == s2);
136 2 : CPPUNIT_ASSERT_EQUAL(sal_True, s2 == s1);
137 2 : CPPUNIT_ASSERT_EQUAL(sal_False, s1 != s2);
138 2 : CPPUNIT_ASSERT_EQUAL(sal_False, s2 != s1);
139 2 : }
140 :
141 2 : void test_diffLen() {
142 2 : sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
143 2 : rtl::ByteSequence s1(a, 5);
144 2 : rtl::ByteSequence s2(a, 4);
145 2 : CPPUNIT_ASSERT_EQUAL(sal_False, s1 == s2);
146 2 : CPPUNIT_ASSERT_EQUAL(sal_False, s2 == s1);
147 2 : CPPUNIT_ASSERT_EQUAL(sal_True, s1 != s2);
148 2 : CPPUNIT_ASSERT_EQUAL(sal_True, s2 != s1);
149 2 : }
150 :
151 2 : void test_diffElem() {
152 2 : sal_Int8 const a1[5] = { 0, 1, 2, 3, 4 };
153 2 : rtl::ByteSequence s1(a1, 5);
154 2 : sal_Int8 const a2[5] = { 0, 1, 10, 3, 4 };
155 2 : rtl::ByteSequence s2(a2, 5);
156 2 : CPPUNIT_ASSERT_EQUAL(sal_False, s1 == s2);
157 2 : CPPUNIT_ASSERT_EQUAL(sal_False, s2 == s1);
158 2 : CPPUNIT_ASSERT_EQUAL(sal_True, s1 != s2);
159 2 : CPPUNIT_ASSERT_EQUAL(sal_True, s2 != s1);
160 2 : }
161 :
162 4 : CPPUNIT_TEST_SUITE(Test);
163 2 : CPPUNIT_TEST(test_default);
164 2 : CPPUNIT_TEST(test_size0);
165 2 : CPPUNIT_TEST(test_size5);
166 2 : CPPUNIT_TEST(test_noinit0);
167 2 : CPPUNIT_TEST(test_noinit5);
168 2 : CPPUNIT_TEST(test_elem0);
169 2 : CPPUNIT_TEST(test_elem5);
170 2 : CPPUNIT_TEST(test_copy);
171 2 : CPPUNIT_TEST(test_fromC);
172 2 : CPPUNIT_TEST(test_noacquire);
173 2 : CPPUNIT_TEST(test_getArray);
174 2 : CPPUNIT_TEST(test_same0);
175 2 : CPPUNIT_TEST(test_diffLen);
176 2 : CPPUNIT_TEST(test_diffElem);
177 4 : CPPUNIT_TEST_SUITE_END();
178 : };
179 :
180 2 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
181 :
182 : }
183 :
184 8 : CPPUNIT_PLUGIN_IMPLEMENT();
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|