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 "gtest/gtest.h"
28 #include "rtl/byteseq.hxx"
29 #include "sal/types.h"
30
31 namespace {
32
33 class Test: public ::testing::Test {
34 public:
35 };
36
TEST_F(Test,test_default)37 TEST_F(Test, test_default) {
38 rtl::ByteSequence s;
39 ASSERT_EQ(sal_Int32(0), s.getLength());
40 }
41
TEST_F(Test,test_size0)42 TEST_F(Test, test_size0) {
43 rtl::ByteSequence s(sal_Int32(0));
44 ASSERT_EQ(sal_Int32(0), s.getLength());
45 }
46
TEST_F(Test,test_size5)47 TEST_F(Test, test_size5) {
48 rtl::ByteSequence s(5);
49 sal_Int8 const * p = s.getConstArray();
50 ASSERT_EQ(sal_Int32(5), s.getLength());
51 ASSERT_EQ(sal_Int8(0), p[0]);
52 ASSERT_EQ(sal_Int8(0), p[1]);
53 ASSERT_EQ(sal_Int8(0), p[2]);
54 ASSERT_EQ(sal_Int8(0), p[3]);
55 ASSERT_EQ(sal_Int8(0), p[4]);
56 }
57
TEST_F(Test,test_noinit0)58 TEST_F(Test, test_noinit0) {
59 rtl::ByteSequence s(0, rtl::BYTESEQ_NODEFAULT);
60 ASSERT_EQ(sal_Int32(0), s.getLength());
61 }
62
TEST_F(Test,test_noinit5)63 TEST_F(Test, test_noinit5) {
64 rtl::ByteSequence s(5, rtl::BYTESEQ_NODEFAULT);
65 ASSERT_EQ(sal_Int32(5), s.getLength());
66 }
67
TEST_F(Test,test_elem0)68 TEST_F(Test, test_elem0) {
69 rtl::ByteSequence s(0, 0);
70 ASSERT_EQ(sal_Int32(0), s.getLength());
71 }
72
TEST_F(Test,test_elem5)73 TEST_F(Test, test_elem5) {
74 sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
75 rtl::ByteSequence s(a, 5);
76 sal_Int8 const * p = s.getConstArray();
77 ASSERT_EQ(sal_Int32(5), s.getLength());
78 ASSERT_EQ(sal_Int8(0), p[0]);
79 ASSERT_EQ(sal_Int8(1), p[1]);
80 ASSERT_EQ(sal_Int8(2), p[2]);
81 ASSERT_EQ(sal_Int8(3), p[3]);
82 ASSERT_EQ(sal_Int8(4), p[4]);
83 }
84
TEST_F(Test,test_copy)85 TEST_F(Test, test_copy) {
86 rtl::ByteSequence s1(5);
87 {
88 rtl::ByteSequence s2(s1);
89 ASSERT_EQ(sal_Int32(5), s2.getLength());
90 ASSERT_EQ(s1.getConstArray(), s2.getConstArray());
91 ASSERT_EQ(s1.getHandle(), s2.getHandle());
92 ASSERT_EQ(sal_Int32(2), s1.getHandle()->nRefCount);
93 }
94 ASSERT_EQ(sal_Int32(1), s1.getHandle()->nRefCount);
95 }
96
TEST_F(Test,test_fromC)97 TEST_F(Test, test_fromC) {
98 sal_Sequence c = { 1, 1, { 0 } };
99 {
100 rtl::ByteSequence s(&c);
101 ASSERT_EQ(sal_Int32(1), s.getLength());
102 ASSERT_EQ(
103 static_cast< void const * >(c.elements),
104 static_cast< void const * >(s.getConstArray()));
105 ASSERT_EQ(&c, s.getHandle());
106 ASSERT_EQ(sal_Int32(2), c.nRefCount);
107 }
108 ASSERT_EQ(sal_Int32(1), c.nRefCount);
109 }
110
TEST_F(Test,test_noacquire)111 TEST_F(Test, test_noacquire) {
112 sal_Sequence c = { 2, 1, { 0 } };
113 {
114 rtl::ByteSequence s(&c, rtl::BYTESEQ_NOACQUIRE);
115 ASSERT_EQ(sal_Int32(1), s.getLength());
116 ASSERT_EQ(
117 static_cast< void const * >(c.elements),
118 static_cast< void const * >(s.getConstArray()));
119 ASSERT_EQ(&c, s.getHandle());
120 ASSERT_EQ(sal_Int32(2), c.nRefCount);
121 }
122 ASSERT_EQ(sal_Int32(1), c.nRefCount);
123 }
124
TEST_F(Test,test_getArray)125 TEST_F(Test, test_getArray) {
126 sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
127 rtl::ByteSequence s1(a, 5);
128 rtl::ByteSequence s2(s1);
129 sal_Int8 * p = s2.getArray();
130 p[2] = 10;
131 ASSERT_EQ(sal_Int32(1), s1.getHandle()->nRefCount);
132 ASSERT_EQ(sal_Int32(1), s2.getHandle()->nRefCount);
133 ASSERT_EQ(sal_Int8(2), s1.getConstArray()[2]);
134 ASSERT_EQ(sal_Int8(10), s2.getConstArray()[2]);
135 }
136
TEST_F(Test,test_same0)137 TEST_F(Test, test_same0) {
138 rtl::ByteSequence s1;
139 rtl::ByteSequence s2;
140 ASSERT_EQ(sal_True, s1 == s2);
141 ASSERT_EQ(sal_True, s2 == s1);
142 ASSERT_EQ(sal_False, s1 != s2);
143 ASSERT_EQ(sal_False, s2 != s1);
144 }
145
TEST_F(Test,test_diffLen)146 TEST_F(Test, test_diffLen) {
147 sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
148 rtl::ByteSequence s1(a, 5);
149 rtl::ByteSequence s2(a, 4);
150 ASSERT_EQ(sal_False, s1 == s2);
151 ASSERT_EQ(sal_False, s2 == s1);
152 ASSERT_EQ(sal_True, s1 != s2);
153 ASSERT_EQ(sal_True, s2 != s1);
154 }
155
TEST_F(Test,test_diffElem)156 TEST_F(Test, test_diffElem) {
157 sal_Int8 const a1[5] = { 0, 1, 2, 3, 4 };
158 rtl::ByteSequence s1(a1, 5);
159 sal_Int8 const a2[5] = { 0, 1, 10, 3, 4 };
160 rtl::ByteSequence s2(a2, 5);
161 ASSERT_EQ(sal_False, s1 == s2);
162 ASSERT_EQ(sal_False, s2 == s1);
163 ASSERT_EQ(sal_True, s1 != s2);
164 ASSERT_EQ(sal_True, s2 != s1);
165 }
166
167
168 }
169
main(int argc,char ** argv)170 int main(int argc, char **argv)
171 {
172 ::testing::InitGoogleTest(&argc, argv);
173 return RUN_ALL_TESTS();
174 }
175
176