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/.
10 #include <sal/config.h>
16 #include <com/sun/star/io/NotConnectedException.hpp>
17 #include <com/sun/star/io/TextInputStream.hpp>
18 #include <com/sun/star/io/XInputStream.hpp>
19 #include <com/sun/star/io/XTextInputStream2.hpp>
20 #include <com/sun/star/uno/Sequence.hxx>
21 #include <com/sun/star/uno/Reference.hxx>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppunit/TestAssert.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/plugin/TestPlugIn.h>
26 #include <osl/mutex.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
29 #include <unotest/bootstrapfixturebase.hxx>
33 class Input
: public cppu::WeakImplHelper
<css::io::XInputStream
> {
35 Input(char* inputData
, sal_Int32 inputSize
):
42 virtual ~Input() override
{}
44 sal_Int32 SAL_CALL
readBytes(css::uno::Sequence
<sal_Int8
> &, sal_Int32
)
46 { CPPUNIT_FAIL("readLine is supposed to call readSomeBytes instead"); }
48 sal_Int32 SAL_CALL
readSomeBytes(
49 css::uno::Sequence
<sal_Int8
> & aData
, sal_Int32 nMaxBytesToRead
) override
51 assert(nMaxBytesToRead
>= 0);
52 osl::MutexGuard
g(mutex_
);
54 assert(index_
>= 0 && index_
<= size
);
55 sal_Int32 n
= std::min
<sal_Int32
>(
56 std::min
<sal_Int32
>(nMaxBytesToRead
, 2), size
- index_
);
57 assert(n
>= 0 && n
<= size
- index_
);
59 std::memcpy(aData
.getArray(), data
+ index_
, n
);
61 assert(index_
>= 0 && index_
<= size
);
65 void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip
) override
67 assert(nBytesToSkip
>= 0);
68 osl::MutexGuard
g(mutex_
);
70 assert(index_
>= 0 && index_
<= size
);
71 index_
+= std::min
<sal_Int32
>(nBytesToSkip
, size
- index_
);
72 assert(index_
>= 0 && index_
<= size
);
75 sal_Int32 SAL_CALL
available() override
77 osl::MutexGuard
g(mutex_
);
79 assert(index_
>= 0 && index_
<= size
);
83 void SAL_CALL
closeInput() override
85 osl::MutexGuard
g(mutex_
);
92 throw css::io::NotConnectedException(
93 u
"test input stream already closed"_ustr
);
106 class Test
: public test::BootstrapFixtureBase
{
108 CPPUNIT_TEST_SUITE(Test
);
109 CPPUNIT_TEST(testReadLine
);
110 CPPUNIT_TEST(testReadLineEndChars
);
111 CPPUNIT_TEST_SUITE_END();
114 void testReadLineEndChars();
116 OUString
readFirstLine(char data1
[], int size
);
119 OUString
Test::readFirstLine(char *inputData
, int inputSize
) {
120 css::uno::Reference
<css::io::XTextInputStream2
> s(
121 css::io::TextInputStream::create(getComponentContext()));
122 s
->setInputStream(new Input(inputData
, inputSize
));
123 return s
->readLine();
126 void Test::testReadLine() {
127 char inputData
[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
128 OUString
l(readFirstLine(inputData
, sizeof(inputData
)));
129 CPPUNIT_ASSERT_EQUAL(u
"123456789"_ustr
, l
);
132 void Test::testReadLineEndChars() {
133 std::vector
<char> inputData
= {'a', 'b', 'c', '\r'};
134 OUString
l(readFirstLine(inputData
.data(), inputData
.size()));
135 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, l
);
137 inputData
= {'a', 'b', 'c', '\n'};
138 l
= readFirstLine(inputData
.data(), inputData
.size());
139 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, l
);
141 inputData
= {'a', 'b', 'c', '\r', '\n'};
142 l
= readFirstLine(inputData
.data(), inputData
.size());
143 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, l
);
145 inputData
= {'a', 'b', 'c', '\r', 'd', 'e', 'f'};
146 l
= readFirstLine(inputData
.data(), inputData
.size());
147 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, l
);
149 inputData
= {'a', 'b', 'c', '\n', 'd', 'e', 'f'};
150 l
= readFirstLine(inputData
.data(), inputData
.size());
151 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, l
);
153 css::uno::Reference
<css::io::XTextInputStream2
> s(
154 css::io::TextInputStream::create(getComponentContext()));
155 inputData
= {'a', 'b', 'c', '\r', '\n', 'd', 'e', 'f'};
156 s
->setInputStream(new Input(inputData
.data(), inputData
.size()));
158 CPPUNIT_ASSERT_EQUAL(u
"abc"_ustr
, l
);
160 CPPUNIT_ASSERT_EQUAL(u
"def"_ustr
, l
);
164 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
168 CPPUNIT_PLUGIN_IMPLEMENT();
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */