Branch libreoffice-5-0-4
[LibreOffice.git] / tools / qa / cppunit / test_stream.cxx
blob6704548a92d2df18ee6515ca4e888908d794abf2
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/.
8 */
10 #include <sal/types.h>
11 #include "cppunit/TestAssert.h"
12 #include "cppunit/TestFixture.h"
13 #include "cppunit/extensions/HelperMacros.h"
14 #include "cppunit/plugin/TestPlugIn.h"
15 #include <tools/stream.hxx>
16 #include <sstream>
18 //Tests for eofbit/badbit/goodbit/failbit
20 namespace
23 class Test: public CppUnit::TestFixture
25 public:
26 void test_stdstream();
27 void test_fastostring();
28 void test_read_cstring();
29 void test_read_pstring();
30 void test_readline();
32 CPPUNIT_TEST_SUITE(Test);
33 CPPUNIT_TEST(test_stdstream);
34 CPPUNIT_TEST(test_fastostring);
35 CPPUNIT_TEST(test_read_cstring);
36 CPPUNIT_TEST(test_read_pstring);
37 CPPUNIT_TEST(test_readline);
38 CPPUNIT_TEST_SUITE_END();
41 void Test::test_stdstream()
43 char foo[] = "foo";
44 std::istringstream iss(foo, std::istringstream::in);
45 SvMemoryStream aMemStream(foo, SAL_N_ELEMENTS(foo)-1, StreamMode::READ);
47 char std_a(78);
48 iss >> std_a;
49 CPPUNIT_ASSERT(std_a == 'f');
51 char tools_a(78);
52 aMemStream.ReadChar( tools_a );
53 CPPUNIT_ASSERT(tools_a == 'f');
55 iss.seekg(0, std::ios_base::end);
56 //seeking to end doesn't set eof, reading past eof does
57 CPPUNIT_ASSERT(!iss.eof());
58 CPPUNIT_ASSERT(iss.good());
60 aMemStream.Seek(STREAM_SEEK_TO_END);
61 //seeking to end doesn't set eof, reading past eof does
62 CPPUNIT_ASSERT(!aMemStream.eof());
63 CPPUNIT_ASSERT(aMemStream.good());
65 std_a = 78;
66 iss >> std_a;
67 //so, now eof is set
68 CPPUNIT_ASSERT(iss.eof());
69 //a failed read doesn't change the data, it remains unchanged
70 CPPUNIT_ASSERT(std_a == 78);
71 //nothing wrong with the stream, so not bad
72 CPPUNIT_ASSERT(!iss.bad());
73 //yet, the read didn't succeed
74 CPPUNIT_ASSERT(!iss.good());
75 CPPUNIT_ASSERT(iss.rdstate() == (std::ios::failbit|std::ios::eofbit));
77 tools_a = 78;
78 aMemStream.ReadChar( tools_a );
79 //so, now eof is set
80 CPPUNIT_ASSERT(aMemStream.eof());
81 //a failed read doesn't change the data, it remains unchanged
82 CPPUNIT_ASSERT(tools_a == 78);
83 //nothing wrong with the stream, so not bad
84 CPPUNIT_ASSERT(!aMemStream.bad());
85 //yet, the read didn't succeed
86 CPPUNIT_ASSERT(!aMemStream.good());
88 //set things up so that there is only one byte available on an attempt
89 //to read a two-byte sal_uInt16. The byte should be consumed, but the
90 //operation should fail, and tools_b should remain unchanged,
91 sal_uInt16 tools_b = 0x1122;
92 aMemStream.SeekRel(-1);
93 CPPUNIT_ASSERT(!aMemStream.eof());
94 CPPUNIT_ASSERT(aMemStream.good());
95 aMemStream.ReadUInt16( tools_b );
96 CPPUNIT_ASSERT(!aMemStream.good());
97 CPPUNIT_ASSERT(aMemStream.eof());
98 CPPUNIT_ASSERT(tools_b == 0x1122);
100 iss.clear();
101 iss.seekg(0);
102 CPPUNIT_ASSERT(iss.good());
103 iss >> std_a;
104 CPPUNIT_ASSERT(std_a == 'f');
106 aMemStream.Seek(0);
107 CPPUNIT_ASSERT(aMemStream.good());
108 aMemStream.ReadChar( tools_a );
109 CPPUNIT_ASSERT(tools_a == 'f');
111 //failbit is rather subtle wrt e.g seeks
113 char buffer[1024];
115 iss.clear();
116 iss.seekg(0);
117 CPPUNIT_ASSERT(iss.good());
118 iss.read(buffer, sizeof(buffer));
119 CPPUNIT_ASSERT(iss.gcount() == 3);
120 CPPUNIT_ASSERT(!iss.good());
121 CPPUNIT_ASSERT(!iss.bad());
122 CPPUNIT_ASSERT(iss.eof());
124 aMemStream.Seek(0);
125 CPPUNIT_ASSERT(aMemStream.good());
126 sal_Size nRet = aMemStream.Read(buffer, sizeof(buffer));
127 CPPUNIT_ASSERT(nRet == 3);
128 CPPUNIT_ASSERT(!aMemStream.good());
129 CPPUNIT_ASSERT(!aMemStream.bad());
130 CPPUNIT_ASSERT(aMemStream.eof());
133 void Test::test_fastostring()
135 char foo[] = "foobar";
136 SvMemoryStream aMemStream(foo, SAL_N_ELEMENTS(foo)-1, StreamMode::READ);
138 OString aOne = read_uInt8s_ToOString(aMemStream, 3);
139 CPPUNIT_ASSERT(aOne == "foo");
141 OString aTwo = read_uInt8s_ToOString(aMemStream, 3);
142 CPPUNIT_ASSERT(aTwo == "bar");
144 OString aThree = read_uInt8s_ToOString(aMemStream, 3);
145 CPPUNIT_ASSERT(aThree.isEmpty());
147 aMemStream.Seek(0);
149 OString aFour = read_uInt8s_ToOString(aMemStream, 100);
150 CPPUNIT_ASSERT(aFour == foo);
153 void Test::test_read_cstring()
155 char foo[] = "foobar";
156 SvMemoryStream aMemStream(foo, SAL_N_ELEMENTS(foo)-1, StreamMode::READ);
158 OString aOne = read_zeroTerminated_uInt8s_ToOString(aMemStream);
159 CPPUNIT_ASSERT(aOne == "foobar");
160 CPPUNIT_ASSERT(!aMemStream.good());
161 CPPUNIT_ASSERT(!aMemStream.bad());
162 CPPUNIT_ASSERT(aMemStream.eof());
164 aMemStream.Seek(0);
165 foo[3] = 0;
166 OString aTwo = read_zeroTerminated_uInt8s_ToOString(aMemStream);
167 CPPUNIT_ASSERT(aTwo == "foo");
168 CPPUNIT_ASSERT(aMemStream.good());
171 void Test::test_read_pstring()
173 char foo[] = "\3foobar";
174 SvMemoryStream aMemStream(foo, SAL_N_ELEMENTS(foo)-1, StreamMode::READ);
176 OString aFoo = read_uInt8_lenPrefixed_uInt8s_ToOString(aMemStream);
177 CPPUNIT_ASSERT(aFoo == "foo");
178 CPPUNIT_ASSERT(aMemStream.good());
179 CPPUNIT_ASSERT(!aMemStream.bad());
180 CPPUNIT_ASSERT(!aMemStream.eof());
182 aMemStream.Seek(0);
183 foo[0] = 10;
184 aFoo = read_uInt8_lenPrefixed_uInt8s_ToOString(aMemStream);
185 CPPUNIT_ASSERT(aFoo == "foobar");
186 CPPUNIT_ASSERT(!aMemStream.good());
187 CPPUNIT_ASSERT(!aMemStream.bad());
188 CPPUNIT_ASSERT(aMemStream.eof());
190 aMemStream.SetEndian(SvStreamEndian::BIG);
191 aMemStream.Seek(0);
192 foo[0] = 0;
193 foo[1] = 3;
194 aFoo = read_uInt16_lenPrefixed_uInt8s_ToOString(aMemStream);
195 CPPUNIT_ASSERT(aFoo == "oob");
196 CPPUNIT_ASSERT(aMemStream.good());
197 CPPUNIT_ASSERT(!aMemStream.bad());
198 CPPUNIT_ASSERT(!aMemStream.eof());
200 aMemStream.SetEndian(SvStreamEndian::LITTLE);
201 aMemStream.Seek(0);
202 foo[0] = 3;
203 foo[1] = 0;
204 foo[2] = 0;
205 foo[3] = 0;
206 aFoo = read_uInt32_lenPrefixed_uInt8s_ToOString(aMemStream);
207 CPPUNIT_ASSERT(aFoo == "bar");
208 CPPUNIT_ASSERT(aMemStream.good());
209 CPPUNIT_ASSERT(!aMemStream.bad());
210 CPPUNIT_ASSERT(!aMemStream.eof());
213 void Test::test_readline()
215 char foo[] = "foo\nbar\n\n";
216 SvMemoryStream aMemStream(foo, SAL_N_ELEMENTS(foo)-1, StreamMode::READ);
218 OString aFoo;
219 bool bRet;
221 bRet = aMemStream.ReadLine(aFoo);
222 CPPUNIT_ASSERT(bRet);
223 CPPUNIT_ASSERT(aFoo == "foo");
224 CPPUNIT_ASSERT(aMemStream.good());
226 bRet = aMemStream.ReadLine(aFoo);
227 CPPUNIT_ASSERT(bRet);
228 CPPUNIT_ASSERT(aFoo == "bar");
229 CPPUNIT_ASSERT(aMemStream.good());
231 bRet = aMemStream.ReadLine(aFoo);
232 CPPUNIT_ASSERT(bRet);
233 CPPUNIT_ASSERT(aFoo.isEmpty());
234 CPPUNIT_ASSERT(aMemStream.good());
236 bRet = aMemStream.ReadLine(aFoo);
237 CPPUNIT_ASSERT(!bRet);
238 CPPUNIT_ASSERT(aFoo.isEmpty());
239 CPPUNIT_ASSERT(aMemStream.eof());
241 foo[3] = 0; //test reading embedded nulls
243 aMemStream.Seek(0);
244 bRet = aMemStream.ReadLine(aFoo);
245 CPPUNIT_ASSERT(bRet);
246 CPPUNIT_ASSERT(aFoo.getLength() == 7 && aFoo[3] == 0);
247 CPPUNIT_ASSERT(aMemStream.good());
249 std::string sStr(foo, RTL_CONSTASCII_LENGTH(foo));
250 std::istringstream iss(sStr, std::istringstream::in);
251 std::getline(iss, sStr, '\n');
252 //embedded null read as expected
253 CPPUNIT_ASSERT(sStr.size() == 7 && sStr[3] == 0);
254 CPPUNIT_ASSERT(iss.good());
256 bRet = aMemStream.ReadLine(aFoo);
257 CPPUNIT_ASSERT(bRet);
258 CPPUNIT_ASSERT(aFoo.isEmpty());
259 CPPUNIT_ASSERT(aMemStream.good());
261 std::getline(iss, sStr, '\n');
262 CPPUNIT_ASSERT(sStr.empty());
263 CPPUNIT_ASSERT(iss.good());
265 bRet = aMemStream.ReadLine(aFoo);
266 CPPUNIT_ASSERT(!bRet);
267 CPPUNIT_ASSERT(aFoo.isEmpty());
268 CPPUNIT_ASSERT(aMemStream.eof() && !aMemStream.bad());
270 std::getline(iss, sStr, '\n');
271 CPPUNIT_ASSERT(sStr.empty());
272 CPPUNIT_ASSERT(iss.eof() && !iss.bad());
274 char bar[] = "foo";
275 SvMemoryStream aMemStreamB(bar, SAL_N_ELEMENTS(bar)-1, StreamMode::READ);
276 bRet = aMemStreamB.ReadLine(aFoo);
277 CPPUNIT_ASSERT(bRet);
278 CPPUNIT_ASSERT(aFoo == "foo");
279 CPPUNIT_ASSERT(!aMemStreamB.eof()); //<-- diff A
281 std::istringstream issB(bar, std::istringstream::in);
282 std::getline(issB, sStr, '\n');
283 CPPUNIT_ASSERT(sStr == "foo");
284 CPPUNIT_ASSERT(issB.eof()); //<-- diff A
287 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */