Fix GNU C++ version check
[LibreOffice.git] / sal / qa / ByteSequence / ByteSequence.cxx
blob350dd383a54286cbe04830687d8da701ca996086
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <rtl/byteseq.hxx>
26 namespace {
28 class Test: public CppUnit::TestFixture {
29 public:
30 void test_default() {
31 rtl::ByteSequence s;
32 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
35 void test_size0() {
36 rtl::ByteSequence s(sal_Int32(0));
37 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
40 void test_size5() {
41 rtl::ByteSequence s(5);
42 sal_Int8 const * p = s.getConstArray();
43 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
44 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
45 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[1]);
46 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[2]);
47 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[3]);
48 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[4]);
51 void test_noinit0() {
52 rtl::ByteSequence s(0, rtl::BYTESEQ_NODEFAULT);
53 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
56 void test_noinit5() {
57 rtl::ByteSequence s(5, rtl::BYTESEQ_NODEFAULT);
58 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
61 void test_elem0() {
62 rtl::ByteSequence s(nullptr, 0);
63 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
66 void test_elem5() {
67 sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
68 rtl::ByteSequence s(a, 5);
69 sal_Int8 const * p = s.getConstArray();
70 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
71 CPPUNIT_ASSERT_EQUAL(sal_Int8(0), p[0]);
72 CPPUNIT_ASSERT_EQUAL(sal_Int8(1), p[1]);
73 CPPUNIT_ASSERT_EQUAL(sal_Int8(2), p[2]);
74 CPPUNIT_ASSERT_EQUAL(sal_Int8(3), p[3]);
75 CPPUNIT_ASSERT_EQUAL(sal_Int8(4), p[4]);
78 void test_copy() {
79 rtl::ByteSequence s1(5);
81 rtl::ByteSequence s2(s1);
82 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s2.getLength());
83 CPPUNIT_ASSERT_EQUAL(s1.getConstArray(), s2.getConstArray());
84 CPPUNIT_ASSERT_EQUAL(s1.getHandle(), s2.getHandle());
85 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s1.getHandle()->nRefCount);
87 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
90 void test_fromC() {
91 sal_Sequence c = { 1, 1, { 0 } };
93 rtl::ByteSequence s(&c);
94 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
95 CPPUNIT_ASSERT_EQUAL(
96 static_cast< void const * >(c.elements),
97 static_cast< void const * >(s.getConstArray()));
98 CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
99 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
101 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
104 void test_noacquire() {
105 sal_Sequence c = { 2, 1, { 0 } };
107 rtl::ByteSequence s(&c, rtl::BYTESEQ_NOACQUIRE);
108 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
109 CPPUNIT_ASSERT_EQUAL(
110 static_cast< void const * >(c.elements),
111 static_cast< void const * >(s.getConstArray()));
112 CPPUNIT_ASSERT_EQUAL(&c, s.getHandle());
113 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), c.nRefCount);
115 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), c.nRefCount);
118 void test_getArray() {
119 sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
120 rtl::ByteSequence s1(a, 5);
121 rtl::ByteSequence s2(s1);
122 sal_Int8 * p = s2.getArray();
123 p[2] = 10;
124 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getHandle()->nRefCount);
125 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s2.getHandle()->nRefCount);
126 CPPUNIT_ASSERT_EQUAL(sal_Int8(2), s1.getConstArray()[2]);
127 CPPUNIT_ASSERT_EQUAL(sal_Int8(10), s2.getConstArray()[2]);
130 void test_same0() {
131 rtl::ByteSequence s1;
132 rtl::ByteSequence s2;
133 CPPUNIT_ASSERT_EQUAL(true, s1 == s2);
134 CPPUNIT_ASSERT_EQUAL(true, s2 == s1);
135 CPPUNIT_ASSERT_EQUAL(false, s1 != s2);
136 CPPUNIT_ASSERT_EQUAL(false, s2 != s1);
139 void test_diffLen() {
140 sal_Int8 const a[5] = { 0, 1, 2, 3, 4 };
141 rtl::ByteSequence s1(a, 5);
142 rtl::ByteSequence s2(a, 4);
143 CPPUNIT_ASSERT_EQUAL(false, s1 == s2);
144 CPPUNIT_ASSERT_EQUAL(false, s2 == s1);
145 CPPUNIT_ASSERT_EQUAL(true, s1 != s2);
146 CPPUNIT_ASSERT_EQUAL(true, s2 != s1);
149 void test_diffElem() {
150 sal_Int8 const a1[5] = { 0, 1, 2, 3, 4 };
151 rtl::ByteSequence s1(a1, 5);
152 sal_Int8 const a2[5] = { 0, 1, 10, 3, 4 };
153 rtl::ByteSequence s2(a2, 5);
154 CPPUNIT_ASSERT_EQUAL(false, s1 == s2);
155 CPPUNIT_ASSERT_EQUAL(false, s2 == s1);
156 CPPUNIT_ASSERT_EQUAL(true, s1 != s2);
157 CPPUNIT_ASSERT_EQUAL(true, s2 != s1);
160 CPPUNIT_TEST_SUITE(Test);
161 CPPUNIT_TEST(test_default);
162 CPPUNIT_TEST(test_size0);
163 CPPUNIT_TEST(test_size5);
164 CPPUNIT_TEST(test_noinit0);
165 CPPUNIT_TEST(test_noinit5);
166 CPPUNIT_TEST(test_elem0);
167 CPPUNIT_TEST(test_elem5);
168 CPPUNIT_TEST(test_copy);
169 CPPUNIT_TEST(test_fromC);
170 CPPUNIT_TEST(test_noacquire);
171 CPPUNIT_TEST(test_getArray);
172 CPPUNIT_TEST(test_same0);
173 CPPUNIT_TEST(test_diffLen);
174 CPPUNIT_TEST(test_diffElem);
175 CPPUNIT_TEST_SUITE_END();
178 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */